From nobody Thu Dec 26 16:43:59 2024 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=pass; 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=pass(p=none dis=none) header.from=gmail.com ARC-Seal: i=1; a=rsa-sha256; t=1732876417; cv=none; d=zohomail.com; s=zohoarc; b=iB3bWRG9lNNTqN4mqAe657SJY9ZmAIxo8U3THF33ErQHmaoQSt0NuIuuyuhq5J4PQeHNrw2wKYJKBZkSQog0naZnQEc4SbBSGpeXM+ZDBoWtYaLG3NGZ3c1KV4Da5/8ltJDgT4MPOirS9EjG1x9xjxhObNjXL9P6PKSgDSA0Gv0= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1732876417; h=Content-Type:Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:Subject:Message-Id:Reply-To:To; bh=dXJ6HW1nN5mggE0fLVWeH4H4Bs/Jql1OpaGTmx1GQo8=; b=V0n6VZg5+TJlyutcjD+kTrepzOhunhC6N9G5C1Y/wOTXqpig3Vpm/9p9f2gJNS2qPwrcyY6mUGgEWyRJojOMlWt534ky9D79K7mR1vGoELCAZjKIgcE0VHHS/fnFwilPpEJEmxUpc2wE4ppoXlcETrey9uh+d6lo+V75DWGzRg4= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; 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=pass header.from= (p=none dis=none) Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1732876417135478.2032282278866; Fri, 29 Nov 2024 02:33:37 -0800 (PST) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1tGyJH-0005rr-DG; Fri, 29 Nov 2024 05:33:15 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1tGyJF-0005rO-3l for qemu-devel@nongnu.org; Fri, 29 Nov 2024 05:33:13 -0500 Received: from mail-ej1-x629.google.com ([2a00:1450:4864:20::629]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1tGyJD-0001UW-Kt for qemu-devel@nongnu.org; Fri, 29 Nov 2024 05:33:12 -0500 Received: by mail-ej1-x629.google.com with SMTP id a640c23a62f3a-a9f1d76dab1so304762566b.0 for ; Fri, 29 Nov 2024 02:33:11 -0800 (PST) Received: from think.fritz.box ([2a02:8109:8384:1400:4166:755a:3476:5f1c]) by smtp.gmail.com with ESMTPSA id a640c23a62f3a-aa599953c81sm160300266b.184.2024.11.29.02.33.09 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 29 Nov 2024 02:33:09 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1732876390; x=1733481190; darn=nongnu.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=dXJ6HW1nN5mggE0fLVWeH4H4Bs/Jql1OpaGTmx1GQo8=; b=mephaf4kb4izOnqoPNPr6b/Z0zaUiG61Jmo+hWD0oMAg2i0gB7NYTnX5Gv2QXR5XEM 96WN6nDWo1XeZWPEaWx3T7E/GOcXebBo3/V42I5Cvak7Qf00yX8KibPMw85xXJOMU6Eg gysSaQVsp2YYpE0ylGBv4dSluxPbNNoZjPIFjm4RkLNOmkyMoCFpIwl0FrvuGmQ+vT6M WzmwVcdm0rU2RZDHjQW61XEFVaWybLtTKSuHEfVevTUmaD/aad0Yf0brCrZMZl0Y+7xZ Gd/PW0XrC8RtYH9Gojn6yrxj7n8BWztgmse7SeUg+i/YKaT0E+j+eJtgc0H/aN8d324g ydhA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1732876390; x=1733481190; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=dXJ6HW1nN5mggE0fLVWeH4H4Bs/Jql1OpaGTmx1GQo8=; b=G9o567OZalqcoTurrL43ZkD2g//MRGyoYJrIiUW7pW0HAKGCQNQHc0YyojBGZR4ays +X+BMsBIqcofnisU1qpx22dpnqH0f3wDmdoVF7T2VQwYjnNQ2uMOe4UkI7IxybMu1TPU g2gHYqbixHIAcMZ+A42fNVYx28Mhpo7jCXbtQ/TBN77VTRSxbkKw9iAcUli31JwGsm6U +qfxqbqSTVRGWThuwrKOPEzeuB4e/ftEwynW/5S9wiU9FXh30Wq//TMfoOhaScVO15kR 3QfkKIs+3+70PnPn6cEbJ/oWrQTi6UvYz28+yVIO6TajRlDtkrQay7G8gtlJZMKHK4gJ o3AA== X-Forwarded-Encrypted: i=1; AJvYcCXasb646ZrHC7bSOdpy3lEgIOcyYKJwuzcOZLQ7nRGdh09G7deQ6QnkRLBDPdhuhXawlopLVg+HhjHB@nongnu.org X-Gm-Message-State: AOJu0YwErLpjXgzKS6pSIVmvmKDWN+5tW+mbEwq3qMzi32Vm9JteRr+J YKWc5sH51rz+ww/bROCoDQkBFD+BBL8JdkhVZX+mxXDBXIx/R47E X-Gm-Gg: ASbGnctQFH7q3MfTGv5y3t+ARtldbBCERbiDhC1dDxWKVOV9r1sFE7acwY8LNQLlHxR Esvjcyet18fGm49Xw0iBPzTLfxDfogFgQLtrwGLqmwnmylFYYE+tlaG0jcRCFA86HbRSuqTLb98 HSQ4iYJM9o99nsOEvzfl31ATqwAmJNnihtfKDFWRKBt7QfRsMysbrpGTU5dbd2nFy7t3re3kfS4 X4zxM9DBsNSGTEPiZ9wiLrdxrT4TXXCivROUJMQdpjItCtYHHPWw0nPgic= X-Google-Smtp-Source: AGHT+IEQVv6woXeDsHkvJ5y2fGtiXtJqlAK2NvGRMO6u74vpDHUqaK/QeiXzyahSd72ojN8vlkaang== X-Received: by 2002:a17:906:310e:b0:a9a:76d:e86c with SMTP id a640c23a62f3a-aa58107180emr1033661366b.49.1732876389923; Fri, 29 Nov 2024 02:33:09 -0800 (PST) From: Roman Penyaev To: Cc: Roman Penyaev , =?UTF-8?q?Marc-Andr=C3=A9=20Lureau?= , Paolo Bonzini , Peter Maydell , =?UTF-8?q?Cl=C3=A9ment=20Mathieu--Drif?= , qemu-devel@nongnu.org Subject: [PATCH v3 1/2] chardev/char-mux: shift unsigned long to avoid 32-bit overflow Date: Fri, 29 Nov 2024 11:32:38 +0100 Message-Id: <20241129103239.464061-2-r.peniaev@gmail.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20241129103239.464061-1-r.peniaev@gmail.com> References: <20241129103239.464061-1-r.peniaev@gmail.com> MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" 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::629; envelope-from=r.peniaev@gmail.com; helo=mail-ej1-x629.google.com X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, FREEMAIL_FROM=0.001, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: qemu-devel-bounces+importer=patchew.org@nongnu.org X-ZohoMail-DKIM: pass (identity @gmail.com) X-ZM-MESSAGEID: 1732876417724019100 Allthough the size of MAX_MUX is equal to 4 and likely will never change, this patch changes type of constant to unsigned long to be on the safe side. Also add a static compile check that MAX_MUX never bigger than `sizeof(d->mux_bitset) * BITS_PER_BYTE`. Signed-off-by: Roman Penyaev Reviewed-by: "Marc-Andr=C3=A9 Lureau" Cc: Paolo Bonzini Cc: Peter Maydell Reviewed-by: Cl=C3=A9ment Mathieu--Drif Cc: qemu-devel@nongnu.org --- chardev/char-mux.c | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/chardev/char-mux.c b/chardev/char-mux.c index bda5c45e6058..9d67b8bd9ab8 100644 --- a/chardev/char-mux.c +++ b/chardev/char-mux.c @@ -316,6 +316,8 @@ bool mux_chr_attach_frontend(MuxChardev *d, CharBackend= *b, { unsigned int bit; =20 + QEMU_BUILD_BUG_ON(MAX_MUX > (sizeof(d->mux_bitset) * BITS_PER_BYTE)); + bit =3D find_next_zero_bit(&d->mux_bitset, MAX_MUX, 0); if (bit >=3D MAX_MUX) { error_setg(errp, @@ -325,7 +327,7 @@ bool mux_chr_attach_frontend(MuxChardev *d, CharBackend= *b, return false; } =20 - d->mux_bitset |=3D (1 << bit); + d->mux_bitset |=3D (1ul << bit); d->backends[bit] =3D b; *tag =3D bit; =20 @@ -341,7 +343,7 @@ bool mux_chr_detach_frontend(MuxChardev *d, unsigned in= t tag) return false; } =20 - d->mux_bitset &=3D ~(1 << bit); + d->mux_bitset &=3D ~(1ul << bit); d->backends[bit] =3D NULL; =20 return true; --=20 2.34.1 From nobody Thu Dec 26 16:43:59 2024 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=pass; 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=pass(p=none dis=none) header.from=gmail.com ARC-Seal: i=1; a=rsa-sha256; t=1732876418; cv=none; d=zohomail.com; s=zohoarc; b=bqNgWr8nuXg4WMfJQUSZc8UKWY3+WolPloyPhGD+lyz4xXNGRgwrw0+qeBMqXYmD9PorUvp1hEtZ+cBEx2DWF0jZdXRuo5fLHh7ekgULE0wwoBLsxPUcsuuhGiKyjKqYeksltJuWo7aTM3D5gpNv76hpcCUwEcUTXYeBgYHPrLs= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1732876418; h=Content-Type:Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:Subject:Message-Id:Reply-To:To; bh=f20yQ3A8rIaA9LjjtPbwfAgssJYyZVaFYuEIkxbdWcA=; b=KvLnEMkn8zUGvvo8+TGQMhS+E8/NEcb5WF7PVV6IGmfBzfxdhvhYUNhnIa9o7g6J1iBRJrpdJqGbZUvP7pgLNiQiovmvblcZckpEXNrtyyIFhKpcQJkFs4OjvDXqCRaqeNdwxUgV7o12lanwbvig2Q+Hd4ljAqh76Bprb8WEswk= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; 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=pass header.from= (p=none dis=none) Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 173287641800360.615741101811636; Fri, 29 Nov 2024 02:33:38 -0800 (PST) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1tGyJI-0005sM-04; Fri, 29 Nov 2024 05:33:16 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1tGyJG-0005rd-8h for qemu-devel@nongnu.org; Fri, 29 Nov 2024 05:33:14 -0500 Received: from mail-ed1-x532.google.com ([2a00:1450:4864:20::532]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1tGyJE-0001VH-QL for qemu-devel@nongnu.org; Fri, 29 Nov 2024 05:33:14 -0500 Received: by mail-ed1-x532.google.com with SMTP id 4fb4d7f45d1cf-5cec9609303so1910061a12.1 for ; Fri, 29 Nov 2024 02:33:12 -0800 (PST) Received: from think.fritz.box ([2a02:8109:8384:1400:4166:755a:3476:5f1c]) by smtp.gmail.com with ESMTPSA id a640c23a62f3a-aa599953c81sm160300266b.184.2024.11.29.02.33.10 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 29 Nov 2024 02:33:10 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1732876391; x=1733481191; darn=nongnu.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=f20yQ3A8rIaA9LjjtPbwfAgssJYyZVaFYuEIkxbdWcA=; b=QsbhR/F4LVEWTgHXyKThDZQ/90e5q+bsojt6c1g6LApxhJC1+2Khc8fdVlwMf9D/A1 vNRh/uNLV2d4ZwrtRo/sVCGg/VJ4vs+mFv7/25BJXqa5h1x8uF9INbNP+rSBNa2h9lqN D0FpzW7jkNDpAc43tDKnWrIwXQlYTxXEez602IEc5D92dGE47Y4k7F1u9pU2xSWs3nUh amPFCjjt5lNni7OsI2iK2SEfy1npJlbvl2jMpehyuLHYIA40bHPyNt5VsIcB/443KkIH OJ6anlTaITnL+5BNp2SWR7XQgmd4A05Ttiv22MYh9Goj+1PglCjX5i/lW/S5WWbuW77i dbLg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1732876391; x=1733481191; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=f20yQ3A8rIaA9LjjtPbwfAgssJYyZVaFYuEIkxbdWcA=; b=ahMpYojxrJhRq8mg1WR3+RdxcSgljsd0/O5X7jMq9Vau94XhQ8DrZ8PmB20gC1ho0G nhBsh6FiCtTyZahlM9bR4h6puxTIX05MEsTHYgasK8aEUmPbkEGmodwetEVtExaXmT0q qMr+tCdKBlDFM+cQBW9cd+VnWHPV5+7srny7CIEzjbWIc8PdyjwxV/5gUKpdWXGCpL6K t1hSG2hvYrff52uyg848oyZBNZHdVgO3wn295Bar8oULO/CrU7G0BEsb0HwSwaKgNlC4 feFsZXwV58jm4W2iczBRBjj6DdIwZ391itR0wc+1Fz70FWW+LZOFXJze4HdeNQmwngmf 8a6w== X-Forwarded-Encrypted: i=1; AJvYcCVVEfssM8DBj/HyKz15vxv7c6rp1kgjYkAMDufakn+rdRCke1F2rHNBrILbmDM6hvM4bfiH2YpYbbfT@nongnu.org X-Gm-Message-State: AOJu0YwyJhJHuIrbIMKVyda4PSh+nfa0PEfHWtiNBevOUJyCCUUVIMmi uk9Arcp4V8SkCKX4WHIkEDMgHo4I+SKWgZjkomijwVgPrJtHD/o0 X-Gm-Gg: ASbGncu/1N6Sv9fFTPzyyR7EVOesXQNowLVMZOa5sYcQFNgy4Ldj9el/4H/2P+HmiJ9 a9p7MK/VuC17saQdrtzffbhabzrABl6+swAPIqrtco2hqBmmeulg5Xyf6t9jGuFygPr3zHukch+ 4WDAqg2xZOOOM9UAF96ePuWGRXI9znpcH/zJRUhsYhQkhrM+ZUu0eFvX0KXxPnH9pfCjgEUSULA fKHyb1STw44qRbsxTnZCsNQjxCsflhpKyqTJfOGv7QedXxwfzTGE5VlJrc= X-Google-Smtp-Source: AGHT+IHcAktCZZC0SsOGWOFYlC/ynrOyUqqjtiCRXIERhgFeKwN3MlRycLL4TytiStGcAQGMmnRsuw== X-Received: by 2002:a17:907:254e:b0:a99:529d:81ae with SMTP id a640c23a62f3a-aa581062752mr768508366b.55.1732876391152; Fri, 29 Nov 2024 02:33:11 -0800 (PST) From: Roman Penyaev To: Cc: Roman Penyaev , =?UTF-8?q?Marc-Andr=C3=A9=20Lureau?= , Peter Maydell , Paolo Bonzini , =?UTF-8?q?Cl=C3=A9ment=20Mathieu--Drif?= , qemu-devel@nongnu.org Subject: [PATCH v3 2/2] chardev/char-mux: make boolean bit check instead of find_next_bit() Date: Fri, 29 Nov 2024 11:32:39 +0100 Message-Id: <20241129103239.464061-3-r.peniaev@gmail.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20241129103239.464061-1-r.peniaev@gmail.com> References: <20241129103239.464061-1-r.peniaev@gmail.com> MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" 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::532; envelope-from=r.peniaev@gmail.com; helo=mail-ed1-x532.google.com X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, FREEMAIL_FROM=0.001, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: qemu-devel-bounces+importer=patchew.org@nongnu.org X-ZohoMail-DKIM: pass (identity @gmail.com) X-ZM-MESSAGEID: 1732876419769019100 This patch simplifies (and makes less confusing) bit checks by replacing `find_next_bit()` calls with boolean AND operation. Resolves: Coverity CID 1563776 Signed-off-by: Roman Penyaev Reviewed-by: "Marc-Andr=C3=A9 Lureau" Cc: Peter Maydell Cc: Paolo Bonzini Reviewed-by: Cl=C3=A9ment Mathieu--Drif Cc: qemu-devel@nongnu.org --- chardev/char-mux.c | 11 ++++------- 1 file changed, 4 insertions(+), 7 deletions(-) diff --git a/chardev/char-mux.c b/chardev/char-mux.c index 9d67b8bd9ab8..e13042d3810c 100644 --- a/chardev/char-mux.c +++ b/chardev/char-mux.c @@ -336,15 +336,12 @@ bool mux_chr_attach_frontend(MuxChardev *d, CharBacke= nd *b, =20 bool mux_chr_detach_frontend(MuxChardev *d, unsigned int tag) { - unsigned int bit; - - bit =3D find_next_bit(&d->mux_bitset, MAX_MUX, tag); - if (bit !=3D tag) { + if (!(d->mux_bitset & (1ul << tag))) { return false; } =20 - d->mux_bitset &=3D ~(1ul << bit); - d->backends[bit] =3D NULL; + d->mux_bitset &=3D ~(1ul << tag); + d->backends[tag] =3D NULL; =20 return true; } @@ -353,7 +350,7 @@ void mux_set_focus(Chardev *chr, unsigned int focus) { MuxChardev *d =3D MUX_CHARDEV(chr); =20 - assert(find_next_bit(&d->mux_bitset, MAX_MUX, focus) =3D=3D focus); + assert(d->mux_bitset & (1ul << focus)); =20 if (d->focus !=3D -1) { mux_chr_send_event(d, d->focus, CHR_EVENT_MUX_OUT); --=20 2.34.1