From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail-pl1-x62c.google.com (mail-pl1-x62c.google.com [IPv6:2607:f8b0:4864:20::62c]) by sourceware.org (Postfix) with ESMTPS id 29D7B3857B9B for ; Sun, 13 Nov 2022 22:12:37 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 29D7B3857B9B Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=gmail.com Received: by mail-pl1-x62c.google.com with SMTP id d20so8483487plr.10 for ; Sun, 13 Nov 2022 14:12:37 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:from:to:cc:subject:date :message-id:reply-to; bh=dbDoqY49jH4nFfuezbZfhtCHbtCjnxLlyjqmKRuq2sI=; b=ifulC2Sa4YMDFqjIAlwsRt4TlvwzgWuotqIvrgQdPdMrQVcKJI02T+kjPgGdhJB+Q5 gtepyFUQeXUQQ6HZYYhb3S+7CNYY4Yc4xuD5A/gHdy/sdTBRViXiXoqqL8dGCe+zgcgC Fpz6h0rlfTkEMXVDUavUcLuo1w/ue7mDiDSaw4d+lbYZnABRxscEJ5ABWbrvHVKhylQS eCQbohdoarjG2NKrvdupGMO2c/r5v7NKG5ZehT0CsdmKwmqcRCtMO4LmSr5R2zPjd9vC JYzukzcIdrxLBsKP85mJBk5TWKd6iFAedn901D+2JtDCfsIDiDWETd++nto1JoEAR+uC CBxA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=dbDoqY49jH4nFfuezbZfhtCHbtCjnxLlyjqmKRuq2sI=; b=OjDkZwomW65uC5v7JWTV5n2r1BwHHV2IVjZVDu7IXzDN3yE2dCweSP+qXKnc0rEdcM XeEmpBBpFsZKpPJS46LeaNuuaXPVu64ww1N70KDX5Eoon11Jo+IVESpC7qXcsOEFbAmW y9FkxdLLPjq5UlN0r5PGwUyze1GJMQ2VcmKHZkeNzzLvecD/EqCSLguDwU/Xv+4V/EoB cA1uWVmkcrv/rNjQ2wXN47J8MlruJoa+RoVgqXXjL+HokBHh0EvPQ6cfEp1DKsiSilPe SMkRwAmOAkgyWzRzTo5NDvCFOWMb1tJNijQTWRzK69roWghKEu1Tr9omIDF6+r4zakOs XkCA== X-Gm-Message-State: ANoB5pl8I0LNJeQCNv5D2+KPWorsItHEuNWCZIh6tHy/wRsZgomkZMBf iVxk3GI2gYCRRL0pddUJldi8nOBBKjNlpoqEyTs= X-Google-Smtp-Source: AA0mqf4fNEprAooeqFbJBy83uXa82YF2kEJcYUKSFhk4Lu4iVjpvm9Y1IVSpCpx0TmurRvTOEjdDINuVG2Hjm4tRaK4= X-Received: by 2002:a17:90a:9ce:b0:206:238e:1379 with SMTP id 72-20020a17090a09ce00b00206238e1379mr11056614pjo.180.1668377555483; Sun, 13 Nov 2022 14:12:35 -0800 (PST) MIME-Version: 1.0 References: <36da94eb-1cac-5ae8-7fea-ec66160cf413@gmail.com> In-Reply-To: <36da94eb-1cac-5ae8-7fea-ec66160cf413@gmail.com> From: Andrew Pinski Date: Sun, 13 Nov 2022 14:12:22 -0800 Message-ID: Subject: Re: [whish] -Wunterminated-string-initialization: new warning To: Alejandro Colomar Cc: gcc@gcc.gnu.org Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable X-Spam-Status: No, score=-1.6 required=5.0 tests=BAYES_00,DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,FREEMAIL_FROM,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 List-Id: On Sun, Nov 13, 2022 at 1:57 PM Alejandro Colomar via Gcc wrote: > > Hi! > > I'd like to get warnings if I write the following code: > > char foo[3] =3D "foo"; This should be easy to add as it is already part of the -Wc++-compat option as for C++ it is invalid code. :2:19: warning: initializer-string for array of 'char' is too long 2 | char two[2] =3D "foo"; // 'f' 'o' | ^~~~~ :3:19: warning: initializer-string for array of 'char' is too long for C++ [-Wc++-compat] 3 | char three[3] =3D "foo"; // 'f' 'o' 'o' | ^~~~~ ... (for your more complex case [though I needed to modify one of the strings to exactly 8] :5:7: warning: initializer-string for array of 'char' is too long for C++ [-Wc++-compat] 5 | "01234567", | ^~~~~~~~~~ else if (warn_cxx_compat && compare_tree_int (TYPE_SIZE_UNIT (type), len) < 0= ) warning_at (init_loc, OPT_Wc___compat, ("initializer-string for array of %qT " "is too long for C++"), typ1); That is the current code which does this warning even so it is just a matter of adding an option to c-family/c.opt and then having c++-compat enable it and using that new option here. Thanks, Andrew Pinski > > It's hard to keep track of sizes to make sure that the string literals al= ways > initialize to terminated strings. It seems something that should be easy= to > implement in the compiler. > > A morecomplex case where it's harder to keep track of sizes is: > > static const char log_levels[][8] =3D { > "alert", > "error", > "warn", > "notice", > "info", > "debug", > }; > > Here, 8 works now (and 7 too, but for aligmnent reasons I chose 8). If t= omorrow > we add or change an entry, It'll be hard to keep it safe. Such a warning= would > help a lot. > > > An example program is: > > $ cat str.c > char two[2] =3D "foo"; // 'f' 'o' > char three[3] =3D "foo"; // 'f' 'o' 'o' > char four[4] =3D "foo"; // 'f' 'o' 'o' '\0' > char five[5] =3D "foo"; // 'f' 'o' 'o' '\0' '\0' > char implicit[] =3D "foo"; // 'f' 'o' 'o' '\0' > > $ cc -Wall -Wextra str.c > str.c:1:19: warning: initializer-string for array of =E2=80=98char=E2=80= =99 is too long > 1 | char two[2] =3D "foo"; // 'f' 'o' > | ^~~~~ > /usr/bin/ld: /usr/lib/gcc/x86_64-linux-gnu/12/../../../x86_64-linux-gnu/S= crt1.o: > in function `_start': > (.text+0x17): undefined reference to `main' > collect2: error: ld returned 1 exit status > > > Here, I'd like that with the new warning, 'three' would also get warned. > > Cheers, > > Alex > -- >