From 4b64b3b453ce531f67740ac0b095d5c844fb5d4c Mon Sep 17 00:00:00 2001 From: Eduardo Silva Date: Thu, 5 Dec 2024 17:49:26 -0600 Subject: [PATCH] sds: use new utf8 decoder for sds_cat_utf8 Signed-off-by: Eduardo Silva --- src/flb_sds.c | 30 +++++++++++++++++++++++++++++- 1 file changed, 29 insertions(+), 1 deletion(-) diff --git a/src/flb_sds.c b/src/flb_sds.c index 759be89d9ab..1d8263b3432 100644 --- a/src/flb_sds.c +++ b/src/flb_sds.c @@ -28,6 +28,7 @@ #include #include #include +#include #include #include @@ -279,13 +280,15 @@ flb_sds_t flb_sds_copy(flb_sds_t s, const char *str, int len) return s; } -flb_sds_t flb_sds_cat_utf8 (flb_sds_t *sds, const char *str, int str_len) +flb_sds_t flb_sds_cat_utf8(flb_sds_t *sds, const char *str, int str_len) { static const char int2hex[] = "0123456789abcdef"; int i; int b; int ret; int hex_bytes; + int offset; + size_t size; uint32_t cp; uint32_t state = 0; unsigned char c; @@ -297,6 +300,7 @@ flb_sds_t flb_sds_cat_utf8 (flb_sds_t *sds, const char *str, int str_len) s = *sds; head = FLB_SDS_HEADER(s); + /* make sure we have at least str_len extra bytes available */ if (flb_sds_avail(s) <= str_len) { tmp = flb_sds_increase(s, str_len); if (tmp == NULL) { @@ -306,6 +310,30 @@ flb_sds_t flb_sds_cat_utf8 (flb_sds_t *sds, const char *str, int str_len) head = FLB_SDS_HEADER(s); } + while (1) { + offset = head->len; + ret = flb_utils_write_str(s, &offset, flb_sds_alloc(s), str, str_len); + if (ret == FLB_FALSE) { + /* realloc */ + size = flb_sds_alloc(s) * 2; + tmp = flb_sds_increase(s, size); + if (tmp == NULL) { + return NULL; + } + *sds = s = tmp; + head = FLB_SDS_HEADER(s); + } + else { + break; + } + } + + flb_sds_len_set(s, offset); + s[head->len] = '\0'; + return s; + + + for (i = 0; i < str_len; i++) { if (flb_sds_avail(s) < 8) { tmp = flb_sds_increase(s, 8);