From 389132400b6d3c1f7c767aaf3546d19e93909eb3 Mon Sep 17 00:00:00 2001 From: Bruce Hill Date: Sun, 22 Jun 2025 13:50:15 -0400 Subject: Do text deserialization in chunks to avoid possible memory issues --- src/stdlib/text.c | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) diff --git a/src/stdlib/text.c b/src/stdlib/text.c index 2107c1df..8b01529d 100644 --- a/src/stdlib/text.c +++ b/src/stdlib/text.c @@ -1617,13 +1617,19 @@ public void Text$serialize(const void *obj, FILE *out, Table_t *pointers, const public void Text$deserialize(FILE *in, void *out, List_t *pointers, const TypeInfo_t *info) { (void)info; - int64_t len = -1; + int64_t len = 0; Int64$deserialize(in, &len, pointers, &Int64$info); - char *buf = GC_MALLOC_ATOMIC((size_t)len+1); - if (fread(buf, sizeof(char), (size_t)len, in) != (size_t)len) - fail("Not enough data in stream to deserialize"); - buf[len+1] = '\0'; - *(Text_t*)out = Text$from_strn(buf, (size_t)len); + if (len < 0) + fail("Invalid text length during deserialization!"); + Text_t text = EMPTY_TEXT; + while (text.length < len) { + static char chunk[1024] = {}; + size_t chunk_size = MIN(sizeof(chunk), (size_t)(len - text.length)); + if (fread(chunk, sizeof(char), chunk_size, in) != chunk_size) + fail("Not enough data in stream to deserialize"); + text = concat2(text, Text$from_strn(chunk, chunk_size)); + } + *(Text_t*)out = text; } public const TypeInfo_t Text$info = { -- cgit v1.2.3