Avoid stack-overflow and add some defensive conditions#604
Merged
Conversation
Contributor
|
Something like the following seems more readable to me: static bool S_ends_with_blank_line(cmark_node *node) {
while (!S_last_line_checked(node)) {
S_set_last_line_checked(node);
if (S_type(node) != CMARK_NODE_LIST && S_type(node) != CMARK_NODE_ITEM)
break;
if (!node->last_child)
break;
node = node->last_child;
}
return S_last_line_blank(node);
} |
The recursive descent through nested LIST/ITEM nodes could exhaust the call stack on deeply nested list input. Convert to a loop that walks node->last_child; the last_line_checked memoization and all branch semantics are preserved. Co-Authored-By: Claude <noreply@anthropic.com>
The outer condition only guarantees pos+2 <= input.len, but the branches indexed data[pos+3] and data[pos+4]. Correctness relied on the strbuf-backed null terminator and on data[pos+2] == '-' ruling out the terminator. Make the bounds explicit so the code does not depend on those invariants. Co-Authored-By: Claude <noreply@anthropic.com>
When ptr[r] is a trailing backslash, the code read ptr[r+1] (the
strbuf's null terminator). Safe in practice since cmark_ispunct('\0')
is false, but the read depends on the null-termination invariant.
Guard with r + 1 < buf->size to keep the read within the logical
buffer.
Co-Authored-By: Claude <noreply@anthropic.com>
_scan_at patched the byte at ptr[c->len] with '\0' around every scanner invocation so that the re2c scanners (built with yyfill:enable = 0) could use it as a stopping sentinel. In the common case the chunk is backed by a cmark_strbuf whose data[size] is already '\0', so the write was an idempotent mutation of shared backing storage. Skip it when the byte is already '\0' to preserve const-correctness and reentrancy on that path; retain the patch fallback for chunks that are not already NUL-terminated. Co-Authored-By: Claude <noreply@anthropic.com>
Member
Author
|
Thanks, I agree, and I adopted your suggestion. |
This file contains hidden or 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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
These are some changes suggested by Claude in response to a prompt to provide a security audit. They all seem reasonable and correct to me. Some of them aren't strictly necessary, given certain assumptions about how the rest of the code works, but they are probably a good idea in the spirit of defensive coding.
@nwellnhof comments welcome.