Skip to content

Commit

Permalink
Merge pull request #596 from Tarsnap/dorjoy03-elastic-array-wrap-arou…
Browse files Browse the repository at this point in the history
…nd-bug

elastic array wrap around bug
  • Loading branch information
cperciva authored Oct 20, 2023
2 parents f40a308 + cd21e46 commit 0e50d22
Showing 1 changed file with 8 additions and 1 deletion.
9 changes: 8 additions & 1 deletion libcperciva/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 0e50d22

Please sign in to comment.