From de9bff4a29db25554e5234ff788617ed46a9436c Mon Sep 17 00:00:00 2001 From: Vicki Pfau Date: Sun, 23 Sep 2018 13:26:32 -0700 Subject: [PATCH] Util: Fix wrapping edge cases in RingFIFO --- CHANGES | 1 + src/util/ring-fifo.c | 12 ++++++------ 2 files changed, 7 insertions(+), 6 deletions(-) diff --git a/CHANGES b/CHANGES index 032f4e537..15bf4355a 100644 --- a/CHANGES +++ b/CHANGES @@ -62,6 +62,7 @@ Bugfixes: - GB Audio: Fix channel 4 initial LFSR - GB, GBA Video: Don't call finishFrame twice in thread proxy - GB Audio: Fix channel 1, 2 and 4 reset timing + - Util: Fix wrapping edge cases in RingFIFO Misc: - GBA Timer: Use global cycles for timers - GBA: Extend oddly-sized ROMs to full address space (fixes mgba.io/i/722) diff --git a/src/util/ring-fifo.c b/src/util/ring-fifo.c index 167690c61..39c10934a 100644 --- a/src/util/ring-fifo.c +++ b/src/util/ring-fifo.c @@ -33,8 +33,8 @@ size_t RingFIFOWrite(struct RingFIFO* buffer, const void* value, size_t length) ATOMIC_LOAD(end, buffer->readPtr); // Wrap around if we can't fit enough in here - if ((intptr_t) data - (intptr_t) buffer->data + length >= buffer->capacity) { - if (end == buffer->data) { + if ((uintptr_t) data - (uintptr_t) buffer->data + length >= buffer->capacity) { + if (end == buffer->data || end > data) { // Oops! If we wrap now, it'll appear empty return 0; } @@ -65,8 +65,8 @@ size_t RingFIFORead(struct RingFIFO* buffer, void* output, size_t length) { ATOMIC_LOAD(end, buffer->writePtr); // Wrap around if we can't fit enough in here - if ((intptr_t) data - (intptr_t) buffer->data + length >= buffer->capacity) { - if (end == data) { + if ((uintptr_t) data - (uintptr_t) buffer->data + length >= buffer->capacity) { + if (end >= data) { // Oops! If we wrap now, it'll appear full return 0; } @@ -78,7 +78,7 @@ size_t RingFIFORead(struct RingFIFO* buffer, void* output, size_t length) { uintptr_t bufferEnd = (uintptr_t) buffer->data + buffer->capacity; remaining = bufferEnd - (uintptr_t) data; } else { - remaining = (intptr_t) end - (intptr_t) data; + remaining = (uintptr_t) end - (uintptr_t) data; } // If the pointers touch, it's empty if (remaining < length) { @@ -87,6 +87,6 @@ size_t RingFIFORead(struct RingFIFO* buffer, void* output, size_t length) { if (output) { memcpy(output, data, length); } - ATOMIC_STORE(buffer->readPtr, (void*) ((intptr_t) data + length)); + ATOMIC_STORE(buffer->readPtr, (void*) ((uintptr_t) data + length)); return length; }