-
-
Notifications
You must be signed in to change notification settings - Fork 30.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[3.12] gh-123229: Fix valgrind warning by initializing the f-string b…
…uffers to 0 in the tokenizer (GH-123263) (#123265) (cherry picked from commit adc5190) Signed-off-by: Pablo Galindo <[email protected]>
- Loading branch information
Showing
3 changed files
with
16 additions
and
1 deletion.
There are no files selected for viewing
2 changes: 2 additions & 0 deletions
2
Misc/NEWS.d/next/Core and Builtins/2024-08-23-13-08-27.gh-issue-123229.aHm-dw.rst
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
Fix valgrind warning by initializing the f-string buffers to 0 in the | ||
tokenizer. Patch by Pablo Galindo |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
diff --git a/Parser/tokenizer.c b/Parser/tokenizer.c | ||
index 3118fb19846..9e0dee8cc38 100644 | ||
--- a/Parser/tokenizer.c | ||
+++ b/Parser/tokenizer.c | ||
@@ -65,7 +65,7 @@ static const char *type_comment_prefix = "# type: "; | ||
|
||
static struct tok_state *tok_new(void) { | ||
struct tok_state *tok = | ||
- (struct tok_state *)PyMem_Malloc(sizeof(struct tok_state)); | ||
+ (struct tok_state *)PyMem_Calloc(1, sizeof(struct tok_state)); | ||
if (tok == NULL) | ||
return NULL; | ||
tok->buf = tok->cur = tok->inp = NULL; |
7dec3d7
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
While this fix undoubtedly works, it makes the code a little inconsistent - previously it exhaustively set each member of the
struct tok_state
, most of them to zero or NULL. But it now it relies onPyMem_Calloc()
to initialise at least one member, in an implementation-defined way that usually corresponds to zero or NULL.If we are happy to assume that
PyMem_Calloc()
's strictly undefined behaviour here is ok in practice, should we remove all the explicit settings to zero or NULL? Otherwise maybe it would be better to revert back toPyMem_Malloc()
and add code to explicitly set the new member(s)?