diff options
| author | Bruce Hill <bruce@bruce-hill.com> | 2025-06-22 13:50:15 -0400 |
|---|---|---|
| committer | Bruce Hill <bruce@bruce-hill.com> | 2025-06-22 13:50:15 -0400 |
| commit | 389132400b6d3c1f7c767aaf3546d19e93909eb3 (patch) | |
| tree | 5aaae0939e4cc789b41088ed7a63eecc20369102 /src/stdlib/text.c | |
| parent | d7a79d7f2f7d96a5bcc3635a14be9771fc76d550 (diff) | |
Do text deserialization in chunks to avoid possible memory issues
Diffstat (limited to 'src/stdlib/text.c')
| -rw-r--r-- | src/stdlib/text.c | 18 |
1 files 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 = { |
