Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Fix incorrect use of Vec::set_len in TensorBase::append#1384

Merged
robertknight merged 1 commit into
mainrobertknight/rten:mainfrom
append-fixrobertknight/rten:append-fixCopy head branch name to clipboard
Jul 26, 2026
Merged

Fix incorrect use of Vec::set_len in TensorBase::append#1384
robertknight merged 1 commit into
mainrobertknight/rten:mainfrom
append-fixrobertknight/rten:append-fixCopy head branch name to clipboard

Conversation

@robertknight

@robertknight robertknight commented Jul 26, 2026

Copy link
Copy Markdown
Owner

When TensorBase::append was used to extend a tensor along an axis other than zero, it could incorrectly extend the storage length to include uninitialized elements. It was hard to trigger a crash due to this because the elements are all Copy, but this is nevertheless UB.

If a tensor was allocated with capacity for (2, 4) and initial shape (2, 0) and then a new column was appended, the storage length would be expanded to 5, but the initialization status of the elements would be:

I U U U
I

Where I = initialized, U = uninitialized.

Fix this by initializing all new elements with a valid value, with a fast path for the case where the tensor is contiguous and we are extending along axis 0. In that case we can avoid initializing the new elements before copying data.

This fix does mean that elements may be written multiple times. In order to address that it would be necessary to replace Vec<T> with a new storage type which permits initialized and uninitialized elements to be interleaved, or use a tensor with MaybeUninit<T> storage and defer marking storage as initialized until the tensor is fully grown.

When `TensorBase::append` was used to extend a tensor along an axis other than
zero, it could incorrectly extend the storage length to include uninitialized
elements.

If a tensor was allocated with capacity for (2, 4) and initial shape (2,
0) and then a new column was appended, the storage length would be expanded to
5, but the initialization status of the elements would be:

```
I U U U
I
```

Where I = initialized, U = uninitialized.

Fix this by initializing all new elements with a valid value, with a fast path
for the case where the tensor is contiguous and we are extending along axis 0.
In that case we can avoid initializing the new elements before copying data.

This fix does mean that elements may be written multiple times. In order to
address that it would be necessary to replace `Vec<T>` with a new storage type
which permits initialized and uninitialized elements to be interleaved, or use a
tensor with `MaybeUninit<T>` storage and defer marking storage as initialized
until the tensor is fully grown.
@robertknight
robertknight merged commit 8a25c96 into main Jul 26, 2026
3 checks passed
@robertknight
robertknight deleted the append-fix branch July 26, 2026 08:37
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.

1 participant

Morty Proxy This is a proxified and sanitized view of the page, visit original site.