Skip to content

Commit 30f8f52

Browse files
committed
Propagate tokenizer column conversion failures
1 parent ec6259a commit 30f8f52

2 files changed

Lines changed: 19 additions & 3 deletions

File tree

‎Parser/pegen.c‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,9 @@ Py_ssize_t
3535
_PyPegen_byte_offset_to_character_offset_line(PyObject *line, Py_ssize_t col_offset, Py_ssize_t end_col_offset)
3636
{
3737
const unsigned char *data = (const unsigned char*)PyUnicode_AsUTF8(line);
38+
if (data == NULL) {
39+
return -1;
40+
}
3841

3942
Py_ssize_t len = 0;
4043
while (col_offset < end_col_offset) {

‎Python/Python-tokenize.c‎

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -190,7 +190,7 @@ _get_current_line(tokenizeriterobject *it, int current_lineno,
190190
return line;
191191
}
192192

193-
static void
193+
static int
194194
_get_col_offsets(tokenizeriterobject *it, const struct token *token,
195195
const char *token_start, const char *line_start,
196196
const char *end_line_start, PyObject *line, int line_changed,
@@ -206,6 +206,9 @@ _get_col_offsets(tokenizeriterobject *it, const struct token *token,
206206
byte_offset = token_start - line_start;
207207
if (line_changed) {
208208
*col_offset = _PyPegen_byte_offset_to_character_offset_line(line, 0, byte_offset);
209+
if (*col_offset < 0) {
210+
return -1;
211+
}
209212
it->byte_col_offset_diff = byte_offset - *col_offset;
210213
}
211214
else {
@@ -218,15 +221,22 @@ _get_col_offsets(tokenizeriterobject *it, const struct token *token,
218221
if (lineno == end_lineno) {
219222
// Avoid rescanning the prefix of a very long line.
220223
Py_ssize_t token_col_offset = _PyPegen_byte_offset_to_character_offset_line(line, byte_offset, end_byte_offset);
224+
if (token_col_offset < 0) {
225+
return -1;
226+
}
221227
*end_col_offset = *col_offset + token_col_offset;
222228
it->byte_col_offset_diff += token_end - token_start - token_col_offset;
223229
}
224230
else {
225231
*end_col_offset = _PyPegen_byte_offset_to_character_offset_raw(end_line_start, end_byte_offset);
232+
if (*end_col_offset < 0) {
233+
return -1;
234+
}
226235
it->byte_col_offset_diff += end_byte_offset - *end_col_offset;
227236
}
228237
}
229238
it->last_lineno = lineno;
239+
return 0;
230240
}
231241

232242
static PyObject *
@@ -295,8 +305,11 @@ tokenizeriter_next(PyObject *op)
295305
Py_ssize_t end_lineno = token.end_loc.lineno;
296306
Py_ssize_t col_offset = -1;
297307
Py_ssize_t end_col_offset = -1;
298-
_get_col_offsets(it, &token, token_start, view.line, view.end_line, line,
299-
line_changed, &col_offset, &end_col_offset);
308+
if (_get_col_offsets(it, &token, token_start, view.line, view.end_line, line,
309+
line_changed, &col_offset, &end_col_offset) < 0) {
310+
Py_DECREF(str);
311+
goto exit;
312+
}
300313

301314
if (it->extra_tokens) {
302315
if (is_trailing_token) {

0 commit comments

Comments
 (0)