byte array slice fix

This commit is contained in:
Brahim Hadriche committed 2025-10-29 09:29:34 -04:00
1 parent 33518607c5
commit 0d949b1752
5 files changed
+121 -2

No files matched your search

+6
View File
@@ -5,6 +5,12 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
## [Unreleased]
### Fixed
- Crash on older Roku OS version due to `roByteArray.Slice` not being available
## [0.43.1] - 2025-10-28
### Added
@@ -1,6 +1,7 @@
import "HttpRequestState.bs"
import "pkg:/components/Web/WebServer/Http/HttpConnection.bs"
import "pkg:/components/Web/WebServer/Http/HttpUtils.bs"
import "pkg:/source/utils/ByteArrayUtils.bs"
import "pkg:/source/utils/Logging.bs"
import "pkg:/source/utils/MathUtils.bs"
import "pkg:/source/utils/Types.bs"
@@ -207,7 +208,7 @@ namespace Http
return false
end if
m.body_bytes = m.buffer.Slice(m.content_index)
m.body_bytes = ByteArrayUtils.Slice(m.buffer, m.content_index)
m.ready_for_response = true
return true
end function
@@ -1,3 +1,5 @@
import "pkg:/source/utils/ByteArrayUtils.bs"
namespace Protobuf
class BinaryReader
@@ -79,7 +81,7 @@ namespace Protobuf
if endPosition > m.buffer.Count()
return invalid
end if
value = m.buffer.Slice(m.position, endPosition)
value = ByteArrayUtils.Slice(m.buffer, m.position, endPosition)
m.position += length
return value
end function
@@ -0,0 +1,65 @@
import "StringUtils.bs"
namespace ByteArrayUtils
function Slice(bytearray as object, start_pos = 0 as dynamic, end_pos = invalid as dynamic) as object
if FindMemberFunction(bytearray, "Slice") <> invalid
start_pos = ValidInt(start_pos)
if end_pos = invalid
return bytearray.Slice(start_pos)
end if
return bytearray.Slice(start_pos, end_pos)
end if
return SliceFallback(bytearray, start_pos, end_pos)
end function
function SliceFallback(bytearray as object, start_pos = 0 as dynamic, end_pos = invalid as dynamic) as object
copy = CreateObject("roByteArray")
arrCount = bytearray.Count()
' default end_pos is array length
if end_pos = invalid
end_pos = arrCount
end if
start_pos = ValidInt(start_pos)
' handle negative start_pos
if start_pos < 0
start_pos = arrCount + start_pos
end if
' handle negative end_pos
if end_pos < 0
end_pos = arrCount + end_pos
end if
' clamp start_pos to bounds
if start_pos < 0
start_pos = 0
else if start_pos > arrCount
start_pos = arrCount
end if
' clamp end_pos to bounds
if end_pos < 0
end_pos = 0
else if end_pos > arrCount
end_pos = arrCount
end if
count = end_pos - start_pos
if count <= 0
return copy
end if
tmp = `tmp:/${StringUtils.RandomId()}.bin`
if not bytearray.WriteFile(tmp, start_pos, count)
DeleteFile(tmp)
return copy
end if
copy.ReadFile(tmp)
DeleteFile(tmp)
return copy
end function
end namespace
@@ -0,0 +1,45 @@
namespace Tests
@suite
class ByteArrayUtilsTests extends Rooibos.BaseTestSuite
protected override function beforeEach()
m.ba = CreateObject("roByteArray")
m.ba.FromHexString("00010203040506070809")
end function
@describe("ByteArrayUtils.Slice and SliceFallback")
@it("slices a byte array correctly")
@params("positive start and end", 2, 5, "020304")
@params("to the end if end is not provided", 7, invalid, "070809")
@params("the whole array if no params provided", invalid, invalid, "00010203040506070809")
@params("with negative start", -4, invalid, "06070809")
@params("with negative end", 0, -3, "00010203040506")
@params("with negative start and negative end", -5, -2, "050607")
@params("returns empty array for start greater than end", 5, 2, "")
@params("returns empty for start out of bounds", 20, invalid, "")
@params("clamps end to array length", 8, 20, "0809")
@params("handles empty slice", 3, 3, "")
function testSlice(description as string, start_pos as dynamic, end_pos as dynamic, expectedHex as string)
resultBa = ByteArrayUtils.Slice(m.ba, start_pos, end_pos)
m.assertEqual(resultBa.ToHexString(), expectedHex, "Slice failed for: " + description)
resultBaFallback = ByteArrayUtils.SliceFallback(m.ba, start_pos, end_pos)
m.assertEqual(resultBaFallback.ToHexString(), expectedHex, "SliceFallback failed for: " + description)
end function
@it("handles empty byte array")
function handles_empty_byte_array()
emptyBa = CreateObject("roByteArray")
resultBa = ByteArrayUtils.Slice(emptyBa, 0, 0)
m.assertEqual(resultBa.ToHexString(), "")
resultBaFallback = ByteArrayUtils.SliceFallback(emptyBa, 0, 0)
m.assertEqual(resultBaFallback.ToHexString(), "")
end function
end class
end namespace