From 78f6d565fbd80282eb300e0d34129a34415798c5 Mon Sep 17 00:00:00 2001 From: Quinn Date: Fri, 13 Jun 2025 01:54:43 +0200 Subject: [PATCH] fix: config getpat appender, using mempcpy and strpcpy instead, since it makes it less prone to errors --- src/util/conf.c | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/src/util/conf.c b/src/util/conf.c index 575c31e..b7d19ba 100644 --- a/src/util/conf.c +++ b/src/util/conf.c @@ -2,6 +2,7 @@ // Licensed under the MIT Licence. See LICENSE for details #include "conf.h" +#include #include #include #include @@ -129,16 +130,18 @@ int conf_procval(struct conf_entry const* opt, char const* restrict val) { /* utility function for conf_getpat to concatenate 3 strings, where we already know the size */ atrb_nonnull(1, 3) static inline char* conf_getpat_concat(char const* restrict s1, char const* restrict s2, char const* restrict s3, size_t s1len, size_t s2len, size_t s3len) { + assert(s2 || (!s2 && !s2len)); // ensuring the programmer passes both s2 and s2len as 0, if they intend to char *buf, *ptr; // allocate enough data for all three to the buffer ptr = malloc(s1len + s2len + s3len + 1); if (!ptr) return NULL; + buf = ptr; // store the head pointer into buf // copy data to the buffer - buf = strcpy(ptr, s1); // copy s1 data to the buffer - if (s2len) ptr = memcpy(ptr + s1len, s2, s2len); // increment ptr by s1len, and copy s2 data to the buffer (excluding null-terminator) - (void)strcpy(ptr + s2len, s3); // add s2len to the pointer and copy s3 as a string, thus including null-terminator + ptr = mempcpy(ptr, s1, s1len); // copy s1 data to the buffer + if (s2len) ptr = mempcpy(ptr, s2, s2len); // copy s2 data to the buffer (excluding null-terminator) + (void)strcpy(ptr, s3); // copy s3 as a string, thus including null-terminator // return the buffer return buf;