[3.10] gh-95778: Correctly pre-check for int-to-str conversion (GH-96537) #96563
+107
−7
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Converting a large enough
int
to a decimal string raisesValueError
as expected. However, the raise comes after the quadratic-time base-conversion algorithm has run to completion. For effective DOS prevention, we need some kind of check before entering the quadratic-time loop. Oops! =)The quick fix: essentially we catch most values that exceed the threshold up front. Those that slip through will still be on the small side (read: sufficiently fast), and will get caught by the existing check so that the limit remains exact.
The justification for the current check. The C code check is:
In GitHub markdown math-speak, writing$M$ for $L$ for $s$ for
$$\left\lfloor\frac{M}{3L}\right\rfloor \le \left\lfloor\frac{s - 11}{10}\right\rfloor$$
max_str_digits
,PyLong_SHIFT
andsize_a
, that check is:From this it follows that
$$\frac{M}{3L} < \frac{s-1}{10}$$
$$\frac{L(s-1)}{M} > \frac{10}{3} > \log_2(10).$$
$$2^{L(s-1)} > 10^M.$$ $a$ satisfies $|a| \ge 2^{L(s-1)}$ , so $|a|$ is larger than $10^M$ . This shows that we don't accidentally capture anything below the intended limit in the check.
hence that
So
But our input integer
Co-authored-by: Gregory P. Smith [Google LLC] greg@krypto.org
(cherry picked from commit b126196)
Co-authored-by: Mark Dickinson dickinsm@gmail.com