From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from ams.source.kernel.org (ams.source.kernel.org [IPv6:2604:1380:4601:e00::1]) by sourceware.org (Postfix) with ESMTPS id 18A423858C01 for ; Mon, 13 Nov 2023 09:55:15 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 18A423858C01 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 18A423858C01 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=2604:1380:4601:e00::1 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1699869317; cv=none; b=xthz6OWTFBHghty4YHv1ClTNPmMsCCDHxAUX5u7ptAYR1rtBDG7s6DMQ2yMR3WNRTG1WfvbVtFtLwX8ZlYqVX/s2qkjNLYRdjDFcBUbaiZdzTA2BTxUi7iVcgr7ykNXIsOP1EXCm+F33uNQpoC+pIbMRjWHFqacsboZ5jX5Y7w4= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1699869317; c=relaxed/simple; bh=blIs8rvVbigd2o7hMlMtlRhNO7v7w7e2ASDrrgQD9FE=; h=DKIM-Signature:Date:From:To:Subject:Message-ID:MIME-Version; b=KAg0eCTVqpyYQLJPKBhbejtir56xcQav0/w/X6AYue5zIG+Qmj4GO6i2GsVgg1FpBHUjvbuBOGQuPjzZotfL7gIS28UO4anqauTJpZG5B/3bSF2uB3Xua4xjJmOurSdzJRsEC0gQdd7TPEhUB4lE625LOr6rizlIAS29VkVSRp4= ARC-Authentication-Results: i=1; server2.sourceware.org Received: from smtp.kernel.org (transwarp.subspace.kernel.org [100.75.92.58]) by ams.source.kernel.org (Postfix) with ESMTP id 1E73FB80CB9; Mon, 13 Nov 2023 09:55:14 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 7B8FBC433C7; Mon, 13 Nov 2023 09:55:11 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1699869313; bh=blIs8rvVbigd2o7hMlMtlRhNO7v7w7e2ASDrrgQD9FE=; h=Date:From:To:Cc:Subject:References:In-Reply-To:From; b=BdlovVj35PzhfEc3/i7ltcWh0K9l0A4mabhPdZdGnGQgsKlrQw5H8xHWPV1D/T23J AgM5xIq0O9mHcO+qpZEzoRTbvOBrBrsWIxovRqKyKDUAHlwW0kBcN5H2r5kFV17ax6 +SEUhzc5u3yDWLykWipwERxoKRjFo+yx8H7scI22Ge4TjO7n1/47qi7m0v0N2yN8NP mr9X3btUklRm5HRb28qfLC81ALDxgox2Gs6drq7t7XtRnKyXvqKlPloE8Mcogo3MyP HELVq3vU66+MgBHT7MLyvnbAXP6ttxQIjXbuuOMqSxVR7zbtwHsxatY6SKIXxyUJl3 eWxkiHdF2Ke8g== Date: Mon, 13 Nov 2023 10:55:09 +0100 From: Alejandro Colomar To: gcc-patches@gcc.gnu.org Cc: Doug McIlroy , "G. Branden Robinson" , Ralph Corderoy , Larry McVoy , Andrew Pinski , Jonathan Wakely , Andrew Clayton , Martin Uecker , David Malcolm Subject: Re: [PATCH v5] C, ObjC: Add -Wunterminated-string-initialization Message-ID: References: <20231001162400.68141-1-alx@kernel.org> MIME-Version: 1.0 Content-Type: multipart/signed; micalg=pgp-sha512; protocol="application/pgp-signature"; boundary="iGHmnTuNKosKfgr8" Content-Disposition: inline In-Reply-To: <20231001162400.68141-1-alx@kernel.org> X-Spam-Status: No, score=-10.2 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 List-Id: --iGHmnTuNKosKfgr8 Content-Type: text/plain; protected-headers=v1; charset=utf-8 Content-Disposition: inline Content-Transfer-Encoding: quoted-printable Date: Mon, 13 Nov 2023 10:55:09 +0100 From: Alejandro Colomar To: gcc-patches@gcc.gnu.org Cc: Doug McIlroy , "G. Branden Robinson" , Ralph Corderoy , Larry McVoy , Andrew Pinski , Jonathan Wakely , Andrew Clayton , Martin Uecker , David Malcolm Subject: Re: [PATCH v5] C, ObjC: Add -Wunterminated-string-initialization Hi, Gentle ping, just again a little before v14 stage 3. Do I need to do anything else with this patch? The CI seemed to say it's ok. Cheers, Alex On Sun, Oct 01, 2023 at 06:24:00PM +0200, Alejandro Colomar wrote: > Warn about the following: >=20 > char s[3] =3D "foo"; >=20 > 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. >=20 > 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. >=20 > char *log_levels[] =3D { "info", "warning", "err" }; > vs. > char log_levels[][7] =3D { "info", "warning", "err" }; >=20 > 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: >=20 > char log_levels[][8] =3D { "info", "warning", "err" }; >=20 > 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. >=20 > Since Wc++-compat now includes this warning, the test has to be modified > to expect the text of the new warning too, in . >=20 > 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 > --- >=20 > v5: >=20 > - Fix existing C++-compat tests. [reported by ] >=20 >=20 > 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-initializat= ion.c >=20 > 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. > =20 > +Wunterminated-string-initialization > +C ObjC Var(warn_unterminated_string_initialization) Warning LangEnabledB= y(C ObjC,Wextra || Wc++-compat) > +Warn about character arrays initialized as unterminated character sequen= ces 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" } */ > =20 > char a1[] =3D "a"; > -char a2[1] =3D "a"; /* { dg-warning "C\[+\]\[+\]" } */ > +char a2[1] =3D "a"; /* { dg-warning "initializer-string for array of 'ch= ar' is too long" } */ > char a3[2] =3D "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[] =3D "a"; > +char a2[1] =3D "a"; /* { dg-warning "initializer-string for array of 'ch= ar' is too long" } */ > +char a3[2] =3D "a"; > --=20 > 2.40.1 >=20 --=20 --iGHmnTuNKosKfgr8 Content-Type: application/pgp-signature; name="signature.asc" -----BEGIN PGP SIGNATURE----- iQIzBAABCgAdFiEE6jqH8KTroDDkXfJAnowa+77/2zIFAmVR8n0ACgkQnowa+77/ 2zIZtg//Ybnwn64nueznGtHKm1Mj99VOtdw1TIq+G3gjKnzNw8bOh0IEpSbx39U1 FKCO8jGergTI6Jkvh2gkSbDCLQ9Fi443UB8ASmCZ6rmj4VIJuPfLjVhYKiCrMXE5 93XZTj04hNRFne4w9feGg8knMfvSOYWdzui1WW5Ehf9i4sKWSgS2kwPGuNIHUiiO 5C6fZtImlSN1XSpsAJ2JH67lyMb9gULCPsxIM4HY6TlzrWXF6klirrzA3PozHHN9 jFr3fQHa09m2OC9rOE4IScjW2IbKznLv45nymSwbWs+wb22ZFxs/wmUu3awFPRMU caN9WOngMAUHMDpZdGtRN/eKGJG0alQjMr1Wt/AUgQNjqXGcYCWYC5rfxB3ZxiJX swjLjPYlCFNUVuFRQqM4is7zPfmfNV66x0GZKpbp/F2cEWnnCshyhJXbND91mMA8 4kkXLBcRF2/LBbEwOvodzJ0TWhEC/m5ckvbnJ0A9WfjkK1UyZOzzkWFnr0djp92E IR4Lh+WD+vR6jFMlk49fsGfljITF0Xy1wPBuYyvljXORDyArwUAMHlzv2WthWpvu EN/ZY52g8uJO+lCnShEupoQukilQYeabfNiG3PGxgdLjE0HQFHyebqAtReBhSztf n9//0i/vD/FUk2H4FN+W8+KKvatiqHEp8WNarG3DWc8eSWUM0Uk= =NoWg -----END PGP SIGNATURE----- --iGHmnTuNKosKfgr8--