From ce31e133c84b035d64f7a4ee7d71d8d9b0db044c Mon Sep 17 00:00:00 2001 From: Volodymyr Chernetskyi <19735328+chernetskyi@users.noreply.github.com> Date: Thu, 10 Sep 2026 10:28:19 +0200 Subject: [PATCH] bugfix: preserve full buffer lengths when restoring encoder state. Use size_t for strbuf_set_length() and saved array and object positions to prevent truncation above INT_MAX. --- lua_cjson.c | 6 ++++-- strbuf.h | 4 ++-- 2 files changed, 6 insertions(+), 4 deletions(-) diff --git a/lua_cjson.c b/lua_cjson.c index f1aa4ad0..f9348e53 100644 --- a/lua_cjson.c +++ b/lua_cjson.c @@ -826,7 +826,8 @@ static void json_append_newline_and_indent(strbuf_t *json, json_config_t *cfg, i static void json_append_array(lua_State *l, json_config_t *cfg, int current_depth, strbuf_t *json, int array_length, int raw) { - int comma, i, json_pos, err; + int comma, i, err; + size_t json_pos; int has_items = 0; strbuf_append_char(json, '['); @@ -1024,7 +1025,8 @@ static int cmp_key_entries(const void *a, const void *b) static void json_append_object(lua_State *l, json_config_t *cfg, int current_depth, strbuf_t *json) { - int comma, keytype, json_pos, err; + int comma, keytype, err; + size_t json_pos; int has_items = 0; keybuf_t *keybuf; key_entry_t key_entry; diff --git a/strbuf.h b/strbuf.h index d77e0f4b..1f2cddc3 100644 --- a/strbuf.h +++ b/strbuf.h @@ -64,7 +64,7 @@ static char *strbuf_string(strbuf_t *s, size_t *len); static void strbuf_ensure_empty_length(strbuf_t *s, size_t len); static char *strbuf_empty_ptr(strbuf_t *s); static void strbuf_extend_length(strbuf_t *s, size_t len); -static void strbuf_set_length(strbuf_t *s, int len); +static void strbuf_set_length(strbuf_t *s, size_t len); /* Update */ static void strbuf_append_mem(strbuf_t *s, const char *c, size_t len); @@ -101,7 +101,7 @@ static inline char *strbuf_empty_ptr(strbuf_t *s) return s->buf + s->length; } -static inline void strbuf_set_length(strbuf_t *s, int len) +static inline void strbuf_set_length(strbuf_t *s, size_t len) { s->length = len; }