From eb67fb9109a24d866fe81abe4dd221e6fe18416f Mon Sep 17 00:00:00 2001 From: Joshua Haberman Date: Fri, 7 Aug 2009 21:05:27 -0700 Subject: Fixed memory leaks in upbc. --- src/upb_msg.c | 2 +- tools/upbc.c | 12 ++++++++++++ 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/src/upb_msg.c b/src/upb_msg.c index c58b470..16c24c0 100644 --- a/src/upb_msg.c +++ b/src/upb_msg.c @@ -193,7 +193,7 @@ void *upb_msg_parsenew(struct upb_msgdef *md, struct upb_string *s) * needs to be freed) iff its set bit is set. */ static void free_value(union upb_value_ptr p, struct upb_msg_fielddef *f) { - if(upb_isarray(f)) { + if(upb_isstring(f)) { free((*p.str)->ptr); free(*p.str); } else if(upb_issubmsg(f)) { diff --git a/tools/upbc.c b/tools/upbc.c index 28991a5..f593ae9 100644 --- a/tools/upbc.c +++ b/tools/upbc.c @@ -434,6 +434,7 @@ static void write_message_c(void *data, struct upb_msgdef *m, fprintf(stream, " {.ptr = &strdata[%d], .byte_len=%d},\n", e->offset, e->e.key.byte_len); } fputs("};\n\n", stream); + free(str_entries); /* Gather a list of types for which we are emitting data, and give each msg * a unique number within its type. */ @@ -568,6 +569,15 @@ static void write_message_c(void *data, struct upb_msgdef *m, fprintf(stream, UPB_STRFMT " *%s = &" UPB_STRFMT "_values[0];\n", UPB_STRARG(toplevel_type->cident), cident, UPB_STRARG(toplevel_type->cident)); + + /* Free tables. */ + for(e = upb_strtable_begin(&types); e; e = upb_strtable_next(&types, &e->e)) { + upb_strfree(e->cident); + free(e->values); + free(e->arrays); + } + upb_strtable_free(&types); + upb_strtable_free(&strings); } const char usage[] = @@ -669,12 +679,14 @@ int main(int argc, char *argv[]) int symcount; struct upb_symtab_entry **entries = strtable_to_array(&c.symtab, &symcount); write_h(entries, symcount, h_filename, cident, h_file); + free(entries); if(cident) { FILE *c_file = fopen(c_filename, "w"); if(!c_file) error("Failed to open .h output file"); write_message_c(fds, c.fds_msg, cident, h_filename, argc, argv, input_file, c_file); fclose(c_file); } + upb_msg_free(fds, c.fds_msg); upb_context_free(&c); upb_strfree(descriptor); fclose(h_file); -- cgit v1.2.3