Received: from eggs.gnu.org ([2001:4830:134:3::10]:53090) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1adHzA-000585-8I for qemu-devel@nongnu.org; Tue, 08 Mar 2016 08:47:41 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1adHz9-0002kh-AV for qemu-devel@nongnu.org; Tue, 08 Mar 2016 08:47:40 -0500 From: Markus Armbruster References: <1457420446-25276-1-git-send-email-peterx@redhat.com> <1457420446-25276-3-git-send-email-peterx@redhat.com> <87twkhe6bm.fsf@blackfin.pond.sub.org> <20160308085337.GA26071@ad.usersys.redhat.com> Date: Tue, 08 Mar 2016 14:47:31 +0100 In-Reply-To: <20160308085337.GA26071@ad.usersys.redhat.com> (Fam Zheng's message of "Tue, 8 Mar 2016 16:53:37 +0800") Message-ID: <874mch9j4c.fsf@blackfin.pond.sub.org> MIME-Version: 1.0 Content-Type: text/plain Subject: Re: [Qemu-devel] [PATCH 2/8] block: fix unbounded stack for dump_qdict List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: Fam Zheng Cc: Kevin Wolf , pbonzini@redhat.com, Peter Xu , qemu-devel@nongnu.org, qemu-block@nongnu.org Fam Zheng writes: > On Tue, 03/08 09:12, Markus Armbruster wrote: >> Peter Xu writes: >> >> > Suggested-by: Paolo Bonzini >> > CC: Markus Armbruster >> > CC: Kevin Wolf >> > CC: qemu-block@nongnu.org >> > Signed-off-by: Peter Xu >> > --- >> > block/qapi.c | 5 ++++- >> > 1 file changed, 4 insertions(+), 1 deletion(-) >> > >> > diff --git a/block/qapi.c b/block/qapi.c >> > index db2d3fb..687e577 100644 >> > --- a/block/qapi.c >> > +++ b/block/qapi.c >> > @@ -638,9 +638,12 @@ static void dump_qdict(fprintf_function func_fprintf, void *f, int indentation, >> > QType type = qobject_type(entry->value); >> > bool composite = (type == QTYPE_QDICT || type == QTYPE_QLIST); >> > const char *format = composite ? "%*s%s:\n" : "%*s%s: "; >> >> Unrelated to your patch: ugh! >> >> Printf formats should be literals whenever possible, to make it easy for >> the compiler to warn you when you screw up. It's trivially possible >> here! Instead of >> >> func_fprintf(f, format, indentation * 4, "", key); >> >> do >> >> func_fprintf(f, "%*s%s:%c", indentation * 4, "", key, >> composite ? '\n', ' ');; >> >> > - char key[strlen(entry->key) + 1]; >> > +#define __KEY_LEN (256) >> > + char key[__KEY_LEN]; >> > int i; >> > >> > + assert(strlen(entry->key) + 1 <= __KEY_LEN); >> > +#undef __KEY_LEN >> > /* replace dashes with spaces in key (variable) names */ >> > for (i = 0; entry->key[i]; i++) { >> > key[i] = entry->key[i] == '-' ? ' ' : entry->key[i]; >> >> I'm afraid this isn't a good idea. It relies on the non-local argument >> that nobody will ever put a key longer than 255 into a qdict that gets >> dumped. That may even be the case, but you need to *prove* it, not just >> assert it. The weakest acceptable proof might be assertions in every >> place that put keys into a dict that might get dumped. I suspect that's >> practical and maintainable only if there's a single place that does it. >> >> If this was a good idea, I'd recommend to avoid the awkward macro: > > Also I think the double underscore identifiers are considered reserved in C, > no? Correct. C99 7.1.3 Reserved identifiers: All identifiers that begin with an underscore and either an uppercase letter or another underscore are always reserved for any use. [...]