From patchwork Tue Feb 6 10:45:38 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alejandro Colomar X-Patchwork-Id: 197310 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:7301:168b:b0:106:860b:bbdd with SMTP id ma11csp1452933dyb; Tue, 6 Feb 2024 02:46:31 -0800 (PST) X-Google-Smtp-Source: AGHT+IEfcvQgbTleVFyzkTHtSi2k0Vs70IBeU2/x+d2x+sIdXdxMxK4Ulj0A3hx2ctWx88Uo7pYk X-Received: by 2002:a05:620a:24c7:b0:783:f77b:6dc9 with SMTP id m7-20020a05620a24c700b00783f77b6dc9mr4257176qkn.18.1707216391084; Tue, 06 Feb 2024 02:46:31 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1707216391; cv=pass; d=google.com; s=arc-20160816; b=zSJ6fQK8MNo0GiS60RJYSug9O5OVqYC2xJGIJpMvsK0FyuFwe890fdGZX50YZUhKf7 YJZFhDY7mVKZniePyBkKKgQmZpVrWZFrEE3PzooGd7lnNHgq86VM34DQvlTEdfDieyH0 audprj6ZS8LndaDBD5zSmEtH1Mw0oDSHofVKhuKbt5YngwcnoIygqysTM+I92vTXeuPt 7S/UAO0wnM7ihivEB5qjJUimd6ON/DfPu21yOy2pCpQOdcKNU/2IeMbNOlEkZnwaVOMY CCDo/Dx9I1KJhtgMF6pzAJH7x1OK/JSuD2ZNAU7TSokKTBrqWJZb9Gc0w3X4Ikw0Yujy WYfw== ARC-Message-Signature: i=2; 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:in-reply-to:content-disposition :mime-version:references:message-id:subject:to:from:date :dkim-signature:arc-filter:dmarc-filter:delivered-to; bh=mNGvaorsRwWSZAz8xjKO4wpxwh/J9jTaYrKFur7Z8cI=; fh=Bdvc7rUOK9pkkbFbh0UekxOnF3vW3ram+9AERkDTSnQ=; b=BYg40ACpsL4W6fGm1Cy1nUYLm7BgDvgTV9/WGFvvUm0XYjpUoIRacJI1YyclZWg39Y zydqZ54ifKkW/0AgwYdeYQ9lpmeEK6l3Pl/6zPOgL1A83e1/6oURlVmrjHEhZPNAi3NQ uEJ6pUDv6ZeyAui7X+/efqAndjHPouEALOi+YSfO1fPA99XaM48sv0FuVR4KK3McAGic 2ivwI+2jxKXtiz6HcOwSEktEOQRC8Qw9NpPSb+IpF3l2mVA/lSzQxz3fJHvOXEXYzShz 1qYzLfZApyEZa+9a3hILC5UA19XO5TNjkVa1jpRVHObSShOTxnmYHGyt5CING+LE9N/7 7G+g==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=nLH0nobq; arc=pass (i=1); spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 2620:52:3:1:0:246e:9693:128c 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 X-Forwarded-Encrypted: i=1; AJvYcCXEMTkqHUKSpy3T70vmYoPRre1RAYhYDA+RIZQL+9KkYZvFcFGfwRAJ4sG7lwefjPiKo28b6fPRPZ9VRQ/dHL9W0594zQ== Received: from server2.sourceware.org (server2.sourceware.org. [2620:52:3:1:0:246e:9693:128c]) by mx.google.com with ESMTPS id bp31-20020a05620a459f00b0078445fa8a26si2296914qkb.38.2024.02.06.02.46.30 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 06 Feb 2024 02:46:31 -0800 (PST) Received-SPF: pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 2620:52:3:1:0:246e:9693:128c as permitted sender) client-ip=2620:52:3:1:0:246e:9693:128c; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=nLH0nobq; arc=pass (i=1); spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 2620:52:3:1:0:246e:9693:128c 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 BC91A3858418 for ; Tue, 6 Feb 2024 10:46:30 +0000 (GMT) X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from dfw.source.kernel.org (dfw.source.kernel.org [IPv6:2604:1380:4641:c500::1]) by sourceware.org (Postfix) with ESMTPS id C088C3858D35 for ; Tue, 6 Feb 2024 10:45:41 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org C088C3858D35 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=kernel.org Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=kernel.org ARC-Filter: OpenARC Filter v1.0.0 sourceware.org C088C3858D35 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=2604:1380:4641:c500::1 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1707216344; cv=none; b=RXRv9XV+YJ+IADsGlAU0Oa8rKrkqq3QxQdyNx79NqLPtacStT5hq+8UNc1PpyHdKbxl6ejucqAZqC/Hri0GJtRtdix5dS2ZlsNDiUGN6gdYw+zng3XSg5WS1NCYsg3oX+NfabdHAOExSN7ixyz83HyJ7S8JcJFItIIYzj82LPOc= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1707216344; c=relaxed/simple; bh=Tnn7j8TxBl785TAm7RPIj1rPqgvJ3pIU4JdrmjMlIHg=; h=DKIM-Signature:Date:From:To:Subject:Message-ID:MIME-Version; b=KAeBh8uAxY6rYGJgjH9zO01ZkJ1K0XjwNkaIEWyyegIcjZOx6WsmTbd8FlYtDidK++st8vC3yLJBLh88ci8vRQ03pWCanz6wL2Vy+JTi1VU0+yPsKB8pEUsydmcHX4QlOmYTEqAjSXRTqC8kcSl93+bNnmeU4FUSPIv5n43fq7Q= ARC-Authentication-Results: i=1; server2.sourceware.org Received: from smtp.kernel.org (transwarp.subspace.kernel.org [100.75.92.58]) by dfw.source.kernel.org (Postfix) with ESMTP id 0F39D6135B for ; Tue, 6 Feb 2024 10:45:41 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 3D213C433F1 for ; Tue, 6 Feb 2024 10:45:40 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1707216340; bh=Tnn7j8TxBl785TAm7RPIj1rPqgvJ3pIU4JdrmjMlIHg=; h=Date:From:To:Subject:References:In-Reply-To:From; b=nLH0nobqbMg95mrxmHeGxmRy9Gd8Zz7SwhYIVzYllZdAxH+wVn3xt06+lEbwPQ5ls EO60QnXzC9CGjlqkkKdknAtKW0jZEZeQx5c0x4u5In7JSKczb7w22JFnyEBHflhpH8 dveZeS7lwTwKpypFbW80pC4NVRogd7x3+eU1gM8/esV74pV0EP7QOKbJIJ4+HwkEyX vgAvkANJBsjkJ79C/kauRq7GEBUNPuW2/OFhJT/wke/LLluB/iQjSmERbScHO3rfs1 Sx9pa6VoJLOlyBn9ufSi/Vs9eVzz3kWeqY9s2cZlF+29mKLm1W5FUBH1QRMG68z7z3 6zdITCAEZpSAg== Date: Tue, 6 Feb 2024 11:45:38 +0100 From: Alejandro Colomar To: gcc-patches@gcc.gnu.org Subject: [PATCH v5 RESEND] C, ObjC: Add -Wunterminated-string-initialization Message-ID: <20240206104529.8347-1-alx@kernel.org> X-Mailer: git-send-email 2.43.0 References: MIME-Version: 1.0 Content-Disposition: inline In-Reply-To: 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, T_SCC_BODY_TEXT_LINE 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: 1790146134491161188 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";