From 1b89eea1cf6126a2f80f51dcc83782ce70265f7e Mon Sep 17 00:00:00 2001 From: Bruce Hill Date: Tue, 6 May 2025 23:14:57 -0400 Subject: Bugfix for serialization of sets and tables with fallbacks --- src/stdlib/lists.c | 2 +- src/stdlib/metamethods.c | 2 +- src/stdlib/optionals.c | 2 +- src/stdlib/tables.c | 15 +++++++++++---- 4 files changed, 14 insertions(+), 7 deletions(-) (limited to 'src/stdlib') diff --git a/src/stdlib/lists.c b/src/stdlib/lists.c index c1c119a0..31e25f15 100644 --- a/src/stdlib/lists.c +++ b/src/stdlib/lists.c @@ -813,7 +813,7 @@ public void List$deserialize(FILE *in, void *obj, List_t *pointers, const TypeIn } else { size_t item_size = (size_t)type->ListInfo.item->size; for (int64_t i = 0; i < len; i++) { - if (fread(list.data + i*list.stride, item_size, 1, in) != item_size) + if (fread(list.data + i*list.stride, item_size, 1, in) != 1) fail("Not enough data in stream to deserialize"); } } diff --git a/src/stdlib/metamethods.c b/src/stdlib/metamethods.c index 5a2dd948..e007a52c 100644 --- a/src/stdlib/metamethods.c +++ b/src/stdlib/metamethods.c @@ -87,7 +87,7 @@ public void _deserialize(FILE *input, void *outval, List_t *pointers, const Type return; } - if (fread(outval, (size_t)type->size, 1, input) != (size_t)type->size) + if (fread(outval, (size_t)type->size, 1, input) != 1) fail("Not enough data in stream to deserialize"); } diff --git a/src/stdlib/optionals.c b/src/stdlib/optionals.c index f33b471d..7df9a827 100644 --- a/src/stdlib/optionals.c +++ b/src/stdlib/optionals.c @@ -56,7 +56,7 @@ public Text_t Optional$as_text(const void *obj, bool colorize, const TypeInfo_t public void Optional$serialize(const void *obj, FILE *out, Table_t *pointers, const TypeInfo_t *type) { bool has_value = !is_none(obj, type->OptionalInfo.type); - fputc((int)has_value, out); + assert(fputc((int)has_value, out) != EOF); if (has_value) _serialize(obj, out, pointers, type->OptionalInfo.type); } diff --git a/src/stdlib/tables.c b/src/stdlib/tables.c index 061c3391..8e3afe45 100644 --- a/src/stdlib/tables.c +++ b/src/stdlib/tables.c @@ -767,10 +767,13 @@ public void Table$serialize(const void *obj, FILE *out, Table_t *pointers, const size_t offset = value_offset(type); for (int64_t i = 0; i < len; i++) { _serialize(t->entries.data + i*t->entries.stride, out, pointers, type->TableInfo.key); - _serialize(t->entries.data + i*t->entries.stride + offset, out, pointers, type->TableInfo.value); + if (type->TableInfo.value->size > 0) + _serialize(t->entries.data + i*t->entries.stride + offset, out, pointers, type->TableInfo.value); } - Optional$serialize(&t->fallback, out, pointers, Optional$info(sizeof(void*), __alignof__(void*), Pointer$info("&", type))); + assert(fputc(t->fallback != NULL ? 1 : 0, out) != EOF); + if (t->fallback) + Table$serialize(t->fallback, out, pointers, type); } #ifdef __GNUC__ @@ -787,11 +790,15 @@ public void Table$deserialize(FILE *in, void *outval, List_t *pointers, const Ty char key[type->TableInfo.key->size]; _deserialize(in, key, pointers, type->TableInfo.key); char value[type->TableInfo.value->size]; - _deserialize(in, value, pointers, type->TableInfo.value); + if (type->TableInfo.value->size > 0) + _deserialize(in, value, pointers, type->TableInfo.value); Table$set(&t, key, value, type); } - Optional$deserialize(in, &t.fallback, pointers, Optional$info(sizeof(void*), __alignof__(void*), Pointer$info("&", type))); + if (fgetc(in) != 0) { + t.fallback = GC_MALLOC(sizeof(Table_t)); + Table$deserialize(in, t.fallback, pointers, type); + } *(Table_t*)outval = t; } -- cgit v1.2.3