mirror of
https://github.com/borgbackup/borg.git
synced 2026-05-28 04:03:21 -04:00
chunkers: check return value of malloc, raise MemoryError
This commit is contained in:
parent
ca3e88f5b1
commit
f94e3dea95
2 changed files with 14 additions and 2 deletions
|
|
@ -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
|
||||
|
|
|
|||
|
|
@ -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
|
||||
|
|
|
|||
Loading…
Reference in a new issue