The International Simutrans Forum

Development => Technical Documentation => Topic started by: Vonjo on January 21, 2013, 05:37:28 AM

Title: gui/money_frame.cc error
Post by: Vonjo on January 21, 2013, 05:37:28 AM
I got this error:


===> CXX gui/money_frame.cc
gui/money_frame.cc: In member function 'virtual void money_frame_t::zeichnen(koord, koord)':
gui/money_frame.cc:439:66: error: format not a string literal and no format arguments [-Werror=format-security]
cc1plus: some warnings being treated as errors
make[1]: *** [build/default/gui/money_frame.o] Error 1


https://launchpadlibrarian.net/128868183/buildlog_ubuntu-precise-i386.simutrans_112.1-1ppa1_FAILEDTOBUILD.txt.gz

In money_frame.cc:439 :
sprintf( str_buf[15], translator::translate("Scenario lost!") );
Title: Re: gui/money_frame.cc error
Post by: Ters on January 21, 2013, 05:46:50 AM
I don't get that error. I don't see why it should be an error either. It barely deserves to be a warning, which I don't get either.
Title: Re: gui/money_frame.cc error
Post by: Dwachs on January 21, 2013, 07:39:01 AM
If the translate-call returns a string which has format specifiers (think of %s) then this piece of code could make problems. Afaict the translator code checks for wrong formt specifiers.

This sprintf call could be strcpy ...
Title: Re: gui/money_frame.cc error
Post by: Ters on January 21, 2013, 04:32:46 PM
Quote from: Dwachs on January 21, 2013, 07:39:01 AM
If the translate-call returns a string which has format specifiers (think of %s) then this piece of code could make problems. Afaict the translator code checks for wrong formt specifiers.

Hence the barely.

I'm still not sure why the compiler is so upset about this. Is passinng dynamic strings, but completely forgetting the format arguments so much more common than passing too few format arguments? Or would it error even if format arguments were passed? If so, then there are lots of problematic *printf calls in Simutrans.
Title: Re: gui/money_frame.cc error
Post by: Vonjo on January 21, 2013, 05:47:38 PM
Quote from: Ters on January 21, 2013, 05:46:50 AM
I don't get that error. I don't see why it should be an error either. It barely deserves to be a warning, which I don't get either.
Yeah, some system treat it as error, some just treat it as warning.
I managed to resolve it by adding -Wno-error=format-security flag.
But is it really a problem?

However, from the buildlog, I can see that -Wformat-security warning only raised once (in gui/money_frame.cc:439:66).
Title: Re: gui/money_frame.cc error
Post by: Ters on January 21, 2013, 07:39:59 PM
That means the compiler freaks out on just one of, for all it knows, multiple possible places for format argument mismatch errors. I see that line 444 use tstrncpy, so doing the same on 439 shouldn't be a problem.

One should perhaps also replace all those sprintf calls with calls to snprintf.
Title: Re: gui/money_frame.cc error
Post by: Dwachs on January 21, 2013, 08:20:29 PM
changed with r6296.
Title: Re: gui/money_frame.cc error
Post by: Vonjo on January 24, 2013, 03:46:31 PM
Thank you. :)