hush: fix FEATURE_CLEAN_UP code (was freeing unallocated memory)

Signed-off-by: Denys Vlasenko <dvlasenk@redhat.com>
This commit is contained in:
Denys Vlasenko 2010-12-21 21:18:12 +01:00
parent 51d714ce7e
commit 75eb9d20e9

View File

@ -931,7 +931,7 @@ static const struct built_in_command bltins2[] = {
*/ */
#if HUSH_DEBUG #if HUSH_DEBUG
/* prevent disasters with G.debug_indent < 0 */ /* prevent disasters with G.debug_indent < 0 */
# define indent() fprintf(stderr, "%*s", (G.debug_indent * 2) & 0xff, "") # define indent() fdprintf(2, "%*s", (G.debug_indent * 2) & 0xff, "")
# define debug_enter() (G.debug_indent++) # define debug_enter() (G.debug_indent++)
# define debug_leave() (G.debug_indent--) # define debug_leave() (G.debug_indent--)
#else #else
@ -941,56 +941,56 @@ static const struct built_in_command bltins2[] = {
#endif #endif
#ifndef debug_printf #ifndef debug_printf
# define debug_printf(...) (indent(), fprintf(stderr, __VA_ARGS__)) # define debug_printf(...) (indent(), fdprintf(2, __VA_ARGS__))
#endif #endif
#ifndef debug_printf_parse #ifndef debug_printf_parse
# define debug_printf_parse(...) (indent(), fprintf(stderr, __VA_ARGS__)) # define debug_printf_parse(...) (indent(), fdprintf(2, __VA_ARGS__))
#endif #endif
#ifndef debug_printf_exec #ifndef debug_printf_exec
#define debug_printf_exec(...) (indent(), fprintf(stderr, __VA_ARGS__)) #define debug_printf_exec(...) (indent(), fdprintf(2, __VA_ARGS__))
#endif #endif
#ifndef debug_printf_env #ifndef debug_printf_env
# define debug_printf_env(...) (indent(), fprintf(stderr, __VA_ARGS__)) # define debug_printf_env(...) (indent(), fdprintf(2, __VA_ARGS__))
#endif #endif
#ifndef debug_printf_jobs #ifndef debug_printf_jobs
# define debug_printf_jobs(...) (indent(), fprintf(stderr, __VA_ARGS__)) # define debug_printf_jobs(...) (indent(), fdprintf(2, __VA_ARGS__))
# define DEBUG_JOBS 1 # define DEBUG_JOBS 1
#else #else
# define DEBUG_JOBS 0 # define DEBUG_JOBS 0
#endif #endif
#ifndef debug_printf_expand #ifndef debug_printf_expand
# define debug_printf_expand(...) (indent(), fprintf(stderr, __VA_ARGS__)) # define debug_printf_expand(...) (indent(), fdprintf(2, __VA_ARGS__))
# define DEBUG_EXPAND 1 # define DEBUG_EXPAND 1
#else #else
# define DEBUG_EXPAND 0 # define DEBUG_EXPAND 0
#endif #endif
#ifndef debug_printf_varexp #ifndef debug_printf_varexp
# define debug_printf_varexp(...) (indent(), fprintf(stderr, __VA_ARGS__)) # define debug_printf_varexp(...) (indent(), fdprintf(2, __VA_ARGS__))
#endif #endif
#ifndef debug_printf_glob #ifndef debug_printf_glob
# define debug_printf_glob(...) (indent(), fprintf(stderr, __VA_ARGS__)) # define debug_printf_glob(...) (indent(), fdprintf(2, __VA_ARGS__))
# define DEBUG_GLOB 1 # define DEBUG_GLOB 1
#else #else
# define DEBUG_GLOB 0 # define DEBUG_GLOB 0
#endif #endif
#ifndef debug_printf_list #ifndef debug_printf_list
# define debug_printf_list(...) (indent(), fprintf(stderr, __VA_ARGS__)) # define debug_printf_list(...) (indent(), fdprintf(2, __VA_ARGS__))
#endif #endif
#ifndef debug_printf_subst #ifndef debug_printf_subst
# define debug_printf_subst(...) (indent(), fprintf(stderr, __VA_ARGS__)) # define debug_printf_subst(...) (indent(), fdprintf(2, __VA_ARGS__))
#endif #endif
#ifndef debug_printf_clean #ifndef debug_printf_clean
# define debug_printf_clean(...) (indent(), fprintf(stderr, __VA_ARGS__)) # define debug_printf_clean(...) (indent(), fdprintf(2, __VA_ARGS__))
# define DEBUG_CLEAN 1 # define DEBUG_CLEAN 1
#else #else
# define DEBUG_CLEAN 0 # define DEBUG_CLEAN 0
@ -1000,9 +1000,9 @@ static const struct built_in_command bltins2[] = {
static void debug_print_strings(const char *prefix, char **vv) static void debug_print_strings(const char *prefix, char **vv)
{ {
indent(); indent();
fprintf(stderr, "%s:\n", prefix); fdprintf(2, "%s:\n", prefix);
while (*vv) while (*vv)
fprintf(stderr, " '%s'\n", *vv++); fdprintf(2, " '%s'\n", *vv++);
} }
#else #else
# define debug_print_strings(prefix, vv) ((void)0) # define debug_print_strings(prefix, vv) ((void)0)
@ -1434,6 +1434,22 @@ static void hush_exit(int exitcode)
builtin_eval(argv); builtin_eval(argv);
} }
#if ENABLE_FEATURE_CLEAN_UP
{
struct variable *cur_var;
if (G.cwd != bb_msg_unknown)
free((char*)G.cwd);
cur_var = G.top_var;
while (cur_var) {
struct variable *tmp = cur_var;
if (!cur_var->max_len)
free(cur_var->varstr);
cur_var = cur_var->next;
free(tmp);
}
}
#endif
#if ENABLE_HUSH_JOB #if ENABLE_HUSH_JOB
fflush_all(); fflush_all();
sigexit(- (exitcode & 0xff)); sigexit(- (exitcode & 0xff));
@ -2176,22 +2192,22 @@ static void debug_print_list(const char *prefix, o_string *o, int n)
int i = 0; int i = 0;
indent(); indent();
fprintf(stderr, "%s: list:%p n:%d string_start:%d length:%d maxlen:%d glob:%d quoted:%d escape:%d\n", fdprintf(2, "%s: list:%p n:%d string_start:%d length:%d maxlen:%d glob:%d quoted:%d escape:%d\n",
prefix, list, n, string_start, o->length, o->maxlen, prefix, list, n, string_start, o->length, o->maxlen,
!!(o->o_expflags & EXP_FLAG_GLOB), !!(o->o_expflags & EXP_FLAG_GLOB),
o->has_quoted_part, o->has_quoted_part,
!!(o->o_expflags & EXP_FLAG_ESC_GLOB_CHARS)); !!(o->o_expflags & EXP_FLAG_ESC_GLOB_CHARS));
while (i < n) { while (i < n) {
indent(); indent();
fprintf(stderr, " list[%d]=%d '%s' %p\n", i, (int)list[i], fdprintf(2, " list[%d]=%d '%s' %p\n", i, (int)(uintptr_t)list[i],
o->data + (int)list[i] + string_start, o->data + (int)(uintptr_t)list[i] + string_start,
o->data + (int)list[i] + string_start); o->data + (int)(uintptr_t)list[i] + string_start);
i++; i++;
} }
if (n) { if (n) {
const char *p = o->data + (int)list[n - 1] + string_start; const char *p = o->data + (int)(uintptr_t)list[n - 1] + string_start;
indent(); indent();
fprintf(stderr, " total_sz:%ld\n", (long)((p + strlen(p) + 1) - o->data)); fdprintf(2, " total_sz:%ld\n", (long)((p + strlen(p) + 1) - o->data));
} }
} }
#else #else
@ -2690,18 +2706,18 @@ static void debug_print_tree(struct pipe *pi, int lvl)
pin = 0; pin = 0;
while (pi) { while (pi) {
fprintf(stderr, "%*spipe %d res_word=%s followup=%d %s\n", lvl*2, "", fdprintf(2, "%*spipe %d res_word=%s followup=%d %s\n", lvl*2, "",
pin, RES[pi->res_word], pi->followup, PIPE[pi->followup]); pin, RES[pi->res_word], pi->followup, PIPE[pi->followup]);
prn = 0; prn = 0;
while (prn < pi->num_cmds) { while (prn < pi->num_cmds) {
struct command *command = &pi->cmds[prn]; struct command *command = &pi->cmds[prn];
char **argv = command->argv; char **argv = command->argv;
fprintf(stderr, "%*s cmd %d assignment_cnt:%d", fdprintf(2, "%*s cmd %d assignment_cnt:%d",
lvl*2, "", prn, lvl*2, "", prn,
command->assignment_cnt); command->assignment_cnt);
if (command->group) { if (command->group) {
fprintf(stderr, " group %s: (argv=%p)%s%s\n", fdprintf(2, " group %s: (argv=%p)%s%s\n",
CMDTYPE[command->cmd_type], CMDTYPE[command->cmd_type],
argv argv
# if !BB_MMU # if !BB_MMU
@ -2715,10 +2731,10 @@ static void debug_print_tree(struct pipe *pi, int lvl)
continue; continue;
} }
if (argv) while (*argv) { if (argv) while (*argv) {
fprintf(stderr, " '%s'", *argv); fdprintf(2, " '%s'", *argv);
argv++; argv++;
} }
fprintf(stderr, "\n"); fdprintf(2, "\n");
prn++; prn++;
} }
pi = pi->next; pi = pi->next;
@ -7463,7 +7479,7 @@ int hush_main(int argc, char **argv)
unsigned builtin_argc; unsigned builtin_argc;
char **e; char **e;
struct variable *cur_var; struct variable *cur_var;
struct variable shell_ver; struct variable *shell_ver;
INIT_G(); INIT_G();
if (EXIT_SUCCESS) /* if EXIT_SUCCESS == 0, it is already done */ if (EXIT_SUCCESS) /* if EXIT_SUCCESS == 0, it is already done */
@ -7472,17 +7488,17 @@ int hush_main(int argc, char **argv)
G.argv0_for_re_execing = argv[0]; G.argv0_for_re_execing = argv[0];
#endif #endif
/* Deal with HUSH_VERSION */ /* Deal with HUSH_VERSION */
memset(&shell_ver, 0, sizeof(shell_ver)); shell_ver = xzalloc(sizeof(*shell_ver));
shell_ver.flg_export = 1; shell_ver->flg_export = 1;
shell_ver.flg_read_only = 1; shell_ver->flg_read_only = 1;
/* Code which handles ${var<op>...} needs writable values for all variables, /* Code which handles ${var<op>...} needs writable values for all variables,
* therefore we xstrdup: */ * therefore we xstrdup: */
shell_ver.varstr = xstrdup(hush_version_str), shell_ver->varstr = xstrdup(hush_version_str);
G.top_var = &shell_ver;
/* Create shell local variables from the values /* Create shell local variables from the values
* currently living in the environment */ * currently living in the environment */
debug_printf_env("unsetenv '%s'\n", "HUSH_VERSION"); debug_printf_env("unsetenv '%s'\n", "HUSH_VERSION");
unsetenv("HUSH_VERSION"); /* in case it exists in initial env */ unsetenv("HUSH_VERSION"); /* in case it exists in initial env */
G.top_var = shell_ver;
cur_var = G.top_var; cur_var = G.top_var;
e = environ; e = environ;
if (e) while (*e) { if (e) while (*e) {
@ -7497,8 +7513,8 @@ int hush_main(int argc, char **argv)
e++; e++;
} }
/* (Re)insert HUSH_VERSION into env (AFTER we scanned the env!) */ /* (Re)insert HUSH_VERSION into env (AFTER we scanned the env!) */
debug_printf_env("putenv '%s'\n", shell_ver.varstr); debug_printf_env("putenv '%s'\n", shell_ver->varstr);
putenv(shell_ver.varstr); putenv(shell_ver->varstr);
/* Export PWD */ /* Export PWD */
set_pwd_var(/*exp:*/ 1); set_pwd_var(/*exp:*/ 1);
@ -7858,18 +7874,6 @@ int hush_main(int argc, char **argv)
parse_and_run_file(stdin); parse_and_run_file(stdin);
final_return: final_return:
#if ENABLE_FEATURE_CLEAN_UP
if (G.cwd != bb_msg_unknown)
free((char*)G.cwd);
cur_var = G.top_var->next;
while (cur_var) {
struct variable *tmp = cur_var;
if (!cur_var->max_len)
free(cur_var->varstr);
cur_var = cur_var->next;
free(tmp);
}
#endif
hush_exit(G.last_exitcode); hush_exit(G.last_exitcode);
} }