[1/2] Fix C/107926: Wrong error message when initializing char array

Message ID 1669828695-18532-1-git-send-email-apinski@marvell.com
State Accepted
Headers
Series [1/2] Fix C/107926: Wrong error message when initializing char array |

Checks

Context Check Description
snail/gcc-patch-check success Github commit url

Commit Message

Li, Pan2 via Gcc-patches Nov. 30, 2022, 5:18 p.m. UTC
  From: Andrew Pinski <apinski@marvell.com>

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 ceck for string cst until
	after the error message.

gcc/testsuite/ChangeLog:

	PR c/107926
	* gcc.dg/init-excess-3.c: New test.
---
 gcc/c/c-typeck.cc                    | 15 ++++++++++-----
 gcc/testsuite/gcc.dg/init-excess-3.c | 15 +++++++++++++++
 2 files changed, 25 insertions(+), 5 deletions(-)
 create mode 100644 gcc/testsuite/gcc.dg/init-excess-3.c
  

Comments

Jakub Jelinek Nov. 30, 2022, 5:29 p.m. UTC | #1
On Wed, Nov 30, 2022 at 09:18:14AM -0800, apinski--- via Gcc-patches wrote:
> gcc/c/ChangeLog:
> 
> 	PR c/107926
> 	* c-typeck.cc (process_init_element):
> 	Move the ceck for string cst until
> 	after the error message.

Just a ChangeLog nit, not a patch review for which I defer to C FE
maintainers/reviewers.
s/ceck/check/, plus don't start the description uselessly on a next line
when half of it would fit on the first line after ):.
 	* c-typeck.cc (process_init_element): Move the check for string cst
	until after the error message.

	Jakub
  
Jeff Law April 30, 2023, 4:23 p.m. UTC | #2
On 11/30/22 10:18, apinski--- via Gcc-patches wrote:
> From: Andrew Pinski <apinski@marvell.com>
> 
> 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 ceck for string cst until
> 	after the error message.
> 
> gcc/testsuite/ChangeLog:
> 
> 	PR c/107926
> 	* gcc.dg/init-excess-3.c: New test.
OK
jeff
  

Patch

diff --git a/gcc/c/c-typeck.cc b/gcc/c/c-typeck.cc
index e06f052..0fc382c 100644
--- a/gcc/c/c-typeck.cc
+++ b/gcc/c/c-typeck.cc
@@ -10623,17 +10623,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 0000000..7741261
--- /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 } */