linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Nick Desaulniers <ndesaulniers@google.com>
To: Miguel Ojeda <miguel.ojeda.sandonis@gmail.com>
Cc: Greg KH <gregkh@linuxfoundation.org>,
	LKML <linux-kernel@vger.kernel.org>,
	adilger.kernel@dilger.ca,
	Masahiro Yamada <yamada.masahiro@socionext.com>,
	Michal Marek <michal.lkml@markovi.net>,
	rostedt@goodmis.org, mchehab+samsung@kernel.org, olof@lxom.net,
	konstantin@linuxfoundation.org,
	"David S. Miller" <davem@davemloft.net>,
	Andrey Ryabinin <aryabinin@virtuozzo.com>,
	Kees Cook <keescook@chromium.org>,
	Thomas Gleixner <tglx@linutronix.de>,
	Ingo Molnar <mingo@kernel.org>,
	Paul Lawrence <paullawrence@google.com>,
	sandipan@linux.vnet.ibm.com,
	Andrey Konovalov <andreyknvl@google.com>,
	David Woodhouse <dwmw2@infradead.org>,
	Will Deacon <will.deacon@arm.com>,
	Philippe Ombredanne <pombredanne@nexb.com>,
	paul.burton@mips.com, David Rientjes <rientjes@google.com>,
	w@1wt.eu, msebor@gmail.com, sparse@chrisli.org,
	Jonathan Corbet <corbet@lwn.net>,
	tytso@mit.edu, Geert Uytterhoeven <geert@linux-m68k.org>,
	Rasmus Villemoes <linux@rasmusvillemoes.dk>,
	joe@perches.com, Arnd Bergmann <arnd@arndb.de>,
	asmadeus@codewreck.org, Stefan Agner <stefan@agner.ch>,
	Luc Van Oostenryck <luc.vanoostenryck@gmail.com>,
	Andrew Morton <akpm@linux-foundation.org>,
	Linus Torvalds <torvalds@linux-foundation.org>,
	linux-doc@vger.kernel.org, linux-ext4@vger.kernel.org,
	linux-sparse@vger.kernel.org,
	Linux Kbuild mailing list <linux-kbuild@vger.kernel.org>
Subject: Re: [PATCH v5 12/15] Compiler Attributes: add support for __nonstring (gcc >= 8)
Date: Thu, 20 Sep 2018 11:07:53 -0700	[thread overview]
Message-ID: <CAKwvOd=0GZi5M3ZRxps+Y1Cvbk=h-VOMM4+cBzHx2-LGO1cLSg@mail.gmail.com> (raw)
In-Reply-To: <20180920172301.21868-13-miguel.ojeda.sandonis@gmail.com>

On Thu, Sep 20, 2018 at 10:23 AM Miguel Ojeda
<miguel.ojeda.sandonis@gmail.com> wrote:
>
> From the GCC manual:
>
>   nonstring
>
>     The nonstring variable attribute specifies that an object or member
>     declaration with type array of char, signed char, or unsigned char,
>     or pointer to such a type is intended to store character arrays that
>     do not necessarily contain a terminating NUL. This is useful in detecting
>     uses of such arrays or pointers with functions that expect NUL-terminated
>     strings, and to avoid warnings when such an array or pointer is used as
>     an argument to a bounded string manipulation function such as strncpy.
>
>   https://gcc.gnu.org/onlinedocs/gcc/Common-Variable-Attributes.html
>
> This attribute can be used for documentation purposes (i.e. replacing
> comments), but it is most helpful when the following warnings are enabled:
>
>   -Wstringop-overflow
>
>     Warn for calls to string manipulation functions such as memcpy and
>     strcpy that are determined to overflow the destination buffer.
>
>     [...]
>
>   -Wstringop-truncation
>
>     Warn for calls to bounded string manipulation functions such as
>     strncat, strncpy, and stpncpy that may either truncate the copied
>     string or leave the destination unchanged.
>
>     [...]
>
>     In situations where a character array is intended to store a sequence
>     of bytes with no terminating NUL such an array may be annotated with
>     attribute nonstring to avoid this warning. Such arrays, however,
>     are not suitable arguments to functions that expect NUL-terminated
>     strings. To help detect accidental misuses of such arrays GCC issues
>     warnings unless it can prove that the use is safe.
>
>   https://gcc.gnu.org/onlinedocs/gcc/Warning-Options.html
>
> Signed-off-by: Miguel Ojeda <miguel.ojeda.sandonis@gmail.com>
> ---
>  include/linux/compiler_attributes.h | 14 ++++++++++++++
>  1 file changed, 14 insertions(+)
>
> diff --git a/include/linux/compiler_attributes.h b/include/linux/compiler_attributes.h
> index f0f9fc398440..6b28c1b7310c 100644
> --- a/include/linux/compiler_attributes.h
> +++ b/include/linux/compiler_attributes.h
> @@ -34,6 +34,7 @@
>  # define __GCC4_has_attribute___externally_visible__  1
>  # define __GCC4_has_attribute___noclone__             1
>  # define __GCC4_has_attribute___optimize__            1
> +# define __GCC4_has_attribute___nonstring__           0
>  # define __GCC4_has_attribute___no_sanitize_address__ (__GNUC_MINOR__ >= 8)
>  #endif
>
> @@ -181,6 +182,19 @@
>   */
>  #define   noinline                      __attribute__((__noinline__))
>
> +/*
> + * Optional: only supported since gcc >= 8
> + * Optional: not supported by clang
> + * Optional: not supported by icc
> + *
> + *   gcc: https://gcc.gnu.org/onlinedocs/gcc/Common-Variable-Attributes.html#index-nonstring-variable-attribute
> + */
> +#if __has_attribute(__nonstring__)
> +# define __nonstring                    __attribute__((__nonstring__))
> +#else
> +# define __nonstring
> +#endif
> +
>  /*
>   *   gcc: https://gcc.gnu.org/onlinedocs/gcc/Common-Function-Attributes.html#index-noreturn-function-attribute
>   * clang: https://clang.llvm.org/docs/AttributeReference.html#noreturn
> --
> 2.17.1
>

Reviewed-by: Nick Desaulniers <ndesaulniers@google.com>

-- 
Thanks,
~Nick Desaulniers

  reply	other threads:[~2018-09-20 18:08 UTC|newest]

Thread overview: 27+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2018-09-20 17:22 [PATCH v5 00/15] Compiler Attributes Miguel Ojeda
2018-09-20 17:22 ` [PATCH v5 01/15] Compiler Attributes: remove unused attributes Miguel Ojeda
2018-09-20 17:22 ` [PATCH v5 02/15] Compiler Attributes: always use the extra-underscores syntax Miguel Ojeda
2018-09-20 17:22 ` [PATCH v5 03/15] Compiler Attributes: remove unneeded tests Miguel Ojeda
2018-09-20 17:22 ` [PATCH v5 04/15] Compiler Attributes: homogenize __must_be_array Miguel Ojeda
2018-09-20 17:22 ` [PATCH v5 05/15] Compiler Attributes: remove unneeded sparse (__CHECKER__) tests Miguel Ojeda
2018-09-20 17:22 ` [PATCH v5 06/15] Compiler Attributes: add missing SPDX ID in compiler_types.h Miguel Ojeda
2018-09-20 17:22 ` [PATCH v5 07/15] Compiler Attributes: use feature checks instead of version checks Miguel Ojeda
2018-09-20 17:22 ` [PATCH v5 08/15] Compiler Attributes: KENTRY used twice the "used" attribute Miguel Ojeda
2018-09-20 17:22 ` [PATCH v5 09/15] Compiler Attributes: remove uses of __attribute__ from compiler.h Miguel Ojeda
2018-09-20 17:22 ` [PATCH v5 10/15] Compiler Attributes: add Doc/process/programming-language.rst Miguel Ojeda
2018-09-20 17:22 ` [PATCH v5 11/15] Compiler Attributes: add MAINTAINERS entry Miguel Ojeda
2018-09-20 17:22 ` [PATCH v5 12/15] Compiler Attributes: add support for __nonstring (gcc >= 8) Miguel Ojeda
2018-09-20 18:07   ` Nick Desaulniers [this message]
2018-09-20 20:08   ` Kees Cook
2018-09-30 11:16     ` Miguel Ojeda
2018-09-20 17:22 ` [PATCH v5 13/15] Compiler Attributes: enable -Wstringop-truncation on W=1 " Miguel Ojeda
2018-09-20 18:16   ` Nick Desaulniers
2018-09-20 19:52   ` Kees Cook
2018-09-30 11:17     ` Miguel Ojeda
2018-09-20 17:23 ` [PATCH v5 14/15] Compiler Attributes: auxdisplay: panel: use __nonstring Miguel Ojeda
2018-09-20 18:11   ` Nick Desaulniers
2018-09-20 17:23 ` [PATCH v5 15/15] Compiler Attributes: ext4: remove local __nonstring definition Miguel Ojeda
2018-09-20 18:13   ` Nick Desaulniers
2018-09-20 18:18 ` [PATCH v5 00/15] Compiler Attributes Nick Desaulniers
2018-09-24 14:36 ` Luc Van Oostenryck
2018-09-30 11:13   ` Miguel Ojeda

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to='CAKwvOd=0GZi5M3ZRxps+Y1Cvbk=h-VOMM4+cBzHx2-LGO1cLSg@mail.gmail.com' \
    --to=ndesaulniers@google.com \
    --cc=adilger.kernel@dilger.ca \
    --cc=akpm@linux-foundation.org \
    --cc=andreyknvl@google.com \
    --cc=arnd@arndb.de \
    --cc=aryabinin@virtuozzo.com \
    --cc=asmadeus@codewreck.org \
    --cc=corbet@lwn.net \
    --cc=davem@davemloft.net \
    --cc=dwmw2@infradead.org \
    --cc=geert@linux-m68k.org \
    --cc=gregkh@linuxfoundation.org \
    --cc=joe@perches.com \
    --cc=keescook@chromium.org \
    --cc=konstantin@linuxfoundation.org \
    --cc=linux-doc@vger.kernel.org \
    --cc=linux-ext4@vger.kernel.org \
    --cc=linux-kbuild@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-sparse@vger.kernel.org \
    --cc=linux@rasmusvillemoes.dk \
    --cc=luc.vanoostenryck@gmail.com \
    --cc=mchehab+samsung@kernel.org \
    --cc=michal.lkml@markovi.net \
    --cc=miguel.ojeda.sandonis@gmail.com \
    --cc=mingo@kernel.org \
    --cc=msebor@gmail.com \
    --cc=olof@lxom.net \
    --cc=paul.burton@mips.com \
    --cc=paullawrence@google.com \
    --cc=pombredanne@nexb.com \
    --cc=rientjes@google.com \
    --cc=rostedt@goodmis.org \
    --cc=sandipan@linux.vnet.ibm.com \
    --cc=sparse@chrisli.org \
    --cc=stefan@agner.ch \
    --cc=tglx@linutronix.de \
    --cc=torvalds@linux-foundation.org \
    --cc=tytso@mit.edu \
    --cc=w@1wt.eu \
    --cc=will.deacon@arm.com \
    --cc=yamada.masahiro@socionext.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).