Skip to content

Commit

Permalink
elasticarray: clarify unsigned arithmetic wrap around is handled
Browse files Browse the repository at this point in the history
This commit adds a comment describing the cases handled by the
if (nalloc < nsize) block.  One such case is the potential wrap
around of EA->alloc * 2.  Without the comment, it was not clear
that the wrap around is properly handled.
  • Loading branch information
dorjoy03 committed Oct 20, 2023
1 parent 1cd5ac8 commit cd21e46
Showing 1 changed file with 7 additions and 0 deletions.
7 changes: 7 additions & 0 deletions libcperciva/datastruct/elasticarray.c
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,13 @@ 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 / 4 > nsize) {
Expand Down

0 comments on commit cd21e46

Please sign in to comment.