Skip to content

Commit 28ac557

Browse files
mkittimusm
andauthored
Guard malloc in compression filters (JuliaIO#1152)
* Guard malloc in compression filters * Update filters/H5Zblosc/src/H5Zblosc.jl Co-authored-by: Mustafa Mohamad <mus-m@outlook.com> --------- Co-authored-by: Mustafa Mohamad <mus-m@outlook.com>
1 parent 612cec7 commit 28ac557

File tree

4 files changed

+10
-1
lines changed

4 files changed

+10
-1
lines changed

filters/H5Zbitshuffle/src/H5Zbitshuffle.jl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,7 @@ function H5Z_filter_bitshuffle(
196196
end
197197

198198
size = nbytes_uncomp ÷ elem_size
199+
buf_size_out <= 0 && error("bitshuffle_h5plugin: Non-positive buf_size_out for malloc: $buf_size_out")
199200
out_buf = Libc.malloc(buf_size_out)
200201
if out_buf == C_NULL
201202
error(

filters/H5Zblosc/src/H5Zblosc.jl

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,7 @@ function blosc_filter(
9696
# the result is larger, we simply return 0. The filter is flagged
9797
# as optional, so HDF5 marks the chunk as uncompressed and proceeds.
9898
outbuf_size = unsafe_load(buf_size)
99+
outbuf_size <= 0 && return Csize_t(0)
99100
outbuf = Libc.malloc(outbuf_size)
100101
outbuf == C_NULL && return Csize_t(0)
101102

@@ -121,6 +122,7 @@ function blosc_filter(
121122
# See https://github.com/JuliaLang/julia/issues/43402
122123
# Resolved in https://github.com/JuliaLang/julia/pull/43408
123124
outbuf_size, cbytes, blocksize = Blosc.cbuffer_sizes(in)
125+
outbuf_size <= 0 && return Csize_t(0)
124126
outbuf = Libc.malloc(outbuf_size)
125127
outbuf == C_NULL && return Csize_t(0)
126128
status = Blosc.blosc_decompress(in, outbuf, outbuf_size)

filters/H5Zbzip2/src/H5Zbzip2.jl

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ function H5Z_filter_bzip2(
4040
# Decompress
4141

4242
outbuflen = nbytes * 3 + 1
43+
outbuflen <= 0 && error("H5Zbzip2: Non-positive outbuflen for malloc: $outbuflen.")
4344
outbuf = Libc.malloc(outbuflen)
4445
if outbuf == C_NULL
4546
error("H5Zbzip2: memory allocation failed for bzip2 decompression.")
@@ -106,6 +107,7 @@ function H5Z_filter_bzip2(
106107

107108
# Prepare the output buffer
108109
outbuflen = nbytes + nbytes ÷ 100 + 600 # worse case (bzip2 docs)
110+
outbuflen <= 0 && error("H5Zbzip2: Non-positive outbuflen for malloc: $outbuflen.")
109111
outbuf = Libc.malloc(outbuflen)
110112
@debug "Allocated" outbuflen outbuf
111113
if outbuf == C_NULL

filters/H5Zlz4/src/H5Zlz4.jl

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,9 @@ function H5Z_filter_lz4(
6666
# malloc a byte buffer of origSize
6767
# outBuf = Vector{UInt8}(undef, origSize)
6868
@debug "OrigSize" origSize
69+
origSize <= 0 && error("H5Zlz4: Non-positive origSize for malloc: $origSize")
6970
outBuf = Libc.malloc(origSize)
71+
outBuf == C_NULL && error("H5Zlz4: Could not allocate memory via malloc")
7072
# Julia should throw an error if it cannot allocate this
7173
roBuf = Ptr{UInt8}(outBuf)
7274
decompSize = 0
@@ -126,7 +128,9 @@ function H5Z_filter_lz4(
126128
nBlocks = (nbytes - 1) ÷ blockSize + 1
127129
maxDestSize =
128130
nBlocks * CodecLz4.LZ4_compressBound(blockSize) + 4 + 8 + nBlocks * 4
131+
maxDestSize <= 0 && error("H5Zlz4: Non-positive maxDestSize for malloc: $maxDestSize")
129132
outBuf = Libc.malloc(maxDestSize)
133+
outBuf == C_NULL && error("H5Zlz4: Could not allocate memory via malloc")
130134

131135
rpos = Ptr{UInt8}(unsafe_load(buf))
132136
roBuf = Ptr{UInt8}(outBuf)
@@ -189,7 +193,7 @@ function H5Z_filter_lz4(
189193
catch err
190194
# "In the case of failure, the return value is 0 (zero) and all pointer arguments are left unchanged."
191195
ret_value = Csize_t(0)
192-
@error "H5Zlz4.jl Non-Fatal ERROR: " err
196+
@async @error "H5Zlz4.jl Non-Fatal ERROR: " err
193197
display(stacktrace(catch_backtrace()))
194198
finally
195199
if outBuf != C_NULL

0 commit comments

Comments
 (0)