From d61316c5477347c6bbf10213f61ff079d4d173d0 Mon Sep 17 00:00:00 2001 From: Martin Larralde Date: Tue, 12 Mar 2024 16:39:31 +0100 Subject: [PATCH] Fix array reallocation formula causing segfaults in `nhmmer` (#62) --- include/capacity.pxd | 9 ++------- include/libhmmer/nhmmer.pxd | 11 +++++------ 2 files changed, 7 insertions(+), 13 deletions(-) diff --git a/include/capacity.pxd b/include/capacity.pxd index 7f283ef4..fd0c165a 100644 --- a/include/capacity.pxd +++ b/include/capacity.pxd @@ -3,11 +3,6 @@ cdef inline size_t new_capacity(size_t capacity, size_t length) nogil: Compute a new capacity for a buffer reallocation. - This is how CPython computes the allocation sizes for the array storing - the references for a `list` object. - """ - cdef size_t new_cap = ( capacity + (capacity >> 3) + 6) & ~( 3) - if (capacity - length) > (new_cap - capacity): - new_cap = ( capacity + 3) & ~( 3) - return new_cap \ No newline at end of file + return ( capacity + (capacity >> 3) + 6) & ~( 3) + diff --git a/include/libhmmer/nhmmer.pxd b/include/libhmmer/nhmmer.pxd index 3fc6f30e..3cc1d323 100644 --- a/include/libhmmer/nhmmer.pxd +++ b/include/libhmmer/nhmmer.pxd @@ -1,6 +1,5 @@ from libc.stdint cimport int64_t, uint64_t from libc.stdlib cimport calloc, malloc, realloc, free -from libc.stdio cimport printf cimport libeasel from libhmmer.p7_tophits cimport P7_TOPHITS @@ -19,7 +18,7 @@ ctypedef struct ID_LENGTH_LIST: size_t size -cdef inline ID_LENGTH_LIST* idlen_list_init(size_t size) nogil: +cdef inline ID_LENGTH_LIST* idlen_list_init(size_t size) noexcept nogil: cdef ID_LENGTH_LIST* l = malloc(sizeof(ID_LENGTH_LIST)) if l != NULL: l.count = 0 @@ -30,13 +29,13 @@ cdef inline ID_LENGTH_LIST* idlen_list_init(size_t size) nogil: l = NULL return l -cdef inline void idlen_list_destroy(ID_LENGTH_LIST* l) nogil: +cdef inline void idlen_list_destroy(ID_LENGTH_LIST* l) noexcept nogil: if l != NULL: if l.id_lengths != NULL: free(l.id_lengths) free(l) -cdef inline int idlen_list_add(ID_LENGTH_LIST* l, int64_t id, int64_t L) nogil: +cdef inline int idlen_list_add(ID_LENGTH_LIST* l, int64_t id, int64_t L) noexcept nogil: if l.count > 0 and l.id_lengths[l.count - 1].id == id: l.id_lengths[l.count - 1].length = L else: @@ -50,7 +49,7 @@ cdef inline int idlen_list_add(ID_LENGTH_LIST* l, int64_t id, int64_t L) nogil: l.count += 1 return libeasel.eslOK -cdef inline int idlen_list_assign(ID_LENGTH_LIST* l, P7_TOPHITS* th) nogil: +cdef inline int idlen_list_assign(ID_LENGTH_LIST* l, P7_TOPHITS* th) noexcept nogil: cdef uint64_t i cdef int64_t j = 0 for i in range(th.N): @@ -61,5 +60,5 @@ cdef inline int idlen_list_assign(ID_LENGTH_LIST* l, P7_TOPHITS* th) nogil: th.hit[i].dcl[0].ad.L = l.id_lengths[j].length return libeasel.eslOK -cdef inline int idlen_list_clear(ID_LENGTH_LIST* l) nogil: +cdef inline int idlen_list_clear(ID_LENGTH_LIST* l) noexcept nogil: l.count = 0