]> git.ipfire.org Git - thirdparty/dovecot/core.git/commitdiff
lib: Add buffer_nul_terminate()
authorTimo Sirainen <timo.sirainen@open-xchange.com>
Tue, 3 Feb 2026 12:25:37 +0000 (14:25 +0200)
committeraki.tuomi <aki.tuomi@open-xchange.com>
Fri, 6 Feb 2026 14:34:18 +0000 (14:34 +0000)
src/lib/buffer.c
src/lib/buffer.h

index c85f1e61db613d4845075dd48ac819e64ecf7d09..76535828b040b4f3c46a3d0796dba8745eea765f 100644 (file)
@@ -348,6 +348,18 @@ void buffer_insert_zero(buffer_t *_buf, size_t pos, size_t data_size)
        }
 }
 
+void buffer_nul_terminate(buffer_t *_buf)
+{
+       struct real_buffer *buf = container_of(_buf, struct real_buffer, buf);
+
+       /* +1 extra byte is always kept for NUL termination */
+       i_assert(buf->used < buf->alloc);
+       /* Buffer might not be writable, so check first if the NUL is
+          already there. */
+       if (((const char *)buf->r_buffer)[buf->used] != '\0')
+               buf->w_buffer[buf->used] = '\0';
+}
+
 void buffer_copy(buffer_t *_dest, size_t dest_pos,
                 const buffer_t *_src, size_t src_pos, size_t copy_size)
 {
index c5eef4bf5f739d0c7fe3b06e582ef3fc39be8ae3..c2c1ced3cac7d1327976056c794d87f808150cfe 100644 (file)
@@ -84,6 +84,9 @@ void buffer_replace(buffer_t *buf, size_t pos, size_t size,
 void buffer_write_zero(buffer_t *buf, size_t pos, size_t data_size);
 void buffer_append_zero(buffer_t *buf, size_t data_size);
 void buffer_insert_zero(buffer_t *buf, size_t pos, size_t data_size);
+/* Terminate the buffer with a NUL character after the buffer's used size.
+   The NUL will not be included in the used size. */
+void buffer_nul_terminate(buffer_t *buf);
 
 /* Copy data from buffer to another. The buffers may be same in which case
    it's internal copying, possibly with overlapping positions (ie. memmove()