Fix JFile empty-document seed and MString's ZString-source pointer/length mismatch

JFile.cpp: JFile and MFile both "virtual public MString" a single shared
base, so a virtual base is initialized only by the *most derived* class's
constructor. JFile's own constructor never mentions MString, so
JString's own "{}" default initializer never ran - a JFile backed by a
new or nonexistent file started as a raw empty string instead of a valid
empty JSON object. Every subsequent locate()/set() call then silently
misbehaved (no leading "{" for ifNext("{") to match, so writes landed at
offset 0 with no braces or separators - fields ended up prepended in
reverse order, concatenated with no delimiters at all). Fixed by seeding
"{}" via append (matching MFile's own load pattern) whenever the loaded
content is empty.

MString.cpp: several ZString-source copy paths (the ZString/MString copy
constructors, both assignment operators, operator<<(ZString), insert(),
write()) called zstring.getData() (the object's absolute start) paired
with zstring.getLength() (which is documented/implemented as "remaining
bytes from cursor to end", i.e. length - (cursor - data)). Pairing an
absolute start with a cursor-relative length silently corrupts any
cursor-advanced source: the copy starts from the wrong (too-early)
position but is truncated by exactly the amount the cursor had already
advanced, keeping unwanted leading bytes while dropping trailing ones.
Found via the CGIFormData-style resumability testing pattern used
elsewhere this session, triggered by JetCore's new "@name" session
variable feature stripping a sigil prefix via ifNext() before streaming
the remainder into an MString. Fixed by pairing zstring.getCursor()
(cursor-relative start) with zstring.getLength() consistently everywhere
this pattern appeared.

Verified via the existing jstring_test/mstring_test/zstring_test/
parser_test suites (all pass, no output changes) plus a new isolated
JFile round-trip test proving persistence across two separate process
instances sharing one file.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
This commit is contained in:
Brad ArantandClaude Sonnet 5 committed 2026-08-23 18:44:59 -07:00
1 parent 5b6d4ea8ab
commit 3e6e410fe8
2 files changed
+24 -11

No files matched your search

+10 -10
View File
@@ -48,13 +48,13 @@ namespace coreutils {
MString::MString(const ZString &zstring) {
setSize(zstring.getLength());
memcpy(data, zstring.getData(), zstring.getLength());
cursor = data;
}
memcpy(data, zstring.getCursor(), zstring.getLength());
cursor = data;
}
MString::MString(ZString &zstring) {
setSize(zstring.getLength());
memcpy(data, zstring.getData(), zstring.getLength());
memcpy(data, zstring.getCursor(), zstring.getLength());
cursor = data;
}
@@ -66,7 +66,7 @@ namespace coreutils {
MString::MString(MString &mstring) {
setSize(mstring.getLength());
memcpy(data, mstring.getData(), mstring.getLength());
memcpy(data, mstring.getCursor(), mstring.getLength());
cursor = data;
}
@@ -106,7 +106,7 @@ namespace coreutils {
MString &MString::operator=(coreutils::MString value) {
setSize(value.getLength());
memcpy(data, value.getData(), value.getLength());
memcpy(data, value.getCursor(), value.getLength());
length = value.getLength();
onChange();
return *this;
@@ -114,7 +114,7 @@ namespace coreutils {
MString &MString::operator=(coreutils::ZString value) {
setSize(value.getLength());
memcpy(data, value.getData(), value.getLength());
memcpy(data, value.getCursor(), value.getLength());
length = value.getLength();
onChange();
return *this;
@@ -195,7 +195,7 @@ namespace coreutils {
int temp = length;
int len = length + zstring.getLength();
setSize(len);
memcpy(data + temp, zstring.getData(), zstring.getLength());
memcpy(data + temp, zstring.getCursor(), zstring.getLength());
onChange();
return *this;
}
@@ -240,7 +240,7 @@ namespace coreutils {
setSize(length + value.getLength());
for (int ix = length; ix >= offset; ix--)
getData()[ix] = getData()[ix - value.getLength()];
std::memcpy(data + offset, value.getData(), value.getLength());
std::memcpy(data + offset, value.getCursor(), value.getLength());
onChange();
}
@@ -285,7 +285,7 @@ namespace coreutils {
MString &MString::write(ZString value) {
int len = length;
setSize(length + value.getLength());
memcpy(data + len, value.getData(), value.getLength());
memcpy(data + len, value.getCursor(), value.getLength());
onChange();
return *this;
}