From nobody Sun Nov 16 20:47:26 2025 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=fail; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail(p=none dis=none) header.from=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1601902622; cv=none; d=zohomail.com; s=zohoarc; b=cA6u9K8bfC3HN7Wlb0Sb4DrDVWTFbFLPXku96r3E2DJKHQJ9QQiwyUUDiZ21NV+k0rAiH3TIbsGbDHOrtvuVojWqtSgEBYsnNDZH5EHAp9T+T0YvRKxUM0Z96KkPDEtBN/LMgSn8xLUM8/fvT0gLuo2/O8ZqnODa2LiLRcEInOg= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1601902622; h=Content-Transfer-Encoding:Cc:Date:From:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:Sender:Subject:To; bh=wlWVSOO/aREpLlLadE6mtcQSK8dWwT8nqz07OWfYY0c=; b=GLDU/ABgm9XHZxVuLQUHoSyaJkFsfxZbeRNI8H1SVIgQyjI2fWaufjh0x3756+Up9WA8nHZhOYaPQVVc9EmFfQqUUqK5LQ58IPCAfXwBfR/t+wSSgHVKefuGRyZiqHQev3hQ7p6qkiJ498l2H2ZijjLtiAl1eVDKG7rry7U7SAY= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=fail; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail header.from= (p=none dis=none) header.from= Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1601902622029768.6372067345425; Mon, 5 Oct 2020 05:57:02 -0700 (PDT) Received: from localhost ([::1]:39554 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1kPQ32-0001A8-Ay for importer@patchew.org; Mon, 05 Oct 2020 08:57:00 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:39656) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1kPQ1c-0000Go-C9; Mon, 05 Oct 2020 08:55:32 -0400 Received: from mail-wm1-x344.google.com ([2a00:1450:4864:20::344]:51088) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1kPQ1a-0000q8-Cx; Mon, 05 Oct 2020 08:55:32 -0400 Received: by mail-wm1-x344.google.com with SMTP id 13so8506397wmf.0; Mon, 05 Oct 2020 05:55:28 -0700 (PDT) Received: from localhost.localdomain ([2001:b07:6468:f312:2e86:3d8b:8b70:920c]) by smtp.gmail.com with ESMTPSA id m14sm4499528wrx.62.2020.10.05.05.55.26 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 05 Oct 2020 05:55:26 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=sender:from:to:cc:subject:date:message-id:mime-version :content-transfer-encoding; bh=wlWVSOO/aREpLlLadE6mtcQSK8dWwT8nqz07OWfYY0c=; b=grZkoh9vK0eDXQ/x75t891GfLow7DrtdbDy1tL4nb473KomrtawONgf3UV58zqppmk Cgtk6ybVhP3UcbTTFMf2z7O4eCb9xV8yA9huBPhjsbC0Ec6dYG1CdliFh9V5UwoFmPMp +7eVgLg9lwVCEOEvShm5lRRv3Ud59NsDN9BpIWSt1JeY1KRlMuCF5HK1snWwqFxDMXy9 wHpOU88lIJ2/V5D0ohzRQMaRJfrEz3/zlOuRfFgiDSg8e0CYS2S2k7aoRjA2FFHDr1EG KJV0/emuDg+fkhVkBCSCipX+fP7lSPU0OLdMEEQtqTVNwLT7t7TWp7EEuBHDxpbU3o+B 9lcg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:sender:from:to:cc:subject:date:message-id :mime-version:content-transfer-encoding; bh=wlWVSOO/aREpLlLadE6mtcQSK8dWwT8nqz07OWfYY0c=; b=lDjl/Dfaoa9ySf5Ua8JD32onR84iJxH4Yt5D/HzD0i8x3ngulU80dGlHyaDhWrVJTa xQ/OjupOkVpQQMCI6Y0CzrA4ldmlqIFif12jetHjZT7gaJ4o2ALmn5AZpMfImNXP3R8D 1Aq0WAHDsFwBwyOM+hMTNa4taHKNoXiM8DzuG9XzvxnZm9r1oBm/Kc5iGGhr2vXHyTTa t0JZJkxewt6Sxo82Vwx63Fgdl3xd5aev//NjGyUElwoWQDJDGxlmZGZ12PVUdmWsefje 05pwnAuIpCjvuWEB4DOdIt9q0XIWKSYM+0swKV8kCqj7mkNkQx3HkA+mLzJnDAlQ3Z7L f78w== X-Gm-Message-State: AOAM532XGDSJ0ELw0RNxj7jHsIsQMkTtz3GFgIpVY8sJynSSSPxIDHxW c5u3Sn0h06hXetq2p5hF3NdJ+r6RoQs= X-Google-Smtp-Source: ABdhPJxr4vYzwP1moB3VcZGzdhFLXFmbfLxYenm70Ah5UcIXbshdeVtFGOrzHknF31YIUlc9Y2IZQQ== X-Received: by 2002:a1c:7c12:: with SMTP id x18mr3109972wmc.107.1601902527029; Mon, 05 Oct 2020 05:55:27 -0700 (PDT) From: Paolo Bonzini To: qemu-devel@nongnu.org Subject: [PATCH] ide: clean up ahci_populate_sglist Date: Mon, 5 Oct 2020 14:55:27 +0200 Message-Id: <20201005125527.429187-1-pbonzini@redhat.com> X-Mailer: git-send-email 2.26.2 MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=2a00:1450:4864:20::344; envelope-from=paolo.bonzini@gmail.com; helo=mail-wm1-x344.google.com X-detected-operating-system: by eggs.gnu.org: No matching host in p0f cache. That's all we know. X-Spam_score_int: -14 X-Spam_score: -1.5 X-Spam_bar: - X-Spam_report: (-1.5 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_EF=-0.1, FREEMAIL_FORGED_FROMDOMAIN=0.248, FREEMAIL_FROM=0.001, HEADER_FROM_DIFFERENT_DOMAINS=0.249, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=no autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: jsnow@redhat.com, qemu-block@nongnu.org Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: fail (Header signature does not verify) Content-Type: text/plain; charset="utf-8" Alex reported an uninitialized variable warning in ahci_populate_sglist. Even though the warning is bogus and happens only because of -Og, the code in the function leaves something to be desired; the condition that triggers the warning is easily shown to be entirely redundant. In particular, the loop's "if" condition can be rewritten from "offset < sum + tbl_entry_size" to "offset - sum < tbl_entry_size"; this is safe since the LHS cannot underflow. Because off_pos is exactly "offset - sum" it is clear that it can never be less than zero or greater than tbl_entry_size. We can therefore keep the off_idx check only and, for documentation purposes, reduce off_pos to an unsigned 32-bit integer. The tracepoint also is not particularly useful at this point, since we know that (if it ever triggers) off_idx will be -1 and off_pos uninitialized. Instead, include the requested offset and the total PRDT length, which will be smaller than the offset. Signed-off-by: Paolo Bonzini Reported-by: Alex Benn=C3=A9e Reviewed-by: Philippe Mathieu-Daud=C3=A9 --- hw/ide/ahci.c | 12 +++++------- hw/ide/trace-events | 2 +- 2 files changed, 6 insertions(+), 8 deletions(-) diff --git a/hw/ide/ahci.c b/hw/ide/ahci.c index 680304a24c..997b67a6fc 100644 --- a/hw/ide/ahci.c +++ b/hw/ide/ahci.c @@ -924,8 +924,7 @@ static int ahci_populate_sglist(AHCIDevice *ad, QEMUSGL= ist *sglist, int r =3D 0; uint64_t sum =3D 0; int off_idx =3D -1; - int64_t off_pos =3D -1; - int tbl_entry_size; + uint32_t off_pos =3D 0; IDEBus *bus =3D &ad->port; BusState *qbus =3D BUS(bus); =20 @@ -952,19 +951,18 @@ static int ahci_populate_sglist(AHCIDevice *ad, QEMUS= GList *sglist, /* Get entries in the PRDT, init a qemu sglist accordingly */ if (prdtl > 0) { AHCI_SG *tbl =3D (AHCI_SG *)prdt; - sum =3D 0; for (i =3D 0; i < prdtl; i++) { - tbl_entry_size =3D prdt_tbl_entry_size(&tbl[i]); - if (offset < (sum + tbl_entry_size)) { + uint32_t tbl_entry_size =3D prdt_tbl_entry_size(&tbl[i]); + if (offset - sum < tbl_entry_size) { off_idx =3D i; off_pos =3D offset - sum; break; } sum +=3D tbl_entry_size; } - if ((off_idx =3D=3D -1) || (off_pos < 0) || (off_pos > tbl_entry_s= ize)) { + if (off_idx =3D=3D -1) { trace_ahci_populate_sglist_bad_offset(ad->hba, ad->port_no, - off_idx, off_pos); + sum, offset); r =3D -1; goto out; } diff --git a/hw/ide/trace-events b/hw/ide/trace-events index 6e357685f9..81706efe80 100644 --- a/hw/ide/trace-events +++ b/hw/ide/trace-events @@ -88,7 +88,7 @@ ahci_populate_sglist(void *s, int port) "ahci(%p)[%d]" ahci_populate_sglist_no_prdtl(void *s, int port, uint16_t opts) "ahci(%p)[= %d]: no sg list given by guest: 0x%04x" ahci_populate_sglist_no_map(void *s, int port) "ahci(%p)[%d]: DMA mapping = failed" ahci_populate_sglist_short_map(void *s, int port) "ahci(%p)[%d]: mapped le= ss than expected" -ahci_populate_sglist_bad_offset(void *s, int port, int off_idx, int64_t of= f_pos) "ahci(%p)[%d]: Incorrect offset! off_idx: %d, off_pos: %"PRId64 +ahci_populate_sglist_bad_offset(void *s, int port, uint64_t sum, uint64_t = offset) "ahci(%p)[%d]: Incorrect offset! total PRDT length %"PRIu64", offse= t: %"PRIu64 ncq_finish(void *s, int port, uint8_t tag) "ahci(%p)[%d][tag:%d]: NCQ tran= sfer finished" execute_ncq_command_read(void *s, int port, uint8_t tag, int count, int64_= t lba) "ahci(%p)[%d][tag:%d]: NCQ reading %d sectors from LBA %"PRId64 execute_ncq_command_unsup(void *s, int port, uint8_t tag, uint8_t cmd) "ah= ci(%p)[%d][tag:%d]: error: unsupported NCQ command (0x%02x) received" --=20 2.26.2