Skip to content

Commit 961703c

Browse files
Fix possibly-unitialized warning in string_parser.c. (GH-21503)
GCC says ``` ../cpython/Parser/string_parser.c: In function ‘fstring_find_expr’: ../cpython/Parser/string_parser.c:404:93: warning: ‘cols’ may be used uninitialized in this function [-Wmaybe-uninitialized] 404 | p2->starting_col_offset = p->tok->first_lineno == p->tok->lineno ? t->col_offset + cols : cols; | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^~~~~~ ../cpython/Parser/string_parser.c:384:16: note: ‘cols’ was declared here 384 | int lines, cols; | ^~~~ ../cpython/Parser/string_parser.c:403:45: warning: ‘lines’ may be used uninitialized in this function [-Wmaybe-uninitialized] 403 | p2->starting_lineno = t->lineno + lines - 1; | ~~~~~~~~~~~~~~~~~~^~~ ../cpython/Parser/string_parser.c:384:9: note: ‘lines’ was declared here 384 | int lines, cols; | ^~~~~ ``` and, indeed, if `PyBytes_AsString` somehow fails, lines & cols will not be initialized. (cherry picked from commit 2ad7e9c) Co-authored-by: Benjamin Peterson <[email protected]>
1 parent f0f6566 commit 961703c

File tree

1 file changed

+16
-15
lines changed

1 file changed

+16
-15
lines changed

Parser/pegen/parse_string.c

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
#include <stdbool.h>
2+
13
#include <Python.h>
24

35
#include "../tokenizer.h"
@@ -282,26 +284,23 @@ _PyPegen_parsestr(Parser *p, int *bytesmode, int *rawmode, PyObject **result,
282284
`n` is the node which locations are going to be fixed relative to parent.
283285
`expr_str` is the child node's string representation, including braces.
284286
*/
285-
static void
287+
static bool
286288
fstring_find_expr_location(Token *parent, char *expr_str, int *p_lines, int *p_cols)
287289
{
288-
char *substr = NULL;
289-
char *start;
290-
int lines = 0;
291-
int cols = 0;
292-
290+
*p_lines = 0;
291+
*p_cols = 0;
293292
if (parent && parent->bytes) {
294293
char *parent_str = PyBytes_AsString(parent->bytes);
295294
if (!parent_str) {
296-
return;
295+
return false;
297296
}
298-
substr = strstr(parent_str, expr_str);
297+
char *substr = strstr(parent_str, expr_str);
299298
if (substr) {
300299
// The following is needed, in order to correctly shift the column
301300
// offset, in the case that (disregarding any whitespace) a newline
302301
// immediately follows the opening curly brace of the fstring expression.
303-
int newline_after_brace = 1;
304-
start = substr + 1;
302+
bool newline_after_brace = 1;
303+
char *start = substr + 1;
305304
while (start && *start != '}' && *start != '\n') {
306305
if (*start != ' ' && *start != '\t' && *start != '\f') {
307306
newline_after_brace = 0;
@@ -317,19 +316,18 @@ fstring_find_expr_location(Token *parent, char *expr_str, int *p_lines, int *p_c
317316
while (start > parent_str && *start != '\n') {
318317
start--;
319318
}
320-
cols += (int)(substr - start);
319+
*p_cols += (int)(substr - start);
321320
}
322321
/* adjust the start based on the number of newlines encountered
323322
before the f-string expression */
324323
for (char* p = parent_str; p < substr; p++) {
325324
if (*p == '\n') {
326-
lines++;
325+
(*p_lines)++;
327326
}
328327
}
329328
}
330329
}
331-
*p_lines = lines;
332-
*p_cols = cols;
330+
return true;
333331
}
334332

335333

@@ -387,7 +385,10 @@ fstring_compile_expr(Parser *p, const char *expr_start, const char *expr_end,
387385
str[len+2] = 0;
388386

389387
int lines, cols;
390-
fstring_find_expr_location(t, str, &lines, &cols);
388+
if (!fstring_find_expr_location(t, str, &lines, &cols)) {
389+
PyMem_FREE(str);
390+
return NULL;
391+
}
391392

392393
// The parentheses are needed in order to allow for leading whitespace withing
393394
// the f-string expression. This consequently gets parsed as a group (see the

0 commit comments

Comments
 (0)