[PATCH] gas: sframe: simplify get_offset_size_in_bytes

Jens Remus jremus@linux.ibm.com
Wed Jan 14 12:59:48 GMT 2026


On 1/14/2026 12:03 PM, Jan Beulich wrote:
> On 14.01.2026 11:21, Jens Remus wrote:
>> Type offsetT is either defined as signed 32-bit or 64-bit integer
>> depending on whether BFD64 is defined.  Do not test for whether an
>> offsetT value exceeds INT32_MIN..INT32_MAX for !BFD64 32-bit offsetT
>> (or INT64_MIN..INT64_MAX for BFD64 64-bit offsetT).  This is always
>> true and may result in a compile error when using compiler option
>> -Werror=type-limits, such as the one resolved with commit
>> 6b8fb74a9403 ("gas: sframe: do not test whether offsetT exceeds
>> INT64_MIN..INT64_MAX") for BFD64 64-bit offsetT:
>>
>> ../../binutils-gdb/gas/gen-sframe.c: In function ‘get_offset_size_in_bytes’:
>> ../../binutils-gdb/gas/gen-sframe.c:213:45: error: comparison is always true due to limited range of data type [-Werror=type-limits]
>>   213 |   else if ((sizeof (offsetT) > 4) && (value <= INT64_MAX && value >= INT64_MIN))
>>       |                                             ^~
>> ../../binutils-gdb/gas/gen-sframe.c:213:67: error: comparison is always true due to limited range of data type [-Werror=type-limits]
>>   213 |   else if ((sizeof (offsetT) > 4) && (value <= INT64_MAX && value >= INT64_MIN))
>>       |                                                                   ^~
>>
>> Instead of testing for whether the value is in range of
>> INT8_MIN..INT8_MAX, INT16_MIN..INT16_MAX, or INT32_MIN..INT32_MAX,
>> test whether the value is unchanged when casted to int8_t, int16_t,
>> or int32_t.  This also improves readability.
>>
>> gas/
>> 	* gen-sframe.c (get_offset_size_in_bytes): Simplify.  Do not
>> 	test whether !BFD64 32-bit offsetT exceeds INT32_MIN..INT32_MAX.
>>
>> Fixes: 58008ed4e6af ("gas: sframe: use standard min/max integer constants")
>> Suggested-by: Jan Beulich <jbeulich@suse.com>
>> Signed-off-by: Jens Remus <jremus@linux.ibm.com>
> 
> Okay, and thanks.

Thank you for the review and feedback on my previous incarnations!

Had some discussion with colleagues after lunch whether signed integer
overflow (as in cast int32_t/int64_t to int8_t/int16_t) would be
undefined behavior in C.  We came to the conclusion that it is
implementation defined behavior, which means that e.g. an BFD64 int64_t
offsetT explicitly cast to int8_t and then implicitly promoted to
int64_t (due to the comparison with int64_t offsetT) would either be:
- the same int64_t offsetT value if in range of int8_t
- not the same int64_t value (but in range int8_t) if not in range of
  int8_t

Committed to mainline.

Regards,
Jens
-- 
Jens Remus
Linux on Z Development (D3303)
jremus@de.ibm.com / jremus@linux.ibm.com

IBM Deutschland Research & Development GmbH; Vorsitzender des Aufsichtsrats: Wolfgang Wendt; Geschäftsführung: David Faller; Sitz der Gesellschaft: Ehningen; Registergericht: Amtsgericht Stuttgart, HRB 243294
IBM Data Privacy Statement: https://www.ibm.com/privacy/



More information about the Binutils mailing list