The International Simutrans Forum

Simutrans Extended => Simutrans-Extended bug reports => Simutrans-Extended development => Simutrans-Extended closed bug reports => Topic started by: RESTRICTED ACCOUNT on May 01, 2018, 11:24:59 AM

Title: Text garbling in map window.
Post by: RESTRICTED ACCOUNT on May 01, 2018, 11:24:59 AM
Text garbling in map window and this occur in any language. Click "show map scale button", and see under the color scale bar.
This display is not in Simutrans-standard, so it is a unique issue of Simutrans-Extended.

(https://simutrans-germany.com/files/upload/text_garbling.png)
(´・ω・`)
Title: Re: Text garbling in map window.
Post by: DrSuperGood on May 01, 2018, 02:32:20 PM
It is called stack garbage. In my case resizing the window caused it to show all kinds of crazy text!

if(scale_visible) {
char scale_text[160] = "";
if(1000 % welt->get_settings().get_meters_per_tile() == 0)
{
// Can use integer
sprintf(scale_text, "%i %s %s", (uint16)(1000 / welt->get_settings().get_meters_per_tile()), translator::translate("tiles"), translator::translate("per 1 km"));
}
else
{
// Otherwise, must use float
sprintf(scale_text, "%f %s %s", (1000.0 / welt->get_settings().get_meters_per_tile()), translator::translate("tiles"), translator::translate("per 1 km"));
}
tile_scale_label.set_text(scale_text, false);
}

Kind of obvious why this is happening. The set_text method takes a pointer to a C string to use. Labels do not retain a copy of the C string they were set to, rather keep a pointer to an externally managed C string. In this case you set the label to source its string from the stack. Since the stack frame is destroyed and overwritten, the result is that the label is trying to print out stack garbage.

The solution is to either make the scale_text string buffer static or a member of the map frame. If making it a member of the map frame, one might as well make it a C++ String object, so that memory is managed better.
Title: Re: Text garbling in map window.
Post by: jamespetts on May 04, 2018, 12:31:40 AM
Splendid, thank you both. I think that I have fixed this based on Dr. Supergood's advice, but I cannot test this, as I could not reproduce the original problem with my debug build. I should be grateful if you could re-test with to-morrow's nightly build.
Title: Re: Text garbling in map window.
Post by: DrSuperGood on May 04, 2018, 05:58:10 AM
Appears fixed.

The likely reason that you could not reproduce it in debug build is that the stack address layout is likely different than release with some variables being optimized away completely in release. Since the entire string was not being overwritten it points towards it being placed very high on the stack, probably so high in the debug builds nothing else was touching that part of the stack.
Title: Re: Text garbling in map window.
Post by: RESTRICTED ACCOUNT on May 04, 2018, 09:03:18 AM
I confirmed this has been fixed too.
Thank you for fixing this.

(´・ω・`)