Skip to content

Commit

Permalink
Fix array reallocation formula causing segfaults in nhmmer (#62)
Browse files Browse the repository at this point in the history
  • Loading branch information
althonos committed Mar 12, 2024
1 parent 824962d commit d61316c
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 13 deletions.
9 changes: 2 additions & 7 deletions include/capacity.pxd
Original file line number Diff line number Diff line change
Expand Up @@ -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 = (<size_t> capacity + (capacity >> 3) + 6) & ~(<size_t> 3)
if (capacity - length) > (new_cap - capacity):
new_cap = (<size_t> capacity + 3) & ~(<size_t> 3)
return new_cap
return (<size_t> capacity + (capacity >> 3) + 6) & ~(<size_t> 3)

11 changes: 5 additions & 6 deletions include/libhmmer/nhmmer.pxd
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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 = <ID_LENGTH_LIST*> malloc(sizeof(ID_LENGTH_LIST))
if l != NULL:
l.count = 0
Expand All @@ -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:
Expand All @@ -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):
Expand All @@ -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

0 comments on commit d61316c

Please sign in to comment.