[PATCH] sched: fix infinite loop in rational approximation

Alexander.Chesnokov at kaspersky.com Alexander.Chesnokov at kaspersky.com
Thu Nov 26 06:22:07 CET 2026


From: Alexander Chesnokov <Alexander.Chesnokov at kaspersky.com>

rte_approx() scales alpha and d by 10 until d reaches 1,
multiplying denum by 10 on every step. For d below 1e-9 the loop
makes 10 or more steps, denum wraps in uint32_t and no longer
equals 10^k. The input check still passes, the search compares
against the wrapped denum, never finds a match and spins forever.
A fuzzer input with alpha = 2.65e-23 and d = 1.13e-23 (23 scaling
steps, denum = 4135583744) hangs the function.

The same wrapping breaks rte_approx_64() for d below 1e-19. Besides,
both variants hang even with an intact denum once denum squared does
not fit the type: the products in the search overflow and the
comparisons produce garbage. The 32-bit variant hangs on
alpha = 0.333333333 with d = 1.5e-9, the 64-bit variant on
alpha = 7.38e-19 with d = 6.78e-19.

Reject requests whose precision cannot be represented: stop scaling
before denum wraps and require denum * denum to fit the type, so
that every product in the search stays exact. Requests outside these
bounds could hang forever, or compute against a wrapped denominator
and return a fraction unrelated to the request. Requests within the
bounds behave exactly as before; the sched configuration calls
rte_approx_64() with the constant 1e-7, which keeps working.

Add sched_approx_autotest cases: the two hanging requests are
rejected, and coarse requests still return the minimal fraction
inside the requested interval.

Fixes: de3cfa2c9823 ("sched: initial import")
Cc: stable at dpdk.org
Cc: jasvinder.singh at intel.com

Signed-off-by: Alexander Chesnokov <Alexander.Chesnokov at kaspersky.com>
---
 app/test/test_sched.c  | 44 ++++++++++++++++++++++++++++++++++++++++++
 lib/sched/rte_approx.c | 10 ++++++++++
 2 files changed, 54 insertions(+)

diff --git a/app/test/test_sched.c b/app/test/test_sched.c
index 96b48f1897..528ee3462a 100644
--- a/app/test/test_sched.c
+++ b/app/test/test_sched.c
@@ -10,6 +10,7 @@
 
 #include "test.h"
 
+#include <rte_approx.h>
 #include <rte_cycles.h>
 #include <rte_ether.h>
 #include <rte_ip.h>
@@ -22,6 +23,13 @@ test_sched(void)
 	printf("sched not supported on Windows, skipping test\n");
 	return TEST_SKIPPED;
 }
+
+static int
+test_sched_approx(void)
+{
+	printf("sched not supported on Windows, skipping test\n");
+	return TEST_SKIPPED;
+}
 #else
 
 #include <rte_sched.h>
@@ -213,6 +221,42 @@ test_sched(void)
 	return 0;
 }
 
+static int
+test_sched_approx(void)
+{
+	uint32_t p32 = 0;
+	uint32_t q32 = 0;
+	uint64_t p64 = 0;
+	uint64_t q64 = 0;
+	int err;
+
+	/* Precision below 1e-4 cannot be represented in uint32_t */
+	err = rte_approx(2.647139521348302e-23, 1.126263171287887e-23,
+		&p32, &q32);
+	TEST_ASSERT_FAIL(err, "rte_approx accepted unrepresentable d\n");
+
+	/* Precision below 1e-9 cannot be represented in uint64_t */
+	err = rte_approx_64(7.383780580184771e-19, 6.781948062808501e-19,
+		&p64, &q64);
+	TEST_ASSERT_FAIL(err, "rte_approx_64 accepted unrepresentable d\n");
+
+	/* Coarse precision still succeeds */
+	err = rte_approx(0.125, 0.01, &p32, &q32);
+	TEST_ASSERT_SUCCESS(err, "rte_approx rejected d = 0.01\n");
+	TEST_ASSERT_EQUAL(p32, 1, "Wrong approximation p\n");
+	TEST_ASSERT_EQUAL(q32, 8, "Wrong approximation q\n");
+
+	/* rte_sched configuration calls rte_approx_64() with d = 1e-7 */
+	err = rte_approx_64(0.125, 1e-7, &p64, &q64);
+	TEST_ASSERT_SUCCESS(err, "rte_approx_64 rejected d = 1e-7\n");
+	TEST_ASSERT_EQUAL(p64, 1, "Wrong approximation p\n");
+	TEST_ASSERT_EQUAL(q64, 8, "Wrong approximation q\n");
+
+	return 0;
+}
+
 #endif /* !RTE_EXEC_ENV_WINDOWS */
 
 REGISTER_FAST_TEST(sched_autotest, NOHUGE_OK, ASAN_OK, test_sched);
+REGISTER_FAST_TEST(sched_approx_autotest, NOHUGE_OK, ASAN_OK,
+	test_sched_approx);
diff --git a/lib/sched/rte_approx.c b/lib/sched/rte_approx.c
index 86c7d1d3fb..7af4191df1 100644
--- a/lib/sched/rte_approx.c
+++ b/lib/sched/rte_approx.c
@@ -157,10 +157,15 @@ int rte_approx(double alpha, double d, uint32_t *p, uint32_t *q)
 	/* Compute alpha_num, d_num and denum */
 	denum = 1;
 	while (d < 1) {
+		if (denum > UINT32_MAX / 10)
+			return -1;
 		alpha *= 10;
 		d *= 10;
 		denum *= 10;
 	}
+	/* the search products must fit in uint32_t */
+	if (denum > UINT32_MAX / denum)
+		return -1;
 	alpha_num = (uint32_t) alpha;
 	d_num = (uint32_t) d;
 
@@ -310,10 +315,15 @@ int rte_approx_64(double alpha, double d, uint64_t *p, uint64_t *q)
 	/* Compute alpha_num, d_num and denum */
 	denum = 1;
 	while (d < 1) {
+		if (denum > UINT64_MAX / 10)
+			return -1;
 		alpha *= 10;
 		d *= 10;
 		denum *= 10;
 	}
+	/* the search products must fit in uint64_t */
+	if (denum > UINT64_MAX / denum)
+		return -1;
 	alpha_num = (uint64_t) alpha;
 	d_num = (uint64_t) d;
 
-- 
2.53.0



More information about the stable mailing list