From 96631d580a853d8c77cf966ae4c1f7815364b031 Mon Sep 17 00:00:00 2001 From: jp9000 Date: Thu, 9 Dec 2021 19:35:01 -0800 Subject: [PATCH] libobs/util: Fix end_pos when pushing empty circlebuf front When pushing to the front of an empty circular buffer, it would not update the end_pos, so end_pos would be left on 0, and it would break when trying to push to the back after that. The reason why this bug was never discovered until now is because breakage only happens when pushing to the front of an empty buffer, then pushing to the back right after that. --- libobs/util/circlebuf.h | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/libobs/util/circlebuf.h b/libobs/util/circlebuf.h index c2fce69de..60ec9b4f6 100644 --- a/libobs/util/circlebuf.h +++ b/libobs/util/circlebuf.h @@ -173,7 +173,12 @@ static inline void circlebuf_push_front(struct circlebuf *cb, const void *data, cb->size += size; circlebuf_ensure_capacity(cb); - if (cb->start_pos < size) { + if (cb->size == size) { + cb->start_pos = 0; + cb->end_pos = size; + memcpy((uint8_t *)cb->data, data, size); + + } else if (cb->start_pos < size) { size_t back_size = size - cb->start_pos; if (cb->start_pos) @@ -216,7 +221,12 @@ static inline void circlebuf_push_front_zero(struct circlebuf *cb, size_t size) cb->size += size; circlebuf_ensure_capacity(cb); - if (cb->start_pos < size) { + if (cb->size == size) { + cb->start_pos = 0; + cb->end_pos = size; + memset((uint8_t *)cb->data, 0, size); + + } else if (cb->start_pos < size) { size_t back_size = size - cb->start_pos; if (cb->start_pos)