From 4dc3d8f8325994d737f1959c1554480f4ccbc60f Mon Sep 17 00:00:00 2001 From: Aryadev Chavali Date: Fri, 30 May 2025 08:12:49 +0100 Subject: Fix issue with memory allocations in lisp::serialise Was to do with sv_make. --- lib/sv.c | 3 ++- lisp/lisp.c | 8 +++++++- oats.org | 24 +++++++++++++++--------- 3 files changed, 24 insertions(+), 11 deletions(-) diff --git a/lib/sv.c b/lib/sv.c index b2425b8..a83f303 100644 --- a/lib/sv.c +++ b/lib/sv.c @@ -60,7 +60,8 @@ sv_t sv_chop(sv_t sv, u64 size) sv_t sv_concat(arena_t *allocator, sv_t a, sv_t b) { - sv_t c = sv_make(allocator, a.data, a.size + b.size); + sv_t c = sv_make(allocator, NULL, a.size + b.size); + memcpy(c.data, a.data, a.size); memcpy(c.data + a.size, b.data, b.size); return c; } diff --git a/lisp/lisp.c b/lisp/lisp.c index 24a4788..e1524a4 100644 --- a/lisp/lisp.c +++ b/lisp/lisp.c @@ -252,7 +252,13 @@ sv_t serialise(context_t *ctx, lisp_t *ptr) lmember = CAR(lmember); sv_t member = serialise(ctx, lmember); - s = sv_concat(&ctx->scratch, s, member); + if (!s.data) + s = member; + else + s = sv_concat(&ctx->scratch, s, member); + + // NOTE: eventually this separator we add per item should be mutable at + // runtime. if (IS_TAG(lsp, CONS) && !CDR(lsp)) continue; else if (IS_TAG(CDR(lsp), CONS)) diff --git a/oats.org b/oats.org index dbad845..b53b516 100644 --- a/oats.org +++ b/oats.org @@ -4,15 +4,6 @@ #+FILETAGS: :oats: * Issues :issues: -** TODO Fix issue with memcpy overlap when string concatenating -[[file:lisp/lisp.c::// FIXME: Something is going wrong here!]] - -Ideas on what's going wrong: -- String sizes seem off -- Maybe something is wrong with arena allocator; we use - [[file:lib/sv.c::newsv.data = arena_realloc(allocator, sv.data, - sv.size, newsv.size);][arena_realloc]] which seems to be the root of - the memcpy-overlap * Features :features: ** WIP Reader :reader: We want something a bit generic: able to handle reading from some @@ -205,3 +196,18 @@ more work than the lowest byte (to look at it we'll need to push that byte all the way down). So we'll be going for a low byte strategy by shifting the pointer up by one byte. This leaves us with the lowest byte to play with as we choose. +** DONE Fix issue with memcpy overlap when string concatenating +[[file:lisp/lisp.c::// FIXME: Something is going wrong here!]] + +Ideas on what's going wrong: +- String sizes seem off +- Maybe something is wrong with arena allocator; we use + [[file:lib/sv.c::newsv.data = arena_realloc(allocator, sv.data, + sv.size, newsv.size);][arena_realloc]] which seems to be the root of + the memcpy-overlap + +2025-05-30 08:11:24: Was an issue with ~sv_make~; if you pass a data +pointer, it will try to copy the size you passed as bytes from the +pointer. In ~sv_concat~ we pass the overall new size of the string +we're allocating and pass in the first argument to copy in the initial +part of the data first. -- cgit v1.2.3-13-gbd6f