Skip to content

Commit

Permalink
bpo-40020: Fix realloc leak on failure in growable_comment_array_add (p…
Browse files Browse the repository at this point in the history
…ythonGH-19083)

Fix a leak and subsequent crash in parsetok.c caused by realloc misuse on a rare codepath. 

Realloc returns a null pointer on failure, and then growable_comment_array_deallocate crashes later when it dereferences it.
  • Loading branch information
ariccio authored Mar 30, 2020
1 parent fc2d8d6 commit 51e3e45
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 3 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fix a leak and subsequent crash in parsetok.c caused by realloc misuse on a rare codepath.
8 changes: 5 additions & 3 deletions Parser/parsetok.c
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,13 @@ growable_comment_array_init(growable_comment_array *arr, size_t initial_size) {
static int
growable_comment_array_add(growable_comment_array *arr, int lineno, char *comment) {
if (arr->num_items >= arr->size) {
arr->size *= 2;
arr->items = realloc(arr->items, arr->size * sizeof(*arr->items));
if (!arr->items) {
size_t new_size = arr->size * 2;
void *new_items_array = realloc(arr->items, new_size * sizeof(*arr->items));
if (!new_items_array) {
return 0;
}
arr->items = new_items_array;
arr->size = new_size;
}

arr->items[arr->num_items].lineno = lineno;
Expand Down

0 comments on commit 51e3e45

Please sign in to comment.