Fix NULL-terminating serialise of rdctype::str adding 0 into elems

* The +1 for internal NULL terminator must be done internally, otherwise
  we end up with "foobar" being a 7-character string of "foobar\0". If
  this is then re-serialised we add more and more null terminators.
This commit is contained in:
baldurk
2017-08-03 20:18:15 +01:00
parent c898fc6d99
commit ca88810df3
2 changed files with 19 additions and 2 deletions
+18
View File
@@ -226,6 +226,24 @@ struct str : public rdctype::array<char>
return *this;
}
void assign(const char *const in, int32_t inCount)
{
Delete();
count = inCount;
if(inCount == 0)
{
elems = (char *)allocate(sizeof(char));
elems[0] = 0;
}
else
{
elems = (char *)allocate(sizeof(char) * (inCount + 1));
if(in)
memcpy(elems, in, sizeof(char) * inCount);
elems[count] = 0;
}
}
operator const char *() const { return elems ? elems : ""; }
const char *c_str() const { return elems ? elems : ""; }
};
+1 -2
View File
@@ -506,10 +506,9 @@ public:
}
else
{
create_array_uninit(el, sz + 1);
el.assign(NULL, sz);
for(int32_t i = 0; i < sz; i++)
Serialise("", el.elems[i]);
el.elems[sz] = 0;
}
}