fix: return default_value from get_env<int> on unparsable env values - #736
fix: return default_value from get_env<int> on unparsable env values#736shoemoney wants to merge 1 commit into
Conversation
get_env<int> left `value` uninitialized and ignored sscanf's return, so a non-numeric value (e.g. EP_BUFFER_DEBUG=false) returned uninitialized stack memory instead of falling back to default_value.
| int value; | ||
| std::sscanf(c_str, "%d", &value); | ||
| int value = default_value; | ||
| if (std::sscanf(c_str, "%d", &value) != 1) |
There was a problem hiding this comment.
🔵 suggestion: 可选改进:sscanf("42abc", "%d") 仍会成功解析为 42,且 false/true 等布尔风格取值只是静默回退到默认值。若希望更严格,可考虑用 std::from_chars 校验整个字符串,或对无法解析的取值打印一次告警,帮助用户发现拼写错误的环境变量值。非阻塞。
🤖 v5
🤖 ds-review-bot Code Reviewv6变更正确检查了 sscanf 的转换结果,并在无法解析整数时返回默认值,修复了未初始化值问题,未发现新的功能性缺陷。 v5该 MR 修复了 csrc/utils/system.hpp 中 get_env<int> 的未定义行为:原实现声明未初始化的 v4p本 MR 修复 Files reviewed: 1 |
get_env<int>(csrc/utils/system.hpp) declaredint value;uninitialized and ignored sscanf's return value. Any env value%dcannot parse (true,false,on,off, empty string) returned indeterminate stack memory instead of falling back todefault_value. In practice the garbage is usually non-zero, soEP_BUFFER_DEBUG=falsereads as enabled. This helper backs 20+ env reads across the codebase.The fix initializes
valuetodefault_valueand returnsdefault_valuewhen sscanf does not report a successful conversion.Verified with a standalone host build of the header: before the change,
EP_X=falsewith default 7 returned 1 (uninitialized stack); after, it returns 7,EP_X=42returns 42, and unset still returns the default.Fixes #733