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")
Cc: [email protected]
Reported-by: Mark Bulatov <[email protected]>
Signed-off-by: Jack Bond-Preston <[email protected]>
Acked-by: Morten Brørup <[email protected]>
---
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..eb404a3dd4 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 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)