[PATCH] bitops: fix integer overflow in align64pow2

Jack Bond-Preston jack.bond-preston at foss.arm.com
Mon Nov 16 15:09:00 CET 2026


On 13/11/2026 18:07, Morten Brørup wrote:
>> 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")
>> Cc: stable at dpdk.org
>>
>> Signed-off-by: Alexey Simakov <bigalex934 at gmail.com>
>> Signed-off-by: Mark Bulatov <ms.bulatov7 at gmail.com>
>> ---
>>   lib/eal/include/rte_bitops.h | 3 +++
>>   1 file changed, 3 insertions(+)
>>
>> diff --git a/lib/eal/include/rte_bitops.h
>> b/lib/eal/include/rte_bitops.h
>> index 45255b68aa..d2ed0c3c61 100644
>> --- a/lib/eal/include/rte_bitops.h
>> +++ b/lib/eal/include/rte_bitops.h
>> @@ -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

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(-)

diff --git a/lib/eal/include/rte_bitops.h b/lib/eal/include/rte_bitops.h
index 45255b68aa..59dcf1e028 100644
--- a/lib/eal/include/rte_bitops.h
+++ b/lib/eal/include/rte_bitops.h
@@ -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)
-- 
2.43.0


More information about the dev mailing list