From nobody Mon Apr 29 10:35:07 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) client-ip=208.118.235.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Authentication-Results: mx.zoho.com; spf=pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; Return-Path: Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) by mx.zohomail.com with SMTPS id 1490967606601262.91707481563014; Fri, 31 Mar 2017 06:40:06 -0700 (PDT) Received: from localhost ([::1]:41153 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1ctwma-0003Ov-7Z for importer@patchew.org; Fri, 31 Mar 2017 09:40:04 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:48990) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1ctwlT-0002q4-Pt for qemu-devel@nongnu.org; Fri, 31 Mar 2017 09:38:56 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1ctwlS-00027p-U7 for qemu-devel@nongnu.org; Fri, 31 Mar 2017 09:38:55 -0400 Received: from orth.archaic.org.uk ([2001:8b0:1d0::2]:49013) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1ctwlS-000268-NZ; Fri, 31 Mar 2017 09:38:54 -0400 Received: from pm215 by orth.archaic.org.uk with local (Exim 4.84_2) (envelope-from ) id 1ctwlO-0004A1-Kz; Fri, 31 Mar 2017 14:38:50 +0100 From: Peter Maydell To: qemu-devel@nongnu.org Date: Fri, 31 Mar 2017 14:38:49 +0100 Message-Id: <1490967529-4767-1-git-send-email-peter.maydell@linaro.org> X-Mailer: git-send-email 2.7.4 X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] X-Received-From: 2001:8b0:1d0::2 Subject: [Qemu-devel] [PATCH] qemu-io-cmds: Assert that global and nofile commands don't use ct->perms X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Kevin Wolf , Max Reitz , qemu-block@nongnu.org, patches@linaro.org Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail: RSF_0 Z_629925259 SPT_0 Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" It would be a bug for a command with the CMD_NOFILE_OK or CMD_FLAG_GLOBAL flags set to also set the ct->perms field, because the former says "OK for a file not to be open" but the latter is a check on a file. Add an assertion in qemuio_add_command() so we can catch that sort of buggy command definition immediately rather than it being a bug that only manifests when a particular set of command line options is used. (Coverity gets confused about this (CID 1371723) and reports that we might dereference a NULL blk pointer in this case, because it can't tell that that code path never happens with the cmdinfo_t that we have. This commit won't help unconfuse it, but it does fix the underlying issue.) Signed-off-by: Peter Maydell --- We could also try to add some kind of assert in command() to persuade coverity that it can't get there with a NULL block and ct->perms non-zero, but I think I'd rather just mark the issue as a false-positive and move on. qemu-io-cmds.c | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/qemu-io-cmds.c b/qemu-io-cmds.c index 2c48f9c..883f53b 100644 --- a/qemu-io-cmds.c +++ b/qemu-io-cmds.c @@ -35,6 +35,13 @@ static int compare_cmdname(const void *a, const void *b) =20 void qemuio_add_command(const cmdinfo_t *ci) { + /* ci->perm assumes a file is open, but the GLOBAL and NOFILE_OK + * flags allow it not to be, so that combination is invalid. + * Catch it now rather than letting it manifest as a crash if a + * particular set of command line options are used. + */ + assert(ci->perm =3D=3D 0 || + (ci->flags & (CMD_FLAG_GLOBAL | CMD_NOFILE_OK)) =3D=3D 0); cmdtab =3D g_renew(cmdinfo_t, cmdtab, ++ncmds); cmdtab[ncmds - 1] =3D *ci; qsort(cmdtab, ncmds, sizeof(*cmdtab), compare_cmdname); --=20 2.7.4