From: John Darrington Date: Sat, 23 Apr 2005 07:55:29 +0000 (+0000) Subject: Plugged some memory leaks. X-Git-Tag: v0.4.0~114 X-Git-Url: https://pintos-os.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=4a4c003e2b2c01274b305e907fc63ba33d8481ae;p=pspp-builds.git Plugged some memory leaks. --- diff --git a/src/dictionary.c b/src/dictionary.c index 86076f9c..c3354237 100644 --- a/src/dictionary.c +++ b/src/dictionary.c @@ -84,6 +84,9 @@ hash_long_name (const void *e_, void *aux UNUSED) return hash; } + + + static char *make_short_name(struct dictionary *dict, const char *longname) ; @@ -96,7 +99,8 @@ dict_create (void) d->var = NULL; d->var_cnt = d->var_cap = 0; d->name_tab = hsh_create (8, compare_var_names, hash_var_name, NULL, NULL); - d->long_name_tab = hsh_create (8, compare_long_names, hash_long_name, NULL, NULL); + d->long_name_tab = hsh_create (8, compare_long_names, hash_long_name, + (hsh_free_func *) free_nte, NULL); d->next_value_idx = 0; d->split = NULL; d->split_cnt = 0; @@ -232,9 +236,8 @@ dict_get_varname_block(const struct dictionary *dict, char **text, int *size) *size = bufsize; } - /* Add a new entry into the dictionary's long name table, and update the - corresponding varible with the relevant long name. + corresponding variable with the relevant long name. */ void dict_add_longvar_entry(struct dictionary *d, @@ -248,7 +251,6 @@ dict_add_longvar_entry(struct dictionary *d, nte->longname = strdup(longname); nte->name = strdup(name); - /* Look up the name in name_tab */ v = hsh_find ( d->name_tab, name); if ( !v ) @@ -260,10 +262,19 @@ dict_add_longvar_entry(struct dictionary *d, v->longname = nte->longname; hsh_insert(d->long_name_tab, nte); - +} +/* Destroy and free up an nte */ +void +free_nte(struct name_table_entry *nte) +{ + assert(nte); + free(nte->longname); + free(nte->name); + free(nte); } + /* Destroys the aux data for every variable in D, by calling var_clear_aux() for each variable. */ void @@ -409,7 +420,11 @@ dict_create_var_x (struct dictionary *d, const char *name, int width, v->name[SHORT_NAME_LEN] = '\0'; } else - strcpy(v->name,make_short_name(d, name)); + { + const char *sn = make_short_name(d, name); + strncpy(v->name, sn, SHORT_NAME_LEN); + free(sn); + } v->index = d->var_cnt; @@ -751,12 +766,17 @@ dict_rename_vars (struct dictionary *d, for (i = 0; i < count; i++) { + char *sn; struct name_table_entry key; struct name_table_entry *nte; assert (new_names[i] != NULL); assert (*new_names[i] != '\0'); assert (strlen (new_names[i]) <= LONG_NAME_LEN ); - strcpy (vars[i]->name, make_short_name(d, new_names[i])); + + sn = make_short_name(d, new_names[i]); + strncpy(vars[i]->name, sn, SHORT_NAME_LEN); + free(sn); + key.longname = vars[i]->longname; diff --git a/src/sfm-write.c b/src/sfm-write.c index f09834b3..2245ab66 100644 --- a/src/sfm-write.c +++ b/src/sfm-write.c @@ -646,6 +646,7 @@ write_longvar_table (struct sfm_writer *w, const struct dictionary *dict) if (!buf_write (w, buf, bufsize)) goto error; + free (buf); return 1; error: diff --git a/src/var.h b/src/var.h index f13606be..63296b9f 100644 --- a/src/var.h +++ b/src/var.h @@ -107,6 +107,9 @@ bool var_is_valid_name (const char *, bool issue_error); int compare_var_names (const void *, const void *, void *); unsigned hash_var_name (const void *, void *); +/* Destroy and free up an nte */ +void free_nte(struct name_table_entry *nte); + unsigned hash_long_name (const void *e_, void *aux UNUSED) ; int compare_long_names(const void *a_, const void *b_, void *aux); diff --git a/src/vars-prs.c b/src/vars-prs.c index dbc14755..2cc02364 100644 --- a/src/vars-prs.c +++ b/src/vars-prs.c @@ -749,7 +749,8 @@ var_set_create_from_array (struct variable *const *var, size_t var_cnt) avs->longname_tab = hsh_create (2 * var_cnt, compare_long_names, hash_long_name, - NULL, NULL); + (hsh_free_func *) free_nte, + NULL); for (i = 0; i < var_cnt; i++) {