Handle read errors safely in StackLineReader to prevent buffer overreads - #472
Open
jdymitarai wants to merge 1 commit into
Open
Handle read errors safely in StackLineReader to prevent buffer overreads#472jdymitarai wants to merge 1 commit into
jdymitarai wants to merge 1 commit into
Conversation
1. Check read <= 0 in LoadFullBuffer and LoadMore to prevent integer underflow and out-of-bounds reads in release builds where assert() is disabled. 2. Gracefully treat read errors as EOF in SkipToNextLine and StackLineReader_NextLine. 3. Support read error simulation in test filesystem harness. 4. Add comprehensive unit tests covering invalid file descriptor, mid-stream read failure, and read errors during line truncation skip mode.
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.
Summary of Changes
Fix integer underflow and buffer overreads on read failures:
LoadFullBufferandLoadMore,CpuFeatures_ReadFilereturns-1on I/O error or invalid file descriptors.NDEBUGdefined),assert(read >= 0)is omitted.size_t reader->view.size(or adding it via+= read) causedreader->view.sizeto underflow toSIZE_MAX(18446744073709551615ULL).IndexOfEol/CpuFeatures_memchrperformed unbounded out-of-bounds reads pastreader->bufferon the stack, leading to segmentation faults or infinite loops.Defensive handling:
LoadFullBuffer, ifread <= 0, resetreader->view.size = 0and return-1(if< 0) or0.LoadMore, ifread <= 0, return immediately without modifyingreader->view.size.SkipToNextLineandStackLineReader_NextLine, checkread <= 0to treat read errors as EOF cleanly.Test Infrastructure & Coverage:
test/filesystem_for_testingto support simulated read errors andFindFileOrNull(int fd)for invalid descriptors.test/stack_line_reader_test.cccovering:-1) initialization and reading.Verification
All tests in
stack_line_reader_test,string_view_test,cpuinfo_x86_test, andbit_utils_testpass cleanly on MSVC.