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 memory safety problem with Array.from_cpointer #3675

Merged
merged 4 commits into from
Jan 27, 2021
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
3 changes: 3 additions & 0 deletions .release-notes/3675.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
## Fix memory safety problem with Array.from_cpointer
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Excellent title.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks!


Previously, the `from_cpointer` method in arrays would trust the user to pass a valid pointer. This created an issue where the user could pass a null pointer using `Pointer.create`, leading to a situation where memory safety could be violated by trying to access an element of the array. This change makes it safe to pass a null pointer to `from_cpointer`, which will create a zero-length array.
14 changes: 9 additions & 5 deletions packages/builtin/array.pony
Original file line number Diff line number Diff line change
Expand Up @@ -126,12 +126,16 @@ class Array[A] is Seq[A]
Create an array from a C-style pointer and length. The contents are not
copied.
"""
_size = len

if alloc > len then
_alloc = alloc
if ptr.is_null() then
_size = 0
_alloc = 0
else
_alloc = len
_size = len
if alloc > len then
_alloc = alloc
else
_alloc = len
end
end
jasoncarr0 marked this conversation as resolved.
Show resolved Hide resolved

_ptr = ptr
Expand Down
22 changes: 22 additions & 0 deletions packages/builtin_test/_test.pony
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ actor Main is TestList
test(_TestStringUnchop)
test(_TestStringRepeatStr)
test(_TestStringConcatOffsetLen)
test(_TestStringFromCPointer)
test(_TestSpecialValuesF32)
test(_TestSpecialValuesF64)
test(_TestArrayAppend)
Expand All @@ -68,6 +69,7 @@ actor Main is TestList
test(_TestArraySwapElements)
test(_TestArrayChop)
test(_TestArrayUnchop)
test(_TestArrayFromCPointer)
test(_TestMath128)
test(_TestRem)
test(_TestMod)
Expand Down Expand Up @@ -1275,6 +1277,16 @@ class iso _TestStringConcatOffsetLen is UnitTest
fun apply(h: TestHelper) =>
h.assert_eq[String](recover String.>concat("ABCD".values(), 1, 2) end, "BC")

class iso _TestStringFromCPointer is UnitTest
"""
Test creating a string from a pointer
"""
fun name(): String => "builtin/String.from_cpointer"

fun apply(h: TestHelper) =>
let str = String.from_cpointer(Pointer[U8], 1, 1)
h.assert_eq[USize](0, str.size())

class iso _TestArrayAppend is UnitTest
fun name(): String => "builtin/Array.append"

Expand Down Expand Up @@ -1681,6 +1693,16 @@ class iso _TestArrayUnchop is UnitTest
error
end

class iso _TestArrayFromCPointer is UnitTest
"""
Test creating an array from a pointer
"""
fun name(): String => "builtin/Array.from_cpointer"

fun apply(h: TestHelper) =>
let arr = Array[U8].from_cpointer(Pointer[U8], 1, 1)
h.assert_eq[USize](0, arr.size())

class iso _TestMath128 is UnitTest
"""
Test 128 bit integer math.
Expand Down