From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail-ed1-x532.google.com (mail-ed1-x532.google.com [IPv6:2a00:1450:4864:20::532]) by sourceware.org (Postfix) with ESMTPS id 874BA3858D35 for ; Mon, 14 Nov 2022 13:15:06 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 874BA3858D35 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-ed1-x532.google.com with SMTP id v27so17275390eda.1 for ; Mon, 14 Nov 2022 05:15:06 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:from:to:cc:subject:date:message-id:reply-to; bh=PvNRGk44szzfnAw1tJeo5QCR4CkAJ0QsCXHIC4fOwwo=; b=Ej4BTh6GTl78RDi3UlcSMzTRZB66cSr0PvA85wWEZNv1GMn7DhgZ7v7/h6t4T7DV5e Tf0B9G5SSz1z2GfGLPKYEJ8kQ2YRYr/dGj8iowTcyj53zS1t1yBHJhpMtbJcph8ho1Ws JqMzUVKnGT1Dn6gMT0j5sF1BNYAgdeJs/2qdtxz04S9v+JBoKV9CG/mx7A1W6ogM0y4r GKIEU9cU4loMdkGi+BRJPAXy+vSobmNLjtD46Yphhp+anC8q4ilz+eXRa1CGtN0sXKAT vCWlSTx1AbQrPzCZ30Jc9ubwTotvKf0lNjU5SmqvkbmfM4B0qPjV6DJ8JfAiWq2ibGBC 2u+Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=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=PvNRGk44szzfnAw1tJeo5QCR4CkAJ0QsCXHIC4fOwwo=; b=nAQ7NgEb/DhYcpKtyQrWWnC5qWQYfH3io0dAwXpOr9KgMqT/LlRFsRCSd0PhOPUDaE VIEfca430wohxGf3F1y7vgFrcMxOSScmTqFzSDUKwQPGZk4ZU7oNIcHivLIoQl1S6vRf H9HD8WQ4jWDc9U0vf8BQDkm4MaoQpm75xgsqHPfNmrXREn/ZsDuwwuUkQCeI0BPuaCqC fXmhnl4YGL4ePSP9DG1i6T/agA2SBEMZGTBIKZLWkxwWSOiKD/4ANApydCJzifY2XUif FyYb0Tf6c9YIebnH+4TUblMmbhtLJQBA4nGyst+linuU0FRS7vc0gxHR5UQJbjWwIRmg eF6g== X-Gm-Message-State: ANoB5pmWssVKo3fk75FORhzLLi0ix9aKQuXRQZuNTaGBGnPOab9R026w /CFdx9weaa6ZDTK1yeTjgEifpugMNCP9XvdVZ7A= X-Google-Smtp-Source: AA0mqf6Z1VkFyAg9Y28U5ZgLkk9vKfZpRJIzht1YPRHWcpYQuYLxCm5XrvJi9JfEsHa+ErFS0xL9KC8D8hvtdo/qcY4= X-Received: by 2002:a05:6402:360d:b0:461:b033:90ac with SMTP id el13-20020a056402360d00b00461b03390acmr11135898edb.257.1668431705266; Mon, 14 Nov 2022 05:15:05 -0800 (PST) MIME-Version: 1.0 References: <36da94eb-1cac-5ae8-7fea-ec66160cf413@gmail.com> <550e6bdb-4eb8-7eca-adb6-78cecc920f01@gmail.com> In-Reply-To: <550e6bdb-4eb8-7eca-adb6-78cecc920f01@gmail.com> From: Jonathan Wakely Date: Mon, 14 Nov 2022 13:14:54 +0000 Message-ID: Subject: Re: [whish] -Wunterminated-string-initialization: new warning To: Alejandro Colomar Cc: Andrew Pinski , gcc@gcc.gnu.org, Douglas McIlroy Content-Type: text/plain; charset="UTF-8" X-Spam-Status: No, score=-0.7 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 Mon, 14 Nov 2022 at 11:38, Alejandro Colomar via Gcc wrote: > > Hi Andrew! > > On 11/13/22 23:12, Andrew Pinski wrote: > > 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] = "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] = "foo"; // 'f' 'o' > > | ^~~~~ > > :3:19: warning: initializer-string for array of 'char' is too > > long for C++ [-Wc++-compat] > > 3 | char three[3] = "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 > > Great! I'd like to implement it myself, as I've never written any GCC code yet, > so it's interesting to me. If you recall any (hopefully recent) case where a > similar thing happened (the warning was already implemented and only needed a > name), it might help me check how it was done. `git log gcc/c-family/c.opt` will show loads of changes adding warnings. > > BTW, I had another idea to add a suffix to string literals to make them > unterminated: > > char foo[3] = "foo"u; // OK > char bar[4] = "bar"; // OK > > char baz[4] = "baz"u; // Warning: initializer is too short. > char etc[3] = "etc"; // Warning: unterminated string. > > Is that doable? Do you think it makes sense? IMHO no. This is not useful enough to add a language extension, it's an incredibly niche use case. Your suggested syntax also looks very confusing with UTF-16 string literals, and is not sufficiently distinct from a normal string literal to be obvious when quickly reading the code. People expect string literals in C to be null-terminated, having a subtle suffix that changes that would be a bug farm. You can do {'b', 'a', 'z'} if you want an explicitly unterminated array of char.