@@ -202,21 +202,27 @@ _get_current_line(tokenizeriterobject *it, const char *line_start, Py_ssize_t si
202202 return line ;
203203}
204204
205- static void
205+ static int
206206_get_col_offsets (tokenizeriterobject * it , struct token token , const char * line_start ,
207207 PyObject * line , int line_changed , Py_ssize_t lineno , Py_ssize_t end_lineno ,
208208 Py_ssize_t * col_offset , Py_ssize_t * end_col_offset )
209209{
210210 _Py_CRITICAL_SECTION_ASSERT_OBJECT_LOCKED (it );
211211 Py_ssize_t byte_offset = -1 ;
212+ Py_ssize_t byte_col_offset_diff = it -> byte_col_offset_diff ;
212213 if (token .start != NULL && token .start >= line_start ) {
213214 byte_offset = token .start - line_start ;
214215 if (line_changed ) {
215- * col_offset = _PyPegen_byte_offset_to_character_offset_line (line , 0 , byte_offset );
216- it -> byte_col_offset_diff = byte_offset - * col_offset ;
216+ Py_ssize_t offset = _PyPegen_byte_offset_to_character_offset_line (
217+ line , 0 , byte_offset );
218+ if (offset < 0 ) {
219+ return -1 ;
220+ }
221+ * col_offset = offset ;
222+ byte_col_offset_diff = byte_offset - * col_offset ;
217223 }
218224 else {
219- * col_offset = byte_offset - it -> byte_col_offset_diff ;
225+ * col_offset = byte_offset - byte_col_offset_diff ;
220226 }
221227 }
222228
@@ -226,17 +232,28 @@ _get_col_offsets(tokenizeriterobject *it, struct token token, const char *line_s
226232 // If the whole token is at the same line, we can just use the token.start
227233 // buffer for figuring out the new column offset, since using line is not
228234 // performant for very long lines.
229- Py_ssize_t token_col_offset = _PyPegen_byte_offset_to_character_offset_line (line , byte_offset , end_byte_offset );
235+ Py_ssize_t token_col_offset = _PyPegen_byte_offset_to_character_offset_line (
236+ line , byte_offset , end_byte_offset );
237+ if (token_col_offset < 0 ) {
238+ return -1 ;
239+ }
230240 * end_col_offset = * col_offset + token_col_offset ;
231- it -> byte_col_offset_diff += token .end - token .start - token_col_offset ;
241+ byte_col_offset_diff += token .end - token .start - token_col_offset ;
232242 }
233243 else {
234- * end_col_offset = _PyPegen_byte_offset_to_character_offset_raw (it -> tok -> line_start , end_byte_offset );
235- it -> byte_col_offset_diff += end_byte_offset - * end_col_offset ;
244+ Py_ssize_t offset = _PyPegen_byte_offset_to_character_offset_raw (
245+ it -> tok -> line_start , end_byte_offset );
246+ if (offset < 0 ) {
247+ return -1 ;
248+ }
249+ * end_col_offset = offset ;
250+ byte_col_offset_diff += end_byte_offset - * end_col_offset ;
236251 }
237252 }
253+ it -> byte_col_offset_diff = byte_col_offset_diff ;
238254 it -> last_lineno = lineno ;
239255 it -> last_end_lineno = end_lineno ;
256+ return 0 ;
240257}
241258
242259static PyObject *
@@ -301,8 +318,11 @@ tokenizeriter_next(PyObject *op)
301318 Py_ssize_t end_lineno = it -> tok -> lineno ;
302319 Py_ssize_t col_offset = -1 ;
303320 Py_ssize_t end_col_offset = -1 ;
304- _get_col_offsets (it , token , line_start , line , line_changed ,
305- lineno , end_lineno , & col_offset , & end_col_offset );
321+ if (_get_col_offsets (it , token , line_start , line , line_changed ,
322+ lineno , end_lineno , & col_offset , & end_col_offset ) < 0 ) {
323+ Py_DECREF (str );
324+ goto exit ;
325+ }
306326
307327 if (it -> tok -> tok_extra_tokens ) {
308328 if (is_trailing_token ) {
0 commit comments