Repository navigation
pathbuf: reserve room for the separator in pathbuf_append_path_len - #318
Merged
hyperrealm merged 1 commit intoNov 29, 2026
Merged
Conversation
pathbuf_append_path_len() asks pathbuf_ensure_capacity() for len + 1 bytes when it is not going to insert a '/' and for only len when it is, which is the wrong way round. ensure_capacity() reserves length + len + 1 and rounds that up to a 128-byte block, so when a separator is written and length + len + 1 is already exactly the capacity, nothing is reallocated and the terminating NUL lands one byte past the allocation. Both appends in example4.c's include_func() are fed from outside the program: the directory part of the @include path in the configuration file, and the names readdir() returns for that directory. A config file containing @include "<120 characters>/*.cfg" together with a 7-character file name in that directory makes config_read_file() abort under -fsanitize=address with a heap-buffer-overflow WRITE of size 1 at pathbuf.c:74. Reserve the byte on the branch that writes it. lib/strbuf.c, which this helper is modelled on, already counts it this way.
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.
pathbuf_append_path_len() reserves the byte for the '/' separator only on the branch that doesn't write one, so an append onto a non-empty buffer whose
length + len + 1is already exactly the 128-byte-rounded capacity puts the terminating NUL one byte past the allocation; example4'sinclude_func()feeds it the directory part of the@includepath and the namesreaddir()returns for that directory, so a config file with@include "<120 characters>/*.cfg"and a 7-character file name in that directory gives asan a heap-buffer-overflow WRITE of size 1 at pathbuf.c:74 underconfig_read_file().