Skip to contentSkip to conversation

bitops: fix integer overflow in align64pow2

PATCH NAK Bulatov Mark 4 participants 8 messages Nov 13 – Nov 19

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 bitops: fix integer overflow in align64pow2

If the function rte_combine64ms1b() returns the maximum possible uint64_t value, it will result in an overflow.

To avoid this, add a check.

Found by Linux Verification Center (linuxtesting.org) with SVACE.

Fixes 7253e3d2a5 ("bitops: move bit count functions")
---
 lib/eal/include/rte_bitops.h | 3 +++
 1 file changed, 3 insertions(+)
lib/eal/include/rte_bitops.h +3 -0
index 45255b68aa..d2ed0c3c61 100644
@@ -1307,6 +1307,9 @@ rte_align64pow2(uint64_t v)
 	v--;
 	v = rte_combine64ms1b(v);
 
+	if (v == UINT64_MAX)
+		v--;
+
 	return v + 1;
 }
 
Signature · 1 line
2.39.5
Reviewed-by on v1 1/1
#1 lore raw
Quoted 21 lines · Bulatov Mark

If the function rte_combine64ms1b() returns the maximum possible uint64_t value, it will result in an overflow.

To avoid this, add a check.

Found by Linux Verification Center (linuxtesting.org) with SVACE.

Fixes 7253e3d2a5 ("bitops: move bit count functions")
---
  lib/eal/include/rte_bitops.h | 3 +++
  1 file changed, 3 insertions(+)
lib/eal/include/rte_bitops.h +0 -0
index 45255b68aa..d2ed0c3c61 100644

lib/eal/include/rte_bitops.h rte_align64pow2(uint64_t v) · line 1315

Quoted 9 lines
lib/eal/include/rte_bitops.h +3 -0
@@ -1307,6 +1307,9 @@ rte_align64pow2(uint64_t v)
  	v--;
  	v = rte_combine64ms1b(v);
 
+	if (v == UINT64_MAX)
+		v--;
+
  	return v + 1;
  }
 
#2 lore raw
In reply to Bulatov Mark
Quoted 23 lines

If the function rte_combine64ms1b() returns the maximum possible uint64_t
value, it will result in an overflow.

To avoid this, add a check.

Found by Linux Verification Center (linuxtesting.org) with SVACE.

Fixes 7253e3d2a5 ("bitops: move bit count functions")
---
 lib/eal/include/rte_bitops.h | 3 +++
 1 file changed, 3 insertions(+)
lib/eal/include/rte_bitops.h +0 -0
index 45255b68aa..d2ed0c3c61 100644

lib/eal/include/rte_bitops.h rte_align64pow2(uint64_t v) · line 1314

Quoted 8 lines
lib/eal/include/rte_bitops.h +3 -0
@@ -1307,6 +1307,9 @@ rte_align64pow2(uint64_t v)
 	v--;
 	v = rte_combine64ms1b(v);
 
+	if (v == UINT64_MAX)
+		v--;
+
 	return v + 1;
 }

NAK.

The overflow bug is present, also in rte_align32pow2().

But the fix is wrong; it does not return a power-of-two value.

There is no solution to fix this overflow.
The only possible fix is to specify in the function documentation that the parameter must not exceed 1 << 63 (0x8000000000000000).

-Morten

#3 lore raw
Quoted 34 lines · Morten Brørup
Quoted 23 lines

If the function rte_combine64ms1b() returns the maximum possible uint64_t
value, it will result in an overflow.

To avoid this, add a check.

Found by Linux Verification Center (linuxtesting.org) with SVACE.

Fixes 7253e3d2a5 ("bitops: move bit count functions")
---
  lib/eal/include/rte_bitops.h | 3 +++
  1 file changed, 3 insertions(+)
lib/eal/include/rte_bitops.h +0 -0
index 45255b68aa..d2ed0c3c61 100644

lib/eal/include/rte_bitops.h rte_align64pow2(uint64_t v) · line 1314

Quoted 8 lines
lib/eal/include/rte_bitops.h +3 -0
@@ -1307,6 +1307,9 @@ rte_align64pow2(uint64_t v)
  	v--;
  	v = rte_combine64ms1b(v);
 
+	if (v == UINT64_MAX)
+		v--;
+
  	return v + 1;
  }

NAK.

Quoted 8 lines

The overflow bug is present, also in rte_align32pow2().

But the fix is wrong; it does not return a power-of-two value.

There is no solution to fix this overflow.
The only possible fix is to specify in the function documentation that the parameter must not exceed 1 << 63 (0x8000000000000000).

-Morten

You are right Morten, sorry about that. No point avoiding the overflow if the return value doesn't even fulfil the purpose of the function itself. Also, I realised the provided fix would change the behaviour for the input value of 0 - currently this returns 0, but with this patch it would return UINT64_MAX.

I guess the change should look something like this:
-----

[PATCH] bitops: document input value range for rte_align[32,64]pow2

rte_align32pow2 and rte_align62pow2 will overflow and return incorrect values for input values exceeding 1<<31 and 1<<63, respectively. There is no way to return a valid result here: the next power of two above these values is unrepresentable with 32/64 bit unsigned integers.

Adjust the documentation for these functions to clarify that input values above these limits will return invalid results, and thus should not be provided to these functions. Additionally, document an existing edge case of the implementation: when the input is 0, it returns 0 (not a power of 2).

---
 lib/eal/include/rte_bitops.h | 22 ++++++++++++++++++----
 1 file changed, 18 insertions(+), 4 deletions(-)
lib/eal/include/rte_bitops.h +18 -4
index 45255b68aa..59dcf1e028 100644
@@ -1261,10 +1261,17 @@ rte_is_power_of_2(uint32_t n)
  * Aligns input parameter to the next power of 2
  *
  * @param x
- *   The integer value to align
+ *   The integer value to align. Must be less than or equal to 1 << 31
+ *   (0x80000000).
  *
  * @return
- *   The smallest power of 2 which is greater than or equal to @c x.
+ *   The smallest power of 2 which is greater than or equal to @c x, or 0 if
+ *   @c v is 0.
+ *
+ * @note If the input parameter exceeds 1 << 31 (0x80000000), then the expected
+ *       result is unrepresentable and the return value is invalid.
+ * @note For the input value 0, this function returns 0, which is not a power of
+ *       2.
  */
 static inline uint32_t
 rte_align32pow2(uint32_t x)
@@ -1296,10 +1303,17 @@ rte_align32prevpow2(uint32_t x)
  * Aligns 64b input parameter to the next power of 2
  *
  * @param v
- *   The 64b value to align
+ *   The 64b value to align. Must be less than or equal to 1 << 63
+ *   (0x8000000000000000).
  *
  * @return
- *   The smallest power of 2 which is greater than or equal to @c v.
+ *   The smallest power of 2 which is greater than or equal to @c v, or 0 if
+ *   @c v is 0.
+ *
+ * @note If the input parameter exceeds 1 << 63 (0x8000000000000000), then the
+ *       expected result is unrepresentable and the return value is invalid.
+ * @note For the input value 0, this function returns 0, which is not a power of
+ *       2.
  */
 static inline uint64_t
 rte_align64pow2(uint64_t v)
Signature · 1 line
2.43.0
#4 lore raw
Quoted 124 lines

From: Jack Bond-Preston [mailto:[email protected]]
Sent: Monday, 16 November 2026 06.09

Quoted 34 lines · Morten Brørup
Quoted 23 lines

If the function rte_combine64ms1b() returns the maximum possible uint64_t
value, it will result in an overflow.

To avoid this, add a check.

Found by Linux Verification Center (linuxtesting.org) with SVACE.

Fixes 7253e3d2a5 ("bitops: move bit count functions")
---
  lib/eal/include/rte_bitops.h | 3 +++
  1 file changed, 3 insertions(+)
lib/eal/include/rte_bitops.h +0 -0
index 45255b68aa..d2ed0c3c61 100644

lib/eal/include/rte_bitops.h rte_align64pow2(uint64_t v) · line 1314

Quoted 8 lines
lib/eal/include/rte_bitops.h +3 -0
@@ -1307,6 +1307,9 @@ rte_align64pow2(uint64_t v)
  	v--;
  	v = rte_combine64ms1b(v);
 
+	if (v == UINT64_MAX)
+		v--;
+
  	return v + 1;
  }

NAK.

Quoted 6 lines

The overflow bug is present, also in rte_align32pow2().

But the fix is wrong; it does not return a power-of-two value.

There is no solution to fix this overflow.
The only possible fix is to specify in the function documentation

that the parameter must not exceed 1 << 63 (0x8000000000000000).

Quoted 2 lines

-Morten

You are right Morten, sorry about that. No point avoiding the overflow if the return value doesn't even fulfil the purpose of the function itself. Also, I realised the provided fix would change the behaviour for
the input value of 0 - currently this returns 0, but with this patch it would return UINT64_MAX.

I guess the change should look something like this:
-----

[PATCH] bitops: document input value range for rte_align[32,64]pow2

rte_align32pow2 and rte_align62pow2 will overflow and return incorrect values for input values exceeding 1<<31 and 1<<63, respectively. There is no way to return a valid result here: the next power of two above these values is unrepresentable with 32/64 bit unsigned integers.

Adjust the documentation for these functions to clarify that input values above these limits will return invalid results, and thus should not be provided to these functions. Additionally, document an existing edge case of the implementation: when the input is 0, it returns 0 (not a power of 2).

---
  lib/eal/include/rte_bitops.h | 22 ++++++++++++++++++----
  1 file changed, 18 insertions(+), 4 deletions(-)
lib/eal/include/rte_bitops.h +3 -2
index 45255b68aa..59dcf1e028 100644
@@ -1261,10 +1261,17 @@ rte_is_power_of_2(uint32_t n)
   * Aligns input parameter to the next power of 2
   *
   * @param x
- *   The integer value to align
+ *   The integer value to align. Must be less than or equal to 1 << 31
+ *   (0x80000000).
   *
   * @return
- *   The smallest power of 2 which is greater than or equal to @c x.
+ *   The smallest power of 2 which is greater than or equal to @c x,

or
0 if
+ * @c v is 0.
+ *
+ * @note If the input parameter exceeds 1 << 31 (0x80000000), then the expected
+ * result is unrepresentable and the return value is invalid.
+ * @note For the input value 0, this function returns 0, which is not a
power of
+ * 2.
*/
static inline uint32_t
rte_align32pow2(uint32_t x)

lib/eal/include/rte_bitops.h +3 -2
@@ -1296,10 +1303,17 @@ rte_align32prevpow2(uint32_t x)
   * Aligns 64b input parameter to the next power of 2
   *
   * @param v
- *   The 64b value to align
+ *   The 64b value to align. Must be less than or equal to 1 << 63
+ *   (0x8000000000000000).
   *
   * @return
- *   The smallest power of 2 which is greater than or equal to @c v.
+ *   The smallest power of 2 which is greater than or equal to @c v,

or
0 if
+ * @c v is 0.
+ *
+ * @note If the input parameter exceeds 1 << 63 (0x8000000000000000), then the
+ * expected result is unrepresentable and the return value is
invalid.
+ * @note For the input value 0, this function returns 0, which is not

Quoted 8 lines

a
power of
+ * 2.
*/
static inline uint64_t
rte_align64pow2(uint64_t v)
--
2.43.0

Yes, please submit a v2; you may add:

#5 lore raw

PATCH v2 1/1 bitops: document max input value for rte_align[32,64]pow2

In reply to Bulatov Mark

From: Jack Bond-Preston <[email protected]>

rte_align32pow2 and rte_align62pow2 will overflow and return incorrect values for input values exceeding 1<<31 and 1<<63, respectively. There is no way to return a valid result here: the next power of two above these values is unrepresentable with 32/64 bit unsigned integers.

Adjust the documentation for these functions to clarify that input values above these limits will return invalid results, and thus should not be provided to these functions. Additionally, document an existing edge case of the implementation: when the input is 0, it returns 0 (not a power of 2).

Fixes 7253e3d2a5 ("bitops: move bit count functions")
---
 lib/eal/include/rte_bitops.h | 22 ++++++++++++++++++----
 1 file changed, 18 insertions(+), 4 deletions(-)
lib/eal/include/rte_bitops.h +18 -4
index 45255b68aa..eb404a3dd4 100644
@@ -1261,10 +1261,17 @@ rte_is_power_of_2(uint32_t n)
  * Aligns input parameter to the next power of 2
  *
  * @param x
- *   The integer value to align
+ *   The integer value to align. Must be less than or equal to 1 << 31
+ *   (0x80000000).
  *
  * @return
- *   The smallest power of 2 which is greater than or equal to @c x.
+ *   The smallest power of 2 which is greater than or equal to @c x, or 0 if
+ *   @c x is 0.
+ *
+ * @note If the input parameter exceeds 1 << 31 (0x80000000), then the expected
+ *       result is unrepresentable and the return value is invalid.
+ * @note For the input value 0, this function returns 0, which is not a power of
+ *       2.
  */
 static inline uint32_t
 rte_align32pow2(uint32_t x)
@@ -1296,10 +1303,17 @@ rte_align32prevpow2(uint32_t x)
  * Aligns 64b input parameter to the next power of 2
  *
  * @param v
- *   The 64b value to align
+ *   The 64b value to align. Must be less than or equal to 1 << 63
+ *   (0x8000000000000000).
  *
  * @return
- *   The smallest power of 2 which is greater than or equal to @c v.
+ *   The smallest power of 2 which is greater than or equal to @c v, or 0 if
+ *   @c v is 0.
+ *
+ * @note If the input parameter exceeds 1 << 63 (0x8000000000000000), then the
+ *       expected result is unrepresentable and the return value is invalid.
+ * @note For the input value 0, this function returns 0, which is not a power of
+ *       2.
  */
 static inline uint64_t
 rte_align64pow2(uint64_t v)
Signature · 1 line
2.54.0 (Apple Git-157)
#6 lore raw

PATCH v3 1/1 bitops: document max input value for rte_align[32,64]pow2

In reply to Bulatov Mark

rte_align32pow2 and rte_align62pow2 will overflow and return incorrect values for input values exceeding 1<<31 and 1<<63, respectively. There is no way to return a valid result here: the next power of two above these values is unrepresentable with 32/64 bit unsigned integers.

Adjust the documentation for these functions to clarify that input values above these limits will return invalid results, and thus should not be provided to these functions. Additionally, document an existing edge case of the implementation: when the input is 0, it returns 0 (not a power of 2).

Fixes 7253e3d2a5b6 ("bitops: move bit count functions")

---
v3:
* fixed wrong email and too short Fixes: commit hash

v2:
* document the limitation instead of avoiding the overflow

 lib/eal/include/rte_bitops.h | 22 ++++++++++++++++++----
 1 file changed, 18 insertions(+), 4 deletions(-)
lib/eal/include/rte_bitops.h +18 -4
index 45255b68aa..eb404a3dd4 100644
@@ -1261,10 +1261,17 @@ rte_is_power_of_2(uint32_t n)
  * Aligns input parameter to the next power of 2
  *
  * @param x
- *   The integer value to align
+ *   The integer value to align. Must be less than or equal to 1 << 31
+ *   (0x80000000).
  *
  * @return
- *   The smallest power of 2 which is greater than or equal to @c x.
+ *   The smallest power of 2 which is greater than or equal to @c x, or 0 if
+ *   @c x is 0.
+ *
+ * @note If the input parameter exceeds 1 << 31 (0x80000000), then the expected
+ *       result is unrepresentable and the return value is invalid.
+ * @note For the input value 0, this function returns 0, which is not a power of
+ *       2.
  */
 static inline uint32_t
 rte_align32pow2(uint32_t x)
@@ -1296,10 +1303,17 @@ rte_align32prevpow2(uint32_t x)
  * Aligns 64b input parameter to the next power of 2
  *
  * @param v
- *   The 64b value to align
+ *   The 64b value to align. Must be less than or equal to 1 << 63
+ *   (0x8000000000000000).
  *
  * @return
- *   The smallest power of 2 which is greater than or equal to @c v.
+ *   The smallest power of 2 which is greater than or equal to @c v, or 0 if
+ *   @c v is 0.
+ *
+ * @note If the input parameter exceeds 1 << 63 (0x8000000000000000), then the
+ *       expected result is unrepresentable and the return value is invalid.
+ * @note For the input value 0, this function returns 0, which is not a power of
+ *       2.
  */
 static inline uint64_t
 rte_align64pow2(uint64_t v)

--
2.54.0 (Apple Git-157)

#7 lore raw

Re: [PATCH v3] bitops: document max input value for rte_align[32, 64]pow2

16/11/2026 15:37, Jack Bond-Preston:

Quoted 11 lines

rte_align32pow2 and rte_align62pow2 will overflow and return incorrect values for input values exceeding 1<<31 and 1<<63, respectively. There is no way to return a valid result here: the next power of two above these values is unrepresentable with 32/64 bit unsigned integers.

Adjust the documentation for these functions to clarify that input values above these limits will return invalid results, and thus should not be provided to these functions. Additionally, document an existing edge case of the implementation: when the input is 0, it returns 0 (not a power of 2).

Quoted 6 lines
Fixes 7253e3d2a5b6 ("bitops: move bit count functions")

Applied, thanks.