Received: by 10.213.65.68 with SMTP id h4csp1000502imn; Sat, 7 Apr 2018 15:02:28 -0700 (PDT) X-Google-Smtp-Source: AIpwx4/yj60fQFtwsc8m2GMkPFOk2AVDbDrSfdOkE4zfGOGcmqtcCG7GLh335JVsQNopz7ZsvT/r X-Received: by 2002:a17:902:8604:: with SMTP id f4-v6mr32613624plo.228.1523138548583; Sat, 07 Apr 2018 15:02:28 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1523138548; cv=none; d=google.com; s=arc-20160816; b=Ww9S3IETyiUywdBuOMNJEaQAI4RO2bCCDCaajjZHYN5GweLxsCcEqpx+1g4yk3eSy/ A4BqSFJEDRJGsdmM2zIIbTsLNuc0HZ/s6qlcqSBXP7Pf6KM0UME9qSsE+VAw6NkrCUJq o+g3Nz2KQZbT0Byf9CQU19AdU/DTfvi0C5BXk6dzAl9+0pgf5t2zbRi4qXlPcEGj6lC4 hHvGgXV2dQrpupUAyFSugKRw+BEMlG5Mrnzlnyryai3DF86y0rr8XcFIiwo57FLo1EBM H/HRfi8DVZTDAmCBJGb2LsUXWJqrfquw6TC5lJIRgeT+ny47noMO1WqK2xZBVnL4f3Oj 2Rhg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:user-agent:content-disposition :mime-version:message-id:subject:cc:to:from:date:dkim-signature :arc-authentication-results; bh=RtI1lsVhMQLQOS90fsqz5JxHKuHdqd51iLAPKhi3fIU=; b=n+kVtEZB7XzMmBvI6hTIts4t7V+WFnM2g1qVDwkFyN1qeoeLVQmjirROH73TUaTtYf 0oZLdOTUaiZs5mS3oQcj5XUk3HLvd4qfJS89iZCZRL7b/2NpZk6g6Hj8dW2h82gRUH7U AmsrQIHOMkFSbDR0TS0GUEd3iuYs10btqhVDdBLpeC9xbqT5eZoe2ftPZi4zN6ZLr8qw fVXzwh5mdS9rpJJHe8zuVt0AJWTs369F4j3UnRy0I5p2hsXeJvIfvLpsrQw6vuW2eMzo jh8SKIo/SYADNsb7JU8F/f04OJW8h7BPUMZRnyIS7hxfeOVVt59VfjB3J60vukI4NZwX Yclg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20161025 header.b=ARgPWwjs; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id a61-v6si11082645pla.271.2018.04.07.15.01.40; Sat, 07 Apr 2018 15:02:28 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=pass header.i=@gmail.com header.s=20161025 header.b=ARgPWwjs; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1752283AbeDGV6V (ORCPT + 99 others); Sat, 7 Apr 2018 17:58:21 -0400 Received: from mail-pl0-f67.google.com ([209.85.160.67]:39221 "EHLO mail-pl0-f67.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1752068AbeDGV6S (ORCPT ); Sat, 7 Apr 2018 17:58:18 -0400 Received: by mail-pl0-f67.google.com with SMTP id e7-v6so773482plt.6; Sat, 07 Apr 2018 14:58:18 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=date:from:to:cc:subject:message-id:mime-version:content-disposition :user-agent; bh=RtI1lsVhMQLQOS90fsqz5JxHKuHdqd51iLAPKhi3fIU=; b=ARgPWwjsFi8+2rIRTYEFg83XMTFjTpp3AsUOLiV2l+wn9XHiO+ga+oOHoyWPLKk8sU PAuLa3+1STO0g+cYCZHrJOqjQksrMtyYRcvfwhWY6pPqusyXR6Xt1Z19UiZvgdaAATaa Vh6N++4o35AovfvTuppvhLF3zcifKFyFgJQUyr/FLUFTzRizuqNiWMpIRY9WpkoKpZpC wvxPZ6pbY1phteJWzfaS48aYdXMqY2Aa1E+EQLG4MpMU0Er9qUsfSDjYOd/kf1F10fXS 8KxgxdeooevxvofPkblN/TqDSLmnDN7n2C+8cJpdAS1JWSHlIGGK6WL0Y9e13SJsBGRO dp6Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:from:to:cc:subject:message-id:mime-version :content-disposition:user-agent; bh=RtI1lsVhMQLQOS90fsqz5JxHKuHdqd51iLAPKhi3fIU=; b=MkxQmlFLoZwpXujV+aul6uWKnRjtvROMtSZKnJObP/i0zOWf0XZSnuiE5Q7yYswEUC g+Ng7IMmVGYq3xoe8qV1EvsCghWvTi18uPNRgA8QmY/V7ls4P5mAVVmsBfjNsVEa016f ZBIC1nZI2Y69jSsb1qzOgnPPvyTYsc0ywWT2oC2eEjhZ4iBAgYmomAn+eL54yF06eW4Y 9xVcd0DsHKY0BYoH6Pdm+4Fp434m05F4rLj2q8bEeJ0fB1MlO+7Q5/wyhhujFUOawQuh qHj52DvuZN6zLU0bROaOb3XeSMcorXWKubARXXxjgEi2s6rbI+ojy8AlFKFG6W8cieWA rilA== X-Gm-Message-State: AElRT7GIK4EFUb2nX1IOgC5fx9o4Zj6bhbbUTNCxpzPFoSKu3ry14udq 7iG6GiRMvgvT4WieyIVSDTanDg== X-Received: by 2002:a17:902:6103:: with SMTP id t3-v6mr32818480plj.76.1523138298182; Sat, 07 Apr 2018 14:58:18 -0700 (PDT) Received: from gmail.com (cpe-98-150-136-16.hawaii.res.rr.com. [98.150.136.16]) by smtp.gmail.com with ESMTPSA id p6sm26475924pfk.104.2018.04.07.14.58.16 (version=TLS1_2 cipher=ECDHE-RSA-CHACHA20-POLY1305 bits=256/256); Sat, 07 Apr 2018 14:58:17 -0700 (PDT) Date: Sat, 7 Apr 2018 11:58:05 -1000 From: Joey Pabalinas To: linux-sparse@vger.kernel.org Cc: Kees Cook , Linus Torvalds , Martin Uecker , Al Viro , Christopher Li , Linux Kernel Mailing List , Joey Pabalinas Subject: [PATCH v2] add -Wpointer-arith sparse flag to toggle sizeof(void) warnings Message-ID: <20180407215805.ze5rx4v3slbjngmo@gmail.com> MIME-Version: 1.0 Content-Type: multipart/signed; micalg=pgp-sha256; protocol="application/pgp-signature"; boundary="h2ampcaefjs7x347" Content-Disposition: inline User-Agent: NeoMutt/20180323 Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org --h2ampcaefjs7x347 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline Content-Transfer-Encoding: quoted-printable Recent changes to the min()/max() macros in include/linux/kernel.h have added a lot of noise when compiling the kernel with Sparse checking enabled. This mostly is due to the *huge* increase in the number of sizeof(void) warnings, a larger number of which can safely be ignored. Add the -Wpointer-arith flag to enable/disable these warnings (along with the warning when applying sizeof to function types exactly like the GCC -Wpointer-arith flag) on demand; the warning itself has been disabled by default to reduce the large influx of noise which was inadvertently added by commit 3c8ba0d61d04ced9f8 (kernel.h: Retain constant expression output for max()/min()). Update the manpage to document the new flag. CC: Kees Cook CC: Linus Torvalds CC: Martin Uecker CC: Al Viro CC: Christopher Li CC: Joey Pabalinas CC: Luc Van Oostenryck Signed-off-by: Joey Pabalinas Signed-off-by: Luc Van Oostenryck 4 files changed, 26 insertions(+), 2 deletions(-) diff --git a/evaluate.c b/evaluate.c index 4e1dffe9c5416380df..f828da37df8e686623 100644 --- a/evaluate.c +++ b/evaluate.c @@ -2193,7 +2193,8 @@ static struct symbol *evaluate_sizeof(struct expressi= on *expr) size =3D type->bit_size; =20 if (size < 0 && is_void_type(type)) { - warning(expr->pos, "expression using sizeof(void)"); + if (Wpointer_arith) + warning(expr->pos, "expression using sizeof(void)"); size =3D bits_in_char; } =20 @@ -2204,7 +2205,8 @@ static struct symbol *evaluate_sizeof(struct expressi= on *expr) } =20 if (is_function(type->ctype.base_type)) { - warning(expr->pos, "expression using sizeof on a function"); + if (Wpointer_arith) + warning(expr->pos, "expression using sizeof on a function"); size =3D bits_in_char; } =20 diff --git a/lib.c b/lib.c index 73d372c36626538bab..f7fdac96674aec4c24 100644 --- a/lib.c +++ b/lib.c @@ -242,6 +242,7 @@ int Woverride_init =3D 1; int Woverride_init_all =3D 0; int Woverride_init_whole_range =3D 0; int Wparen_string =3D 0; +int Wpointer_arith =3D 0; int Wptr_subtraction_blows =3D 0; int Wreturn_void =3D 0; int Wshadow =3D 0; @@ -654,6 +655,7 @@ static const struct flag warnings[] =3D { { "return-void", &Wreturn_void }, { "shadow", &Wshadow }, { "sizeof-bool", &Wsizeof_bool }, + { "pointer-arith", &Wpointer_arith }, { "sparse-error", &Wsparse_error }, { "tautological-compare", &Wtautological_compare }, { "transparent-union", &Wtransparent_union }, diff --git a/lib.h b/lib.h index 3050b5577ba4d42e97..e34bb9a02ebac03f52 100644 --- a/lib.h +++ b/lib.h @@ -151,6 +151,7 @@ extern int Woverride_init; extern int Woverride_init_all; extern int Woverride_init_whole_range; extern int Wparen_string; +extern int Wpointer_arith; extern int Wptr_subtraction_blows; extern int Wreturn_void; extern int Wshadow; diff --git a/sparse.1 b/sparse.1 index 88343f3170f195297a..4379406999c94b806e 100644 --- a/sparse.1 +++ b/sparse.1 @@ -288,6 +288,25 @@ Standard C syntax does not permit a parenthesized stri= ng as an array initializer. GCC allows this syntax as an extension. With \fB\-Wparen\-string\fR, Sparse will warn about this syntax. =20 +Sparse does not issue these warnings by default. +. +.TP +.B \-Wpointer-arith +Warn about anything that depends on the \fBsizeof\fR a function type or of= void. + +C99 does not allow the \fBsizeof\fR operator to be applied to function typ= es or to +incomplete types such as void. GCC allows \fBsizeof\fR to be applied to th= ese +types as an extension and assigns these types a size of \fI1\fR. + +Although non-standard (and somewhat illogical), constructs such as \fBsize= of(void)\fR +are often useful when the intent is to operate on an expression without ev= aluating +it, e.g. in the following integer constant expression predicate: + +.nf +#define __is_constexpr(x) \\ + (sizeof(int) =3D=3D sizeof(*(8 ? ((void *)((long)(x) * 0l)) : (int *)8))) +.fi + Sparse does not issue these warnings by default. . .TP --=20 2.17.0.rc1.35.g90bbd502d54fe92035.dirty --h2ampcaefjs7x347 Content-Type: application/pgp-signature; name="signature.asc" -----BEGIN PGP SIGNATURE----- iQIzBAEBCAAdFiEEKlZXrihdNOcUPZTNruvLfWhyVBkFAlrJPu0ACgkQruvLfWhy VBmEBBAAki2R+iz46YMJgP277W7FbL45lnS1CyyCGKYbKpS5l7npKobkO12q0Uuj 68frc/IH7ApxaEoUUnd44Dvz2V+C0USKrnitXPCqlyCNqXLK55RDYomSJZHdd8HB VTcXoetEejvJHOdzOuvkVKKzAAWCsE4tMiIfXHeK7BvyBPh1fsT56w8XzIkG8DdN oW7yccYbnWQCUId/GcfwdSkliMzH1Y4Y6rx3c+CyyZlhI0mPJQLyKAHewbactQ6v sQ6FtlQ1LCLnuy5NmZtXCyl2CJphhabe20DiFEhPP2/e5gPbs9BkHrTnNCbaZyrv vNo/EXLGiMCvhQtsT4gbKC6pt3Ef1McPUGoENa7tvCMedCbAKMPn0gmZTctsvPZ8 zZPoqOStQjr35psJ8u3wy/4gh070pjsgzj2ri9eoCkmm9iMauT+QspbnQYX4Iv5x elQf3Egk5CtgiUUbunsnP8oOC3BuD9LouMpiNQtCTFjtvG5n/L7hA4pqQYxJrZHu B5Egoll/V8/GiV5O4XR0I191bCKsKD5GX12fD7rxrUHR9Yy3aYJtKSYm8QNRFBLl OmiwLA0+1yuIFQJ4pIsSW0HOhdr0UZHqHiJRs82eum6hB8i8QKeG+n96KYpLo/SR RIBnlR2DkSLmMtdTWLs9aDA+aXKk+ezR0EtjnXaqN5CuXnisIEk= =ysXp -----END PGP SIGNATURE----- --h2ampcaefjs7x347--