From patchwork Thu Sep 20 17:22:58 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Miguel Ojeda X-Patchwork-Id: 10608277 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id C623315E8 for ; Thu, 20 Sep 2018 17:24:21 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id AD3412E283 for ; Thu, 20 Sep 2018 17:24:21 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id A0E782E29A; Thu, 20 Sep 2018 17:24:21 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-8.0 required=2.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,FREEMAIL_FROM,MAILING_LIST_MULTI,RCVD_IN_DNSWL_HI autolearn=ham version=3.3.1 Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 014BA2E298 for ; Thu, 20 Sep 2018 17:24:21 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S2388228AbeITXIY (ORCPT ); Thu, 20 Sep 2018 19:08:24 -0400 Received: from mail-wr1-f68.google.com ([209.85.221.68]:34984 "EHLO mail-wr1-f68.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S2388191AbeITXIX (ORCPT ); Thu, 20 Sep 2018 19:08:23 -0400 Received: by mail-wr1-f68.google.com with SMTP id j26-v6so10224146wre.2; Thu, 20 Sep 2018 10:23:51 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=0RA0ONh6m9Yv1rVJTvrOsFSDcGT4jsVl1K//X96wXxE=; b=GE0rQn0ChqI0rlDQfCmMochFlu5MMVWGter0daDflRe0EGBZrniL5RHjfQGrTC6GYT qfR1w5xluVEr7xEqbyI/VTwPRv/8UvHdBb72KxZbHEuiZqJEVeRfMf/BV+ZnYWwED7p3 UJ4OOyWsraVc9JrkV48UUJGD/Qxk16WiuB0qMontOBX+ola5TlvBcJk+UHcSJ/kLz352 Rcj/hmuKUOkEkTQXUIwTqbGkTDsRvDMoUdhxdJI+Gd6zBGhJNh1GQ6nBztzm1q3qHvNu 8ko6ABiGoBd0/AC7zfH2T5u1VyEeNy3qmpLVDkBx60M2BitxaL0U4CsXKNNQKIF4AlOv RW3w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=0RA0ONh6m9Yv1rVJTvrOsFSDcGT4jsVl1K//X96wXxE=; b=O7gtivy8POKtRojjuR8A7r/LKA8r0QnQHlIIEmEpH6PC2LJCKgUK8pQcZ53W97XUxa SCaVJZyOQUksvYFF2NoKHHH+H/I6nQNPrc/t1JXfq6sejWdIxbTa9WechjIQjtuJapVk yuV0qHkgMzt9BofkZSTTh7qiZgZ9dqvJS5+dNiRgQvfs1lin0drbgnCkgThWTopDuIuB FiElwmWCMqpYSZhAXCv6N66OduINkJPsav7Hg9ENgd/CV1zngVHCH0EQ0fIP726oEBjh lHzLnlB3ZKRQ0Nr0gwhi7Hl3zSw53H9wKKQKS5qap6wqqloogih0XAZljqaqkNCQzL6p VXDg== X-Gm-Message-State: APzg51B2q6EAqYga8J7KsNQpHUZCAwkzqD3asT2Ypqcp8vNIDbvG4N5S zFCheKZE5OwiJ1MVB/GKnAI= X-Google-Smtp-Source: ANB0VdbPnQwPA61LSbOk68RibKZDCx6D9g5+HXMH3vzL0u2Tt+FXc8SSYTN2I8wpooNOM3LvvH/GNA== X-Received: by 2002:adf:8b50:: with SMTP id v16-v6mr12546686wra.282.1537464230740; Thu, 20 Sep 2018 10:23:50 -0700 (PDT) Received: from localhost.localdomain (62.83.35.8.dyn.user.ono.com. [62.83.35.8]) by smtp.gmail.com with ESMTPSA id q200-v6sm3466562wmd.2.2018.09.20.10.23.48 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Thu, 20 Sep 2018 10:23:50 -0700 (PDT) From: Miguel Ojeda To: Greg Kroah-Hartman Cc: linux-kernel@vger.kernel.org, Miguel Ojeda , Andreas Dilger , Masahiro Yamada , Michal Marek , Steven Rostedt , Mauro Carvalho Chehab , Olof Johansson , Konstantin Ryabitsev , "David S . Miller" , Andrey Ryabinin , Kees Cook , Thomas Gleixner , Ingo Molnar , Paul Lawrence , Sandipan Das , Andrey Konovalov , David Woodhouse , Will Deacon , Philippe Ombredanne , Paul Burton , David Rientjes , Willy Tarreau , Martin Sebor , Christopher Li , Jonathan Corbet , Theodore Ts'o , Geert Uytterhoeven , Rasmus Villemoes , Joe Perches , Arnd Bergmann , Dominique Martinet , Stefan Agner , Luc Van Oostenryck , Nick Desaulniers , Andrew Morton , Linus Torvalds , linux-doc@vger.kernel.org, linux-ext4@vger.kernel.org, linux-sparse@vger.kernel.org, linux-kbuild@vger.kernel.org Subject: [PATCH v5 12/15] Compiler Attributes: add support for __nonstring (gcc >= 8) Date: Thu, 20 Sep 2018 19:22:58 +0200 Message-Id: <20180920172301.21868-13-miguel.ojeda.sandonis@gmail.com> In-Reply-To: <20180920172301.21868-1-miguel.ojeda.sandonis@gmail.com> References: <20180920172301.21868-1-miguel.ojeda.sandonis@gmail.com> Sender: linux-kbuild-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kbuild@vger.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP 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 Reviewed-by: Nick Desaulniers Reviewed-by: Kees Cook --- 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