From: Timo Sirainen Date: Tue, 3 Feb 2026 12:25:37 +0000 (+0200) Subject: lib: Add buffer_nul_terminate() X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=035e31b6c42a2d66ed2badd0fb5236bd47e49b92;p=thirdparty%2Fdovecot%2Fcore.git lib: Add buffer_nul_terminate() --- diff --git a/src/lib/buffer.c b/src/lib/buffer.c index c85f1e61db..76535828b0 100644 --- a/src/lib/buffer.c +++ b/src/lib/buffer.c @@ -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) { diff --git a/src/lib/buffer.h b/src/lib/buffer.h index c5eef4bf5f..c2c1ced3ca 100644 --- a/src/lib/buffer.h +++ b/src/lib/buffer.h @@ -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()