Skip to content

Commit f9fa6ba

Browse files
authored
gh-124064: Fix -Wconversion warnings in Parser/string_parser.c (#124204)
Fix integer overflow check in decode_unicode_with_escapes(): use PY_SSIZE_T_MAX instead of SIZE_MAX.
1 parent 5cd50cb commit f9fa6ba

File tree

1 file changed

+8
-8
lines changed

1 file changed

+8
-8
lines changed

Parser/string_parser.c

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ warn_invalid_escape_sequence(Parser *p, const char *first_invalid_escape, Token
1818
// to avoid showing the warning twice.
1919
return 0;
2020
}
21-
unsigned char c = *first_invalid_escape;
21+
unsigned char c = (unsigned char)*first_invalid_escape;
2222
if ((t->type == FSTRING_MIDDLE || t->type == FSTRING_END) && (c == '{' || c == '}')) {
2323
// in this case the tokenizer has already emitted a warning,
2424
// 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)
9090
const char *end;
9191

9292
/* check for integer overflow */
93-
if (len > SIZE_MAX / 6) {
93+
if (len > (size_t)PY_SSIZE_T_MAX / 6) {
9494
return NULL;
9595
}
9696
/* "ä" (2 bytes) may become "\U000000E4" (10 bytes), or 1:5
9797
"\ä" (3 bytes) may become "\u005c\U000000E4" (16 bytes), or ~1:6 */
98-
u = PyBytes_FromStringAndSize((char *)NULL, len * 6);
98+
u = PyBytes_FromStringAndSize((char *)NULL, (Py_ssize_t)len * 6);
9999
if (u == NULL) {
100100
return NULL;
101101
}
@@ -142,11 +142,11 @@ decode_unicode_with_escapes(Parser *parser, const char *s, size_t len, Token *t)
142142
*p++ = *s++;
143143
}
144144
}
145-
len = p - buf;
145+
len = (size_t)(p - buf);
146146
s = buf;
147147

148148
const char *first_invalid_escape;
149-
v = _PyUnicode_DecodeUnicodeEscapeInternal(s, len, NULL, NULL, &first_invalid_escape);
149+
v = _PyUnicode_DecodeUnicodeEscapeInternal(s, (Py_ssize_t)len, NULL, NULL, &first_invalid_escape);
150150

151151
// HACK: later we can simply pass the line no, since we don't preserve the tokens
152152
// when we are decoding the string but we preserve the line numbers.
@@ -185,7 +185,7 @@ PyObject *
185185
_PyPegen_decode_string(Parser *p, int raw, const char *s, size_t len, Token *t)
186186
{
187187
if (raw) {
188-
return PyUnicode_DecodeUTF8Stateful(s, len, NULL, NULL);
188+
return PyUnicode_DecodeUTF8Stateful(s, (Py_ssize_t)len, NULL, NULL);
189189
}
190190
return decode_unicode_with_escapes(p, s, len, t);
191191
}
@@ -274,9 +274,9 @@ _PyPegen_parse_string(Parser *p, Token *t)
274274
}
275275
}
276276
if (rawmode) {
277-
return PyBytes_FromStringAndSize(s, len);
277+
return PyBytes_FromStringAndSize(s, (Py_ssize_t)len);
278278
}
279-
return decode_bytes_with_escapes(p, s, len, t);
279+
return decode_bytes_with_escapes(p, s, (Py_ssize_t)len, t);
280280
}
281281
return _PyPegen_decode_string(p, rawmode, s, len, t);
282282
}

0 commit comments

Comments
 (0)