Skip to content

Conversation

@Fan-Yunfan
Copy link
Contributor

@Fan-Yunfan Fan-Yunfan commented Aug 8, 2025

Problem

The fmtstr_ function in the current implementation(cpp/tensorrt_llm/common/stringUtils.cpp) uses va_copy(args0, args) to duplicate the va_list for safe usage in vsnprintf. However, it does not call va_end(args0) to clean up the copied va_list, which can lead to:

  • Resource leaks on certain platforms where va_list requires explicit cleanup.

  • Undefined behavior (UB) per the C/C++ standard, since va_copy must always be paired with va_end.

image

Current Implementation

cpp/tensorrt_llm/common/stringUtils.cpp

void fmtstr_(char const* format, fmtstr_allocator alloc, void* target, va_list args)
{
    va_list args0;
    va_copy(args0, args);

    size_t constexpr init_size = 2048;
    char fixed_buffer[init_size];
    auto const size = std::vsnprintf(fixed_buffer, init_size, format, args0);
    TLLM_CHECK_WITH_INFO(size >= 0, std::string(std::strerror(errno)));
    if (size == 0)
    {
        return;
    }

    auto* memory = alloc(target, size);

    if (static_cast<size_t>(size) < init_size)
    {
        std::memcpy(memory, fixed_buffer, size + 1);
    }
    else
    {
        auto const size2 = std::vsnprintf(memory, size + 1, format, args);
        TLLM_CHECK_WITH_INFO(size2 == size, std::string(std::strerror(errno)));
    }
}

Solution

Add va_end(args0) after vsnprintf finishes using args0. This ensures:

  • Compliance with the C/C++ standard (va_copy + va_end pairing).

  • No resource leaks on platforms where va_list manages allocated memory (e.g., some ABIs for variadic functions).

void fmtstr_(char const* format, fmtstr_allocator alloc, void* target, va_list args)
{
    va_list args0;
    va_copy(args0, args);
    
    size_t constexpr init_size = 2048;
    char fixed_buffer[init_size];
    auto const size = std::vsnprintf(fixed_buffer, init_size, format, args0);
+   va_end(args0);  // Clean up copied va_list
    TLLM_CHECK_WITH_INFO(size >= 0, std::string(std::strerror(errno)));
    // ... rest unchanged ...
}

Summary by CodeRabbit

  • Bug Fixes

    • Safer handling of variable-argument string formatting to prevent double-cleanup and lifetime issues.
    • Hardened fallback formatting paths to reduce crashes and incorrect output in edge cases.
    • Improved stability and reliability for runtime string formatting.
  • Refactor

    • Internal resource lifetime management reorganized for more robust and maintainable formatting logic.

@coderabbitai
Copy link
Contributor

coderabbitai bot commented Aug 8, 2025

📝 Walkthrough

Walkthrough

Adds an RAII va_list_guard (anonymous namespace) to manage va_list in fmtstr(). fmtstr_ still copies the incoming va_list for the initial vsnprintf, calls va_end on that copy, and uses the original va_list for fallback. Public signatures unchanged.

Changes

Cohort / File(s) Change Summary
Header: RAII wrapper
cpp/include/tensorrt_llm/common/stringUtils.h
Added an anonymous-namespace va_list_guard RAII type that holds a va_list&, calls va_end in its destructor, is non-copyable/non-movable, and is constructed in fmtstr() so va_end is performed automatically; public signatures unchanged.
Implementation: va_list lifetime handling
cpp/tensorrt_llm/common/stringUtils.cpp
fmtstr_ continues to va_copy into a local args0 for the initial vsnprintf and now explicitly calls va_end(args0) after that call. Fallback vsnprintf still uses the original va_list (now managed by the header's guard). No public API changes.

Sequence Diagram(s)

sequenceDiagram
    participant Caller
    participant fmtstr
    participant fmtstr_
    participant vsnprintf

    Caller->>fmtstr: call fmtstr(format, ...)
    fmtstr->>fmtstr: va_start(args)\nconstruct va_list_guard(args)
    fmtstr->>fmtstr_: call fmtstr_(format, alloc, target, args)
    fmtstr_->>fmtstr_: va_copy(args0, args)\nvsnprintf(..., args0)\nva_end(args0)
    alt need more space
        fmtstr_->>vsnprintf: vsnprintf(..., args)
    end
    fmtstr->>fmtstr: va_list_guard destructor -> va_end(args)
    fmtstr->>Caller: return result
Loading

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~8 minutes


📜 Recent review details

Configuration used: .coderabbit.yaml
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 287f6c2 and b6b2c7a.

📒 Files selected for processing (1)
  • cpp/tensorrt_llm/common/stringUtils.cpp (1 hunks)
🚧 Files skipped from review as they are similar to previous changes (1)
  • cpp/tensorrt_llm/common/stringUtils.cpp
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (1)
  • GitHub Check: Pre-commit Check
✨ Finishing Touches
  • 📝 Generate Docstrings
🧪 Generate unit tests
  • Create PR with unit tests
  • Post copyable unit tests in a comment

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.

Support

Need help? Create a ticket on our support page for assistance with any issues or questions.

CodeRabbit Commands (Invoked using PR/Issue comments)

Type @coderabbitai help to get the list of available commands.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai or @coderabbitai title anywhere in the PR title to generate the title automatically.

Status, Documentation and Community

  • Visit our Status Page to check the current availability of CodeRabbit.
  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

@Fan-Yunfan Fan-Yunfan changed the title [None][Fix] Fix: Add missing va_end(args0) in fmtstr_ to prevent potential resource leak [None][fix] Fix: Add missing va_end(args0) in fmtstr_ to prevent potential resource leak Aug 8, 2025
Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 0

🔭 Outside diff range comments (1)
cpp/tensorrt_llm/common/stringUtils.cpp (1)

46-55: Document fmtstr_allocator’s null-terminator contract
The default allocator (std::string::resize(count)) guarantees storage for count characters plus a trailing '\0', so callers of alloc(target, n) get at least n+1 bytes. Make this explicit in the hot path to prevent future one-byte overflows:

File: cpp/tensorrt_llm/common/stringUtils.cpp
Lines: 46

-    auto* memory = alloc(target, size);
+    // alloc(target, size) is required to return a buffer of at least (size + 1) bytes
+    // so we can safely write size characters plus the null terminator.
+    auto* memory = alloc(target, size);
🧹 Nitpick comments (1)
cpp/tensorrt_llm/common/stringUtils.cpp (1)

2-2: Nit: Update copyright year

Guidelines require headers to include the current year. Consider updating to 2025.

- * Copyright (c) 2022-2024, NVIDIA CORPORATION.  All rights reserved.
+ * Copyright (c) 2022-2025, NVIDIA CORPORATION.  All rights reserved.
📜 Review details

Configuration used: .coderabbit.yaml
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between d45236b and 0bb3e99.

📒 Files selected for processing (1)
  • cpp/tensorrt_llm/common/stringUtils.cpp (1 hunks)
🧰 Additional context used
📓 Path-based instructions (2)
**/*.{cpp,h,hpp,cc,cxx}

📄 CodeRabbit Inference Engine (CODING_GUIDELINES.md)

**/*.{cpp,h,hpp,cc,cxx}: Closing braces of namespaces should have a comment saying the namespace it closes (e.g., } // namespace foo).
Prefer const or constexpr variables over #defines whenever possible.
A variable that is not modified after its initialization should be declared as const.
Except 0 (used for checking signness/existence/emptiness), nullptr, true, false, all other literals should only be used for variable initialization.
Use the Allman indentation style for braces in C++ code.
Put the semicolon for an empty for or while loop in a new line.
The statement forming the body of a switch, while, do..while, or for statement shall be a compound statement (use brace-delimited statements).
If and else should always be followed by brace-delimited statements, even if empty or a single statement.
C++ filenames should use camel case with the first letter lowercase (e.g., thisIsAFilename.cpp), and all files involved in a compilation target must have case-insensitive unique filenames.
All types (including class names) should use camel case with uppercase first letter (e.g., FooBarClass).
Local variables, methods, and namespaces should use camel case with first letter lowercase (e.g., localFooBar).
Non-magic-number global variables that are non-static and not defined in anonymous namespace should use camel case prefixed by 'g' (e.g., gDontUseGlobalFoos).
Non-magic-number global variables that are static or defined in an anonymous namespace should use camel case prefixed by 's' (e.g., sMutableStaticGlobal).
Locally visible static variables should use camel case with lowercase prefix 's' as the first letter (e.g., static std::once_flag sFlag;).
Class member variables should use camel case prefixed with 'm' (e.g., mNbFooValues). Public member variables do not require the 'm' prefix but it is encouraged for clarity.
Enumerations, global constants, static constants at class-scope, and function-scope magic-number/literal constants should be uppercase snake case with prefix...

Files:

  • cpp/tensorrt_llm/common/stringUtils.cpp
**/*.{cpp,h,hpp,cc,cxx,cu,py}

📄 CodeRabbit Inference Engine (CODING_GUIDELINES.md)

All TensorRT-LLM Open Source Software code should contain an NVIDIA copyright header that includes the current year. This includes .cpp, .h, .cu, .py, and any other source files which are compiled or interpreted.

Files:

  • cpp/tensorrt_llm/common/stringUtils.cpp
🧠 Learnings (3)
📓 Common learnings
Learnt from: fyf2016
PR: NVIDIA/TensorRT-LLM#6679
File: cpp/include/tensorrt_llm/common/logger.h:63-68
Timestamp: 2025-08-07T01:45:23.301Z
Learning: In C++ variadic template functions, the compiler implicitly inserts positional placeholders for template parameter packs during __attribute__((format(printf, ...))) processing, causing an offset in parameter numbering. The format string parameter index should account for this template expansion mechanism rather than the nominal parameter position.
📚 Learning: 2025-08-07T01:45:23.301Z
Learnt from: fyf2016
PR: NVIDIA/TensorRT-LLM#6679
File: cpp/include/tensorrt_llm/common/logger.h:63-68
Timestamp: 2025-08-07T01:45:23.301Z
Learning: In C++ variadic template functions, the compiler implicitly inserts positional placeholders for template parameter packs during __attribute__((format(printf, ...))) processing, causing an offset in parameter numbering. The format string parameter index should account for this template expansion mechanism rather than the nominal parameter position.

Applied to files:

  • cpp/tensorrt_llm/common/stringUtils.cpp
📚 Learning: 2025-08-07T01:45:23.301Z
Learnt from: fyf2016
PR: NVIDIA/TensorRT-LLM#6679
File: cpp/include/tensorrt_llm/common/logger.h:63-68
Timestamp: 2025-08-07T01:45:23.301Z
Learning: In GCC/Clang, printf format attributes with C++ variadic templates use the same parameter numbering as C-style variadics: parameter 1 is the format string, parameter 2 is where the template parameter pack begins. There are no implicit positional placeholders that offset the numbering. The syntax is __attribute__((format(printf, fmt_idx, args_idx))) where fmt_idx and args_idx are 1-based indices.

Applied to files:

  • cpp/tensorrt_llm/common/stringUtils.cpp
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (1)
  • GitHub Check: Pre-commit Check
🔇 Additional comments (1)
cpp/tensorrt_llm/common/stringUtils.cpp (1)

37-39: Correct: Pair va_copy with va_end to avoid UB/resource leaks

Good fix. This correctly matches the va_copy(args0, args) with va_end(args0) and prevents UB on ABIs where va_list requires cleanup. Placement right after the first vsnprintf ensures cleanup even on error paths.

@Fan-Yunfan
Copy link
Contributor Author

Fan-Yunfan commented Aug 8, 2025

Dear @tongyuantongyu ,I noticed that you have made some related modifications to the stringUtils.cpp file (a139eae), so I was wondering if you could take a look at this PR I submitted when you have some free time?

image

By the way, I have another PR(https://github.com/NVIDIA/TensorRT-LLM/pull/6723) that needs your review.

@tongyuantongyu
Copy link
Member

Thank you for the contribution. Can you fix the issue in fmtstr as well?

Notice that if fmtstr_ throws, va_end will never be called.

@Fan-Yunfan
Copy link
Contributor Author

Fan-Yunfan commented Aug 11, 2025

Thank you for the contribution. Can you fix the issue in fmtstr as well?

Notice that if fmtstr_ throws, va_end will never be called.

Of course, I'll update the code later tonight to fix this part. Thank you for your review ~

image

( By the way, I have another PR about the const modifier(#6679) that needs your review.

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 2

📜 Review details

Configuration used: .coderabbit.yaml
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 0bb3e99 and af5cc02.

📒 Files selected for processing (2)
  • cpp/include/tensorrt_llm/common/stringUtils.h (4 hunks)
  • cpp/tensorrt_llm/common/stringUtils.cpp (2 hunks)
🧰 Additional context used
📓 Path-based instructions (3)
**/*.{cpp,h,hpp,cc,cxx}

📄 CodeRabbit Inference Engine (CODING_GUIDELINES.md)

**/*.{cpp,h,hpp,cc,cxx}: Closing braces of namespaces should have a comment saying the namespace it closes (e.g., } // namespace foo).
Prefer const or constexpr variables over #defines whenever possible.
A variable that is not modified after its initialization should be declared as const.
Except 0 (used for checking signness/existence/emptiness), nullptr, true, false, all other literals should only be used for variable initialization.
Use the Allman indentation style for braces in C++ code.
Put the semicolon for an empty for or while loop in a new line.
The statement forming the body of a switch, while, do..while, or for statement shall be a compound statement (use brace-delimited statements).
If and else should always be followed by brace-delimited statements, even if empty or a single statement.
C++ filenames should use camel case with the first letter lowercase (e.g., thisIsAFilename.cpp), and all files involved in a compilation target must have case-insensitive unique filenames.
All types (including class names) should use camel case with uppercase first letter (e.g., FooBarClass).
Local variables, methods, and namespaces should use camel case with first letter lowercase (e.g., localFooBar).
Non-magic-number global variables that are non-static and not defined in anonymous namespace should use camel case prefixed by 'g' (e.g., gDontUseGlobalFoos).
Non-magic-number global variables that are static or defined in an anonymous namespace should use camel case prefixed by 's' (e.g., sMutableStaticGlobal).
Locally visible static variables should use camel case with lowercase prefix 's' as the first letter (e.g., static std::once_flag sFlag;).
Class member variables should use camel case prefixed with 'm' (e.g., mNbFooValues). Public member variables do not require the 'm' prefix but it is encouraged for clarity.
Enumerations, global constants, static constants at class-scope, and function-scope magic-number/literal constants should be uppercase snake case with prefix...

Files:

  • cpp/include/tensorrt_llm/common/stringUtils.h
  • cpp/tensorrt_llm/common/stringUtils.cpp
**/*.{h,hpp}

📄 CodeRabbit Inference Engine (CODING_GUIDELINES.md)

Use a preprocessor guard in header files. The guard name must have prefix TRTLLM_ followed by the filename, all in caps, and no trailing underscore.

Files:

  • cpp/include/tensorrt_llm/common/stringUtils.h
**/*.{cpp,h,hpp,cc,cxx,cu,py}

📄 CodeRabbit Inference Engine (CODING_GUIDELINES.md)

All TensorRT-LLM Open Source Software code should contain an NVIDIA copyright header that includes the current year. This includes .cpp, .h, .cu, .py, and any other source files which are compiled or interpreted.

Files:

  • cpp/include/tensorrt_llm/common/stringUtils.h
  • cpp/tensorrt_llm/common/stringUtils.cpp
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (1)
  • GitHub Check: Pre-commit Check
🔇 Additional comments (5)
cpp/include/tensorrt_llm/common/stringUtils.h (3)

77-109: Excellent RAII implementation for va_list management.

The va_list_wrapper class properly addresses the resource leak issue by ensuring va_end() is always called via destructor. The design is solid:

  • Two constructors handle both va_start and va_copy scenarios
  • Non-copyable to prevent double-free issues
  • RAII ensures cleanup even with exceptions
  • get() method provides access to underlying va_list

139-139: Function signature correctly updated for RAII approach.

The change from va_list args to va_list_wrapper& args aligns with the RAII design and ensures proper resource management.


151-165: Clean integration of va_list_wrapper in fmtstr().

The function now properly constructs the wrapper and passes it by reference to fmtstr_(). The RAII wrapper eliminates the need for explicit va_end() calls, making the code more exception-safe.

cpp/tensorrt_llm/common/stringUtils.cpp (2)

29-29: Function signature correctly updated to match header.

The parameter change to va_list_wrapper& args is consistent with the header declaration and RAII approach.


35-35: Proper usage of wrapper in first vsnprintf call.

Using args0.get() correctly accesses the underlying va_list from the wrapper.

@Fan-Yunfan Fan-Yunfan changed the title [None][fix] Fix: Add missing va_end(args0) in fmtstr_ to prevent potential resource leak [None][fix] Fix: Using RAII to automatically manage the allocation and release of va_list for potential resource leak Aug 11, 2025
@Fan-Yunfan
Copy link
Contributor Author

Fan-Yunfan commented Aug 11, 2025

Thank you for the contribution. Can you fix the issue in fmtstr as well?

Notice that if fmtstr_ throws, va_end will never be called.

Dear @tongyuantongyu,

Thank you for raising the issue regarding fmtstr_ throws, va_end will never be called. This was something I initially overlooked. The fix for this issue has now been completed, and you can find the details in the Files changed section. Below, I’ll briefly explain my solution and some of the reasoning behind it.

Solution:

I employed the RAII (Resource Acquisition Is Initialization) mechanism to automatically manage the allocation and deallocation of args (in stringUtils.h and stringUtils.cpp) and its copy args0 (in stringUtils.cpp). Compared to using large try-catch blocks, the RAII approach is more elegant in design—it delegates resource management to the class itself, reducing the likelihood of human error (e.g., forgetting to release resources).

Key Changes & Design Philosophy:

  1. Created the va_list_wrapper class within an anonymous namespace to implement RAII while avoiding namespace pollution.

  2. Modified the declaration of the fmtstr_ function from void fmtstr_(..., va_list args) to void fmtstr_(..., va_list_wrapper& args_wrapper) to ensure that the lifecycle of args is automatically managed by RAII inside fmtstr_.

  3. Inside fmtstr_, I used args_wrapper.get() twice instead of storing it in a variable (e.g., args) first. This decision was based on the following considerations:

    • Avoiding unnecessary variable scope expansion: A temporary variable like args would extend its lifetime to the entire function, even though it’s only needed locally.

    • Explicitly conveying intent: Each call to get() clearly indicates that the latest state is being used (which is particularly important for objects like va_list that may be modified).

Let me know if you have any further questions or suggestions!

By the way, would you mind reviewing another PR(https://github.com/NVIDIA/TensorRT-LLM/pull/6679)? (This one may not touch areas you've previously worked on)

… va_list

Format: Code style alignment

Signed-off-by: fanyunfan <[email protected]>
Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 0

🧹 Nitpick comments (1)
cpp/include/tensorrt_llm/common/stringUtils.h (1)

135-135: Consider marking the function parameter as const reference.

Since fmtstr_ only needs to read from the va_list (via va_copy), the parameter could be marked as const va_list_guard& to better express the intent and prevent accidental modifications.

-void fmtstr_(char const* format, fmtstr_allocator alloc, void* target, va_list_guard& args_guard);
+void fmtstr_(char const* format, fmtstr_allocator alloc, void* target, const va_list_guard& args_guard);

Note: If you apply this change, you'll also need to add a const version of the get() method in the va_list_guard class:

    va_list& get()
    {
        return args_;
    }
+
+   const va_list& get() const
+   {
+       return args_;
+   }
📜 Review details

Configuration used: .coderabbit.yaml
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 1e6a6ca and 5360c90.

📒 Files selected for processing (2)
  • cpp/include/tensorrt_llm/common/stringUtils.h (4 hunks)
  • cpp/tensorrt_llm/common/stringUtils.cpp (2 hunks)
🧰 Additional context used
📓 Path-based instructions (3)
**/*.{cpp,h,hpp,cc,cxx}

📄 CodeRabbit Inference Engine (CODING_GUIDELINES.md)

**/*.{cpp,h,hpp,cc,cxx}: Closing braces of namespaces should have a comment saying the namespace it closes (e.g., } // namespace foo).
Prefer const or constexpr variables over #defines whenever possible.
A variable that is not modified after its initialization should be declared as const.
Except 0 (used for checking signness/existence/emptiness), nullptr, true, false, all other literals should only be used for variable initialization.
Use the Allman indentation style for braces in C++ code.
Put the semicolon for an empty for or while loop in a new line.
The statement forming the body of a switch, while, do..while, or for statement shall be a compound statement (use brace-delimited statements).
If and else should always be followed by brace-delimited statements, even if empty or a single statement.
C++ filenames should use camel case with the first letter lowercase (e.g., thisIsAFilename.cpp), and all files involved in a compilation target must have case-insensitive unique filenames.
All types (including class names) should use camel case with uppercase first letter (e.g., FooBarClass).
Local variables, methods, and namespaces should use camel case with first letter lowercase (e.g., localFooBar).
Non-magic-number global variables that are non-static and not defined in anonymous namespace should use camel case prefixed by 'g' (e.g., gDontUseGlobalFoos).
Non-magic-number global variables that are static or defined in an anonymous namespace should use camel case prefixed by 's' (e.g., sMutableStaticGlobal).
Locally visible static variables should use camel case with lowercase prefix 's' as the first letter (e.g., static std::once_flag sFlag;).
Class member variables should use camel case prefixed with 'm' (e.g., mNbFooValues). Public member variables do not require the 'm' prefix but it is encouraged for clarity.
Enumerations, global constants, static constants at class-scope, and function-scope magic-number/literal constants should be uppercase snake case with prefix...

Files:

  • cpp/include/tensorrt_llm/common/stringUtils.h
  • cpp/tensorrt_llm/common/stringUtils.cpp
**/*.{h,hpp}

📄 CodeRabbit Inference Engine (CODING_GUIDELINES.md)

Use a preprocessor guard in header files. The guard name must have prefix TRTLLM_ followed by the filename, all in caps, and no trailing underscore.

Files:

  • cpp/include/tensorrt_llm/common/stringUtils.h
**/*.{cpp,h,hpp,cc,cxx,cu,py}

📄 CodeRabbit Inference Engine (CODING_GUIDELINES.md)

All TensorRT-LLM Open Source Software code should contain an NVIDIA copyright header that includes the current year. This includes .cpp, .h, .cu, .py, and any other source files which are compiled or interpreted.

Files:

  • cpp/include/tensorrt_llm/common/stringUtils.h
  • cpp/tensorrt_llm/common/stringUtils.cpp
🧠 Learnings (1)
📓 Common learnings
Learnt from: fyf2016
PR: NVIDIA/TensorRT-LLM#6679
File: cpp/include/tensorrt_llm/common/logger.h:63-68
Timestamp: 2025-08-07T01:45:23.301Z
Learning: In C++ variadic template functions, the compiler implicitly inserts positional placeholders for template parameter packs during __attribute__((format(printf, ...))) processing, causing an offset in parameter numbering. The format string parameter index should account for this template expansion mechanism rather than the nominal parameter position.
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (1)
  • GitHub Check: Pre-commit Check
🔇 Additional comments (5)
cpp/include/tensorrt_llm/common/stringUtils.h (2)

77-104: LGTM! Well-implemented RAII wrapper for va_list management.

The va_list_guard class correctly manages the lifetime of a va_list using RAII principles. The implementation properly:

  • Takes a reference to avoid copying the va_list
  • Calls va_end in the destructor for automatic cleanup
  • Prevents copying and moving to maintain single ownership
  • Deletes the rvalue get() to prevent use-after-move scenarios

143-165: Excellent exception-safe implementation using RAII.

The refactored fmtstr function properly leverages the va_list_guard wrapper to ensure exception safety. The wrapper's destructor will automatically call va_end even if fmtstr_ throws an exception, addressing the reviewer's concern about exception safety.

cpp/tensorrt_llm/common/stringUtils.cpp (3)

29-38: Well-implemented automatic cleanup for duplicated va_list.

The function correctly:

  1. Creates a copy of the va_list using va_copy
  2. Wraps it in a va_list_guard for automatic cleanup
  3. Uses the guard's get() method to access the underlying va_list

This ensures that va_end is called on args0 when args0_guard goes out of scope, fixing the original resource leak issue.


53-54: Correct usage of the original va_list through the guard.

The code properly uses args_guard.get() to access the original va_list for the second vsnprintf call when the buffer needs to be reallocated. The RAII wrapper ensures proper cleanup regardless of the execution path.


76-76: Missing comment for namespace closing brace.

According to the coding guidelines, closing braces of namespaces should have a comment indicating the namespace being closed.

-} // namespace tensorrt_llm::common
+} // namespace tensorrt_llm::common

Wait, the comment is already there. This is correct.

@tongyuantongyu
Copy link
Member

/bot run

@tensorrt-cicd
Copy link
Collaborator

PR_Github #15005 [ run ] triggered by Bot

@tensorrt-cicd
Copy link
Collaborator

PR_Github #15005 [ run ] completed with state SUCCESS
/LLM/main/L0_MergeRequest_PR pipeline #11334 completed with status: 'FAILURE'

@Fan-Yunfan
Copy link
Contributor Author

/bot run

Thank you for reviewing my code even though it's so late. I really appreciate it.

@tongyuantongyu
Copy link
Member

/bot run

@tensorrt-cicd
Copy link
Collaborator

PR_Github #15048 [ run ] triggered by Bot

@tensorrt-cicd
Copy link
Collaborator

PR_Github #15048 [ run ] completed with state SUCCESS
/LLM/main/L0_MergeRequest_PR pipeline #11366 completed with status: 'FAILURE'

@tongyuantongyu
Copy link
Member

/bot run

@tensorrt-cicd
Copy link
Collaborator

PR_Github #15209 [ run ] triggered by Bot

@tongyuantongyu
Copy link
Member

/bot run

@tensorrt-cicd
Copy link
Collaborator

PR_Github #15272 [ run ] triggered by Bot

@tensorrt-cicd
Copy link
Collaborator

PR_Github #15272 [ run ] completed with state SUCCESS
/LLM/main/L0_MergeRequest_PR pipeline #11530 completed with status: 'FAILURE'

@tongyuantongyu
Copy link
Member

/bot run

@tensorrt-cicd
Copy link
Collaborator

PR_Github #15377 [ run ] triggered by Bot

@tensorrt-cicd
Copy link
Collaborator

PR_Github #15377 [ run ] completed with state SUCCESS
/LLM/main/L0_MergeRequest_PR pipeline #11592 completed with status: 'SUCCESS'
Pipeline passed with automatic retried tests. Check the rerun report for details.

@tongyuantongyu tongyuantongyu merged commit 22d59a6 into NVIDIA:main Aug 16, 2025
4 checks passed
dominicshanshan pushed a commit to dominicshanshan/TensorRT-LLM that referenced this pull request Aug 17, 2025
…ease of va_list for potential resource leak (NVIDIA#6758)

Signed-off-by: fanyunfan <[email protected]>
Co-authored-by: fanyunfan <[email protected]>
Co-authored-by: Yunfan Fan <[email protected]>
Co-authored-by: Yuan Tong <[email protected]>
dominicshanshan pushed a commit to dominicshanshan/TensorRT-LLM that referenced this pull request Aug 17, 2025
…ease of va_list for potential resource leak (NVIDIA#6758)

Signed-off-by: fanyunfan <[email protected]>
Co-authored-by: fanyunfan <[email protected]>
Co-authored-by: Yunfan Fan <[email protected]>
Co-authored-by: Yuan Tong <[email protected]>
Signed-off-by: Wangshanshan <[email protected]>
dominicshanshan pushed a commit to dominicshanshan/TensorRT-LLM that referenced this pull request Aug 17, 2025
…ease of va_list for potential resource leak (NVIDIA#6758)

Signed-off-by: fanyunfan <[email protected]>
Co-authored-by: fanyunfan <[email protected]>
Co-authored-by: Yunfan Fan <[email protected]>
Co-authored-by: Yuan Tong <[email protected]>
Signed-off-by: Wangshanshan <[email protected]>
dominicshanshan pushed a commit to dominicshanshan/TensorRT-LLM that referenced this pull request Aug 17, 2025
…ease of va_list for potential resource leak (NVIDIA#6758)

Signed-off-by: fanyunfan <[email protected]>
Co-authored-by: fanyunfan <[email protected]>
Co-authored-by: Yunfan Fan <[email protected]>
Co-authored-by: Yuan Tong <[email protected]>
dominicshanshan pushed a commit to dominicshanshan/TensorRT-LLM that referenced this pull request Aug 17, 2025
…ease of va_list for potential resource leak (NVIDIA#6758)

Signed-off-by: fanyunfan <[email protected]>
Co-authored-by: fanyunfan <[email protected]>
Co-authored-by: Yunfan Fan <[email protected]>
Co-authored-by: Yuan Tong <[email protected]>
Signed-off-by: Wangshanshan <[email protected]>
dominicshanshan pushed a commit to dominicshanshan/TensorRT-LLM that referenced this pull request Aug 17, 2025
…ease of va_list for potential resource leak (NVIDIA#6758)

Signed-off-by: fanyunfan <[email protected]>
Co-authored-by: fanyunfan <[email protected]>
Co-authored-by: Yunfan Fan <[email protected]>
Co-authored-by: Yuan Tong <[email protected]>
dominicshanshan pushed a commit to dominicshanshan/TensorRT-LLM that referenced this pull request Aug 18, 2025
…ease of va_list for potential resource leak (NVIDIA#6758)

Signed-off-by: fanyunfan <[email protected]>
Co-authored-by: fanyunfan <[email protected]>
Co-authored-by: Yunfan Fan <[email protected]>
Co-authored-by: Yuan Tong <[email protected]>
Signed-off-by: Wangshanshan <[email protected]>
dominicshanshan pushed a commit to dominicshanshan/TensorRT-LLM that referenced this pull request Aug 18, 2025
…ease of va_list for potential resource leak (NVIDIA#6758)

Signed-off-by: fanyunfan <[email protected]>
Co-authored-by: fanyunfan <[email protected]>
Co-authored-by: Yunfan Fan <[email protected]>
Co-authored-by: Yuan Tong <[email protected]>
dominicshanshan pushed a commit to dominicshanshan/TensorRT-LLM that referenced this pull request Aug 18, 2025
…ease of va_list for potential resource leak (NVIDIA#6758)

Signed-off-by: fanyunfan <[email protected]>
Co-authored-by: fanyunfan <[email protected]>
Co-authored-by: Yunfan Fan <[email protected]>
Co-authored-by: Yuan Tong <[email protected]>
Signed-off-by: Wangshanshan <[email protected]>
dominicshanshan pushed a commit to dominicshanshan/TensorRT-LLM that referenced this pull request Aug 18, 2025
…ease of va_list for potential resource leak (NVIDIA#6758)

Signed-off-by: fanyunfan <[email protected]>
Co-authored-by: fanyunfan <[email protected]>
Co-authored-by: Yunfan Fan <[email protected]>
Co-authored-by: Yuan Tong <[email protected]>
Signed-off-by: Wangshanshan <[email protected]>
dominicshanshan pushed a commit to dominicshanshan/TensorRT-LLM that referenced this pull request Aug 18, 2025
…ease of va_list for potential resource leak (NVIDIA#6758)

Signed-off-by: fanyunfan <[email protected]>
Co-authored-by: fanyunfan <[email protected]>
Co-authored-by: Yunfan Fan <[email protected]>
Co-authored-by: Yuan Tong <[email protected]>
Signed-off-by: Wangshanshan <[email protected]>
dominicshanshan pushed a commit to dominicshanshan/TensorRT-LLM that referenced this pull request Aug 18, 2025
…ease of va_list for potential resource leak (NVIDIA#6758)

Signed-off-by: fanyunfan <[email protected]>
Co-authored-by: fanyunfan <[email protected]>
Co-authored-by: Yunfan Fan <[email protected]>
Co-authored-by: Yuan Tong <[email protected]>
Signed-off-by: Wangshanshan <[email protected]>
dominicshanshan pushed a commit to dominicshanshan/TensorRT-LLM that referenced this pull request Aug 18, 2025
…ease of va_list for potential resource leak (NVIDIA#6758)

Signed-off-by: fanyunfan <[email protected]>
Co-authored-by: fanyunfan <[email protected]>
Co-authored-by: Yunfan Fan <[email protected]>
Co-authored-by: Yuan Tong <[email protected]>
Signed-off-by: Wangshanshan <[email protected]>
dominicshanshan pushed a commit to dominicshanshan/TensorRT-LLM that referenced this pull request Aug 18, 2025
…ease of va_list for potential resource leak (NVIDIA#6758)

Signed-off-by: fanyunfan <[email protected]>
Co-authored-by: fanyunfan <[email protected]>
Co-authored-by: Yunfan Fan <[email protected]>
Co-authored-by: Yuan Tong <[email protected]>
Signed-off-by: Wangshanshan <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Community want to contribute PRs initiated from Community

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants