]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
gh-91421: Use constant value check during runtime (GH-91422) (GH-91493)
authorMiss Islington (bot) <31488909+miss-islington@users.noreply.github.com>
Thu, 14 Apr 2022 01:38:55 +0000 (18:38 -0700)
committerGitHub <noreply@github.com>
Thu, 14 Apr 2022 01:38:55 +0000 (18:38 -0700)
The left-hand side expression of the if-check can be converted to a
constant by the compiler, but the addition on the right-hand side is
performed during runtime.

Move the addition from the right-hand side to the left-hand side by
turning it into a subtraction there. Since the values are known to
be large enough to not turn negative, this is a safe operation.

Prevents a very unlikely integer overflow on 32 bit systems.

Fixes GH-91421.
(cherry picked from commit 0859368335d470b9ff33fc53ed9a85ec2654b278)

Co-authored-by: Tobias Stoeckmann <stoeckmann@users.noreply.github.com>
Misc/NEWS.d/next/Core and Builtins/2022-04-10-22-57-27.gh-issue-91421.dHhv6U.rst [new file with mode: 0644]
Objects/unicodeobject.c

diff --git a/Misc/NEWS.d/next/Core and Builtins/2022-04-10-22-57-27.gh-issue-91421.dHhv6U.rst b/Misc/NEWS.d/next/Core and Builtins/2022-04-10-22-57-27.gh-issue-91421.dHhv6U.rst
new file mode 100644 (file)
index 0000000..898eb0d
--- /dev/null
@@ -0,0 +1 @@
+Fix a potential integer overflow in _Py_DecodeUTF8Ex.
index 7767d140e6c3958e5b9f4ad05ff85b86190b88d5..369f91342e3ac1b19337657c8ad6ed05c15a967a 100644 (file)
@@ -5219,7 +5219,7 @@ _Py_DecodeUTF8Ex(const char *s, Py_ssize_t size, wchar_t **wstr, size_t *wlen,
 
     /* Note: size will always be longer than the resulting Unicode
        character count */
-    if (PY_SSIZE_T_MAX / (Py_ssize_t)sizeof(wchar_t) < (size + 1)) {
+    if (PY_SSIZE_T_MAX / (Py_ssize_t)sizeof(wchar_t) - 1 < size) {
         return -1;
     }