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 code 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 code comment, it was not quite apparent
upon seeing the code that the wrap around is properly handled.
  • Loading branch information
dorjoy03 committed Oct 19, 2023
1 parent 6b2e3cb commit e7500c0
Showing 1 changed file with 6 additions and 0 deletions.
6 changes: 6 additions & 0 deletions libcperciva/datastruct/elasticarray.c
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,12 @@ 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 e7500c0

Please sign in to comment.