From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from smtp2.osuosl.org (smtp2.osuosl.org [140.211.166.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 321562BDC2B for ; Fri, 26 Sep 2025 11:46:08 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=140.211.166.133 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1758887170; cv=none; b=Ngh07Trv5jopfp4Xhl94CeqbhruZWAPhoAKSCw+TtndyhDnL5nGCQzrmybbag4WyD/pfpwDYtZ1b1Uw3oG1jGH2cbyyHJeoxaLBcFghYDQz23ILAt7e8AwJAIx0HI9qIMCidglaA5kcC1l24hKKOXmlouX21nazCPNQn5ivkckw= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1758887170; c=relaxed/simple; bh=1id3B6Wi8UowOcZ+9wbUIN5SOAeHkmeYMI59q8iHO5g=; h=Date:From:To:Cc:Subject:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=PclkVcHMX0QjQ/SMX1LeyqXF/N7rKlf1ObDP4QVNzd79CJ/3X6D7ZiBZnYQe24n3xTTv9jPaTWiLLdKIJX5atnrSJJVy9RR9wmjA2R9+DLpsadb95aY41WtQALvO6BOpBwhsIV0Pzv5ThE7H1ii7V/Z1ApyEwu1U60NVEOJ1DLE= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=HExdGLaM; arc=none smtp.client-ip=140.211.166.133 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="HExdGLaM" Received: from localhost (localhost [127.0.0.1]) by smtp2.osuosl.org (Postfix) with ESMTP id B0442424CC for ; Fri, 26 Sep 2025 11:46:08 +0000 (UTC) X-Virus-Scanned: amavis at osuosl.org X-Spam-Flag: NO X-Spam-Score: -2.099 X-Spam-Level: Received: from smtp2.osuosl.org ([127.0.0.1]) by localhost (smtp2.osuosl.org [127.0.0.1]) (amavis, port 10024) with ESMTP id Ag9gMvJt2Kj5 for ; Fri, 26 Sep 2025 11:46:04 +0000 (UTC) Received-SPF: Pass (mailfrom) identity=mailfrom; client-ip=2a00:1450:4864:20::330; helo=mail-wm1-x330.google.com; envelope-from=david.laight.linux@gmail.com; receiver= DMARC-Filter: OpenDMARC Filter v1.4.2 smtp2.osuosl.org A39AF4244E Authentication-Results: smtp2.osuosl.org; dmarc=pass (p=none dis=none) header.from=gmail.com DKIM-Filter: OpenDKIM Filter v2.11.0 smtp2.osuosl.org A39AF4244E Authentication-Results: smtp2.osuosl.org; dkim=pass (2048-bit key, unprotected) header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20230601 header.b=HExdGLaM Received: from mail-wm1-x330.google.com (mail-wm1-x330.google.com [IPv6:2a00:1450:4864:20::330]) by smtp2.osuosl.org (Postfix) with ESMTPS id A39AF4244E for ; Fri, 26 Sep 2025 11:46:00 +0000 (UTC) Received: by mail-wm1-x330.google.com with SMTP id 5b1f17b1804b1-46e23a6617dso20758005e9.0 for ; Fri, 26 Sep 2025 04:46:00 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1758887159; x=1759491959; darn=lists.linuxfoundation.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:subject:cc:to:from:date:from:to:cc:subject:date :message-id:reply-to; bh=WkRDFqbG55HD/5PHsPObmdh+Ov28CTpaGeY2bB6QWYU=; b=HExdGLaMu7rLdVMn8LQO38qpNQAF/9V/rmicCxnwuf1Dy3vW5PQ01rMToIn3HvG1BB O4AHIWYdC7QW2iyaja/3UpT/yQPwGYNS0XnmZRyywUCsx0VhSp5eD+PBZXZtCAGJlgTx ixihjV1+Kq0WfVdctDZONahSUSCzx4M9PXNshDVEZcXZJ08JH4VZrhT8Anx9Mlbk3iHi El9Acjv/IG1us8hzHDZKsiZBu12k/2uMQ6CzsrQdKyeeKnZUeWQHYMTwhp6ngf21d+rS t6PGM3XFPGt7ys17eGLi/KypxtNH/ZuklDAOoc0OU7w11EEfmtaPfOKQ/rmIVQPoXX2w 0qVw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1758887159; x=1759491959; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:subject:cc:to:from:date:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=WkRDFqbG55HD/5PHsPObmdh+Ov28CTpaGeY2bB6QWYU=; b=OeKFkF/pxba8BEav3BGeMffn46AmoHHjwYb3lMCdKI9rbE8g3UpAa0CVzn5cJVVDsS EFi1m5xw11DuOt5JVxiBMDtrre/8c2dYyc+PpcIT2Kn7NUzgW1vvzYB+1w6L/el2vm0R 0IcjMd3eBjNqBOkpBI4SxYy/LKduEpTrmVlyYS7srs02urQM9VdTTxM7rBAOte/B2J8s 1igIv0y1AVMXVaC3AHJhT7mcEiv5zw2XqRXVu21VNUG+fubPV2c7wvxWsMOeAjeAKE6y U6spGvwNOzwgZ3dNaZBfATzIiHTMwBjXzzCk/cA8MwjlkFlxT0P8VKVJCq0tsdxO78AN XW7A== X-Forwarded-Encrypted: i=1; AJvYcCWb0jzMno1BCcNElwHruOrPgxbWjwxawgpqW3tP72QsRG4vKKr2mqUppXu8uwzYXG17Wmgz75r6HQ9s2yq0JqXlVUuefA==@lists.linuxfoundation.org X-Gm-Message-State: AOJu0YwP8SXoL9m92doGx35YOuN7SsvyUAgGvtqyaxaal8k+iXaz+jQP MB/e/zgi/qN9euaaMU2wOGrBXy5k+5IpAdHOur/BKedF5zHQo/Zpymrf X-Gm-Gg: ASbGncvTtbJ9FDGeAkDL12D+jx6OccNPjUNhQczzNjtirgPNOAYFP06PlaWr6fq+EvA JoCUa+3RTXtNLDgZy1uIspQ6vteNknwCPGypwFOXoppDTOWTn/qCnwJA6kkEFhl1s5Sp+J9yKyN rb8Ezjg17u71IO/1iDE5FvoQgumcxKzMB2jHMJ8RKW5xL9C0F6kno1WBjAIIM+bjY429ohh5tXv pWtU00GPzFKXEMHhnS66Futh9UP8rjJ/ybcOTHU6vZeoKd2SaMssNUiQp/arP5wZYKl5JBlqgjE KKiNPcaxOCQKOOEOVLmr4ZdxjFPhdn8Td0BudlXUayEb5sLhP24eQUdo88cD6bTIThcMKmMuA38 jIpY+DxqEpHcFB8qxdGgBIB5h79izqtfUXhKQxUMfq26itw35tLPX46oGEVwJvlaG X-Google-Smtp-Source: AGHT+IEPTFDb685fmiqtI/zbxG32d7v3MRP+VRk71KQ8OOHsQCWx3c0SFbrsAeMCnBPznWcIWNgGnA== X-Received: by 2002:a05:600d:15a:20b0:46e:1e31:8d06 with SMTP id 5b1f17b1804b1-46e329f62b4mr74383735e9.16.1758887158355; Fri, 26 Sep 2025 04:45:58 -0700 (PDT) Received: from pumpkin (82-69-66-36.dsl.in-addr.zen.co.uk. [82.69.66.36]) by smtp.gmail.com with ESMTPSA id 5b1f17b1804b1-46e33105e0bsm37380245e9.5.2025.09.26.04.45.57 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 26 Sep 2025 04:45:58 -0700 (PDT) Date: Fri, 26 Sep 2025 12:45:55 +0100 From: David Laight To: Mehdi Ben Hadj Khelifa Cc: andrii@kernel.org, eddyz87@gmail.com, ast@kernel.org, daniel@iogearbox.net, martin.lau@linux.dev, song@kernel.org, yonghong.song@linux.dev, john.fastabend@gmail.com, kpsingh@kernel.org, sdf@fomichev.me, haoluo@google.com, jolsa@kernel.org, shuah@kernel.org, matttbe@kernel.org, martineau@kernel.org, geliang@kernel.org, davem@davemloft.net, kuba@kernel.org, hawk@kernel.org, linux@jordanrome.com, ameryhung@gmail.com, toke@redhat.com, houtao1@huawei.com, emil@etsalapatis.com, yatsenko@meta.com, isolodrai@meta.com, a.s.protopopov@gmail.com, dxu@dxuuu.xyz, memxor@gmail.com, vmalik@redhat.com, bigeasy@linutronix.de, tj@kernel.org, gregkh@linuxfoundation.org, paul@paul-moore.com, bboscaccy@linux.microsoft.com, James.Bottomley@HansenPartnership.com, mrpre@163.com, jakub@cloudflare.com, bpf@vger.kernel.org, linux-kernel@vger.kernel.org, linux-kselftest@vger.kernel.org, netdev@vger.kernel.org, mptcp@lists.linux.dev, linux-kernel-mentees@lists.linuxfoundation.org, skhan@linuxfoundation.org, david.hunter.linux@gmail.com Subject: Re: [PATCH] selftests/bpf: Add -Wsign-compare C compilation flag Message-ID: <20250926124555.009bfcd6@pumpkin> In-Reply-To: <20250924162408.815137-1-mehdi.benhadjkhelifa@gmail.com> References: <20250924162408.815137-1-mehdi.benhadjkhelifa@gmail.com> X-Mailer: Claws Mail 4.1.1 (GTK 3.24.38; arm-unknown-linux-gnueabihf) Precedence: bulk X-Mailing-List: linux-kernel-mentees@lists.linux.dev List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: 7bit On Wed, 24 Sep 2025 17:23:49 +0100 Mehdi Ben Hadj Khelifa wrote: > -Change all the source files and the corresponding headers > to having matching sign comparisons. 'Fixing' -Wsign-compare by adding loads of casts doesn't seem right. The only real way is to change all the types to unsigned ones. But it is of questionable benefit and make the code harder to read. Consider the following: int x = read(fd, buf, len); if (x < 0) return -1; if (x > sizeof (struct fubar)) return -1; That will generate a 'sign-compare' error, but min(x, sizeof (struct fubar)) doesn't generate an error because the compiler knows 'x' isn't negative. A well known compiler also rejects: unsigned char a; unsigned int b; if (b > a) return; because 'a' is promoted to 'signed int' before it does the check. So until the compilers start looking at the known domain of the value (not just the type) I enabling -Wsign-compare' is pretty pointless. As a matter of interest did you actually find any bugs? David > > Signed-off-by: Mehdi Ben Hadj Khelifa > --- > As suggested by the TODO, -Wsign-compare was added to the C compilation > flags for the selftests/bpf/Makefile and all corresponding files in > selftests and a single file under tools/lib/bpf/usdt.bpf.h have been > carefully changed to account for correct sign comparisons either by > explicit casting or changing the variable type.Only local variables > and variables which are in limited scope have been changed in cases > where it doesn't break the code.Other struct variables or global ones > have left untouched to avoid other conflicts and opted to explicit > casting in this case.This change will help avoid implicit type > conversions and have predictable behavior. > > I have already compiled all bpf tests with no errors as well as the > kernel and have ran all the selftests with no obvious side effects. > I would like to know if it's more convinient to have all changes as > a single patch like here or if it needs to be divided in some way > and sent as a patch series. > > Best Regards, > Mehdi Ben Hadj Khelifa ...