summaryrefslogtreecommitdiffstats
path: root/Parser
diff options
context:
space:
mode:
authorMiss Islington (bot) <31488909+miss-islington@users.noreply.github.com>2020-07-16 13:25:31 (GMT)
committerGitHub <noreply@github.com>2020-07-16 13:25:31 (GMT)
commit961703cdc82536b6ff897ad7d4b3bbf22718d1b5 (patch)
tree84e76da4f4911420f1837bdc7e2e8d8d25b1cd70 /Parser
parentf0f6566d47839c4dd395c8179724dacdf47c1858 (diff)
downloadcpython-961703cdc82536b6ff897ad7d4b3bbf22718d1b5.zip
cpython-961703cdc82536b6ff897ad7d4b3bbf22718d1b5.tar.gz
cpython-961703cdc82536b6ff897ad7d4b3bbf22718d1b5.tar.bz2
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 2ad7e9c011b7606c5c7307176df07419a0e60134) Co-authored-by: Benjamin Peterson <benjamin@python.org>
Diffstat (limited to 'Parser')
-rw-r--r--Parser/pegen/parse_string.c31
1 files changed, 16 insertions, 15 deletions
diff --git a/Parser/pegen/parse_string.c b/Parser/pegen/parse_string.c
index 88b10c3..d762e65 100644
--- a/Parser/pegen/parse_string.c
+++ b/Parser/pegen/parse_string.c
@@ -1,3 +1,5 @@
+#include <stdbool.h>
+
#include <Python.h>
#include "../tokenizer.h"
@@ -282,26 +284,23 @@ _PyPegen_parsestr(Parser *p, int *bytesmode, int *rawmode, PyObject **result,
`n` is the node which locations are going to be fixed relative to parent.
`expr_str` is the child node's string representation, including braces.
*/
-static void
+static bool
fstring_find_expr_location(Token *parent, char *expr_str, int *p_lines, int *p_cols)
{
- char *substr = NULL;
- char *start;
- int lines = 0;
- int cols = 0;
-
+ *p_lines = 0;
+ *p_cols = 0;
if (parent && parent->bytes) {
char *parent_str = PyBytes_AsString(parent->bytes);
if (!parent_str) {
- return;
+ return false;
}
- substr = strstr(parent_str, expr_str);
+ char *substr = strstr(parent_str, expr_str);
if (substr) {
// The following is needed, in order to correctly shift the column
// offset, in the case that (disregarding any whitespace) a newline
// immediately follows the opening curly brace of the fstring expression.
- int newline_after_brace = 1;
- start = substr + 1;
+ bool newline_after_brace = 1;
+ char *start = substr + 1;
while (start && *start != '}' && *start != '\n') {
if (*start != ' ' && *start != '\t' && *start != '\f') {
newline_after_brace = 0;
@@ -317,19 +316,18 @@ fstring_find_expr_location(Token *parent, char *expr_str, int *p_lines, int *p_c
while (start > parent_str && *start != '\n') {
start--;
}
- cols += (int)(substr - start);
+ *p_cols += (int)(substr - start);
}
/* adjust the start based on the number of newlines encountered
before the f-string expression */
for (char* p = parent_str; p < substr; p++) {
if (*p == '\n') {
- lines++;
+ (*p_lines)++;
}
}
}
}
- *p_lines = lines;
- *p_cols = cols;
+ return true;
}
@@ -387,7 +385,10 @@ fstring_compile_expr(Parser *p, const char *expr_start, const char *expr_end,
str[len+2] = 0;
int lines, cols;
- fstring_find_expr_location(t, str, &lines, &cols);
+ if (!fstring_find_expr_location(t, str, &lines, &cols)) {
+ PyMem_FREE(str);
+ return NULL;
+ }
// The parentheses are needed in order to allow for leading whitespace withing
// the f-string expression. This consequently gets parsed as a group (see the