bpo-40780: Fix failure of _Py_dg_dtoa to remove trailing zeros (GH-20435)

* Fix failure of _Py_dg_dtoa to remove trailing zeros

* Add regression test and news entry

* Add explanation about why it's safe to strip trailing zeros

* Make code safer, clean up comments, add change note at top of file

* Nitpick: avoid implicit int-to-float conversion in tests
This commit is contained in:
Mark Dickinson 2020-05-29 14:23:57 +01:00 committed by GitHub
parent 2831642212
commit 895c9c1d43
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 24 additions and 0 deletions

View file

@ -64,6 +64,9 @@
* 7. _Py_dg_strtod has been modified so that it doesn't accept strings with
* leading whitespace.
*
* 8. A corner case where _Py_dg_dtoa didn't strip trailing zeros has been
* fixed. (bugs.python.org/issue40780)
*
***************************************************************/
/* Please send bug reports for the original dtoa.c code to David M. Gay (dmg
@ -2563,6 +2566,14 @@ _Py_dg_dtoa(double dd, int mode, int ndigits,
}
++*s++;
}
else {
/* Strip trailing zeros. This branch was missing from the
original dtoa.c, leading to surplus trailing zeros in
some cases. See bugs.python.org/issue40780. */
while (s > s0 && s[-1] == '0') {
--s;
}
}
break;
}
}