public inbox for gcc-cvs@sourceware.org
help / color / mirror / Atom feed
From: Andrew Pinski <pinskia@gcc.gnu.org>
To: gcc-cvs@gcc.gnu.org
Subject: [gcc r14-363] Fix C/107926: Wrong error message when initializing char array
Date: Sun, 30 Apr 2023 21:49:25 +0000 (GMT)	[thread overview]
Message-ID: <20230430214925.DE4443858D35@sourceware.org> (raw)

https://gcc.gnu.org/g:a6b810ae783acf8cec2d2272a46bd6de0976f496

commit r14-363-ga6b810ae783acf8cec2d2272a46bd6de0976f496
Author: Andrew Pinski <apinski@marvell.com>
Date:   Wed Nov 30 02:54:57 2022 +0000

    Fix C/107926: Wrong error message when initializing char array
    
    The problem here is the code which handles {"a"} is supposed
    to handle the case where the is something after the string but
    it only handles the case where there is another string so
    we go down the other path and error out saying "excess elements
    in struct initializer" even though this was a character array.
    To fix this, we need to move the ckeck if the initializer is
    a string after the check for array and initializer.
    
    OK? Bootstrapped and tested on x86_64-linux-gnu with no regressions.
    
    Thanks,
    Adnrew Pinski
    
    gcc/c/ChangeLog:
    
            PR c/107926
            * c-typeck.cc (process_init_element): Move the check
            for string cst until after the error message.
    
    gcc/testsuite/ChangeLog:
    
            PR c/107926
            * gcc.dg/init-excess-3.c: New test.

Diff:
---
 gcc/c/c-typeck.cc                    | 15 ++++++++++-----
 gcc/testsuite/gcc.dg/init-excess-3.c | 15 +++++++++++++++
 2 files changed, 25 insertions(+), 5 deletions(-)

diff --git a/gcc/c/c-typeck.cc b/gcc/c/c-typeck.cc
index a17879698ec..9cc7e0143a9 100644
--- a/gcc/c/c-typeck.cc
+++ b/gcc/c/c-typeck.cc
@@ -10639,17 +10639,22 @@ process_init_element (location_t loc, struct c_expr value, bool implicit,
 
   /* Handle superfluous braces around string cst as in
      char x[] = {"foo"}; */
-  if (string_flag
-      && constructor_type
+  if (constructor_type
       && !was_designated
       && TREE_CODE (constructor_type) == ARRAY_TYPE
       && INTEGRAL_TYPE_P (TREE_TYPE (constructor_type))
       && integer_zerop (constructor_unfilled_index))
     {
       if (constructor_stack->replacement_value.value)
-	error_init (loc, "excess elements in %<char%> array initializer");
-      constructor_stack->replacement_value = value;
-      return;
+	{
+	  error_init (loc, "excess elements in %<char%> array initializer");
+	  return;
+	}
+      else if (string_flag)
+	{
+	  constructor_stack->replacement_value = value;
+	  return;
+	}
     }
 
   if (constructor_stack->replacement_value.value != NULL_TREE)
diff --git a/gcc/testsuite/gcc.dg/init-excess-3.c b/gcc/testsuite/gcc.dg/init-excess-3.c
new file mode 100644
index 00000000000..7741261bd49
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/init-excess-3.c
@@ -0,0 +1,15 @@
+/* Test for various cases of excess initializers for char arrays,
+   bug 107926. */
+/* { dg-do compile } */
+/* { dg-options "" } */
+
+
+char s0[] = {"abc",1}; /* { dg-error "array initializer|near init" } */
+char s1[] = {"abc","a"}; /* { dg-error "array initializer|near init" } */
+char s2[] = {1,"abc"}; /* { dg-error "array initializer|near init|computable at load time" } */
+/* { dg-warning "integer from pointer without a cast" "" { target *-*-* } .-1 } */
+
+char s3[5] = {"abc",1}; /* { dg-error "array initializer|near init" } */
+char s4[5] = {"abc","a"}; /* { dg-error "array initializer|near init" } */
+char s5[5] = {1,"abc"}; /* { dg-error "array initializer|near init|computable at load time" } */
+/* { dg-warning "integer from pointer without a cast" "" { target *-*-* } .-1 } */

                 reply	other threads:[~2023-04-30 21:49 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20230430214925.DE4443858D35@sourceware.org \
    --to=pinskia@gcc.gnu.org \
    --cc=gcc-cvs@gcc.gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).