On certain large BYOND games (I experienced this when testing a library on tgstation), when calling
Byond_ToString
with a ref value as its first argument, the `buf_len` argument is greater than the actual number of bytes in the valid string representation of the value.Numbered Steps to Reproduce Problem:
1. clone the tgstation repo
2. insert the below dm snippet in a location where it will compile after
code/game/world.dm
3. compile the c++ snippet into a library
4. replace LIB in the dm snippet with the path to the library, relative to the tgstation dme
5. Launch tgstation
Code Snippet (if applicable) to Reproduce Problem:
DM code (insert into the tgstation codebase):
/world/New()
..()
var/test_object = new/obj/machinery/power/supermatter_crystal/engine()
var/external_length = call_ext(LIB, "byond:bug_case")(test_object)
log_world("[length("[test_object]")+1], [external_length]")
C++ code:
extern "C" BYOND_EXPORT CByondValue bug_case(int n, CByondValue v[]) {
CByondValue result;
char *buf = (char*)malloc(1024); // we need to allocate something to write to, even if we aren't going to return anything.
if(!buf || n < 1) { // we couldn't allocate memory or we don't have enough arguments
ByondValue_Clear(&result);
return result;
}
int out_len;
Byond_ToString(v[0], buf, &out_len);
free(buf);
ByondValue_SetNum(&result, out_len);
}
Expected Results:
The string "24, 24" should be output to
world.log
. This is because the string representation of an unmodified /obj/machinery/power/supermatter_crystal is "The supermatter crystal", which has a length of 23 characters (the extra 1 is to account for the null terminator)Actual Results:
The second number in the output string is larger than the first.
Does the problem occur:
Every time? Or how often?
Whenever the
name
var has been assigned from dm, but only in certain large projects.In other games?
The bug did not occur in a minimal project I wrote to test byondapi features.
On other computers?
Untested
When does the problem NOT occur?
If the
name
var has been set to a string from byondapi, the bug stops occurring until the var is assigned from dm. This problem also does not occur in small projects, such as one I used to test when the bug happens.Did the problem NOT occur in any earlier versions? If so, what was the last version that worked? (Visit http://www.byond.com/download/build to download old versions for testing.)
Untested.
Workarounds:
Any code that assumes that the last value in an output buffer is a null byte should truncate the buffer to the first null byte, which occurs at the end of the portion of the buffer corresponding to the valid string of the atom's string conversion.
It sounds like this report needs to bake a little longer until there's a test case that can reproduce the issue.