Skip to content

Commit 6655d02

Browse files
committed
Fix pre-existing issues in set_slice error handling
- Set current_nchunk before compress_ctx in the update-chunk path (needed by prefilters, was missing unlike all other compress sites) - Add PyBuffer_Release on error paths that were leaking the buffer - Add comp_rc == 0 and _check_comp_length guards for consistency with insert_data/update_data
1 parent 9d093a9 commit 6655d02

1 file changed

Lines changed: 15 additions & 0 deletions

File tree

src/blosc2/blosc2_ext.pyx

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1963,10 +1963,12 @@ cdef class SChunk:
19631963
rc = blosc2_schunk_decompress_chunk(self.schunk, self.schunk.nchunks - 1, data, chunk_nbytes)
19641964
if rc < 0:
19651965
free(data)
1966+
PyBuffer_Release(&buf)
19661967
raise RuntimeError("Error while decompressing the chunk")
19671968
data_start = self.schunk.nbytes - (self.schunk.nchunks - 1) * self.schunk.chunksize
19681969
memcpy(data + data_start, buf_ptr + buf_pos, nbytes_copy)
19691970
chunk = <uint8_t *> malloc(chunk_nbytes + BLOSC2_MAX_OVERHEAD)
1971+
self.schunk.current_nchunk = self.schunk.nchunks - 1
19701972
if RELEASEGIL:
19711973
with nogil:
19721974
comp_rc = blosc2_compress_ctx(self.schunk.cctx, data, chunk_nbytes, chunk, chunk_nbytes + BLOSC2_MAX_OVERHEAD)
@@ -1975,10 +1977,16 @@ cdef class SChunk:
19751977
free(data)
19761978
if comp_rc < 0:
19771979
free(chunk)
1980+
PyBuffer_Release(&buf)
19781981
raise RuntimeError("Error while compressing the data")
1982+
elif comp_rc == 0:
1983+
free(chunk)
1984+
PyBuffer_Release(&buf)
1985+
raise RuntimeError("The result could not fit")
19791986
rc = blosc2_schunk_update_chunk(self.schunk, self.schunk.nchunks - 1, chunk, True)
19801987
free(chunk)
19811988
if rc < 0:
1989+
PyBuffer_Release(&buf)
19821990
raise RuntimeError("Error while updating the chunk")
19831991
buf_pos += nbytes_copy
19841992
# Append data if needed
@@ -2001,11 +2009,18 @@ cdef class SChunk:
20012009
comp_rc = blosc2_compress_ctx(self.schunk.cctx, buf_ptr + buf_pos, chunksize, chunk, alloc_len)
20022010
if comp_rc < 0:
20032011
free(chunk)
2012+
PyBuffer_Release(&buf)
20042013
raise RuntimeError("Error while compressing the chunk")
2014+
elif comp_rc == 0:
2015+
free(chunk)
2016+
PyBuffer_Release(&buf)
2017+
raise RuntimeError("The result could not fit")
20052018
chunk = <uint8_t*> realloc(chunk, comp_rc)
2019+
_check_comp_length('chunk', comp_rc)
20062020
rc = blosc2_schunk_append_chunk(self.schunk, chunk, False)
20072021
if rc < 0:
20082022
free(chunk)
2023+
PyBuffer_Release(&buf)
20092024
raise RuntimeError("Error while appending the chunk")
20102025
buf_pos += chunksize
20112026
else:

0 commit comments

Comments
 (0)