Skip to content

Raise when Pointer#[]= would read past a sized source - #219

Open
adityawrk wants to merge 1 commit into
ruby:masterfrom
adityawrk:fix/pointer-source-bounds
Open

adityawrk wants to merge 1 commit into
ruby:masterfrom
adityawrk:fix/pointer-source-bounds

Conversation

@adityawrk

@adityawrk adityawrk commented Nov 29, 2026 •

Copy link
Copy Markdown

Fiddle::Pointer#[]= copied the requested length from a pointer or string without looking at the source size. A 4-byte pointer used as the source of a 16-byte copy was read past its allocation:

backing = Fiddle::Pointer.malloc(16, Fiddle::RUBY_FREE)
backing[0, 16] = "AAAABBBBCCCCDDDD"
src = Fiddle::Pointer.new(backing.to_i, 4) # size is 4
dst = Fiddle::Pointer.malloc(16, Fiddle::RUBY_FREE)
dst[0, 16] = src
dst[0, 16] #=> "AAAABBBBCCCCDDDD"

The same call now raises IndexError: copy length (16) exceeds source pointer size (4) and leaves the destination unchanged. A copy that fits the source still works (dst[0, 4] = src copies "AAAA").

A pointer whose size is 0, and an integer used as a raw address, are still copied as before, because those sources have no known length. The same check is in the C extension and the FFI backend. A string source shorter than the requested length raises the same way.

Fixes #214.

Fiddle::Pointer#[]= copied the requested length from a pointer or
string without checking the source size, so dst[0, 16] = src read
past a 4-byte allocation.

Reject that copy when the source size is known and shorter than the
requested length. Unsized pointers and raw addresses are unchanged.

This branch has not been deployed

No deployments
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.

Fiddle::Pointer#[]= copies beyond the source pointer size

1 participant