Pelles C forum

C language => Expert questions => Topic started by: John Z on June 21, 2025, 12:02:04 PM

Title: swprintf question
Post by: John Z on June 21, 2025, 12:02:04 PM
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
Title: Re: swprintf question
Post by: Vortex on June 21, 2025, 01:12:48 PM
Hi John,

Could you send the complete code here?
Title: Re: swprintf question
Post by: John Z on June 21, 2025, 03:13:34 PM
Hi Vortex,

Here is a mini project for it.

John Z
Title: Re: swprintf question
Post by: Pelle on June 21, 2025, 03:58:30 PM
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.

Title: Re: swprintf question
Post by: John Z on June 22, 2025, 02:14:12 AM
Thanks for the explanation Pelle!

👍

John Z