From 458da2563fa674bbcfd32ade2254b3915f751491 Mon Sep 17 00:00:00 2001 From: Josh Haberman Date: Tue, 16 Feb 2016 23:13:53 -0800 Subject: Addressed code review comments. --- upb/json/parser.rl | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-) (limited to 'upb/json/parser.rl') diff --git a/upb/json/parser.rl b/upb/json/parser.rl index b85fd21..4ccca6c 100644 --- a/upb/json/parser.rl +++ b/upb/json/parser.rl @@ -1344,6 +1344,11 @@ static void add_jsonname_table(upb_json_parsermethod *m, const upb_msgdef* md) { upb_msg_field_iter i; upb_strtable *t; + /* It would be nice to stack-allocate this, but protobufs do not limit the + * length of fields to any reasonable limit. */ + char *buf = NULL; + size_t len = 0; + if (upb_inttable_lookupptr(&m->name_tables, md, NULL)) { return; } @@ -1357,17 +1362,20 @@ static void add_jsonname_table(upb_json_parsermethod *m, const upb_msgdef* md) { !upb_msg_field_done(&i); upb_msg_field_next(&i)) { const upb_fielddef *f = upb_msg_iter_field(&i); - /* It would be nice to stack-allocate this, but protobufs do not limit the - * length of fields to any reasonable limit. */ - char *buf = malloc(strlen(upb_fielddef_name(f)) + 1); - upb_fielddef_getjsonname(f, buf); + size_t field_len = upb_fielddef_getjsonname(f, buf, len); + if (field_len > len) { + buf = realloc(buf, field_len); + len = field_len; + upb_fielddef_getjsonname(f, buf, len); + } upb_strtable_insert(t, buf, upb_value_constptr(f)); - free(buf); if (upb_fielddef_issubmsg(f)) { add_jsonname_table(m, upb_fielddef_msgsubdef(f)); } } + + free(buf); } /* Public API *****************************************************************/ -- cgit v1.2.3