Received: by 2002:a05:6a10:1a4d:0:0:0:0 with SMTP id nk13csp900837pxb; Tue, 1 Feb 2022 12:46:44 -0800 (PST) X-Google-Smtp-Source: ABdhPJwPbHCJ1KHfWMd6IMZp1hgwCl68x3gDCy77FF55SXXeBL3DOISXJSXDJmaj6E021vSxbI+x X-Received: by 2002:a17:90a:f0c9:: with SMTP id fa9mr4346219pjb.131.1643748403964; Tue, 01 Feb 2022 12:46:43 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1643748403; cv=none; d=google.com; s=arc-20160816; b=N+/1UKYtHMVCmwv9mw9Bycg/k2ZM5rPC+v4OPiJeyBj/3n5y3K1eZ2Q+41V+ztTamB CBlPo5aGvlNO7oFD+PCTwkpnFoq5a/DMttGcrgh7X5Zj4/cvobFZUaEKF4cNZs1kb1L+ ZWxKmikYHO75P/gMDYM58/HFmqsZtmyhXH3wynVVmjnTXWZlQb6z7dIYuLAxnlC/+4l7 Gx/2AyryvD6Jm5/oz6KASsHgoWM3sghPIrgDjmHRxcEV0VRAtWrD3T9p8KXyuRjwUVNT VZrJIETrWQYp7LqPQMobPUEdjxBWunB/r6zmv7tr1cRWAfGrNMnq/4nsMU6zwY4b2nTF J9pw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:content-disposition :mime-version:message-id:subject:cc:to:from:date:dkim-signature; bh=5vGwoTC1e4kUUVS5mywGbNPZbTA57tIfGZpAbVJZdPc=; b=kwxwbWXXggf189bKQZeO6T2sqnL/dqX1rycsTSFOsBeBwZuJVm5k7ssgGWJe4hSAlB wi3WbPDpVxMveA3Z271LfYPsn957Ngex07QzBXmNXwS2coqapuHDnC+TGOx5/PFtb01P 1h4I6dw+iYAAs3d6N2CA7NAfe69u7qU3/RuDMDe4+20Z6SV1cLU3eBHwPIKMmVULX890 JdfOAXTzgffL+hhLF65zuuz5d/UI5iq1grm+XWTz1zZNZIPFoMMFXZBTwJuFlfTzfAeh Y7oAiq+lbdJg1s0SYqL8FhuNwk6fUNstYoGem+VtlZLPR+pLe2DQ/V8XahdWLYVszbXH r7vQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@usp.br header.s=usp-google header.b="uPAa/sk2"; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=QUARANTINE sp=NONE dis=NONE) header.from=usp.br Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id y11si19237150plg.343.2022.02.01.12.46.32; Tue, 01 Feb 2022 12:46:43 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@usp.br header.s=usp-google header.b="uPAa/sk2"; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=QUARANTINE sp=NONE dis=NONE) header.from=usp.br Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1350290AbiAaSXI (ORCPT + 99 others); Mon, 31 Jan 2022 13:23:08 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:34530 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1354821AbiAaSWt (ORCPT ); Mon, 31 Jan 2022 13:22:49 -0500 Received: from mail-ot1-x32a.google.com (mail-ot1-x32a.google.com [IPv6:2607:f8b0:4864:20::32a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 6382EC06173B for ; Mon, 31 Jan 2022 10:22:49 -0800 (PST) Received: by mail-ot1-x32a.google.com with SMTP id j38-20020a9d1926000000b0059fa6de6c71so13808000ota.10 for ; Mon, 31 Jan 2022 10:22:49 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=usp.br; s=usp-google; h=date:from:to:cc:subject:message-id:mime-version:content-disposition :content-transfer-encoding; bh=5vGwoTC1e4kUUVS5mywGbNPZbTA57tIfGZpAbVJZdPc=; b=uPAa/sk2IOzeJCHeNYiZWThya96U6MZM4TNQ3W7xElP3WnOciW2NWRUaZxJqVc/BWD SL9+Z0BaL2cO27liA1EJhEw4sAE9ciMh8O+O4iz/CJqfzFRMEHGzU+1OOZ5WFXzR/YE+ g1Q8GfZ4Hpqk2ocPtEBaUT3M8xpubQ9wvpghAK6NWnS0MkjnNLfCK55fLZFBoSyXYCnP tdF07TqTEznFa0zfc2THo6oHqjc6ZackuizbICp61oOu6IHX6qQ5K3OycwutEP6Fv99c X34D8BlZb6UKTusYwwckqKWB+lmWP1YmTeOEbtEUUsy+0rEg1FJl/gbV3AzZWoYGZkhC VCWA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:date:from:to:cc:subject:message-id:mime-version :content-disposition:content-transfer-encoding; bh=5vGwoTC1e4kUUVS5mywGbNPZbTA57tIfGZpAbVJZdPc=; b=CTXnJjT/RuMb2VrLHAz5hMy7JszzZpsDH9KNYD4C2N+vIsdxYu2NEjM0C0d6ITviMD d8PrDQWyTcjzpt6heHUOGTCQFUWgjQJ8ogm7vDxuSXtTNfMB3gnwhu8UVUdCmlHuoNwz e8hMEl/IFIrlBe4GBOo2jgrYqc0OGAW5EZYResBbfUedCiD6mK9YqSLVyIFdxLOnEN2i rgB08daoDdgEnv8mVKNQAZ55d9HR4Ky9PIIC+NhrE3i/mKu9z0HqXwHBF73IqW2s4EkI 4yP3t0YwENGo5olDj58KA75htLknEgKNujkY8R2quXUNXXwfxq6ii27PJWs4RoWiJpw3 VGBg== X-Gm-Message-State: AOAM530eHvwf9ymlXg4Rmh6soHR6LTntLi5FocXFIYP98AKd8pCbADzY EqInpz0hGpCvGvUh70C2OtEctw== X-Received: by 2002:a9d:1f0:: with SMTP id e103mr9137176ote.234.1643653368677; Mon, 31 Jan 2022 10:22:48 -0800 (PST) Received: from fedora ([187.36.236.204]) by smtp.gmail.com with ESMTPSA id m14sm1003163ooj.22.2022.01.31.10.22.44 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 31 Jan 2022 10:22:48 -0800 (PST) Date: Mon, 31 Jan 2022 15:22:42 -0300 From: =?iso-8859-1?Q?Ma=EDra?= Canal To: gregkh@linuxfoundation.org, tj@kernel.org, viro@zeniv.linux.org.uk, nathan@kernel.org, ndesaulniers@google.com, willy@infradead.org Cc: linux-kernel@vger.kernel.org, linux-fsdevel@vger.kernel.org, llvm@lists.linux.dev Subject: [PATCH v3] seq_file: fix NULL pointer arithmetic warning Message-ID: MIME-Version: 1.0 Content-Type: text/plain; charset=iso-8859-1 Content-Disposition: inline Content-Transfer-Encoding: 8bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Implement conditional logic in order to replace NULL pointer arithmetic. The use of NULL pointer arithmetic was pointed out by clang with the following warning: fs/kernfs/file.c:128:15: warning: performing pointer arithmetic on a null pointer has undefined behavior [-Wnull-pointer-arithmetic] return NULL + !*ppos; ~~~~ ^ fs/seq_file.c:559:14: warning: performing pointer arithmetic on a null pointer has undefined behavior [-Wnull-pointer-arithmetic] return NULL + (*pos == 0); Signed-off-by: Ma?ra Canal --- V1 -> V2: - Use SEQ_START_TOKEN instead of open-coding it - kernfs_seq_start call single_start instead of open-coding it V2 -> V3: - Remove the EXPORT of the single_start symbol --- fs/kernfs/file.c | 7 +------ fs/seq_file.c | 4 ++-- include/linux/seq_file.h | 1 + 3 files changed, 4 insertions(+), 8 deletions(-) diff --git a/fs/kernfs/file.c b/fs/kernfs/file.c index 9414a7a60a9f..7aefaca876a0 100644 --- a/fs/kernfs/file.c +++ b/fs/kernfs/file.c @@ -120,13 +120,8 @@ static void *kernfs_seq_start(struct seq_file *sf, loff_t *ppos) if (next == ERR_PTR(-ENODEV)) kernfs_seq_stop_active(sf, next); return next; - } else { - /* - * The same behavior and code as single_open(). Returns - * !NULL if pos is at the beginning; otherwise, NULL. - */ - return NULL + !*ppos; } + return single_start(sf, ppos); } static void *kernfs_seq_next(struct seq_file *sf, void *v, loff_t *ppos) diff --git a/fs/seq_file.c b/fs/seq_file.c index f8e1f4ee87ff..7ab8a58c29b6 100644 --- a/fs/seq_file.c +++ b/fs/seq_file.c @@ -554,9 +554,9 @@ int seq_dentry(struct seq_file *m, struct dentry *dentry, const char *esc) } EXPORT_SYMBOL(seq_dentry); -static void *single_start(struct seq_file *p, loff_t *pos) +void *single_start(struct seq_file *p, loff_t *pos) { - return NULL + (*pos == 0); + return *pos ? NULL : SEQ_START_TOKEN; } static void *single_next(struct seq_file *p, void *v, loff_t *pos) diff --git a/include/linux/seq_file.h b/include/linux/seq_file.h index 88cc16444b43..60820ab511d2 100644 --- a/include/linux/seq_file.h +++ b/include/linux/seq_file.h @@ -162,6 +162,7 @@ int seq_dentry(struct seq_file *, struct dentry *, const char *); int seq_path_root(struct seq_file *m, const struct path *path, const struct path *root, const char *esc); +void *single_start(struct seq_file *, loff_t *); int single_open(struct file *, int (*)(struct seq_file *, void *), void *); int single_open_size(struct file *, int (*)(struct seq_file *, void *), void *, size_t); int single_release(struct inode *, struct file *); -- 2.34.1