max. length of a string
by Thomas Bang · in Torque Game Engine · 12/12/2009 (8:25 am) · 8 replies
I refer to this problem: TGE Crashes with latest drivers on NVIDIA [FIX included]
I did a test with the console-function getVideoDriverInfo();
The string has a length of 4366 characters. But i did not get a crash with dSprintf and dVsprintf (both not modified).
I did a test with the console-function getVideoDriverInfo();
echo("Video Driver Info");
echo(getVideoDriverInfo());
echo("Length of String is: " @ strlen(getVideoDriverInfo()));The string has a length of 4366 characters. But i did not get a crash with dSprintf and dVsprintf (both not modified).
#2
Bottom line is, 4096 is the max. You may or may not experience a crash if you go beyond that; but you are asking for trouble unless you implement the fix I posted.
12/12/2009 (12:23 pm)
4096 is the printf buffer size in stock Torque. The problem is in the way dVsprintf works. The buffer size is passed to dVsprintf so dVsprintf won't write beyond the bounds of the buffer. That is good. Trouble is, if the string you pass in to printf exceeds the buffer size, dVstprintf will fill that buffer space right up to the end, without adding the proper null char to terminate the string. This MAY cause a crash later on when some function reads that buffer expecting a null-terminated string. Likely as not, as you discovered, the result will just be garbage chars at the end as the computer will just keep reading random memory until it hits a null char. Where you're likely to see a crash, I suspect, is when buffer is located near the end of the program's memory space. Linux at least will smack you down if you attempt to read outside of your program's allotted bounds. I'm not sure how strict Windows is about that, but I'm sure there's still potential for trouble. Bottom line is, 4096 is the max. You may or may not experience a crash if you go beyond that; but you are asking for trouble unless you implement the fix I posted.
#3
From what I'm reading in compiledEval.cc, it looks like there is no upper limit to string size in TorqueScript. The function validateBufferSize() should allocate more space as necessary.
Of course that doesn't mean that all function that use strings are unlimited. There are probably a number of places where an upper limit is imposed or where an oversize string could cause trouble in a particular function (like printf). Still, at least the core script compile/eval routines seem to be set up to handle whatever you throw at them.
12/12/2009 (12:56 pm)
NOTE: 4096 is the max printf size. That doesn't meant TorqueScript strings are limited to 4096 chars, only that you can't print strings > 4096 chars to the Console. (echo uses printf)From what I'm reading in compiledEval.cc, it looks like there is no upper limit to string size in TorqueScript. The function validateBufferSize() should allocate more space as necessary.
Of course that doesn't mean that all function that use strings are unlimited. There are probably a number of places where an upper limit is imposed or where an oversize string could cause trouble in a particular function (like printf). Still, at least the core script compile/eval routines seem to be set up to handle whatever you throw at them.
#4
Console.log displays the same amount of characters like before (max. 4096) but the length of the string (strlen) is 56000.
Engine modified. I am happy.
12/12/2009 (1:09 pm)
You are right. It seems to be that TGE can handle strings larger than 4096. I tried it with 56000 characters. Console.log displays the same amount of characters like before (max. 4096) but the length of the string (strlen) is 56000.
Engine modified. I am happy.
#5
12/12/2009 (1:12 pm)
Ups, sorry, my mistake. Console.log displays 4089 characters.
#6
I just ran:
for (%i = 0; %i < 8000; %i++) { $foo = $foo @ "a"; }
echo($foo);
and got the expected 4095 "a"s.
12/12/2009 (1:32 pm)
Curious. Are you sure? I'm getting the full 4095 chars printed in console.log (4095 because the last one has to be the null char).I just ran:
for (%i = 0; %i < 8000; %i++) { $foo = $foo @ "a"; }
echo($foo);
and got the expected 4095 "a"s.
#7
The bug is in line 7.
The echo-command has an additional string "Text: ". And these 6 characters are subtracted from 4095.
4095-6=4089.
12/12/2009 (2:48 pm)
I found the bug. I wrote this code: $text = "";
for ($i = 1; $i<8000; $i++)
{
$text = $text @ "a";
}
echo("Text: " @ $text);
echo("Length of $text is: " @ strlen($text));The bug is in line 7.
The echo-command has an additional string "Text: ". And these 6 characters are subtracted from 4095.
4095-6=4089.
Torque 3D Owner Thomas Bang
$text = ""; for ($i = 1; $i<4091; $i++) { $text = $text @ "a"; } echo("Text: " @ $text); echo("Length of $text is: " @ strlen($text));What i found out is the following: If the text has a length of 4090 (or more) characters, the output (echo command) has some cryptic characters at the end. Otherwise, 4089 (or less) characters are working like expected.