fix(log_router): stop buffer overrun#717
Open
Vincent-Dalstra wants to merge 1 commit into
Open
Conversation
6 tasks
The return value of vsnprintf() is used to determine how long the message was. However, when vsprintf() truncates a message this value will exceed the buffer! Then, vprintf_buffer[len] = '\0'; will cause undefined behaviour. Add a check for truncation, which resets 'len' to the number of bytes in the buffer. Also removed the -1 from the 'size' argument to vsnprintf(), because it already accounts for the terminating '\0' byte.
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.
Description
The return value of vsnprintf() is used to determine how long the message was. However, when vsprintf() truncates a message this value will exceed the buffer! Then, vprintf_buffer[len] = '\0'; will cause undefined behaviour.
Add a check for truncation, which will then set the value of
lento match the number of bytes in the bufferAlso removed the -1 from the 'size' argument to vsnprintf(), because it already accounts for the terminating '\0' byte.
Testing
I discovered this because my program would always crash after a particular log message. The stack trace showed that something was corrupting the
g_esp_log_router_vprintffunction pointer and cause any subsequent log messages to panic. Using the ESP32-S3 on-chip debugger I could put a watchpoint ong_esp_log_router_vprintfand thus detect when it was changed byvprintf_buffer[len] = '\0';After fixing the problem, the program no longer crashes after that message, or other long messages.
Checklist
Before submitting a Pull Request, please ensure the following: