Skip to content

Commit

Permalink
Fix a error handling in UUID parser (#167)
Browse files Browse the repository at this point in the history
  • Loading branch information
Drvi authored May 16, 2023
1 parent b879c62 commit 7b04eaa
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 2 deletions.
8 changes: 6 additions & 2 deletions src/hexadecimal.jl
Original file line number Diff line number Diff line change
Expand Up @@ -88,9 +88,10 @@ function typeparser(::Type{UUID}, source, pos, len, b, code, pl, options)

@inbounds begin
if len - pos + 1 < 36
while pos <= len && (_HEX_LUT[b + 0x01] != 0xFFFF || b == UInt8('-'))
while (_HEX_LUT[b + 0x01] != 0xFFFF || b == UInt8('-'))
pos += 1
incr!(source)
pos <= len || break
b = peekbyte(source, pos)
end
eof(source, pos, len) && (code |= EOF)
Expand Down Expand Up @@ -176,9 +177,12 @@ end
@noinline function _backtrack_error(source, pos, len, code, segment_len, hi, lo, pl)
pos -= segment_len # Backtrack to the start of the invalid hex
fastseek!(source, pos - 1)
while _HEX_LUT[peekbyte(source, pos) + 0x01] != 0xFFFF
b = peekbyte(source, pos)
while (_HEX_LUT[b + 0x01] != 0xFFFF || b == UInt8('-'))
pos += 1
incr!(source)
pos <= len || break
b = peekbyte(source, pos)
end
eof(source, pos, len) && (code |= EOF)
return (pos, code | INVALID, poslen(pl.pos, pos - pl.pos), UUID((hi, lo)))
Expand Down
12 changes: 12 additions & 0 deletions test/hexadecimal.jl
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,12 @@ using Parsers

res = Parsers.xparse(UUID, IOBuffer(us), 1, i, _OPTIONS)
@test Parsers.invalid(res.code)

res = Parsers.xparse(UUID, view(us, 1:i), 1, i, _OPTIONS)
@test Parsers.invalid(res.code)

res = Parsers.xparse(UUID, IOBuffer(view(us, 1:i)), 1, i, _OPTIONS)
@test Parsers.invalid(res.code)
end
res = Parsers.xparse(UUID, us, 1, 36, _OPTIONS)
@test Parsers.ok(res.code)
Expand Down Expand Up @@ -52,6 +58,12 @@ using Parsers

res = Parsers.xparse(Parsers.SHA1, IOBuffer(ss), 1, i, _OPTIONS)
@test Parsers.invalid(res.code)

res = Parsers.xparse(Parsers.SHA1, view(ss, 1:i), 1, i, _OPTIONS)
@test Parsers.invalid(res.code)

res = Parsers.xparse(Parsers.SHA1, IOBuffer(view(ss, 1:i)), 1, i, _OPTIONS)
@test Parsers.invalid(res.code)
end
res = Parsers.xparse(Parsers.SHA1, ss, 1, 40, _OPTIONS)
@test Parsers.ok(res.code)
Expand Down

0 comments on commit 7b04eaa

Please sign in to comment.