src/buffer.c

changeset 1128
3672b89e606c
parent 1110
a0e9be7ed131
--- a/src/buffer.c	Wed Jan 15 19:42:49 2025 +0100
+++ b/src/buffer.c	Thu Jan 16 18:56:17 2025 +0100
@@ -205,8 +205,8 @@
 
 static size_t cx_buffer_flush_helper(
         const CxBuffer *buffer,
+        const unsigned char *src,
         size_t size,
-        const unsigned char *src,
         size_t nitems
 ) {
     // flush data from an arbitrary source
@@ -236,7 +236,7 @@
     unsigned char *space = buffer->bytes;
     size_t remaining = buffer->pos / size;
     size_t flushed_total = cx_buffer_flush_helper(
-        buffer, size, space, remaining);
+        buffer, space, size, remaining);
 
     // shift the buffer left after flushing
     // IMPORTANT: up to this point, copy on write must have been
@@ -317,16 +317,20 @@
         if (buffer->pos == 0) {
             // if we don't have data in the buffer, but are instructed
             // to flush, it means that we are supposed to relay the data
-            items_flush = cx_buffer_flush_helper(buffer, size, ptr, nitems);
+            items_flush = cx_buffer_flush_helper(buffer, ptr, size, nitems);
             if (items_flush == 0) {
                 // we needed to flush, but could not flush anything
                 // give up and avoid endless trying
                 return 0;
             }
             size_t ritems = nitems - items_flush;
-            const unsigned char *rest = ptr;
-            rest += items_flush * size;
-            return items_flush + cxBufferWrite(rest, size, ritems, buffer);
+            if (ritems > 0) {
+                const unsigned char *rest = ptr;
+                rest += items_flush * size;
+                return items_flush + cxBufferWrite(rest, size, ritems, buffer);
+            } else {
+                return items_flush;
+            }
         } else {
             items_flush = cx_buffer_flush_impl(buffer, size);
             if (items_flush == 0) {

mercurial