Calculate num_strings based on all dimensions for OrtStringViewTensor…#1038
Open
bhkumar007 wants to merge 1 commit intomicrosoft:mainfrom
Open
Calculate num_strings based on all dimensions for OrtStringViewTensor…#1038bhkumar007 wants to merge 1 commit intomicrosoft:mainfrom
bhkumar007 wants to merge 1 commit intomicrosoft:mainfrom
Conversation
…Storage
Summary
OrtStringViewTensorStorage computes num_strings using only the first shape dimension (shape[0]) instead of the product of all dimensions. This causes a heap buffer overflow when processing string tensors with rank ≥ 2.
Bug
In custom_op_lite.h, the OrtStringViewTensorStorage constructor calculates the number of strings as:
size_t num_strings = 1;
if ((*shape_).size() > 0) {
num_strings = static_cast<size_t>((*shape_)[0]); // ← only first dimension
}
This under-counts the total number of strings for any tensor with rank ≥ 2. The under-sized offsets vector is then passed to GetStringTensorContent, which writes beyond its bounds.
For example, a string tensor with shape [2, 3] contains 6 strings, but this code allocates offsets for only 2.
Note: the sibling class OrtStringTensorStorage computes this correctly by multiplying all dimensions.
Fix
Replace the partial dimension calculation with a simple loop over all shape dimensions:
While String tensors in ONNX are often rank 1 in practice, The ORT API doesn't care about intent. GetStringTensorContent expects offsets_count to equal the actual total number of strings in the tensor. If ORT has a [2, 3] tensor with 6 strings and you pass an offsets buffer of size 2, it writes 6 entries into a 2-slot buffer. There's no graceful "I only want the first dimension's worth" mode — it's an unconditional overflow. Also, the sibling class OrtStringTensorStorage handles all dimensions, and both classes implement the same IStringTensorStorage interface — callers have no way to know which backing class they're hitting
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
OrtStringViewTensorStoragecomputesnum_stringsusing only the first shape dimension (shape[0]) instead of the product of all dimensions. This causes a heap buffer overflow when processing string tensors with rank ≥ 2.Bug
In
custom_op_lite.h, theOrtStringViewTensorStorageconstructor calculates the number of strings as:This under-counts the total number of strings for any tensor with rank ≥ 2. The under-sized offsets vector is then passed to
GetStringTensorContent, which writes beyond its bounds.For example, a string tensor with shape [2, 3] contains 6 strings, but this code allocates offsets for only 2.
Note: the sibling class
OrtStringTensorStoragecomputes this correctly by multiplying all dimensions.Fix
Replace the partial dimension calculation with a simple loop over all shape dimensions:
While String tensors in ONNX are often rank 1 in practice, The ORT API doesn't care about intent.
GetStringTensorContentexpectsoffsets_countto equal the actual total number of strings in the tensor. If ORT has a [2, 3] tensor with 6 strings and you pass an offsets buffer of size 2, it writes 6 entries into a 2-slot buffer. There's no graceful "I only want the first dimension's worth" mode — it's an unconditional overflow. Also, the sibling classOrtStringTensorStoragehandles all dimensions, and both classes implement the sameIStringTensorStorageinterface — callers have no way to know which backing class they're hitting