Skip to content

Fix snprintf buffer size in matxException#1136

Open
tbensonatl wants to merge 1 commit intomainfrom
bugfix/fix-snprintf-in-matx-exception
Open

Fix snprintf buffer size in matxException#1136
tbensonatl wants to merge 1 commit intomainfrom
bugfix/fix-snprintf-in-matx-exception

Conversation

@tbensonatl
Copy link
Collaborator

snprintf in the string-input version of the matxException constructor uses the size of the input error string rather than the pre-allocated output string buffer as its size argument. Fix this to use of the size of the output buffer. The constructor taking a const char * input already uses the correct size.

snprintf in the string-input version of the matxException constructor uses
the size of the input error string rather than the pre-allocated output string
buffer as its size argument. Fix this to use of the size of the output buffer.
The constructor taking a const char * input already uses the correct size.

Signed-off-by: Thomas Benson <tbenson@nvidia.com>
@tbensonatl tbensonatl self-assigned this Mar 10, 2026
@copy-pr-bot
Copy link

copy-pr-bot bot commented Mar 10, 2026

This pull request requires additional validation before any workflows can run on NVIDIA's runners.

Pull request vetters can view their responsibilities here.

Contributors can view more details about this message here.

@greptile-apps
Copy link
Contributor

greptile-apps bot commented Mar 10, 2026

Greptile Summary

This PR fixes a one-line bug in include/matx/core/error.h where the std::string overload of the matxException constructor passed s.size() (the length of the input error string) as the snprintf buffer size limit instead of sizeof(str) (the size of the output char str[400] buffer). The fix makes this constructor consistent with the const char * overload, which already correctly used sizeof(str).

Key changes:

  • snprintf(str, s.size(), ...)snprintf(str, sizeof(str), ...) in the std::string overload of matxException.

Impact of the original bug:

  • If s.size() was smaller than the formatted output, the message would be silently truncated to s.size() - 1 characters (or even to an empty string if s was empty).
  • If s.size() exceeded 400 (the actual buffer size), snprintf would still be safe because snprintf does not overflow — but the enforced limit would have been wrong and potentially misleading.
  • The fix ensures the full 400-character buffer is available for the formatted exception message in all cases.

Confidence Score: 5/5

  • This is a clear, minimal, correct bug fix with no side effects — safe to merge.
  • The change is a single-line targeted fix that corrects the snprintf size argument from the input string length to the output buffer size, exactly matching the already-correct const char * sibling constructor. There is no risk of regression, no behavioral change for callers, and no other files affected.
  • No files require special attention.

Important Files Changed

Filename Overview
include/matx/core/error.h Fixes snprintf buffer size argument in std::string overload of matxException constructor — was incorrectly passing input string length (s.size()) instead of the output buffer size (sizeof(str) = 400).

Flowchart

%%{init: {'theme': 'neutral'}}%%
flowchart TD
    A["MATX_THROW(error, str_arg)"] --> B{str_arg type?}
    B -- "const char *" --> C["matxException(error, const char*, file, line)"]
    B -- "std::string" --> D["matxException(error, const std::string&, file, line)"]

    C --> E["snprintf(str, sizeof(str), fmt, ...)\n✅ Correct — uses output buffer size (400)"]
    D --> F["snprintf(str, sizeof(str), fmt, ...)\n✅ Fixed — now uses output buffer size (400)\n❌ Before fix: used s.size() (input string length)"]

    E --> G["detail::printStackTrace(stack)"]
    F --> G
    G --> H["Exception ready: str + stack"]
Loading

Last reviewed commit: ef2ad6d

@tbensonatl
Copy link
Collaborator Author

/build

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants