fix: add sscanf return value check to prevent uninitialized value return#286
Open
hobostay wants to merge 1 commit intodeepseek-ai:mainfrom
Open
fix: add sscanf return value check to prevent uninitialized value return#286hobostay wants to merge 1 commit intodeepseek-ai:mainfrom
hobostay wants to merge 1 commit intodeepseek-ai:mainfrom
Conversation
- Check sscanf return value when parsing int from environment variable - Return default value if parsing fails instead of returning uninitialized value - This prevents potential undefined behavior when env var contains invalid integer Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
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
This PR fixes a potential bug in
csrc/utils/system.hppwhere the return value ofstd::sscanfwas not being checked when parsing integer environment variables.Problem
In the
get_env<int>()function template,std::sscanfwas used to parse an environment variable string as an integer, but the return value was never checked. If the environment variable contains a value that cannot be parsed as an integer (e.g., "abc", "12.3", or an empty string),sscanffails and thevaluevariable remains uninitialized. This uninitialized value is then returned, leading to undefined behavior.Solution
Added a check for
sscanf's return value (which should be 1 for successful parsing of one integer). If parsing fails, the function now returns the provided default value instead of the uninitialized variable.Changes
Test plan
Impact
🤖 Generated with Claude Code