News:

Download Pelles C here: http://www.smorgasbordet.com/pellesc/

Main Menu

swprintf question

Started by John Z, June 21, 2025, 12:02:04 PM

Previous topic - Next topic

John Z

An accidental deletion when cleaning up some code found that -

swprintf(zmsg,-1,L"%s",Info.Procedure);

does not raise an error or warning.  The original line was -

swprintf(zmsg,MaxZmsg-1,L"%s",Info.Procedure);

So looks like the -1 is interpreted as max int ?  This defeats the limit restriction I guess -

Both versions put the string into zmsg variable, which fortunately in this case is much larger than the incoming string would ever be so no overrun.

Feature or Bug  :)  ?

John Z

Vortex

Hi John,

Could you send the complete code here?
Code it... That's all...

John Z

Hi Vortex,

Here is a mini project for it.

John Z

Pelle

Quote from: John Z on June 21, 2025, 12:02:04 PMFeature or Bug  :)  ?
Yes.

For historical reasons in Microsoft mode, there is no diagnostic for the implicit conversion from -1 to -1U (or 0xFFFFFFFF...). A modern version of the MSVC compiler seems happy to complain here, so perhaps this should be changed.

Another chance to detect a problem with your code is in the static code analyzer (bounds checker). This fails because -1U in this case is reported internally as a store size of 0, meaning "don't know". This is a problem that should be fixed, if I can figure out an easy way to do so.

/Pelle

John Z

Thanks for the explanation Pelle!

👍

John Z