Skip to content

Conversation

@yuslepukhin
Copy link
Member

@yuslepukhin yuslepukhin commented Sep 4, 2025

Description

OrtMemoryInfo has a name which is pointer.
With the recent changes a user can pass an arbitrary name and then deallocate the string
as the API does not require it. Make the name a std::strng to own it and refactor.

Motivation and Context

We will have a dangling pointer especially when it comes to other languages.
The way it used to work in the past, is that the string would be compared to an internal constant and that constant would be used then.

Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull Request Overview

This PR changes the name field in OrtMemoryInfo from a const char* to std::string to prevent dangling pointer issues when users pass arbitrary names that may be deallocated before the API requires them.

  • Updates the OrtMemoryInfo struct to use std::string for the name field instead of const char*
  • Refactors all code that accesses the name field to use .c_str() when C-string interface is needed
  • Adds input validation to API functions to prevent null pointer issues

Reviewed Changes

Copilot reviewed 8 out of 8 changed files in this pull request and generated 3 comments.

Show a summary per file
File Description
include/onnxruntime/core/framework/ortmemoryinfo.h Changes name field from const char* to std::string and updates comparison operations
onnxruntime/core/framework/allocator.cc Adds input validation and updates API functions to work with string names
onnxruntime/core/session/environment.cc Updates string comparison to use std::string equality
onnxruntime/core/framework/bfc_arena.cc Updates constructor call to use .c_str()
onnxruntime/core/session/lora_adapters.cc Updates string comparisons for memory info names
onnxruntime/test/framework/TestAllocatorManager.cc Updates constructor call to use .c_str()
onnxruntime/test/framework/allocator_test.cc Updates test assertion to use .c_str()
onnxruntime/test/framework/tensor_test.cc Updates test assertions to use .c_str() or direct string comparison

Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.

@yuslepukhin yuslepukhin merged commit 8ad0614 into main Sep 5, 2025
90 of 92 checks passed
@yuslepukhin yuslepukhin deleted the yuslepukhin/fix_dangling_ort_info_name branch September 5, 2025 22:50
snnn pushed a commit that referenced this pull request Sep 8, 2025
### Description
<!-- Describe your changes. -->
OrtMemoryInfo has a name which is pointer.
With the recent changes a user can pass an arbitrary name and then
deallocate the string
as the API does not require it. Make the name a `std::strng` to own it
and refactor.

### Motivation and Context
<!-- - Why is this change required? What problem does it solve?
- If it fixes an open issue, please link to the issue here. -->
We will have a dangling pointer especially when it comes to other
languages.
The way it used to work in the past, is that the string would be
compared to an internal constant and that constant would be used then.

(cherry picked from commit 8ad0614)
snnn pushed a commit that referenced this pull request Sep 8, 2025
### Description
<!-- Describe your changes. -->
OrtMemoryInfo has a name which is pointer.
With the recent changes a user can pass an arbitrary name and then
deallocate the string
as the API does not require it. Make the name a `std::strng` to own it
and refactor.

### Motivation and Context
<!-- - Why is this change required? What problem does it solve?
- If it fixes an open issue, please link to the issue here. -->
We will have a dangling pointer especially when it comes to other
languages.
The way it used to work in the past, is that the string would be
compared to an internal constant and that constant would be used then.

(cherry picked from commit 8ad0614)
snnn pushed a commit that referenced this pull request Sep 8, 2025
This PR cherry-picks the following PRs to the rel-1.23.0 branch:

* #25938
* #25957
* #25960
* #25968
* #25971

---------

Co-authored-by: Dmitri Smirnov <yuslepukhin@users.noreply.github.com>
Co-authored-by: Adrian Lizarraga <adlizarraga@microsoft.com>
Co-authored-by: Hariharan Seshadri <shariharan91@gmail.com>
@snnn
Copy link
Contributor

snnn commented Sep 8, 2025

This PR has been cherry-picked into the rel-1.23.0 branch in PR #25985. Removing the release:1.23.0 label.

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.

4 participants