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 1 commit
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
16 changes: 11 additions & 5 deletions packages/builtin/array.pony
Original file line number Diff line number Diff line change
Expand Up @@ -128,13 +128,19 @@ class Array[A] is Seq[A]
"""
_size = len

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

_ptr = ptr
_ptr = ptr
end
jasoncarr0 marked this conversation as resolved.
Show resolved Hide resolved

fun _copy_to(
ptr: Pointer[this->A!],
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