Skip to content

Commit

Permalink
store: don't underflow pointer
Browse files Browse the repository at this point in the history
when number of snips is 0, `end - 1` will go one below the
buffer. computing out of bound pointer (with the exception of
one past the end) is technically UB [0] so avoid it by returning
early when nr_snips == 0.

0: https://port70.net/~nsz/c/c11/n1570.html#6.5.6p9
  • Loading branch information
N-R-K committed Jun 10, 2024
1 parent 1113ca8 commit 7a3855c
Showing 1 changed file with 2 additions and 2 deletions.
4 changes: 2 additions & 2 deletions src/store.c
Original file line number Diff line number Diff line change
Expand Up @@ -562,7 +562,7 @@ int cs_add(struct clip_store *cs, const char *content, uint64_t *out_hash) {
*/
bool cs_snip_iter(struct ref_guard *guard, enum cs_iter_direction direction,
struct cs_snip **snip) {
if (guard->status < 0) {
if (guard->status < 0 || guard->cs->header->nr_snips == 0) {
return false;
}

Expand All @@ -578,7 +578,7 @@ bool cs_snip_iter(struct ref_guard *guard, enum cs_iter_direction direction,
*snip = direction == CS_ITER_NEWEST_FIRST ? end - 1 : start;
}

return *snip >= start && *snip < end;
return *snip < end;
}

/**
Expand Down

0 comments on commit 7a3855c

Please sign in to comment.