ZenKit icon indicating copy to clipboard operation
ZenKit copied to clipboard

Pool of `_m_temporary_strings` is too small

Open Try opened this issue 1 month ago • 2 comments

De facto it's only one string at the moment :P

Example of failing code (de-compilation, CoM):

SYSTEMTIMEPTR = MEM_ALLOC(16);
CALL_PTRPARAM(SYSTEMTIMEPTR);
CALL__STDCALL(GETLOCALTIME);
TIMESTR = INTTOSTRING(MEM_READINT(SYSTEMTIMEPTR) & 2047);
TIMESTR = CONCATSTRINGS(CONCATSTRINGS(TIMESTR, "-"), INTTOSTRING(MEM_READINT(SYSTEMTIMEPTR + 2) & 2047));
TIMESTR = CONCATSTRINGS(CONCATSTRINGS(TIMESTR, "-"), INTTOSTRING(MEM_READINT(SYSTEMTIMEPTR + 6) & 2047));
TIMESTR = CONCATSTRINGS(CONCATSTRINGS(TIMESTR, " "), INTTOSTRING(MEM_READINT(SYSTEMTIMEPTR + 8) & 2047));
TIMESTR = CONCATSTRINGS(CONCATSTRINGS(TIMESTR, ":"), INTTOSTRING(MEM_READINT(SYSTEMTIMEPTR + 10) & 2047));
TIMESTR = CONCATSTRINGS(CONCATSTRINGS(TIMESTR, ":"), INTTOSTRING(MEM_READINT(SYSTEMTIMEPTR + 12) & 2047));
MEM_FREE(SYSTEMTIMEPTR);
MEM_INFO("");
ZERROR_SETTEMPLEVEL(1);
MEM_INFO(CONCATSTRINGS("Time: ", TIMESTR));

Nested CONCATSTRINGS cause corruption of _m_temporary_strings. Replacing to arbitrary larger size works, however proper dynamic allocation would be ideal.

Try avatar Nov 10 '25 20:11 Try

Yeah, so the problem is about knowing when to mark a string as being free. Sure, I can make it so that the dynamic strings grow infinitely, however if you pop a temporary string, you obtain a mutable reference to it which could be stored somewhere. Then, when the next string is pushed, it it will overwrite the previously popped string causing unintended behaviour.

I've implemented a version of this for testing and pushed it to feat/variable-temp-strings. I don't have a modded Gothic installation right now so am unable to test properly.

lmichaelis avatar Nov 15 '25 07:11 lmichaelis

Tested on code above:

[zpy]: Time:               2025-11-16 19:19:37

grow is triggered and feature seem to work well.

mutable reference to it which could be stored somewhere

As, often in C++ the rule of thumb: any references obtained from object is invalidated, after call to non-const method and it's responsibility of called to make copy if required. I think it's fine.

Small tangent: in ZenLib, I've been using non-standard copy-on-write string, so it was possible to push them by value, without worrying of anything.

Try avatar Nov 16 '25 18:11 Try

Merged :)

lmichaelis avatar Nov 24 '25 17:11 lmichaelis