Skip to contentSkip to conversation

lib/fault-inject.c: Fix off-by-one error in probability

RFC PATCH In review George Spelvin 1 participant 1 message Mar 28, 2020

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

RFC PATCH 2/50 lib/fault-inject.c: Fix off-by-one error in probability

This is the combination of two fixes. First, use prandom_u32_max() for efficiency:
- if (attr->probability <= prandom_u32() % 100)
+ if (attr->probability <= prandom_u32_max(100))
return false

And then a bug-fix:
- if (attr->probability <= prandom_u32_max(100))
+ if (attr->probability < prandom_u32_max(100))
return false

Before, probability = 1 would succeed 2% of the time and 99 would succeed 100% of the time. (0% was caught by an earlier test.)

Cc Akinobu Mita <[email protected]>
Cc Anton Blanchard <[email protected]>
---
 lib/fault-inject.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
lib/fault-inject.c +1 -1
index 8186ca84910bc..e20151fa5515e 100644
@@ -134,7 +134,7 @@ bool should_fail(struct fault_attr *attr, ssize_t size)
 			return false;
 	}
 
-	if (attr->probability <= prandom_u32() % 100)
+	if (attr->probability < prandom_u32_max(100))
 		return false;
 
 	if (!fail_stacktrace(attr))
Signature · 1 line
2.26.0