Received: by 10.223.185.116 with SMTP id b49csp2056428wrg; Sat, 17 Feb 2018 11:11:54 -0800 (PST) X-Google-Smtp-Source: AH8x225n2DKDPEiSTcEpV55USNyUELvsWolI00QDuYmgLpm8L1OouXnF+FzOlHke6z8n4eutGRLm X-Received: by 2002:a17:902:12e:: with SMTP id 43-v6mr9514992plb.77.1518894714481; Sat, 17 Feb 2018 11:11:54 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1518894714; cv=none; d=google.com; s=arc-20160816; b=MM2iUcRH9zF2iyq4WsJA8tGOPPpjkjkl9vXoqtWAO391i3ZE9eZEZw31OAlAd5lD5C 5wrUKaaiBp9gvq3KGVVkPTEEzY4QxiSBSwkAm9Gyq7A8x9iLytMDAsB6wf/1nI4PPCLF t2egg5hNdZ3MfC3074XmCbnWKESJx/3dXlPkfW/6iXut7bVNQE8jPUPtJeNQCTb9SEXB jnFSm475U7Y0/Q3MQ1FvTTqTE7Ossxcisvw73f9+UEup0YK5gDfJ/28TiR+DSs8Bpf7o ykPqeIJzFTnYqtNOTmELhBoQqAyXqr/5uvzcgBfAziuTcumv1HXB6fkP803KPZi60Ols Mvhg== 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:to:from:date:dkim-signature :arc-authentication-results; bh=GRjfS5XSnFMj+OAVb200NhhnKlX3943C4nrKZnzWwCY=; b=JBw3PEFPd0Y0N/yVDaYpck5Jmrf+LGWDUNL4MovWqAsNDOkS4ykTPMAw/OfqVxbzS9 ig/xW4sNtUiUKt6QUl+i+QuobSvSYC7my5IFnGpoKDW08sKqVBzKdeNYB+wdgfuZ/Mjp EWtYIk6cctFzaTnHBQt16rABgT+JgdQFTb8RNR7qenafIGyA3u8kiYQ0KzY+oEewsP1+ fPiDDGDkrK9kGl9jK5QtR3bGhm/yh0J4/JPdUtbwgAQiVa1s7J5+Os0AoJkozu9oULcb 6+rhVIqD3DKFck205kGGXePTN2oejWV8FuE2d6oraJa9F0Ns/b/S9yXpcaC9TVIPwI/R YoSw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20161025 header.b=l322XWu5; 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 44-v6si1474809pld.724.2018.02.17.11.11.40; Sat, 17 Feb 2018 11:11:54 -0800 (PST) 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=l322XWu5; 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 S1751285AbeBQTLD (ORCPT + 99 others); Sat, 17 Feb 2018 14:11:03 -0500 Received: from mail-wr0-f195.google.com ([209.85.128.195]:46877 "EHLO mail-wr0-f195.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1751240AbeBQTLB (ORCPT ); Sat, 17 Feb 2018 14:11:01 -0500 Received: by mail-wr0-f195.google.com with SMTP id 34so5862239wre.13 for ; Sat, 17 Feb 2018 11:11:01 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=date:from:to:subject:message-id:mime-version:content-disposition :user-agent; bh=GRjfS5XSnFMj+OAVb200NhhnKlX3943C4nrKZnzWwCY=; b=l322XWu54ZaFw7TA7iBXzuPe5y1uIWlQAwSKIfmjVP3TNXqFybU9urydUF6sHzXxXX FL4SRf0evprAVkmAURDF+4Il8QUpWTPD9ENW8R03QitRvjrHfXRicnh2AX+eKqmpzJKF k/pyRF3xq6G5w/B9oeDPZOytIJe0BodrZpCG7oSFhUVxUM6VN8b7Z++ywvOjwnxaluFb N46EvPb0Wjvz5JMu8Hz+aU4ATgTMd+tF50xlPk1Nw4u7HgAuUVMa6C5of2WaNiKZ96pq GpzM1miRMwVgpXfU3R3IIu+OII2mp3WTU/BFtvSISO5H+agONSJXjOzeE6QMJxLKwu+1 jndg== 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:subject:message-id:mime-version :content-disposition:user-agent; bh=GRjfS5XSnFMj+OAVb200NhhnKlX3943C4nrKZnzWwCY=; b=bqlGgVE3EiQBjFO1BepQuWXS+HHrAqD13AGzrl1UsLsWCSQS/ye6EviUn7wZnNVlne 8I8BOA2IIwcnYMCnhYPjwSBrAuHo3nO/F7eT9SQ1CQTC2GcKySW6GC3nnp+22coFtrwX 1tUrcrL+tfGCd4nifot7eNznkrI1PRfe59m7p2kpFNaVYHusDmCTiCESOVoU/KVFI6xX nJMNTCqTp/EusvM6tO94wRegQlBmNN/lFPoBBeQaUdLa/C0r2dVo2yHnjx7dDzSjxp8v UyOGk6w8QqZ7H2+C/syeNH1MQ83I1CeXl/fE3AlgTvUQD2VY40Y/u3jHRIpsML7UoBGV Oerw== X-Gm-Message-State: APf1xPCATeWDpUoLT/yWmdVmULoDZHvMef/N8VzXxy1YkqjMvrhKmegQ UNztBHQSNlIoXsUf3ePG4I45MZCN7ZI= X-Received: by 10.223.183.74 with SMTP id n10mr8700714wre.124.1518894660615; Sat, 17 Feb 2018 11:11:00 -0800 (PST) Received: from gmail.com ([193.125.157.122]) by smtp.gmail.com with ESMTPSA id d63sm10273358wmh.16.2018.02.17.11.10.59 (version=TLS1_2 cipher=ECDHE-RSA-CHACHA20-POLY1305 bits=256/256); Sat, 17 Feb 2018 11:11:00 -0800 (PST) Date: Sat, 17 Feb 2018 20:10:35 +0100 From: Miguel Ojeda To: mingo@kernel.org, jpoimboe@redhat.com, keescook@chromium.org, akpm@linux-foundation.org, tglx@linutronix.de, geert@linux-m68k.org, gregkh@linuxfoundation.org, thomas.lendacky@amd.com, rientjes@google.com, will.deacon@arm.com, linux-kernel@vger.kernel.org Subject: [PATCH] Support the nonstring variable attribute (gcc >= 8) Message-ID: <20180217191035.gol4woxsgzpo4bfq@gmail.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline User-Agent: elm/2 Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From the GCC manual: The nonstring variable attribute specifies that an object or member declaration with type array of char or pointer to char is intended to store character arrays that do not necessarily contain a terminating NUL character. 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 Some reports are already coming to the LKML regarding these warnings. When they are false positives, we can use __nonstring to let gcc know a NUL character is not required; like in this case: https://lkml.org/lkml/2018/1/16/135 Signed-off-by: Miguel Ojeda Cc: Ingo Molnar Cc: Josh Poimboeuf Cc: Kees Cook Cc: Andrew Morton Cc: Geert Uytterhoeven Cc: Will Deacon Cc: Greg Kroah-Hartman Cc: David Rientjes --- Another option is using -Wno-stringop-truncation, but it remains to be seen how useful the new warning will be. We can try to keep it for the moment until the real bugs and false positives are dealt with and see if it is worth it. At least in the reported case at drivers/auxdisplay, using __nonstring is enough and it can actually replace a comment that was there about the non-stringness of the char arrays that gcc complained about. See https://godbolt.org/g/dydPah to play with the warning in this case. include/linux/compiler-gcc.h | 14 ++++++++++++++ include/linux/compiler_types.h | 4 ++++ 2 files changed, 18 insertions(+) diff --git a/include/linux/compiler-gcc.h b/include/linux/compiler-gcc.h index 73bc63e0a1c4..6a9784c0c7f3 100644 --- a/include/linux/compiler-gcc.h +++ b/include/linux/compiler-gcc.h @@ -317,6 +317,20 @@ #define __designated_init __attribute__((designated_init)) #endif +#if GCC_VERSION >= 80000 +/* + * The nonstring variable attribute specifies that an object or member + * declaration with type array of char or pointer to char is intended + * to store character arrays that do not necessarily contain a terminating + * NUL character. 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 + */ +#define __nonstring __attribute__((nonstring)) +#endif + #endif /* gcc version >= 40000 specific checks */ #if !defined(__noclone) diff --git a/include/linux/compiler_types.h b/include/linux/compiler_types.h index 6b79a9bba9a7..654dd3114052 100644 --- a/include/linux/compiler_types.h +++ b/include/linux/compiler_types.h @@ -271,4 +271,8 @@ struct ftrace_likely_data { # define __native_word(t) (sizeof(t) == sizeof(char) || sizeof(t) == sizeof(short) || sizeof(t) == sizeof(int) || sizeof(t) == sizeof(long)) #endif +#ifndef __nonstring +# define __nonstring +#endif + #endif /* __LINUX_COMPILER_TYPES_H */ -- 2.14.1