Skip to content

Commit

Permalink
Merge pull request #505 from Tarsnap/libcperciva-import
Browse files Browse the repository at this point in the history
Improve handling of wrapping array sizes in elasticarray.
  • Loading branch information
cperciva authored Dec 19, 2023
2 parents 394a92e + 837a01a commit 1207756
Showing 1 changed file with 8 additions and 1 deletion.
9 changes: 8 additions & 1 deletion datastruct/elasticarray.c
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,16 @@ resize(struct elasticarray * EA, size_t nsize)
if (EA->alloc < nsize) {
/* We need to enlarge the buffer. */
nalloc = EA->alloc * 2;

/*
* Handle if nalloc is not large enough to hold nsize, which can
* happen when: 1) EA->alloc is small enough that EA->alloc * 2
* is still smaller than nsize, or 2) EA->alloc is large enough
* that EA->alloc * 2 wraps around, becoming smaller than nsize.
*/
if (nalloc < nsize)
nalloc = nsize;
} else if (EA->alloc > nsize * 4) {
} else if (EA->alloc / 4 > nsize) {
/* We need to shrink the buffer. */
nalloc = nsize * 2;
} else {
Expand Down

0 comments on commit 1207756

Please sign in to comment.