Skip to content

Commit

Permalink
read_header_size: avoid modifying buffer & try/catch
Browse files Browse the repository at this point in the history
  • Loading branch information
StefanKarpinski committed Jun 4, 2021
1 parent 71ce072 commit 3175c90
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 14 deletions.
18 changes: 8 additions & 10 deletions src/extract.jl
Original file line number Diff line number Diff line change
Expand Up @@ -624,15 +624,11 @@ end

function read_header_size(buf::AbstractVector{UInt8})
r = index_range(:size)
b1 = buf[r[1]]
b1 = buf[r[1]] # high bit set for binary
b1 & 0x80 == 0 && return read_header_int(buf, :size)
# high bit set for binary
buf[r[1]] &= ~0x80
n = try read_header_bin(buf, :size)
finally
buf[r[1]] = b1
end
return n
b1 == 0x80 && return read_header_bin(buf, :size, r[1]+1:r[end])
val = String(buf[r])
header_error(buf, "binary integer size value too large: $(repr(val))")
end

function read_header_chr(buf::AbstractVector{UInt8}, fld::Symbol)
Expand Down Expand Up @@ -671,8 +667,10 @@ function read_header_int(buf::AbstractVector{UInt8}, fld::Symbol)
return n
end

function read_header_bin(buf::AbstractVector{UInt8}, fld::Symbol)
r = index_range(fld)
function read_header_bin(
buf::AbstractVector{UInt8}, fld::Symbol,
r::UnitRange{<:Integer} = index_range(fld),
)
n = Int64(0)
for i in r
if leading_zeros(n) <= 8
Expand Down
8 changes: 4 additions & 4 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -913,13 +913,13 @@ end
# smallest too large binary size
str = lpad("\x80"*"\x00"^7, 11, "\0")
tarball = write_modified_header(data, :size => "\x80$str")
test_error_prefix("binary integer size value too large: $(repr("\0$str"))") do
test_error_prefix("binary integer size value too large: $(repr(str))") do
open(Tar.read_header, tarball)
end
# largest binary size (also too large)
str = "\xff"^11
tarball = write_modified_header(data, :size => "\xff$str")
test_error_prefix("binary integer size value too large: $(repr("\x7f$str"))") do
str = "\xff"^12
tarball = write_modified_header(data, :size => str)
test_error_prefix("binary integer size value too large: $(repr(str))") do
open(Tar.read_header, tarball)
end
# malformed modes
Expand Down

0 comments on commit 3175c90

Please sign in to comment.