Skip to contentSkip to conversation

maketable: Add check for ZERO for variable sigma2

PATCH In review Maks Mishin 2 participants 2 messages Jan 6, 2024 – Jan 8, 2024

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

PATCH 1/1 maketable: Add check for ZERO for variable sigma2

If variable limit == 1, then n == 1 and then second for-loop will not do because of variable sigma2 maybe ZERO.
Added check for ZERO for sigma2 before it is used as denominator.

---
 netem/maketable.c | 4 ++++
 1 file changed, 4 insertions(+)
netem/maketable.c +4 -0
index ad8620a4..56b1d0bb 100644
@@ -68,6 +68,10 @@ arraystats(double *x, int limit, double *mu, double *sigma, double *rho)
 		sigma2 += ((double)x[i-1] - *mu)*((double)x[i-1] - *mu);
 
 	}
+	if (sigma2 == 0) {
+		perror("Division by zero in top/sigma2");
+		exit(3);
+	}
 	*rho = top/sigma2;
 }
 
Signature · 1 line
2.34.1
#1 lore raw
Quoted 14 lines · On Sun, 7 Jan 2024 00:14:22 +0300 Maks Mishin <[email protected]> wrote:

If variable limit == 1, then n == 1 and then second for-loop will not do because of variable sigma2 maybe ZERO.
Added check for ZERO for sigma2 before it is used as denominator.

---
 netem/maketable.c | 4 ++++
 1 file changed, 4 insertions(+)
netem/maketable.c +0 -0
index ad8620a4..56b1d0bb 100644

netem/maketable.c arraystats(double *x, int limit, double *mu, double *sigma, double *rho) · line 77

Quoted 10 lines
netem/maketable.c +4 -0
@@ -68,6 +68,10 @@ arraystats(double *x, int limit, double *mu, double *sigma, double *rho)
 		sigma2 += ((double)x[i-1] - *mu)*((double)x[i-1] - *mu);
 
 	}
+	if (sigma2 == 0) {
+		perror("Division by zero in top/sigma2");
+		exit(3);
+	}
 	*rho = top/sigma2;
 }
 

This looks like a purely theoretical not practical problem.
Just tried the tool with input files likely to create the problem (no data, one sample, etc) and could not reproduce any problem.

What input could make this happen?