diff mbox

[wide-int] Add more assertions

Message ID 87zjj0ng27.fsf@sandifor-thinkpad.stglab.manchester.uk.ibm.com
State New
Headers show

Commit Message

Richard Sandiford May 2, 2014, 7:20 p.m. UTC
This patch adds some assertions against sext (.., 0) and zext (..., 0).
The former is undefined at the sext_hwi level and the latter is disallowed
for consistency with the former.

Also, set_bit (rightly IMO) can't handle bit >= precision.  For
precision <= HOST_BITS_PER_WIDE_INT it would invoke undefined
behaviour while for other precisions I think it would crash.
A case with precision <= HOST_BITS_PER_WIDE_INT showed up in java
(fix posted separately).

Tested on x86_64-linux-gnu and powerpc64-linux-gnu.  OK to install?

Thanks,
Richard

Comments

Kenneth Zadeck May 3, 2014, 1:40 a.m. UTC | #1
These are fine.

On 05/02/2014 03:20 PM, Richard Sandiford wrote:
> This patch adds some assertions against sext (.., 0) and zext (..., 0).
> The former is undefined at the sext_hwi level and the latter is disallowed
> for consistency with the former.
>
> Also, set_bit (rightly IMO) can't handle bit >= precision.  For
> precision <= HOST_BITS_PER_WIDE_INT it would invoke undefined
> behaviour while for other precisions I think it would crash.
> A case with precision <= HOST_BITS_PER_WIDE_INT showed up in java
> (fix posted separately).
>
> Tested on x86_64-linux-gnu and powerpc64-linux-gnu.  OK to install?
>
> Thanks,
> Richard
>
>
> Index: gcc/wide-int.h
> ===================================================================
> --- gcc/wide-int.h	2014-05-02 16:28:09.561842842 +0100
> +++ gcc/wide-int.h	2014-05-02 16:44:04.015463718 +0100
> @@ -2046,6 +2046,8 @@ wi::sext (const T &x, unsigned int offse
>     unsigned int precision = get_precision (result);
>     WIDE_INT_REF_FOR (T) xi (x, precision);
>   
> +  gcc_checking_assert (offset != 0);
> +
>     if (offset <= HOST_BITS_PER_WIDE_INT)
>       {
>         val[0] = sext_hwi (xi.ulow (), offset);
> @@ -2065,6 +2067,8 @@ wi::zext (const T &x, unsigned int offse
>     unsigned int precision = get_precision (result);
>     WIDE_INT_REF_FOR (T) xi (x, precision);
>   
> +  gcc_checking_assert (offset != 0);
> +
>     /* This is not just an optimization, it is actually required to
>        maintain canonization.  */
>     if (offset >= precision)
> @@ -2102,6 +2106,9 @@ wi::set_bit (const T &x, unsigned int bi
>     WI_UNARY_RESULT_VAR (result, val, T, x);
>     unsigned int precision = get_precision (result);
>     WIDE_INT_REF_FOR (T) xi (x, precision);
> +
> +  gcc_checking_assert (bit < precision);
> +
>     if (precision <= HOST_BITS_PER_WIDE_INT)
>       {
>         val[0] = xi.ulow () | ((unsigned HOST_WIDE_INT) 1 << bit);
Richard Sandiford May 3, 2014, 7 a.m. UTC | #2
Kenneth Zadeck <zadeck@naturalbridge.com> writes:
> These are fine.

Thanks.  I'll hold off applying it until the java fix has been reviewed
(which could be after the merge -- no need to hold it up for this IMO).

Richard

>
> On 05/02/2014 03:20 PM, Richard Sandiford wrote:
>> This patch adds some assertions against sext (.., 0) and zext (..., 0).
>> The former is undefined at the sext_hwi level and the latter is disallowed
>> for consistency with the former.
>>
>> Also, set_bit (rightly IMO) can't handle bit >= precision.  For
>> precision <= HOST_BITS_PER_WIDE_INT it would invoke undefined
>> behaviour while for other precisions I think it would crash.
>> A case with precision <= HOST_BITS_PER_WIDE_INT showed up in java
>> (fix posted separately).
>>
>> Tested on x86_64-linux-gnu and powerpc64-linux-gnu.  OK to install?
>>
>> Thanks,
>> Richard
>>
>>
>> Index: gcc/wide-int.h
>> ===================================================================
>> --- gcc/wide-int.h	2014-05-02 16:28:09.561842842 +0100
>> +++ gcc/wide-int.h	2014-05-02 16:44:04.015463718 +0100
>> @@ -2046,6 +2046,8 @@ wi::sext (const T &x, unsigned int offse
>>     unsigned int precision = get_precision (result);
>>     WIDE_INT_REF_FOR (T) xi (x, precision);
>>   
>> +  gcc_checking_assert (offset != 0);
>> +
>>     if (offset <= HOST_BITS_PER_WIDE_INT)
>>       {
>>         val[0] = sext_hwi (xi.ulow (), offset);
>> @@ -2065,6 +2067,8 @@ wi::zext (const T &x, unsigned int offse
>>     unsigned int precision = get_precision (result);
>>     WIDE_INT_REF_FOR (T) xi (x, precision);
>>   
>> +  gcc_checking_assert (offset != 0);
>> +
>>     /* This is not just an optimization, it is actually required to
>>        maintain canonization.  */
>>     if (offset >= precision)
>> @@ -2102,6 +2106,9 @@ wi::set_bit (const T &x, unsigned int bi
>>     WI_UNARY_RESULT_VAR (result, val, T, x);
>>     unsigned int precision = get_precision (result);
>>     WIDE_INT_REF_FOR (T) xi (x, precision);
>> +
>> +  gcc_checking_assert (bit < precision);
>> +
>>     if (precision <= HOST_BITS_PER_WIDE_INT)
>>       {
>>         val[0] = xi.ulow () | ((unsigned HOST_WIDE_INT) 1 << bit);
diff mbox

Patch

Index: gcc/wide-int.h
===================================================================
--- gcc/wide-int.h	2014-05-02 16:28:09.561842842 +0100
+++ gcc/wide-int.h	2014-05-02 16:44:04.015463718 +0100
@@ -2046,6 +2046,8 @@  wi::sext (const T &x, unsigned int offse
   unsigned int precision = get_precision (result);
   WIDE_INT_REF_FOR (T) xi (x, precision);
 
+  gcc_checking_assert (offset != 0);
+
   if (offset <= HOST_BITS_PER_WIDE_INT)
     {
       val[0] = sext_hwi (xi.ulow (), offset);
@@ -2065,6 +2067,8 @@  wi::zext (const T &x, unsigned int offse
   unsigned int precision = get_precision (result);
   WIDE_INT_REF_FOR (T) xi (x, precision);
 
+  gcc_checking_assert (offset != 0);
+
   /* This is not just an optimization, it is actually required to
      maintain canonization.  */
   if (offset >= precision)
@@ -2102,6 +2106,9 @@  wi::set_bit (const T &x, unsigned int bi
   WI_UNARY_RESULT_VAR (result, val, T, x);
   unsigned int precision = get_precision (result);
   WIDE_INT_REF_FOR (T) xi (x, precision);
+
+  gcc_checking_assert (bit < precision);
+
   if (precision <= HOST_BITS_PER_WIDE_INT)
     {
       val[0] = xi.ulow () | ((unsigned HOST_WIDE_INT) 1 << bit);