Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix Pointer#copy_to overflow on unsigned size and different target type #13269

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions spec/std/pointer_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,13 @@ describe "Pointer" do
p1.copy_to(p2 || p3, 4)
4.times { |i| p2[i].should eq(p1[i]) }
end

it "doesn't raise OverflowError on unsigned size and different target type" do
p1 = Pointer.malloc(4, 1)
p2 = Pointer.malloc(4, 0 || nil)
p1.copy_to(p2, 4_u32)
4.times { |i| p2[i].should eq(p1[i]) }
end
end

describe "move_from" do
Expand Down
3 changes: 2 additions & 1 deletion src/pointer.cr
Original file line number Diff line number Diff line change
Expand Up @@ -247,7 +247,8 @@ struct Pointer(T)
if self.class == source.class
Intrinsics.memcpy(self.as(Void*), source.as(Void*), bytesize(count), false)
else
while (count -= 1) >= 0
while count > 0
count &-= 1
self[count] = source[count]
end
end
Expand Down