diff mbox

[C] Fix set_nonincremental_init_from_string (PR c/67410)

Message ID 20160812155455.GD14857@tucnak.redhat.com
State New
Headers show

Commit Message

Jakub Jelinek Aug. 12, 2016, 3:54 p.m. UTC
Hi!

The wide-int conversion of set_nonincremental_init_from_string broke this
function.
Before we used val[bitpos < HOST_BITS_PER_WIDE_INT], but val[1] and val[0]
has been swapped, so either it should be bitpos >= HOST_BITS_PER_WIDE_INT,
or bitpos / HOST_BITS_PER_WIDE_INT.

Bootstrapped/regtested on x86_64-linux and i686-linux, ok for trunk/6.2?

2016-08-12  Jakub Jelinek  <jakub@redhat.com>
	    Martin Liska  <mliska@suse.cz>

	PR c/67410
	* c-typeck.c (set_nonincremental_init_from_string): Use / instead of
	% to determine val element to change.  Assert that
	wchar_bytes * charwidth fits into val array.

	* gcc.dg/pr67410.c: New test.


	Jakub

Comments

Joseph Myers Aug. 12, 2016, 4:02 p.m. UTC | #1
On Fri, 12 Aug 2016, Jakub Jelinek wrote:

> Hi!
> 
> The wide-int conversion of set_nonincremental_init_from_string broke this
> function.
> Before we used val[bitpos < HOST_BITS_PER_WIDE_INT], but val[1] and val[0]
> has been swapped, so either it should be bitpos >= HOST_BITS_PER_WIDE_INT,
> or bitpos / HOST_BITS_PER_WIDE_INT.
> 
> Bootstrapped/regtested on x86_64-linux and i686-linux, ok for trunk/6.2?

OK.
Marek Polacek Aug. 12, 2016, 4:03 p.m. UTC | #2
On Fri, Aug 12, 2016 at 05:54:55PM +0200, Jakub Jelinek wrote:
> Hi!
> 
> The wide-int conversion of set_nonincremental_init_from_string broke this
> function.
> Before we used val[bitpos < HOST_BITS_PER_WIDE_INT], but val[1] and val[0]
> has been swapped, so either it should be bitpos >= HOST_BITS_PER_WIDE_INT,
> or bitpos / HOST_BITS_PER_WIDE_INT.
> 
> Bootstrapped/regtested on x86_64-linux and i686-linux, ok for trunk/6.2?
> 
> 2016-08-12  Jakub Jelinek  <jakub@redhat.com>
> 	    Martin Liska  <mliska@suse.cz>
> 
> 	PR c/67410
> 	* c-typeck.c (set_nonincremental_init_from_string): Use / instead of
> 	% to determine val element to change.  Assert that
> 	wchar_bytes * charwidth fits into val array.
> 
> 	* gcc.dg/pr67410.c: New test.

LGTM.

	Marek
diff mbox

Patch

--- gcc/c/c-typeck.c.jj	2016-08-06 12:11:47.000000000 +0200
+++ gcc/c/c-typeck.c	2016-08-12 15:18:40.629878806 +0200
@@ -8558,6 +8558,8 @@  set_nonincremental_init_from_string (tre
 
   wchar_bytes = TYPE_PRECISION (TREE_TYPE (TREE_TYPE (str))) / BITS_PER_UNIT;
   charwidth = TYPE_PRECISION (char_type_node);
+  gcc_assert ((size_t) wchar_bytes * charwidth
+	      <= ARRAY_SIZE (val) * HOST_BITS_PER_WIDE_INT);
   type = TREE_TYPE (constructor_type);
   p = TREE_STRING_POINTER (str);
   end = p + TREE_STRING_LENGTH (str);
@@ -8583,7 +8585,7 @@  set_nonincremental_init_from_string (tre
 		bitpos = (wchar_bytes - byte - 1) * charwidth;
 	      else
 		bitpos = byte * charwidth;
-	      val[bitpos % HOST_BITS_PER_WIDE_INT]
+	      val[bitpos / HOST_BITS_PER_WIDE_INT]
 		|= ((unsigned HOST_WIDE_INT) ((unsigned char) *p++))
 		   << (bitpos % HOST_BITS_PER_WIDE_INT);
 	    }
--- gcc/testsuite/gcc.dg/pr67410.c.jj	2016-08-12 14:58:19.643334006 +0200
+++ gcc/testsuite/gcc.dg/pr67410.c	2016-08-12 15:02:06.391451195 +0200
@@ -0,0 +1,15 @@ 
+/* PR c/67410 */
+/* { dg-do run } */
+/* { dg-options "-std=gnu11" } */
+
+struct {
+  __CHAR16_TYPE__ s[2];
+} a[] = { u"ff", [0].s[0] = u'x', [1] = u"\u1234\u4567", [1].s[0] = u'\u89ab' };
+
+int
+main ()
+{
+  if (a[0].s[0] != u'x' || a[0].s[1] != u'f' || a[1].s[0] != u'\u89ab' || a[1].s[1] != u'\u4567')
+    __builtin_abort ();
+  return 0;
+}