From patchwork Sun Oct 1 16:24:00 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alejandro Colomar X-Patchwork-Id: 147131 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:612c:2a8e:b0:403:3b70:6f57 with SMTP id in14csp953941vqb; Sun, 1 Oct 2023 09:24:54 -0700 (PDT) X-Google-Smtp-Source: AGHT+IEkebKapgpM1oy9AsQtnBzffzvKlw5G9g4FgCvUt0dx41Rft+jiiZVu6tTzFG0fhBZ6BfsY X-Received: by 2002:a05:6402:1357:b0:530:8801:47aa with SMTP id y23-20020a056402135700b00530880147aamr8411213edw.19.1696177494084; Sun, 01 Oct 2023 09:24:54 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1696177494; cv=none; d=google.com; s=arc-20160816; b=rIAG85n2Bb8tyIQJi00v5SYdW5yt8ierp+ojNQZrs37V3cKHw/S8mJo0ioQhzE4w3E Ojyse57OhaqMHw7t5zC8Y6sdjfoM3NBV8e63WUdOwaiiugQarqaPBC8kKfE/sWhqAUKh v2zJwYR/gNy+d/rvrejxV2bL2OiEcQlrrnDti2DdTq7fbDNTO1rb5oCR53eTZxfSsvnV 9PD/7To6QHPxZtGAY3V5Yt4bnmJj0+yq+2Y+GviJ1lE9tf7ePicELigz3Kk6G2y9s5HC gCVSIpnqEaJOpETIUj4t3M7/KhR1/gAipOXf3Ej6rdlcy6xREy5BGfWJmpaztaZA8KVk sPXw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=errors-to:list-subscribe:list-help:list-post:list-archive :list-unsubscribe:list-id:precedence:content-transfer-encoding :mime-version:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature:dmarc-filter:delivered-to; bh=GNx3Qib+69EQHpWeQnDXVfzh/oTL/rejGtLMZ+5fEu4=; fh=lrdUOwe2KbvBPmMC51v2GQKGTRlkYYn7I4swQNusl3U=; b=ZYRk1iFqXgimXePXDnuyPafuj5hCN+I427dhyngphkHNk+3czlYfz8KxU/bx16ujw3 BT0D41A4IkG9SemxT/StqQA3CfWrM1A1RZRPnseUXo9oia9Trj7nvxgiRCViee48uIrO rXcvNuOkuR7NCM+b7SeFXt9OW5mxufm2CejYV2e3anP0MpP78DWWT9uo4rhn2HwuA1On OK51YoiLwObrRyCP2GGXiRcVqeeEPgyHrttqqxcV2u0Xq6Db6K4mGl/3WDD4yVmFuIZD xLmobA9HGwBM+TxYSxNTzDrAEQOJqEfToeaJFmzVHaHCLl+h23wk0zZosjzoKT5DSX3g 1RCw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=CGG0SaeV; spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 8.43.85.97 as permitted sender) smtp.mailfrom="gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: from server2.sourceware.org (ip-8-43-85-97.sourceware.org. [8.43.85.97]) by mx.google.com with ESMTPS id r2-20020aa7da02000000b00528287f1488si17572602eds.410.2023.10.01.09.24.53 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 01 Oct 2023 09:24:54 -0700 (PDT) Received-SPF: pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 8.43.85.97 as permitted sender) client-ip=8.43.85.97; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=CGG0SaeV; spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 8.43.85.97 as permitted sender) smtp.mailfrom="gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id BC6F1385AC26 for ; Sun, 1 Oct 2023 16:24:45 +0000 (GMT) X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from ams.source.kernel.org (ams.source.kernel.org [IPv6:2604:1380:4601:e00::1]) by sourceware.org (Postfix) with ESMTPS id 1265C3858D1E for ; Sun, 1 Oct 2023 16:24:18 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 1265C3858D1E Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=kernel.org Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=kernel.org Received: from smtp.kernel.org (transwarp.subspace.kernel.org [100.75.92.58]) by ams.source.kernel.org (Postfix) with ESMTP id 1606DB80CA6; Sun, 1 Oct 2023 16:24:17 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 45F08C433C8; Sun, 1 Oct 2023 16:24:13 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1696177455; bh=F4b+ICY334PCcka7EjXLS8EqvxsbkyABeJAkIu5T++I=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=CGG0SaeVrFM3/H0R7ZooMba08/NcACgqeZN8JvKTTxSlX9L2XG0H3QqB06cFtBf5Y pyrxBUElZ1W6CIyZ1RMYcoIFTBQveaBFuv6iz+z5c3cpDdWkskJArwEck5l0OmeuFL w1L4697ZnEMAsrZmQ905pFw/BIWl3PSyD1bKXXx785kWLuevTkcMvoX5XHWQvZfYby 26LjQa0F5Ed18MXcUgeVC/MHqEt/LU3r0Ku2zruGPHsdP8eFSvdL0xDzbnTIzUxODS RI0Ks6EkmpqczO0LQfc7wzmNf+MLk1zGJDIAOLSoM3qqsgLtBd+hVddlhbhzc8YvMB fXA18YhmRIwjw== From: Alejandro Colomar To: gcc-patches@gcc.gnu.org Cc: Alejandro Colomar , Doug McIlroy , "G. Branden Robinson" , Ralph Corderoy , Dave Kemper , Larry McVoy , Andrew Pinski , Jonathan Wakely , Andrew Clayton , Martin Uecker , David Malcolm Subject: [PATCH v5] C, ObjC: Add -Wunterminated-string-initialization Date: Sun, 1 Oct 2023 18:24:00 +0200 Message-Id: <20231001162400.68141-1-alx@kernel.org> X-Mailer: git-send-email 2.40.1 In-Reply-To: References: MIME-Version: 1.0 X-Spam-Status: No, score=-10.1 required=5.0 tests=BAYES_00, DKIMWL_WL_HIGH, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, SPF_HELO_NONE, SPF_PASS, TXREP autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org X-BeenThere: gcc-patches@gcc.gnu.org X-Mailman-Version: 2.1.30 Precedence: list List-Id: Gcc-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1761258065294118016 X-GMAIL-MSGID: 1778571012134420476 Warn about the following: char s[3] = "foo"; Initializing a char array with a string literal of the same length as the size of the array is usually a mistake. Rarely is the case where one wants to create a non-terminated character sequence from a string literal. In some cases, for writing faster code, one may want to use arrays instead of pointers, since that removes the need for storing an array of pointers apart from the strings themselves. char *log_levels[] = { "info", "warning", "err" }; vs. char log_levels[][7] = { "info", "warning", "err" }; This forces the programmer to specify a size, which might change if a new entry is later added. Having no way to enforce null termination is very dangerous, however, so it is useful to have a warning for this, so that the compiler can make sure that the programmer didn't make any mistakes. This warning catches the bug above, so that the programmer will be able to fix it and write: char log_levels[][8] = { "info", "warning", "err" }; This warning already existed as part of -Wc++-compat, but this patch allows enabling it separately. It is also included in -Wextra, since it may not always be desired (when unterminated character sequences are wanted), but it's likely to be desired in most cases. Since Wc++-compat now includes this warning, the test has to be modified to expect the text of the new warning too, in . Link: Link: Link: Acked-by: Doug McIlroy Cc: "G. Branden Robinson" Cc: Ralph Corderoy Cc: Dave Kemper Cc: Larry McVoy Cc: Andrew Pinski Cc: Jonathan Wakely Cc: Andrew Clayton Cc: Martin Uecker Cc: David Malcolm Signed-off-by: Alejandro Colomar --- v5: - Fix existing C++-compat tests. [reported by ] gcc/c-family/c.opt | 4 ++++ gcc/c/c-typeck.cc | 6 +++--- gcc/testsuite/gcc.dg/Wcxx-compat-14.c | 2 +- gcc/testsuite/gcc.dg/Wunterminated-string-initialization.c | 6 ++++++ 4 files changed, 14 insertions(+), 4 deletions(-) create mode 100644 gcc/testsuite/gcc.dg/Wunterminated-string-initialization.c diff --git a/gcc/c-family/c.opt b/gcc/c-family/c.opt index 44b9c862c14..e8f6b836836 100644 --- a/gcc/c-family/c.opt +++ b/gcc/c-family/c.opt @@ -1407,6 +1407,10 @@ Wunsuffixed-float-constants C ObjC Var(warn_unsuffixed_float_constants) Warning Warn about unsuffixed float constants. +Wunterminated-string-initialization +C ObjC Var(warn_unterminated_string_initialization) Warning LangEnabledBy(C ObjC,Wextra || Wc++-compat) +Warn about character arrays initialized as unterminated character sequences by a string literal. + Wunused C ObjC C++ ObjC++ LangEnabledBy(C ObjC C++ ObjC++,Wall) ; documented in common.opt diff --git a/gcc/c/c-typeck.cc b/gcc/c/c-typeck.cc index e55e887da14..7df9de819ed 100644 --- a/gcc/c/c-typeck.cc +++ b/gcc/c/c-typeck.cc @@ -8399,11 +8399,11 @@ digest_init (location_t init_loc, tree type, tree init, tree origtype, pedwarn_init (init_loc, 0, ("initializer-string for array of %qT " "is too long"), typ1); - else if (warn_cxx_compat + else if (warn_unterminated_string_initialization && compare_tree_int (TYPE_SIZE_UNIT (type), len) < 0) - warning_at (init_loc, OPT_Wc___compat, + warning_at (init_loc, OPT_Wunterminated_string_initialization, ("initializer-string for array of %qT " - "is too long for C++"), typ1); + "is too long"), typ1); if (compare_tree_int (TYPE_SIZE_UNIT (type), len) < 0) { unsigned HOST_WIDE_INT size diff --git a/gcc/testsuite/gcc.dg/Wcxx-compat-14.c b/gcc/testsuite/gcc.dg/Wcxx-compat-14.c index 23783711be6..6df0ee197cc 100644 --- a/gcc/testsuite/gcc.dg/Wcxx-compat-14.c +++ b/gcc/testsuite/gcc.dg/Wcxx-compat-14.c @@ -2,5 +2,5 @@ /* { dg-options "-Wc++-compat" } */ char a1[] = "a"; -char a2[1] = "a"; /* { dg-warning "C\[+\]\[+\]" } */ +char a2[1] = "a"; /* { dg-warning "initializer-string for array of 'char' is too long" } */ char a3[2] = "a"; diff --git a/gcc/testsuite/gcc.dg/Wunterminated-string-initialization.c b/gcc/testsuite/gcc.dg/Wunterminated-string-initialization.c new file mode 100644 index 00000000000..13d5dbc6640 --- /dev/null +++ b/gcc/testsuite/gcc.dg/Wunterminated-string-initialization.c @@ -0,0 +1,6 @@ +/* { dg-do compile } */ +/* { dg-options "-Wunterminated-string-initialization" } */ + +char a1[] = "a"; +char a2[1] = "a"; /* { dg-warning "initializer-string for array of 'char' is too long" } */ +char a3[2] = "a";