From patchwork Wed Mar 9 06:03:38 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Peter Xu X-Patchwork-Id: 594837 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from lists.gnu.org (lists.gnu.org [IPv6:2001:4830:134:3::11]) (using TLSv1 with cipher AES256-SHA (256/256 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id BE938140BAC for ; Wed, 9 Mar 2016 17:04:03 +1100 (AEDT) Received: from localhost ([::1]:39284 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1adXE1-0006ls-U3 for incoming@patchwork.ozlabs.org; Wed, 09 Mar 2016 01:04:01 -0500 Received: from eggs.gnu.org ([2001:4830:134:3::10]:59056) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1adXDo-0006VK-IZ for qemu-devel@nongnu.org; Wed, 09 Mar 2016 01:03:49 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1adXDl-0004Ku-Dl for qemu-devel@nongnu.org; Wed, 09 Mar 2016 01:03:48 -0500 Received: from mx1.redhat.com ([209.132.183.28]:51055) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1adXDl-0004Kf-95 for qemu-devel@nongnu.org; Wed, 09 Mar 2016 01:03:45 -0500 Received: from int-mx09.intmail.prod.int.phx2.redhat.com (int-mx09.intmail.prod.int.phx2.redhat.com [10.5.11.22]) by mx1.redhat.com (Postfix) with ESMTPS id 6BDC8C00F5EE for ; Wed, 9 Mar 2016 06:03:44 +0000 (UTC) Received: from pxdev.xzpeter.org.com (dhcp-14-238.nay.redhat.com [10.66.14.238]) by int-mx09.intmail.prod.int.phx2.redhat.com (8.14.4/8.14.4) with ESMTP id u2963dXi015029; Wed, 9 Mar 2016 01:03:41 -0500 From: Peter Xu To: qemu-devel@nongnu.org Date: Wed, 9 Mar 2016 14:03:38 +0800 Message-Id: <1457503418-31299-1-git-send-email-peterx@redhat.com> X-Scanned-By: MIMEDefang 2.68 on 10.5.11.22 X-detected-operating-system: by eggs.gnu.org: GNU/Linux 3.x X-Received-From: 209.132.183.28 Cc: pbonzini@redhat.com, peterx@redhat.com Subject: [Qemu-devel] [PATCH] qdict: fix unbounded stack for qdict_array_entries X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.14 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org Sender: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org Signed-off-by: Peter Xu Reviewed-by: Eric Blake --- qobject/qdict.c | 15 ++++++--------- 1 file changed, 6 insertions(+), 9 deletions(-) diff --git a/qobject/qdict.c b/qobject/qdict.c index 9833bd0..9188a87 100644 --- a/qobject/qdict.c +++ b/qobject/qdict.c @@ -704,19 +704,16 @@ int qdict_array_entries(QDict *src, const char *subqdict) for (i = 0; i < INT_MAX; i++) { QObject *subqobj; int subqdict_entries; - size_t slen = 32 + subqdict_len; - char indexstr[slen], prefix[slen]; - size_t snprintf_ret; + char *prefix = g_strdup_printf("%s%u.", subqdict, i); - snprintf_ret = snprintf(indexstr, slen, "%s%u", subqdict, i); - assert(snprintf_ret < slen); + subqdict_entries = qdict_count_prefixed_entries(src, prefix); - subqobj = qdict_get(src, indexstr); + /* Remove ending "." */ + prefix[strlen(prefix) - 1] = 0x00; + subqobj = qdict_get(src, prefix); - snprintf_ret = snprintf(prefix, slen, "%s%u.", subqdict, i); - assert(snprintf_ret < slen); + g_free(prefix); - subqdict_entries = qdict_count_prefixed_entries(src, prefix); if (subqdict_entries < 0) { return subqdict_entries; }