chunkers: check return value of malloc, raise MemoryError

This commit is contained in:
Thomas Waldmann 2026-05-12 17:19:22 +02:00
parent ca3e88f5b1
commit f94e3dea95
No known key found for this signature in database
GPG key ID: 243ACFA951F78E01
2 changed files with 14 additions and 2 deletions

View file

@ -83,10 +83,12 @@ cdef extern from *:
@cython.boundscheck(False) # Deactivate bounds checking
@cython.wraparound(False) # Deactivate negative indexing.
cdef uint32_t* buzhash_init_table(uint32_t seed):
cdef uint32_t* buzhash_init_table(uint32_t seed) except NULL:
"""Initialize the buzhash table with the given seed."""
cdef int i
cdef uint32_t* table = <uint32_t*>malloc(1024) # 256 * sizeof(uint32_t)
if table == NULL:
raise MemoryError("Failed to allocate buzhash table")
for i in range(256):
table[i] = table_base[i] ^ seed
return table
@ -141,6 +143,8 @@ cdef class Chunker:
cdef bint sparse
def __cinit__(self, int seed, int chunk_min_exp, int chunk_max_exp, int hash_mask_bits, int hash_window_size, bint sparse=False):
self.table = NULL
self.data = NULL
min_size = 1 << chunk_min_exp
max_size = 1 << chunk_max_exp
assert max_size <= len(zeros)
@ -153,6 +157,8 @@ cdef class Chunker:
self.table = buzhash_init_table(seed & 0xffffffff)
self.buf_size = max_size
self.data = <uint8_t*>malloc(self.buf_size)
if self.data == NULL:
raise MemoryError("Failed to allocate chunker buffer")
self.fh = -1
self.done = 0
self.eof = 0

View file

@ -40,7 +40,7 @@ cdef extern from *:
@cython.boundscheck(False) # Deactivate bounds checking
@cython.wraparound(False) # Deactivate negative indexing.
cdef uint64_t* buzhash64_init_table(bytes key):
cdef uint64_t* buzhash64_init_table(bytes key) except NULL:
"""
Generate a balanced pseudo-random table deterministically from a 256-bit key.
Balanced means that for each bit position 0..63, exactly 50% of the table values have the bit set to 1.
@ -50,6 +50,8 @@ cdef uint64_t* buzhash64_init_table(bytes key):
cdef int i, j, bit_pos
cdef uint64_t* table = <uint64_t*>malloc(2048) # 256 * sizeof(uint64_t)
if table == NULL:
raise MemoryError("Failed to allocate buzhash64 table")
# Initialize all values to 0
for i in range(256):
@ -118,6 +120,8 @@ cdef class ChunkerBuzHash64:
cdef bint sparse
def __cinit__(self, bytes key, int chunk_min_exp, int chunk_max_exp, int hash_mask_bits, int hash_window_size, bint sparse=False):
self.table = NULL
self.data = NULL
min_size = 1 << chunk_min_exp
max_size = 1 << chunk_max_exp
assert max_size <= len(zeros)
@ -130,6 +134,8 @@ cdef class ChunkerBuzHash64:
self.table = buzhash64_init_table(key)
self.buf_size = max_size
self.data = <uint8_t*>malloc(self.buf_size)
if self.data == NULL:
raise MemoryError("Failed to allocate chunker buffer")
self.fh = -1
self.done = 0
self.eof = 0