Skip to content

Commit f86460b

Browse files
committed
gh-158446: Address review nits
Note the dtoa.c deviations in its header list, cross-reference the shared bound between dtoa.c and pystrtod.c, PEP 7 brace placement, and wrap a long test comment. No functional change.
1 parent c6a8db9 commit f86460b

3 files changed

Lines changed: 14 additions & 6 deletions

File tree

‎Lib/test/test_format.py‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -641,8 +641,8 @@ def test_precision_c_limits(self):
641641

642642
@support.cpython_only
643643
def test_precision_near_int_max(self):
644-
# gh-158446: Precisions just below INT_MAX are rejected before any output
645-
# buffer size is computed from them.
644+
# gh-158446: Precisions just below INT_MAX are rejected before any
645+
# output buffer size is computed from them.
646646
_testcapi = import_module("_testcapi")
647647
INT_MAX = _testcapi.INT_MAX
648648

‎Python/dtoa.c‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,10 @@
6767
* 8. A corner case where _Py_dg_dtoa didn't strip trailing zeros has been
6868
* fixed. (bugs.python.org/issue40780)
6969
*
70+
* 9. _Py_dg_dtoa clamps ndigits in modes 3 and 5 so that its buffer size
71+
* arithmetic cannot exceed the int range, and rv_alloc's size doubling
72+
* uses size_t. (gh-158446)
73+
*
7074
***************************************************************/
7175

7276
/* Please send bug reports for the original dtoa.c code to David M. Gay (dmg
@@ -2376,7 +2380,8 @@ _Py_dg_dtoa(double dd, int mode, int ndigits,
23762380
/* -330 < k < 330 for any finite nonzero double. Clamp ndigits so
23772381
that ndigits + k + 1 stays within int range; no double has
23782382
anywhere near this many decimal digits so the digits returned
2379-
are unaffected. */
2383+
are unaffected (*decpt saturates in the no_digits case). Same
2384+
bound as DOUBLE_TO_STRING_PRECISION_MAX in pystrtod.c. */
23802385
if (ndigits > INT_MAX - 1024)
23812386
ndigits = INT_MAX - 1024;
23822387
else if (ndigits < -(INT_MAX - 1024))

‎Python/pystrtod.c‎

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -406,7 +406,8 @@ _Py_string_to_number_with_underscores(
406406
Py_ssize_t arithmetic on roughly precision + (digits before the point, at
407407
most DBL_MAX_10_EXP + 1 == 309) + a few bytes of sign, point and exponent.
408408
Staying this far inside the int range keeps all of those sums in range.
409-
(Only C callers can pass a negative precision.) */
409+
(Only C callers can pass a negative precision.) _Py_dg_dtoa() applies
410+
the same bound to its ndigits argument. */
410411
#define DOUBLE_TO_STRING_PRECISION_MAX (INT_MAX - 1024)
411412

412413
#if _PY_SHORT_FLOAT_REPR == 0
@@ -775,7 +776,8 @@ char * PyOS_double_to_string(double val,
775776
int upper = 0;
776777

777778
if (precision > DOUBLE_TO_STRING_PRECISION_MAX
778-
|| precision < -DOUBLE_TO_STRING_PRECISION_MAX) {
779+
|| precision < -DOUBLE_TO_STRING_PRECISION_MAX)
780+
{
779781
PyErr_SetString(PyExc_ValueError, "precision too big");
780782
return NULL;
781783
}
@@ -1242,7 +1244,8 @@ char * PyOS_double_to_string(double val,
12421244
int mode;
12431245

12441246
if (precision > DOUBLE_TO_STRING_PRECISION_MAX
1245-
|| precision < -DOUBLE_TO_STRING_PRECISION_MAX) {
1247+
|| precision < -DOUBLE_TO_STRING_PRECISION_MAX)
1248+
{
12461249
PyErr_SetString(PyExc_ValueError, "precision too big");
12471250
return NULL;
12481251
}

0 commit comments

Comments
 (0)