Skip to content

Commit d144466

Browse files
authored
Use memchr for safe and performant read_bytestring (#703)
1 parent 25d60ea commit d144466

File tree

2 files changed

+8
-8
lines changed

2 files changed

+8
-8
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
## Unreleased
22
- Support for directly reading from and writing to Vector{UInt8}
33
- Fix for working with opened IOStream objects
4+
- Proper boundscheck for internal `read_bytestring`
45

56
## 0.6.3
67
- Fix edge case in datatype loading (#692)

src/io/mmapio.jl

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -201,14 +201,13 @@ end
201201

202202
# Read a null-terminated string
203203
function read_bytestring(io::MmapIO)
204-
# Find the null terminator position without allocating
205204
ptr = pconvert(Ptr{UInt8}, io.curptr)
206-
len = 0
207-
while unsafe_load(ptr, len + 1) != 0x00
208-
len += 1
209-
end
210-
str = len == 0 ? "" : unsafe_string(ptr, len)
211-
io.curptr += len + 1
205+
maxbytes = io.endptr - io.curptr + 1
206+
q = @ccall memchr(ptr::Ptr{UInt8}, 0::Int32, maxbytes::Csize_t)::Ptr{UInt8}
207+
q == C_NULL && throw(EOFError())
208+
209+
str = unsafe_string(ptr, (q - ptr) % Int)
210+
io.curptr += (q - ptr) % Int + 1
212211
str
213212
end
214213

@@ -239,4 +238,4 @@ function end_checksum(io::MmapIO)
239238
@inbounds v = io.checksum_pos[io.nchecksum]
240239
io.nchecksum -= 1
241240
Lookup3.hash(Ptr{UInt8}(io.startptr + v), position(io) - v)
242-
end
241+
end

0 commit comments

Comments
 (0)