From patchwork Fri Mar 24 13:39:28 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alejandro Colomar X-Patchwork-Id: 74557 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:b0ea:0:b0:3b6:4342:cba0 with SMTP id b10csp648065vqo; Fri, 24 Mar 2023 07:03:01 -0700 (PDT) X-Google-Smtp-Source: AKy350ZunaBsg6l0eIoGa/H76XU39mPgNP7wMizIonzTjNAmqThPz1xyewAMc7e0G0FoJSkJx6P3 X-Received: by 2002:aa7:d806:0:b0:4fc:c644:6149 with SMTP id v6-20020aa7d806000000b004fcc6446149mr3204958edq.0.1679666581354; Fri, 24 Mar 2023 07:03:01 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1679666581; cv=none; d=google.com; s=arc-20160816; b=dum7KFt+q5Ct3QM/CVI7r91lcSMniu/ghNZmZ6GNTy3w8erRMsH3VGw+FCqtpSIC7i 6Nk8k+XkxwV68O4CMwCT6wBMyk/IV0/tMzXxgjMDgREGqD6LzmnWq9WMjp7lFk60wcWm 2CbrjygkIjNSl2J9ZT5NdRaQJbH+es/0IHexGdaok+gLS0naVbjLrRBoefivbPLgzu5e KumbkthaTO9JdqKs2lMH11liq/29Qz+zWMtEGn06LDrms7XUyjY3bKTGhUWF6EY4OGv4 iXUxgCxVmlyyoboQDL9c0LvKrW18FvRvdVYc7i+Av3rRXu8Iyv78z2EgjW45kFV7HIHy 2hLA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:reply-to:from:list-subscribe:list-help:list-post :list-archive:list-unsubscribe:list-id:precedence :content-transfer-encoding:mime-version:message-id:date:subject:cc :to:dmarc-filter:delivered-to:dkim-signature:dkim-filter; bh=DyNaD6rcMw68Zi+Id1SkQf4/zGY6/tSTfwc/2d9AtpI=; b=Ugj9NkIgvJa4mQ3+gnwptI3YL41kFkLiRmQnFasy+tPk1e8d5kq1yIp5NxOUf32TPd 3qvt87itYA53dtaNnN/kHPxLJ4GlyGnefG3ZZQwUDK+0iCHaCwyvHpYko3K2H+cc3+f1 zGKklvN+aUqCa/MXBk6bGKihxaUVCennG5Y18BRi2sg5DoPvlIq/g2we4puvbCrKufAw 4KiEsTqhvUsjFh4WaBJTHCLgJs/BVnsGgkr5p8dORUZ+itN5i0StE06WW5DOKuzYe+Ny jdiuo6CHOseiG6Gpabp1hPBFwFkTri0RHLP/TqykrVdhQzaPhNrQ6HZinCLeqEEQIsF+ gf9w== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gcc.gnu.org header.s=default header.b=Vqcj7UvG; 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=gnu.org Received: from sourceware.org (server2.sourceware.org. [8.43.85.97]) by mx.google.com with ESMTPS id t3-20020a056402020300b00502238699b5si1214266edv.670.2023.03.24.07.03.01 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 24 Mar 2023 07:03:01 -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=@gcc.gnu.org header.s=default header.b=Vqcj7UvG; 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=gnu.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 349D5383EC76 for ; Fri, 24 Mar 2023 14:02:48 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 349D5383EC76 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1679666568; bh=DyNaD6rcMw68Zi+Id1SkQf4/zGY6/tSTfwc/2d9AtpI=; h=To:Cc:Subject:Date:List-Id:List-Unsubscribe:List-Archive: List-Post:List-Help:List-Subscribe:From:Reply-To:From; b=Vqcj7UvGZYMmTdr11OKF1HkbxYomr7qjqujcasXG/QXnlHgpoPP0JLmpZQFtU/nU0 6LKJnGavHckn5z9kE9aURJJuC2Bb9rmobTJd80CFo4bkPLnJgVhYjPWerrEa/7Ljrk 8PuZ0MlMK8CRWcD2JcxgsymxZkvSGcjDTnP17NeY= X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from mail-ed1-x52e.google.com (mail-ed1-x52e.google.com [IPv6:2a00:1450:4864:20::52e]) by sourceware.org (Postfix) with ESMTPS id EBAAF3858D35 for ; Fri, 24 Mar 2023 14:01:52 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org EBAAF3858D35 Received: by mail-ed1-x52e.google.com with SMTP id t10so8222093edd.12 for ; Fri, 24 Mar 2023 07:01:52 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; t=1679666511; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=DyNaD6rcMw68Zi+Id1SkQf4/zGY6/tSTfwc/2d9AtpI=; b=GaFmXJBKwEn6HprtM/zjzeuheoSCSstQvmW0OSGmoQjizVuCtBRDGRpZZdNsoafkGr ETswKRqLTc9Bk9eYgx2s+RxLnFuxpqkmk2E3es/+MLWKDB5K2mfW8OtTXaP9mhb/Hbms bGfKuWn9i6IIYi1/3fKKCWIzOReni1AaVc93u6g8ZyNWsuqpO85pyW2XrFdU/XM/tu0E MtoComqzqfHfhkTzz9f6wVMZD0VjH54vD62YeaXPdOThi8GLn4diaLMZse9MSc9FzyOS U3UgQctEm/eJmMXRQnsti9yKsDNCjh26/Uh0kx3BjBvWbnKf45uSttMqbiQYNmoYwYAk 5bWQ== X-Gm-Message-State: AAQBX9eeqqGZzfMlDp+1XSJT+QVH7E1fj+yLXCq7I6FRHLEsCWOL/lNj vlM916ir2sLUwnWU+2OtYOkjwVlwwvY= X-Received: by 2002:adf:efc7:0:b0:2da:45b6:a1d5 with SMTP id i7-20020adfefc7000000b002da45b6a1d5mr2080024wrp.3.1679665182153; Fri, 24 Mar 2023 06:39:42 -0700 (PDT) Received: from asus5775.alejandro-colomar.es ([170.253.51.134]) by smtp.googlemail.com with ESMTPSA id c15-20020adffb0f000000b002c6e8cb612fsm18353058wrr.92.2023.03.24.06.39.41 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 24 Mar 2023 06:39:41 -0700 (PDT) X-Google-Original-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 Subject: [PATCH v2] C, ObjC: Add -Wunterminated-string-initialization Date: Fri, 24 Mar 2023 14:39:28 +0100 Message-Id: <20230324133928.14753-1-alx@kernel.org> X-Mailer: git-send-email 2.39.2 MIME-Version: 1.0 X-Spam-Status: No, score=-10.2 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, FREEMAIL_FROM, GIT_PATCH_0, RCVD_IN_DNSWL_NONE, 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.29 Precedence: list List-Id: Gcc-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Patchwork-Original-From: Alejandro Colomar via Gcc-patches From: Alejandro Colomar Reply-To: Alejandro Colomar Errors-To: gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org Sender: "Gcc-patches" X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1761258065294118016?= X-GMAIL-MSGID: =?utf-8?q?1761258065294118016?= 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. 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 Signed-off-by: Alejandro Colomar --- Hi, I sent v1 to the wrong list. This time I've made sure to write to gcc-patches@. v2 adds some draft of a test, as suggested by Martin. However, I don't know yet how to write those, so the test is just a draft. But I did test the feature, by compiling GCC and compiling some small program with it. Cheers, Alex Range-diff against v1: 1: 61ddf1eb816 ! 1: e40d8f54942 C, ObjC: Add -Wunterminated-string-initialization @@ Commit message Cc: Andrew Pinski Cc: Jonathan Wakely Cc: Andrew Clayton + Cc: Martin Uecker Signed-off-by: Alejandro Colomar ## gcc/c-family/c.opt ## @@ gcc/c/c-typeck.cc: digest_init (location_t init_loc, tree type, tree init, tree if (compare_tree_int (TYPE_SIZE_UNIT (type), len) < 0) { unsigned HOST_WIDE_INT size + + ## gcc/testsuite/gcc.dg/Wunterminated-string-initialization.c (new) ## +@@ ++/* { dg-do compile } */ ++/* { dg-options "-Wunterminated-string-initialization" } */ ++ ++char a1[] = "a"; ++char a2[1] = "a"; /* { dg-warning "unterminated char sequence" } */ ++char a3[2] = "a"; gcc/c-family/c.opt | 4 ++++ gcc/c/c-typeck.cc | 6 +++--- gcc/testsuite/gcc.dg/Wunterminated-string-initialization.c | 6 ++++++ 3 files changed, 13 insertions(+), 3 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 3333cddeece..7f1fccfe02b 100644 --- a/gcc/c-family/c.opt +++ b/gcc/c-family/c.opt @@ -1382,6 +1382,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 45bacc06c47..ce2750f98bb 100644 --- a/gcc/c/c-typeck.cc +++ b/gcc/c/c-typeck.cc @@ -8420,11 +8420,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/Wunterminated-string-initialization.c b/gcc/testsuite/gcc.dg/Wunterminated-string-initialization.c new file mode 100644 index 00000000000..c6517702d51 --- /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 "unterminated char sequence" } */ +char a3[2] = "a";