PATCH COMMITTED: Avoid signed overflow in expmed.c

Ian Lance Taylor iant@google.com
Sat Jan 20 17:55:00 GMT 2007


Andrew Haley <aph@redhat.com> writes:

>  > 2007-01-19  Ian Lance Taylor  <iant@google.com>
>  > 
>  > 	* expmed.c (expand_divmod) [TRUNC_MOD_EXPR, TRUNC_DIV_EXPR]: Cast
>  > 	constant to unsigned HOST_WIDE_INT before negating.
>  > 
> 
> A little comment here would be really nice, even if it's only something like
> "make sure that -MIN_INT doesn't cause integer overflow".

I dunno, maybe I'm too invested in this, but the explicit cast seems
sufficiently obvious to me to not require a comment.

Any other opinions?

Ian

>  > Index: expmed.c
>  > ===================================================================
>  > --- expmed.c	(revision 120860)
>  > +++ expmed.c	(working copy)
>  > @@ -4174,7 +4174,9 @@ expand_divmod (int rem_flag, enum tree_c
>  >  		int lgup, post_shift;
>  >  		rtx mlr;
>  >  		HOST_WIDE_INT d = INTVAL (op1);
>  > -		unsigned HOST_WIDE_INT abs_d = d >= 0 ? d : -d;
>  > +		unsigned HOST_WIDE_INT abs_d;
>  > +
>  > +		abs_d = d >= 0 ? d : - (unsigned HOST_WIDE_INT) d;
>  >  
>  >  		/* n rem d = n rem -d */
>  >  		if (rem_flag && d < 0)



More information about the Gcc-patches mailing list