Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix a error handling in UUID parser #167

Merged
merged 1 commit into from
May 16, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should we check pos is valid here?

maybe a couple lines up add an @assert 1 <= pos <= len(is that right?)

Copy link
Collaborator Author

@Drvi Drvi May 16, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

pos should always be valid here -- we only use _backtrack_error once we check the length would fit a UUID value and then we iterate in fixed sized steps (segment_len).

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