Fix UB in dbxout.c (PR debug/84637)

Message ID 20180417205419.GJ8577@tucnak
State New
Headers show
Series
  • Fix UB in dbxout.c (PR debug/84637)
Related show

Commit Message

Jakub Jelinek April 17, 2018, 8:54 p.m.
Hi!

The first hunk is I think rather obvious, -2147483648 should be printed
as -2147483648 and doesn't need to introduce UB in the compiler while doing
so.  The changes to stabstr_D are analogous, but in addition to that I don't
see why we should just strip away the upper bits, it is again just a string
containing decimal digits.  What that function implemented is that
values from -4294967295 to 4294967295 were printed as expected and the rest
had some bits lost and HOST_WIDE_INT signed minimum invoked UB.

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

2018-04-17  Jakub Jelinek  <jakub@redhat.com>

	PR debug/84637
	* dbxout.c (dbxout_int): Perform negation in unsigned int type.
	(stabstr_D): Change type of unum from unsigned int to
	unsigned HOST_WIDE_INT.  Perform negation in unsigned HOST_WIDE_INT
	type.


	Jakub

Comments

Jim Wilson April 17, 2018, 10:05 p.m. | #1
On Tue, 2018-04-17 at 22:54 +0200, Jakub Jelinek wrote:
> 	PR debug/84637

> 	* dbxout.c (dbxout_int): Perform negation in unsigned int type.

> 	(stabstr_D): Change type of unum from unsigned int to

> 	unsigned HOST_WIDE_INT.  Perform negation in unsigned

> HOST_WIDE_INT

> 	type.


OK.

Jim
Jeff Law April 18, 2018, 4:58 p.m. | #2
On 04/17/2018 02:54 PM, Jakub Jelinek wrote:
> Hi!

> 

> The first hunk is I think rather obvious, -2147483648 should be printed

> as -2147483648 and doesn't need to introduce UB in the compiler while doing

> so.  The changes to stabstr_D are analogous, but in addition to that I don't

> see why we should just strip away the upper bits, it is again just a string

> containing decimal digits.  What that function implemented is that

> values from -4294967295 to 4294967295 were printed as expected and the rest

> had some bits lost and HOST_WIDE_INT signed minimum invoked UB.

> 

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

> 

> 2018-04-17  Jakub Jelinek  <jakub@redhat.com>

> 

> 	PR debug/84637

> 	* dbxout.c (dbxout_int): Perform negation in unsigned int type.

> 	(stabstr_D): Change type of unum from unsigned int to

> 	unsigned HOST_WIDE_INT.  Perform negation in unsigned HOST_WIDE_INT

> 	type.

OK.
jeff

Patch

--- gcc/dbxout.c.jj	2018-01-18 21:11:58.613207125 +0100
+++ gcc/dbxout.c	2018-04-17 16:23:59.456993115 +0200
@@ -464,7 +464,7 @@  dbxout_int (int num)
   if (num < 0)
     {
       putc ('-', asm_out_file);
-      unum = -num;
+      unum = -(unsigned int) num;
     }
   else
     unum = num;
@@ -671,7 +671,7 @@  stabstr_D (HOST_WIDE_INT num)
 {
   char buf[64];
   char *p = buf + sizeof buf;
-  unsigned int unum;
+  unsigned HOST_WIDE_INT unum;
 
   if (num == 0)
     {
@@ -681,7 +681,7 @@  stabstr_D (HOST_WIDE_INT num)
   if (num < 0)
     {
       stabstr_C ('-');
-      unum = -num;
+      unum = -(unsigned HOST_WIDE_INT) num;
     }
   else
     unum = num;