diff options
| author | Miss Islington (bot) <31488909+miss-islington@users.noreply.github.com> | 2024-09-30 01:13:13 (GMT) |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2024-09-30 01:13:13 (GMT) |
| commit | 5dcce94e9dfd80d0e09c083f97af758ab1caa272 (patch) | |
| tree | 8f645ac83db1ecb28f920fcfd3595a7bd7114b75 /Parser/string_parser.c | |
| parent | 32acfdb678e3a4cbff7dcbbc5233d169cdafe1a9 (diff) | |
| download | cpython-5dcce94e9dfd80d0e09c083f97af758ab1caa272.zip cpython-5dcce94e9dfd80d0e09c083f97af758ab1caa272.tar.gz cpython-5dcce94e9dfd80d0e09c083f97af758ab1caa272.tar.bz2 | |
[3.13] gh-124064: Fix -Wconversion warnings in Parser/string_parser.c (GH-124204) (#124216)
gh-124064: Fix -Wconversion warnings in Parser/string_parser.c (GH-124204)
Fix integer overflow check in decode_unicode_with_escapes(): use
PY_SSIZE_T_MAX instead of SIZE_MAX.
(cherry picked from commit f9fa6ba4f8d90ae12bc1f6a792d66903bb169ba8)
Co-authored-by: Victor Stinner <vstinner@python.org>
Diffstat (limited to 'Parser/string_parser.c')
| -rw-r--r-- | Parser/string_parser.c | 16 |
1 files changed, 8 insertions, 8 deletions
diff --git a/Parser/string_parser.c b/Parser/string_parser.c index 93ad92b..9537c54 100644 --- a/Parser/string_parser.c +++ b/Parser/string_parser.c @@ -18,7 +18,7 @@ warn_invalid_escape_sequence(Parser *p, const char *first_invalid_escape, Token // to avoid showing the warning twice. return 0; } - unsigned char c = *first_invalid_escape; + unsigned char c = (unsigned char)*first_invalid_escape; if ((t->type == FSTRING_MIDDLE || t->type == FSTRING_END) && (c == '{' || c == '}')) { // in this case the tokenizer has already emitted a warning, // see Parser/tokenizer/helpers.c:warn_invalid_escape_sequence @@ -90,12 +90,12 @@ decode_unicode_with_escapes(Parser *parser, const char *s, size_t len, Token *t) const char *end; /* check for integer overflow */ - if (len > SIZE_MAX / 6) { + if (len > (size_t)PY_SSIZE_T_MAX / 6) { return NULL; } /* "ä" (2 bytes) may become "\U000000E4" (10 bytes), or 1:5 "\ä" (3 bytes) may become "\u005c\U000000E4" (16 bytes), or ~1:6 */ - u = PyBytes_FromStringAndSize((char *)NULL, len * 6); + u = PyBytes_FromStringAndSize((char *)NULL, (Py_ssize_t)len * 6); if (u == NULL) { return NULL; } @@ -142,11 +142,11 @@ decode_unicode_with_escapes(Parser *parser, const char *s, size_t len, Token *t) *p++ = *s++; } } - len = p - buf; + len = (size_t)(p - buf); s = buf; const char *first_invalid_escape; - v = _PyUnicode_DecodeUnicodeEscapeInternal(s, len, NULL, NULL, &first_invalid_escape); + v = _PyUnicode_DecodeUnicodeEscapeInternal(s, (Py_ssize_t)len, NULL, NULL, &first_invalid_escape); // HACK: later we can simply pass the line no, since we don't preserve the tokens // when we are decoding the string but we preserve the line numbers. @@ -185,7 +185,7 @@ PyObject * _PyPegen_decode_string(Parser *p, int raw, const char *s, size_t len, Token *t) { if (raw) { - return PyUnicode_DecodeUTF8Stateful(s, len, NULL, NULL); + return PyUnicode_DecodeUTF8Stateful(s, (Py_ssize_t)len, NULL, NULL); } return decode_unicode_with_escapes(p, s, len, t); } @@ -274,9 +274,9 @@ _PyPegen_parse_string(Parser *p, Token *t) } } if (rawmode) { - return PyBytes_FromStringAndSize(s, len); + return PyBytes_FromStringAndSize(s, (Py_ssize_t)len); } - return decode_bytes_with_escapes(p, s, len, t); + return decode_bytes_with_escapes(p, s, (Py_ssize_t)len, t); } return _PyPegen_decode_string(p, rawmode, s, len, t); } |
