From: Ezra Peisach Date: Wed, 26 Sep 2007 15:15:33 +0000 (+0000) Subject: profile library memory leaks introduced when malloc returns 0 X-Git-Tag: krb5-1.7-alpha1~851 X-Git-Url: http://git.tremily.us/?a=commitdiff_plain;h=cf3a311a561ade506913c4ba0e38b6e5adf9f9b4;p=krb5.git profile library memory leaks introduced when malloc returns 0 I have a modified version of valgrind that will allow me to have malloc fail in a controlled way. A number of memory leaks in error return passes exist in the profile library. They are essentially inconsequental - but my goal is to eventually create a test harness that tries to cover all code - including error returns... prof_parse.c: (profile_parse_file): Free node being created if parse_line() fails. prof_file.c (profile_open_file): free prf_data_t on malloc failure prof_tree.c (profile_create_node): The magic element must be set before calling profile_free_node for it to release memory. ticket: new git-svn-id: svn://anonsvn.mit.edu/krb5/trunk@19981 dc483132-0cff-0310-8789-dd5450dbe970 --- diff --git a/src/util/profile/prof_file.c b/src/util/profile/prof_file.c index 74d553ee6..cb9bfbc5a 100644 --- a/src/util/profile/prof_file.c +++ b/src/util/profile/prof_file.c @@ -226,8 +226,10 @@ errcode_t profile_open_file(const_profile_filespec_t filespec, len += strlen(home_env); } expanded_filename = malloc(len); - if (expanded_filename == 0) + if (expanded_filename == 0) { + free(prf); return errno; + } if (home_env) { strcpy(expanded_filename, home_env); strcat(expanded_filename, filespec+1); diff --git a/src/util/profile/prof_parse.c b/src/util/profile/prof_parse.c index db491591d..665b9d90c 100644 --- a/src/util/profile/prof_parse.c +++ b/src/util/profile/prof_parse.c @@ -242,6 +242,7 @@ errcode_t profile_parse_file(FILE *f, struct profile_node **root) #ifndef PROFILE_SUPPORTS_FOREIGN_NEWLINES retval = parse_line(bptr, &state); if (retval) { + profile_free_node(state.root_section); free (bptr); return retval; } @@ -286,6 +287,7 @@ errcode_t profile_parse_file(FILE *f, struct profile_node **root) newp = p + strlen (p) + 1; retval = parse_line (p, &state); if (retval) { + profile_free_node(state.root_section); free (bptr); return retval; } diff --git a/src/util/profile/prof_tree.c b/src/util/profile/prof_tree.c index b014e245d..2ea02a547 100644 --- a/src/util/profile/prof_tree.c +++ b/src/util/profile/prof_tree.c @@ -92,6 +92,8 @@ errcode_t profile_create_node(const char *name, const char *value, if (!new) return ENOMEM; memset(new, 0, sizeof(struct profile_node)); + /* Set magic here so profile_free_node will free memory */ + new->magic = PROF_MAGIC_NODE; new->name = strdup(name); if (new->name == 0) { profile_free_node(new); @@ -104,7 +106,6 @@ errcode_t profile_create_node(const char *name, const char *value, return ENOMEM; } } - new->magic = PROF_MAGIC_NODE; *ret_node = new; return 0;