[PATCH] gas: sframe: simplify get_offset_size_in_bytes

Jens Remus jremus@linux.ibm.com
Wed Jan 14 10:21:19 GMT 2026


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>
---
 gas/gen-sframe.c | 10 +++++-----
 1 file changed, 5 insertions(+), 5 deletions(-)

diff --git a/gas/gen-sframe.c b/gas/gen-sframe.c
index bf5cd62f6fcb..f5e0f3792cca 100644
--- a/gas/gen-sframe.c
+++ b/gas/gen-sframe.c
@@ -204,14 +204,14 @@ get_offset_size_in_bytes (offsetT value)
 {
   unsigned int size = 0;
 
-  if (value <= INT8_MAX && value >= INT8_MIN)
+  if (value == (int8_t)value)
     size = 1;
-  else if (value <= INT16_MAX && value >= INT16_MIN)
+  else if (value == (int16_t)value)
     size = 2;
-  else if (value <= INT32_MAX && value >= INT32_MIN)
+  else if (value == (int32_t)value)
     size = 4;
-  else if (sizeof (offsetT) > 4)
-    size = 8;
+  else
+    return 8;
 
   return size;
 }
-- 
2.51.0



More information about the Binutils mailing list