From 5ef3c031254c6a0c455f50d64bb49611b0e379a5 Mon Sep 17 00:00:00 2001 From: Michael Tremer Date: Wed, 8 Oct 2025 09:33:16 +0000 Subject: [PATCH] string: Add function to copy strings of a set input length Signed-off-by: Michael Tremer --- src/daemon/string.h | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/src/daemon/string.h b/src/daemon/string.h index 0af03a9..e8d50f5 100644 --- a/src/daemon/string.h +++ b/src/daemon/string.h @@ -100,6 +100,24 @@ static inline int __collecty_string_set(char* s, const size_t length, const char return -ENOBUFS; } +#define collecty_string_setn(s, value, l) \ + __collecty_string_setn(s, sizeof(s), value, l) + +static inline int __collecty_string_setn(char* s, const size_t length, const char* value, const size_t l) { + // Fail if we don't have enough space + if (unlikely(l >= length)) + return -ENOBUFS; + + // Copy the data + if (likely(l > 0)) + memcpy(s, value, l); + + // Terminate the result buffer + s[l] = '\0'; + + return l; +} + static inline void collecty_string_lstrip(char* s) { if (!s) return; -- 2.47.3