Skip to contentSkip to conversation

lib/random.c fix undefined behavior on signed integer overflow.

PATCH In review Lukas Herbolt 2 participants 2 messages Oct 16 – Nov 2 Library code

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 lib/random.c fix undefined behavior on signed integer overflow.

The random.c now expects that signed integer to overflow and triggers the ^MASK branch. But signed int overflow is undefined behavior and GCC can optimize this branch out with certain CFLAGS/LDFLAGS. And cleaning up the dead code.

---
 lib/random.c | 34 +++++++---------------------------
 1 file changed, 7 insertions(+), 27 deletions(-)
lib/random.c +7 -27
index d5c81be817c8..558da4e6888d 100644
@@ -162,38 +162,18 @@ static int32_t mt [128] =   {
           69069
 		};
 
-double 
-_random (int32_t is [2])
-{
-	int32_t it, leh, nit;
-
-	it = is [0];
-	leh = is [1];
-	if (it <= 0)	
-		it = (it + it) ^ MASK;
-	else
-		it = it + it;
-	nit = it - 1;
-/*	to ensure all-ones pattern omitted    */
-	leh = leh * mt[nit & 127] + nit;
-	is [0] = it;    is [1] = leh;
-	if (leh < 0) leh = ~leh;
-	return (SCALE * ((int32_t) (leh | 1)));
-}
-
-
-
 int32_t 
 _irandm (int32_t is [2])
 {
-	int32_t it, leh, nit;
-
+	int32_t it, leh, nit, apply_mask;
 	it = is [0];
 	leh = is [1];
-	if (it <= 0)	
-		it = (it + it) ^ MASK;
-	else
-		it = it + it;
+
+/* 	evaluate on original value - no UB here */
+	apply_mask = (it <= 0);
+/*	double via unsigned shift - well-defined */
+	it = (unsigned)it << 1;
+	if (apply_mask) it ^= MASK;
 	nit = it - 1;
 /*	to ensure all-ones pattern omitted    */
 	leh = leh * mt[nit & 127] + nit;
Signature · 1 line
2.56.0
Reviewed-by on v2 1/1
#1 lore raw
Quoted 7 lines · Lukas Herbolt

The random.c now expects that signed integer to overflow and triggers the ^MASK branch. But signed int overflow is undefined behavior and GCC can optimize this branch out with certain CFLAGS/LDFLAGS. And cleaning up the dead code.

---

V2 looks good to me,

Quoted 56 lines
 lib/random.c | 34 +++++++---------------------------
 1 file changed, 7 insertions(+), 27 deletions(-)
lib/random.c +7 -27
index d5c81be817c8..558da4e6888d 100644
@@ -162,38 +162,18 @@ static int32_t mt [128] =   {
           69069
 		};
 
-double 
-_random (int32_t is [2])
-{
-	int32_t it, leh, nit;
-
-	it = is [0];
-	leh = is [1];
-	if (it <= 0)	
-		it = (it + it) ^ MASK;
-	else
-		it = it + it;
-	nit = it - 1;
-/*	to ensure all-ones pattern omitted    */
-	leh = leh * mt[nit & 127] + nit;
-	is [0] = it;    is [1] = leh;
-	if (leh < 0) leh = ~leh;
-	return (SCALE * ((int32_t) (leh | 1)));
-}
-
-
-
 int32_t 
 _irandm (int32_t is [2])
 {
-	int32_t it, leh, nit;
-
+	int32_t it, leh, nit, apply_mask;
 	it = is [0];
 	leh = is [1];
-	if (it <= 0)	
-		it = (it + it) ^ MASK;
-	else
-		it = it + it;
+
+/* 	evaluate on original value - no UB here */
+	apply_mask = (it <= 0);
+/*	double via unsigned shift - well-defined */
+	it = (unsigned)it << 1;
+	if (apply_mask) it ^= MASK;
 	nit = it - 1;
 /*	to ensure all-ones pattern omitted    */
 	leh = leh * mt[nit & 127] + nit;
Signature · 1 line
2.56.0