Skip to content

Reset capacity_ in ShareData/ShareDiff to prevent stale shared memory reuse#7096

Open
Chessing234 wants to merge 1 commit intoBVLC:masterfrom
Chessing234:fix/share-data-capacity-reset
Open

Reset capacity_ in ShareData/ShareDiff to prevent stale shared memory reuse#7096
Chessing234 wants to merge 1 commit intoBVLC:masterfrom
Chessing234:fix/share-data-capacity-reset

Conversation

@Chessing234
Copy link
Copy Markdown

Summary

  • After ShareData(other), data_ points to other's memory but capacity_ retains the old blob's value
  • If Reshape() is later called with a size ≤ the stale capacity_, it skips allocation (line 40: if (count_ > capacity_)) and silently reuses the shared pointer — corrupting the other blob's data
  • Setting capacity_ = 0 forces Reshape() to allocate fresh memory, consistent with how constructors initialize capacity_ (lines 66, 73)
  • Same fix applied to ShareDiff for the identical issue with diff_

Fixes #5439

Changes

  • src/caffe/blob.cpp: Add capacity_ = 0; after the pointer assignment in both ShareData() and ShareDiff()

Test plan

  • Existing unit tests pass (make runtest)
  • Verify Reshape() after ShareData() allocates new memory instead of reusing the shared pointer

🤖 Generated with Claude Code

… reuse

After ShareData(other), data_ points to other's memory but capacity_
retains the old blob's value. If Reshape() is later called with a size
<= the stale capacity_, it skips allocation and silently reuses the
shared pointer — corrupting other's data or causing use-after-free.

Setting capacity_ to 0 forces Reshape() to allocate fresh memory,
which is consistent with how capacity_ is initialized in constructors.

Fixes BVLC#5439

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
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.

ShareData() should reset capacity_

1 participant