diff options
author | Aidan MacDonald <amachronic@protonmail.com> | 2022-03-27 00:08:28 +0000 |
---|---|---|
committer | Aidan MacDonald <amachronic@protonmail.com> | 2022-04-09 15:20:57 +0100 |
commit | 7718b244011661a5273121d1b545a18f1a5cd497 (patch) | |
tree | 68ee6e02cd3985581e67e803e70b16b412bb0527 | |
parent | bd444ebd0a6cb98faf7ca569c273f4ca860ab65d (diff) | |
download | rockbox-7718b24401.tar.gz rockbox-7718b24401.zip |
buffering: fix signed overflow in next_handle_id()
Not sure what the comment is talking about - signed overflow
is undefined behavior and we don't use -fwrapv or other flags
to make it defined. I can't see how a compiler could abuse it
here, but the overflow is nonetheless easily avoided.
Change-Id: Ibed6d7c0d841db2aa86b9d8ba4c6a0d08c413354
-rw-r--r-- | apps/buffering.c | 13 |
1 files changed, 5 insertions, 8 deletions
diff --git a/apps/buffering.c b/apps/buffering.c index 3adbc4a6b9..f80d73a4a8 100644 --- a/apps/buffering.c +++ b/apps/buffering.c @@ -71,8 +71,6 @@ /* amount of data to read in one read() call */ #define BUFFERING_DEFAULT_FILECHUNK (1024*32) -#define BUF_HANDLE_MASK 0x7FFFFFFF - enum handle_flags { H_CANWRAP = 0x1, /* Handle data may wrap in buffer */ @@ -295,12 +293,11 @@ static int next_handle_id(void) { static int cur_handle_id = 0; - /* Wrap signed int is safe and 0 doesn't happen */ - int next_hid = (cur_handle_id + 1) & BUF_HANDLE_MASK; - if (next_hid == 0) - next_hid = 1; - - cur_handle_id = next_hid; + int next_hid = cur_handle_id + 1; + if (next_hid == INT_MAX) + cur_handle_id = 0; /* next would overflow; reset the counter */ + else + cur_handle_id = next_hid; return next_hid; } |