From 3237f634fa01acdef53636b5ab83f2dec373bb69 Mon Sep 17 00:00:00 2001 From: skewb1k Date: Mon, 8 Sep 2025 01:45:34 +0300 Subject: [PATCH] fix(cjson): fix `strbuf_set_length` incorrectness #35565 `strbuf_set_length` was incorrectly updating length with `+=` instead of assignment. Also syncs minor updates with upstream. (cherry picked from commit ad22d0ace9965907a83088fb52dd407b92222992) --- src/cjson/fpconv.c | 2 +- src/cjson/strbuf.c | 7 ++++--- src/cjson/strbuf.h | 2 +- 3 files changed, 6 insertions(+), 5 deletions(-) diff --git a/src/cjson/fpconv.c b/src/cjson/fpconv.c index 0ef7f18cbe..64208f8dc4 100644 --- a/src/cjson/fpconv.c +++ b/src/cjson/fpconv.c @@ -130,7 +130,7 @@ double fpconv_strtod(const char *nptr, char **endptr) /* Duplicate number into buffer */ if (buflen >= FPCONV_G_FMT_BUFSIZE) { /* Handle unusually large numbers */ - buf = malloc(buflen + 1); + buf = (char *)malloc(buflen + 1); if (!buf) { fprintf(stderr, "Out of memory"); abort(); diff --git a/src/cjson/strbuf.c b/src/cjson/strbuf.c index 0bcff9b8cc..5b5ac85fba 100644 --- a/src/cjson/strbuf.c +++ b/src/cjson/strbuf.c @@ -39,7 +39,7 @@ static void die(const char *fmt, ...) va_end(arg); fprintf(stderr, "\n"); - exit(-1); + abort(); } void strbuf_init(strbuf_t *s, size_t len) @@ -51,7 +51,7 @@ void strbuf_init(strbuf_t *s, size_t len) else size = len + 1; /* \0 terminator */ if (size < len) - die("Overflow, len %zu", len); + die("Overflow, len: %zu", len); s->buf = NULL; s->size = size; s->length = 0; @@ -132,7 +132,7 @@ static size_t calculate_new_size(strbuf_t *s, size_t len) /* Ensure there is room for optional NULL termination */ reqsize = len + 1; if (reqsize < len) - die("Overflow, len %zu", len); + die("Overflow, len: %zu", len); /* If the user has requested to shrink the buffer, do it exactly */ if (s->size > reqsize) @@ -194,5 +194,6 @@ void strbuf_append_string(strbuf_t *s, const char *str) } } + /* vi:ai et sw=4 ts=4: */ diff --git a/src/cjson/strbuf.h b/src/cjson/strbuf.h index bee245701b..d77e0f4b17 100644 --- a/src/cjson/strbuf.h +++ b/src/cjson/strbuf.h @@ -103,7 +103,7 @@ static inline char *strbuf_empty_ptr(strbuf_t *s) static inline void strbuf_set_length(strbuf_t *s, int len) { - s->length += len; + s->length = len; } static inline void strbuf_extend_length(strbuf_t *s, size_t len)