fix implementation of cxBufferTerminate() - fixes #631

Fri, 11 Apr 2025 16:45:20 +0200

author
Mike Becker <universe@uap-core.de>
date
Fri, 11 Apr 2025 16:45:20 +0200
changeset 1284
b2103354baed
parent 1283
89935fea4b7c
child 1285
7acbaa74fbd0

fix implementation of cxBufferTerminate() - fixes #631

CHANGELOG file | annotate | diff | comparison | revisions
docs/Writerside/topics/about.md file | annotate | diff | comparison | revisions
src/buffer.c file | annotate | diff | comparison | revisions
src/cx/buffer.h file | annotate | diff | comparison | revisions
tests/test_buffer.c file | annotate | diff | comparison | revisions
--- a/CHANGELOG	Fri Apr 11 15:12:20 2025 +0200
+++ b/CHANGELOG	Fri Apr 11 16:45:20 2025 +0200
@@ -3,6 +3,7 @@
 
  * adds cxMempoolTransfer() and cxMempoolTransferObject()
  * changes grow strategy for the mempory pool to reduce reallocations
+ * fixes implementation of cxBufferTerminate()
 
 Version 3.1 - 2025-02-11
 ------------------------
--- a/docs/Writerside/topics/about.md	Fri Apr 11 15:12:20 2025 +0200
+++ b/docs/Writerside/topics/about.md	Fri Apr 11 16:45:20 2025 +0200
@@ -30,6 +30,7 @@
 
 * adds cxMempoolTransfer() and cxMempoolTransferObject()
 * changes grow strategy for the mempory pool to reduce reallocations
+* fixes implementation of cxBufferTerminate()
 
 ### Version 3.1 - 2025-02-11 {collapsible="true"}
 
--- a/src/buffer.c	Fri Apr 11 15:12:20 2025 +0200
+++ b/src/buffer.c	Fri Apr 11 16:45:20 2025 +0200
@@ -400,10 +400,8 @@
 }
 
 int cxBufferTerminate(CxBuffer *buffer) {
-    bool success = 0 == cxBufferPut(buffer, 0);
-    if (success) {
-        buffer->pos--;
-        buffer->size--;
+    if (0 == cxBufferPut(buffer, 0)) {
+        buffer->size = buffer->pos - 1;
         return 0;
     } else {
         return -1;
--- a/src/cx/buffer.h	Fri Apr 11 15:12:20 2025 +0200
+++ b/src/cx/buffer.h	Fri Apr 11 16:45:20 2025 +0200
@@ -674,11 +674,10 @@
 /**
  * Writes a terminating zero to a buffer at the current position.
  *
- * On successful write, @em neither the position @em nor the size of the buffer is
- * increased.
+ * If successful, sets the size to the current position and advances the position by one.
  *
  * The purpose of this function is to have the written data ready to be used as
- * a C string.
+ * a C string with the buffer's size being the length of that string.
  *
  * @param buffer the buffer to write to
  * @return zero, if the terminator could be written, non-zero otherwise
--- a/tests/test_buffer.c	Fri Apr 11 15:12:20 2025 +0200
+++ b/tests/test_buffer.c	Fri Apr 11 16:45:20 2025 +0200
@@ -1050,7 +1050,7 @@
         buf.flags |= CX_BUFFER_AUTO_EXTEND;
         CX_TEST_ASSERT(0 == cxBufferTerminate(&buf));
         CX_TEST_ASSERT(buf.size == 8);
-        CX_TEST_ASSERT(buf.pos == 8);
+        CX_TEST_ASSERT(buf.pos == 9);
         CX_TEST_ASSERT(buf.capacity > 8);
         CX_TEST_ASSERT(0 == memcmp(buf.space, "preptest\0", 9));
     }

mercurial