From cb13d54278046dc4915cef5ff2ee97be6d986e5b Mon Sep 17 00:00:00 2001 From: martinvuyk Date: Fri, 15 Nov 2024 16:05:08 -0300 Subject: [PATCH 1/4] Fix unsafe String.write_bytes() Signed-off-by: martinvuyk --- stdlib/src/collections/string.mojo | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/stdlib/src/collections/string.mojo b/stdlib/src/collections/string.mojo index 917b56e99b..416a0d03b1 100644 --- a/stdlib/src/collections/string.mojo +++ b/stdlib/src/collections/string.mojo @@ -847,14 +847,13 @@ struct String( # ===------------------------------------------------------------------=== # fn write_bytes(inout self, bytes: Span[Byte, _]): - """ - Write a byte span to this String. + """Write a byte span to this String. Args: bytes: The byte span to write to this String. Must NOT be - null terminated. + null terminated. """ - self._iadd[True](bytes) + self._iadd[False](bytes) fn write[*Ts: Writable](inout self, *args: *Ts): """Write a sequence of Writable arguments to the provided Writer. From c1c5516ab9d330175ebc810766f5d76ff087d14f Mon Sep 17 00:00:00 2001 From: martinvuyk Date: Fri, 15 Nov 2024 17:19:18 -0300 Subject: [PATCH 2/4] add debug_assert for null termination Signed-off-by: martinvuyk --- stdlib/src/collections/string.mojo | 3 +++ 1 file changed, 3 insertions(+) diff --git a/stdlib/src/collections/string.mojo b/stdlib/src/collections/string.mojo index 416a0d03b1..e7fd3940f0 100644 --- a/stdlib/src/collections/string.mojo +++ b/stdlib/src/collections/string.mojo @@ -853,6 +853,9 @@ struct String( bytes: The byte span to write to this String. Must NOT be null terminated. """ + # TODO: maybe check byte by byte that there is no 0 ? + # need Span.count() to be faster + debug_assert(bytes[-1] != 0, "byte Span must not be null terminated") self._iadd[False](bytes) fn write[*Ts: Writable](inout self, *args: *Ts): From a2625506a9096baac176536d2b9442b764756728 Mon Sep 17 00:00:00 2001 From: martinvuyk Date: Fri, 15 Nov 2024 17:24:50 -0300 Subject: [PATCH 3/4] fix bounds check Signed-off-by: martinvuyk --- stdlib/src/collections/string.mojo | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/stdlib/src/collections/string.mojo b/stdlib/src/collections/string.mojo index e7fd3940f0..cea9922bb2 100644 --- a/stdlib/src/collections/string.mojo +++ b/stdlib/src/collections/string.mojo @@ -855,7 +855,10 @@ struct String( """ # TODO: maybe check byte by byte that there is no 0 ? # need Span.count() to be faster - debug_assert(bytes[-1] != 0, "byte Span must not be null terminated") + if len(bytes) > 0: + debug_assert( + bytes[-1] != 0, "byte Span must not be null terminated" + ) self._iadd[False](bytes) fn write[*Ts: Writable](inout self, *args: *Ts): From 8dd4acd167cfffbb216d0c0a46301d0d663bac9f Mon Sep 17 00:00:00 2001 From: martinvuyk Date: Fri, 15 Nov 2024 17:40:11 -0300 Subject: [PATCH 4/4] revert debug assert Signed-off-by: martinvuyk --- stdlib/src/collections/string.mojo | 6 ------ 1 file changed, 6 deletions(-) diff --git a/stdlib/src/collections/string.mojo b/stdlib/src/collections/string.mojo index cea9922bb2..416a0d03b1 100644 --- a/stdlib/src/collections/string.mojo +++ b/stdlib/src/collections/string.mojo @@ -853,12 +853,6 @@ struct String( bytes: The byte span to write to this String. Must NOT be null terminated. """ - # TODO: maybe check byte by byte that there is no 0 ? - # need Span.count() to be faster - if len(bytes) > 0: - debug_assert( - bytes[-1] != 0, "byte Span must not be null terminated" - ) self._iadd[False](bytes) fn write[*Ts: Writable](inout self, *args: *Ts):