From patchwork Fri Nov 23 07:47:38 2012 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Robert Wang X-Patchwork-Id: 201274 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) (using TLSv1 with cipher AES256-SHA (256/256 bits)) (Client did not present a certificate) by ozlabs.org (Postfix) with ESMTPS id AFE8B2C0091 for ; Fri, 23 Nov 2012 20:11:19 +1100 (EST) Received: from localhost ([::1]:39414 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1Tbo0k-0005gP-Vq for incoming@patchwork.ozlabs.org; Fri, 23 Nov 2012 02:49:18 -0500 Received: from eggs.gnu.org ([208.118.235.92]:53703) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1Tbnzz-0003sS-Bu for qemu-devel@nongnu.org; Fri, 23 Nov 2012 02:48:35 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1Tbnzv-0001t3-4s for qemu-devel@nongnu.org; Fri, 23 Nov 2012 02:48:31 -0500 Received: from mail-ie0-f173.google.com ([209.85.223.173]:33630) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1Tbnzu-0001rA-VQ for qemu-devel@nongnu.org; Fri, 23 Nov 2012 02:48:27 -0500 Received: by mail-ie0-f173.google.com with SMTP id e13so2761118iej.4 for ; Thu, 22 Nov 2012 23:48:26 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=sender:from:to:cc:subject:date:message-id:x-mailer:in-reply-to :references; bh=nu4oqvk3zkO3xrJesEPb9XjsRlF+kPNa3bLeHoiiL+k=; b=MiJ7l9ZA9Vd+EJL3V+iA1k7kuuzjDUbqO8VNF9BJrgl4mCLOQp6hSuVCFU019QUVYT Vad20gQbp36qnKgFqUUBZWW1+3awdvp1JR/KQ1I9Ubew0l6R20sPy2iAhqBQHfvfifKi +NEMH9dZyWfCXrKqDLPNUGQb8Qj/dESxmEzLfsvestv9FrnKD1AOAggkO4ENTBeW/OEM X9lHTcuhyf3gSGdDTsM41CutQOBSTrcJyJpi0hsoFxbW7LWyt5MQUBOS2vMOCNwhwqyi hT2qG1eX57jNSikZkTDgOcddrovqcoDBzTm82QtOhgCuflkcSX/ZQQAEK/lUKXKF43Cv ud+w== Received: by 10.50.53.193 with SMTP id d1mr2754452igp.69.1353656906647; Thu, 22 Nov 2012 23:48:26 -0800 (PST) Received: from localhost.localdomain ([202.108.130.194]) by mx.google.com with ESMTPS id az6sm3859043igb.11.2012.11.22.23.48.24 (version=TLSv1/SSLv3 cipher=OTHER); Thu, 22 Nov 2012 23:48:25 -0800 (PST) From: Dong Xu Wang To: qemu-devel@nongnu.org Date: Fri, 23 Nov 2012 15:47:38 +0800 Message-Id: <1353656865-20910-4-git-send-email-wdongxu@linux.vnet.ibm.com> X-Mailer: git-send-email 1.7.1 In-Reply-To: <1353656865-20910-1-git-send-email-wdongxu@linux.vnet.ibm.com> References: <1353656865-20910-1-git-send-email-wdongxu@linux.vnet.ibm.com> X-detected-operating-system: by eggs.gnu.org: GNU/Linux 3.x [fuzzy] X-Received-From: 209.85.223.173 Cc: kwolf@redhat.com, Dong Xu Wang Subject: [Qemu-devel] [PATCH V6 03/10] qemu-option: qemu_opt_set_bool(): fix code duplication 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 It will set opt->str in qemu_opt_set_bool, without opt->str, there will be some potential bugs. These are uses of opt->str, and what happens when it isn't set: * qemu_opt_get(): returns NULL, which means "not set". Bug can bite when value isn't the default value. * qemu_opt_parse(): passes NULL to parse_option_bool(), which treats it like "on". Wrong if the value is actually false. Bug can bite when qemu_opts_validate() runs after qemu_opt_set_bool(). * qemu_opt_del(): passes NULL to g_free(), which is just fine. * qemu_opt_foreach(): passes NULL to the callback, which is unlikely to be prepared for it. * qemu_opts_print(): prints NULL, which crashes on some systems. * qemu_opts_to_qdict(): passes NULL to qstring_from_str(), which crashes. It also makes qemu_opt_set_bool more readable by using find_desc_by_name and opts_accepts_any. It is based on Luiz's patch and uses Markus's comments. Discussions can be found at: http://lists.nongnu.org/archive/html/qemu-devel/2012-07/msg02716.html Signed-off-by: Dong Xu Wang --- qemu-option.c | 27 +++++++++------------------ 1 files changed, 9 insertions(+), 18 deletions(-) diff --git a/qemu-option.c b/qemu-option.c index 74321bb..e0131ce 100644 --- a/qemu-option.c +++ b/qemu-option.c @@ -677,30 +677,21 @@ int qemu_opt_set_bool(QemuOpts *opts, const char *name, bool val) { QemuOpt *opt; const QemuOptDesc *desc = opts->list->desc; - int i; - for (i = 0; desc[i].name != NULL; i++) { - if (strcmp(desc[i].name, name) == 0) { - break; - } - } - if (desc[i].name == NULL) { - if (i == 0) { - /* empty list -> allow any */; - } else { - qerror_report(QERR_INVALID_PARAMETER, name); - return -1; - } + opt = g_malloc0(sizeof(*opt)); + opt->desc = find_desc_by_name(desc, name); + if (!opt->desc && !opts_accepts_any(opts)) { + qerror_report(QERR_INVALID_PARAMETER, name); + g_free(opt); + return -1; } - opt = g_malloc0(sizeof(*opt)); opt->name = g_strdup(name); opt->opts = opts; - QTAILQ_INSERT_TAIL(&opts->head, opt, next); - if (desc[i].name != NULL) { - opt->desc = desc+i; - } opt->value.boolean = !!val; + opt->str = g_strdup(val ? "on" : "off"); + QTAILQ_INSERT_TAIL(&opts->head, opt, next); + return 0; }