Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 6e27a0d

Browse files
authored
bpo-37798: Prevent undefined behavior in direct calls to the C helper function. (#16149)
1 parent 09dc2c6 commit 6e27a0d

1 file changed

Lines changed: 17 additions & 1 deletion

File tree

Modules/_statisticsmodule.c

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,11 @@ _statistics__normal_dist_inv_cdf_impl(PyObject *module, double p, double mu,
3232
/*[clinic end generated code: output=02fd19ddaab36602 input=24715a74be15296a]*/
3333
{
3434
double q, num, den, r, x;
35+
if (p <= 0.0 || p >= 1.0 || sigma <= 0.0) {
36+
goto error;
37+
}
38+
3539
q = p - 0.5;
36-
// Algorithm AS 241: The Percentage Points of the Normal Distribution
3740
if(fabs(q) <= 0.425) {
3841
r = 0.180625 - q * q;
3942
// Hash sum-55.8831928806149014439
@@ -53,10 +56,16 @@ _statistics__normal_dist_inv_cdf_impl(PyObject *module, double p, double mu,
5356
6.8718700749205790830e+2) * r +
5457
4.2313330701600911252e+1) * r +
5558
1.0);
59+
if (den == 0.0) {
60+
goto error;
61+
}
5662
x = num / den;
5763
return mu + (x * sigma);
5864
}
5965
r = (q <= 0.0) ? p : (1.0 - p);
66+
if (r <= 0.0 || r >= 1.0) {
67+
goto error;
68+
}
6069
r = sqrt(-log(r));
6170
if (r <= 5.0) {
6271
r = r - 1.6;
@@ -97,11 +106,18 @@ _statistics__normal_dist_inv_cdf_impl(PyObject *module, double p, double mu,
97106
5.99832206555887937690e-1) * r +
98107
1.0);
99108
}
109+
if (den == 0.0) {
110+
goto error;
111+
}
100112
x = num / den;
101113
if (q < 0.0) {
102114
x = -x;
103115
}
104116
return mu + (x * sigma);
117+
118+
error:
119+
PyErr_SetString(PyExc_ValueError, "inv_cdf undefined for these parameters");
120+
return -1.0;
105121
}
106122

107123

0 commit comments

Comments
 (0)