Browse Source

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.
jp9000 4 years ago
parent
commit
96631d580a
1 changed files with 12 additions and 2 deletions
  1. 12 2
      libobs/util/circlebuf.h

+ 12 - 2
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)