Index: pw.c =================================================================== --- pw.c +++ pw.c @@ -106,6 +106,11 @@ struct pwconf conf; +/* from pw_vpw.c, we need to free() these at the end of execution */ +extern struct password *pw_last; +extern struct group *gr_last; +static bool must_free = false; + static int getindex(const char *words[], const char *word); static void cmdhelp(int mode, int which); @@ -116,6 +121,7 @@ struct stat st; char arg, *arg1; bool relocated, nis; + int retval; arg1 = NULL; relocated = nis = false; @@ -156,6 +162,7 @@ --argc; } memcpy(&PWF, &VPWF, sizeof PWF); + must_free = true; if (arg == 'R') { strlcpy(conf.rootdir, optarg, sizeof(conf.rootdir)); @@ -196,7 +203,16 @@ if (conf.rootfd == -1) errx(EXIT_FAILURE, "Unable to open '%s'", conf.rootdir); - return (cmdfunc[which][mode](argc, argv, arg1)); + retval = cmdfunc[which][mode](argc, argv, arg1); + if (must_free) + { + if (pw_last != NULL) + free(pw_last); + if (gr_last != NULL) + free(gr_last); + } + + return (retval); } Index: pw_vpw.c =================================================================== --- pw_vpw.c +++ pw_vpw.c @@ -46,6 +46,8 @@ static FILE * pwd_fp = NULL; static int pwd_scanflag; static const char *pwd_filename; +struct password *pw_last = NULL; +struct group *gr_last = NULL; void vendpwent(void) @@ -113,6 +115,10 @@ } free(line); + if (pw_last != NULL) + free(pw_last); + pw_last = pw; + return (pw); } @@ -192,6 +198,10 @@ } free(line); + if (gr_last != NULL) + free(gr_last); + gr_last = gr; + return (gr); }