[PR #286] [CLOSED] fix: add sscanf return value check to prevent uninitialized value return #344

Closed
opened 2026-10-26 21:16:30 +00:00 by bsakaguchi · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/deepseek-ai/DeepGEMM/pull/286
Author: @hobostay
Created: 2/9/2026
Status: ❌ Closed

Base: main ← Head: fix/sscanf-return-value-check


📝 Commits (1)

  • 8774365 fix: add sscanf return value check to prevent uninitialized value return

📊 Changes

1 file changed (+4 additions, -1 deletions)

View changed files

📝 csrc/utils/system.hpp (+4 -1)

📄 Description

Summary

This PR fixes a potential bug in csrc/utils/system.hpp where the return value of std::sscanf was not being checked when parsing integer environment variables.

Problem

In the get_env<int>() function template, std::sscanf was 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), sscanf fails and the value variable 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

// Before: Uninitialized value returned on sscanf failure
} else if constexpr (std::is_same_v<dtype_t, int>) {
    int value;
    std::sscanf(c_str, "%d", &value);
    return value;
}

// After: Returns default value on parsing failure
} else if constexpr (std::is_same_v<dtype_t, int>) {
    int value;
    if (std::sscanf(c_str, "%d", &value) != 1) {
        // Failed to parse as int, return default value
        return default_value;
    }
    return value;
}

Test plan

  • Code compiles successfully
  • No behavior change for valid integer environment variables
  • Graceful handling of invalid integer environment variables (returns default instead of UB)
  • Minimal code change, focused fix

Impact

  • Fixes potential undefined behavior when environment variables contain invalid integer values
  • Makes the function more robust and predictable
  • No breaking changes to API or behavior for valid inputs

🤖 Generated with Claude Code


🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.

## 📋 Pull Request Information **Original PR:** https://github.com/deepseek-ai/DeepGEMM/pull/286 **Author:** [@hobostay](https://github.com/hobostay) **Created:** 2/9/2026 **Status:** ❌ Closed **Base:** `main` ← **Head:** `fix/sscanf-return-value-check` --- ### 📝 Commits (1) - [`8774365`](https://github.com/deepseek-ai/DeepGEMM/commit/877436549176b51632301d8cc0d1bf9a953144fb) fix: add sscanf return value check to prevent uninitialized value return ### 📊 Changes **1 file changed** (+4 additions, -1 deletions) <details> <summary>View changed files</summary> 📝 `csrc/utils/system.hpp` (+4 -1) </details> ### 📄 Description ## Summary This PR fixes a potential bug in `csrc/utils/system.hpp` where the return value of `std::sscanf` was not being checked when parsing integer environment variables. ## Problem In the `get_env<int>()` function template, `std::sscanf` was 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), `sscanf` fails and the `value` variable 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 ```cpp // Before: Uninitialized value returned on sscanf failure } else if constexpr (std::is_same_v<dtype_t, int>) { int value; std::sscanf(c_str, "%d", &value); return value; } // After: Returns default value on parsing failure } else if constexpr (std::is_same_v<dtype_t, int>) { int value; if (std::sscanf(c_str, "%d", &value) != 1) { // Failed to parse as int, return default value return default_value; } return value; } ``` ## Test plan - [x] Code compiles successfully - [x] No behavior change for valid integer environment variables - [x] Graceful handling of invalid integer environment variables (returns default instead of UB) - [x] Minimal code change, focused fix ## Impact - Fixes potential undefined behavior when environment variables contain invalid integer values - Makes the function more robust and predictable - No breaking changes to API or behavior for valid inputs 🤖 Generated with [Claude Code](https://claude.com/claude-code) --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
bsakaguchi 2026-10-26 21:16:30 +00:00
Sign in to join this conversation.
No labels
pull-request
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
deepseek-ai/DeepGEMM#344
No description provided.