Skip to content

Commit

Permalink
Fix is_null_terminated reading arbitrary memory (issue #1425) (#1429)
Browse files Browse the repository at this point in the history
When `_alloc` is equal to `_size`, the `is_null_terminated` method will
point to arbitrary memory when checking for the `0` byte. This PR makes
that method first check that `_alloc != _size` before reading the
`_size` byte of the `Pointer[U8]`.

Fixes #1425
  • Loading branch information
Perelandric authored and jemc committed Nov 23, 2016
1 parent 868138d commit c54d46f
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 1 deletion.
2 changes: 1 addition & 1 deletion packages/builtin/string.pony
Original file line number Diff line number Diff line change
Expand Up @@ -376,7 +376,7 @@ actor Main
which may be present earlier in the content of the string.
If you need a null-terminated copy of this string, use the clone method.
"""
(_alloc > 0) and (_ptr._apply(_size) == 0)
(_alloc > 0) and (_alloc != _size) and (_ptr._apply(_size) == 0)

fun utf32(offset: ISize): (U32, U8) ? =>
"""
Expand Down
7 changes: 7 additions & 0 deletions packages/builtin_test/_test.pony
Original file line number Diff line number Diff line change
Expand Up @@ -437,6 +437,13 @@ class iso _TestStringIsNullTerminated is UnitTest
h.assert_true("0123456".trim(2, 4).clone().is_null_terminated())
h.assert_false("0123456".trim(2, 4).is_null_terminated())

h.assert_true(String.from_iso_array(recover
['a', 'b', 'c']
end).is_null_terminated())
h.assert_false(String.from_iso_array(recover
['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h'] // power of two sized array
end).is_null_terminated())


class iso _TestSpecialValuesF32 is UnitTest
"""
Expand Down

0 comments on commit c54d46f

Please sign in to comment.