This repository has been archived by the owner on Feb 15, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 660
Don't crash when memory is exhausted #389
Open
DemiMarie
wants to merge
1
commit into
google:master
Choose a base branch
from
DemiMarie:fix-oom
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
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 |
---|---|---|
|
@@ -1954,6 +1954,32 @@ TEST_F(GumboParserTest, FragmentWithNamespace) { | |
EXPECT_EQ(0, GetChildCount(div)); | ||
} | ||
|
||
TEST_F(GumboParserTest, OutOfMemory) { | ||
GumboOptions options; | ||
memcpy((void*)&options, (void*)&kGumboDefaultOptions, sizeof options); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There's no need for GumboOptions options = kGumboDefaultOptions; |
||
struct Count { | ||
size_t count = 0; | ||
size_t count_max = 0; | ||
} count; | ||
options.userdata = (void*)&count; | ||
options.allocator = [](void *userdata, size_t size) { | ||
auto count = (Count*)userdata; | ||
if (count->count) { | ||
count->count--; | ||
return malloc(size); | ||
} else { | ||
count->count = ++count->count_max; | ||
return (void*)nullptr; | ||
} | ||
}; | ||
const char buf[] = "<html><head><title>dummy</title></head><body>some text</body></html>"; | ||
GumboOutput *output; | ||
do { | ||
output = gumbo_parse_with_options(&options, buf, sizeof buf - 1); | ||
} while (output == nullptr); | ||
gumbo_destroy_output(&options, output); | ||
} | ||
|
||
TEST_F(GumboParserTest, FragmentWithTwoNodes) { | ||
ParseFragment("<h1>Hi</h1><br>", GUMBO_TAG_BODY, GUMBO_NAMESPACE_HTML); | ||
|
||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Does this actually deallocate all the used memory? What about all the allocations for nodes and errors?
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.
It doesn’t, even if I add a call to
gumbo_destroy_output
at the end whenever there would be something non-NULL
to pass to it.One option that is guaranteed to work is to place all allocations in a linked list and on error destroy the list. On the other hand, the user can always do that themselves, since Gumbo can call user-provided callbacks.
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.
I was considering using the arena pull request to solve this problem for lua-gumbo. Unfortunately, the state of that pull request as it stands still contains dozens of unchecked calls to
malloc
and has a few other problems I can't recall off the top of my head.Gumbo is essentially an abandoned project at this point though. It seems unlikely that any of the newer bug reports will be fixed in this repo.