Skip to content

[3.9] Fix possibly-unitialized warning in string_parser.c. (GH-21503) #21507

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 16, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 16 additions & 15 deletions Parser/pegen/parse_string.c
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#include <stdbool.h>

#include <Python.h>

#include "../tokenizer.h"
Expand Down Expand Up @@ -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;
Expand All @@ -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;
}


Expand Down Expand Up @@ -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
Expand Down