Received: from eggs.gnu.org ([2001:4830:134:3::10]:36868) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1Zrs0q-0007jm-IT for qemu-devel@nongnu.org; Thu, 29 Oct 2015 14:33:25 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1Zrs0n-00008U-PO for qemu-devel@nongnu.org; Thu, 29 Oct 2015 14:33:24 -0400 Received: from mx1.redhat.com ([209.132.183.28]:37927) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1Zrs0n-000080-JC for qemu-devel@nongnu.org; Thu, 29 Oct 2015 14:33:21 -0400 Received: from int-mx11.intmail.prod.int.phx2.redhat.com (int-mx11.intmail.prod.int.phx2.redhat.com [10.5.11.24]) by mx1.redhat.com (Postfix) with ESMTPS id E15E7AB87A for ; Thu, 29 Oct 2015 18:33:20 +0000 (UTC) From: Markus Armbruster References: <1446122683-2355-1-git-send-email-armbru@redhat.com> <1446122683-2355-4-git-send-email-armbru@redhat.com> <56324B26.50104@redhat.com> Date: Thu, 29 Oct 2015 19:33:09 +0100 In-Reply-To: <56324B26.50104@redhat.com> (Eric Blake's message of "Thu, 29 Oct 2015 10:36:54 -0600") Message-ID: <87bnbhsexm.fsf@blackfin.pond.sub.org> MIME-Version: 1.0 Content-Type: text/plain Subject: Re: [Qemu-devel] [PATCH 3/4] check-qjson: Add test for JSON nesting depth limit List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: Eric Blake Cc: qemu-devel@nongnu.org, lcapitulino@redhat.com Eric Blake writes: > On 10/29/2015 06:44 AM, Markus Armbruster wrote: >> This would have prevented the regression mentioned in the previous >> commit. >> >> Signed-off-by: Markus Armbruster >> --- >> tests/check-qjson.c | 29 +++++++++++++++++++++++++++++ >> 1 file changed, 29 insertions(+) > > Better late than never. > >> +++ b/tests/check-qjson.c >> @@ -1484,6 +1484,34 @@ static void unterminated_literal(void) >> g_assert(obj == NULL); >> } >> >> +static char *make_nest(char *buf, size_t cnt) >> +{ >> + int i; >> + >> + for (i = 0; i < cnt - 1; i++) { >> + buf[i] = '['; >> + buf[2 * cnt - i - 1] = ']'; >> + } >> + buf[cnt - 1] = '{'; >> + buf[cnt] = '}'; >> + buf[2 * cnt] = 0; >> + return buf; >> +} > > So buf must be at least 2*cnt+1 bytes long. (Function is static, so > lack of comments don't hurt too badly). For a cnt of 3 (buffer size at > least 7), this creates "[[{}]]". Larger cnt adds more outer [] pairs. > The mixed content proves that patch 1/4 covers the combined limit of [] > and {} when counting nesting. > > Minor optimization - make the for loop bound be 'i < cnt - 2', so you > aren't writing [] in the middle just to rewrite it to {} after the loop > (works as long as caller never passes cnt == 1, which happens to be the > case). The loop's first assignment fills 0..cnt-2 inclusive, the second one fills 2*cnt-1..(2*cnt-1)-(cnt-2) = 2*cnt-1..cnt+1 inclusive. If I need to respin, I'll change to a pair of memset(). >> +static void limits_nesting(void) >> +{ >> + enum { max_nesting = 1024 }; /* see qobject/json-streamer.c */ >> + char buf[2 * (max_nesting + 1) + 1]; >> + QObject *obj; >> + >> + obj = qobject_from_json(make_nest(buf, max_nesting)); >> + g_assert(obj != NULL); >> + qobject_decref(obj); > > Proves that we can hit our max, > >> + >> + obj = qobject_from_json(make_nest(buf, max_nesting + 1)); >> + g_assert(obj == NULL); > > and that we gracefully diagnose one beyond max. > >> +} >> + >> int main(int argc, char **argv) >> { >> g_test_init(&argc, &argv, NULL); >> @@ -1519,6 +1547,7 @@ int main(int argc, char **argv) >> g_test_add_func("/errors/invalid_array_comma", invalid_array_comma); >> g_test_add_func("/errors/invalid_dict_comma", invalid_dict_comma); >> g_test_add_func("/errors/unterminated/literal", unterminated_literal); >> + g_test_add_func("/errors/limits/nesting", limits_nesting); >> >> return g_test_run(); >> } >> > > Reviewed-by: Eric Blake