Pelles C forum

Pelles C => Bug reports => Topic started by: huirad on May 26, 2010, 09:38:07 PM

Title: Bug in tchar.h?
Post by: huirad on May 26, 2010, 09:38:07 PM
Hi,

I think that there is a bug in tchar.h of PellesC 6.0

Compare line 222
  #define _stprintf  snprintf  /* 03-07-27: sprintf -> snprintf */
with the Microsoft definitions http://msdn.microsoft.com/en-us/library/ee479545.aspx or the Microsoft header files (e.g. from VS2008 Express).
In my opinion and with with personal experience,
  #define _stprintf  sprintf
would be correct.

Regards,

Helmut
Title: Re: Bug in tchar.h?
Post by: TimoVJL on May 27, 2010, 07:57:46 AM
From Microsoft:

swprintf has been changed to conform with the ISO C standard, adding an extra character count parameter.
To use traditional Microsoft swprintf, set _CRT_NON_CONFORMING_SWPRINTFS.
Title: Re: Bug in tchar.h?
Post by: huirad on June 03, 2010, 09:59:02 AM
Hi timovjl, thank you for your reply.

After reading the PellesC 2.5 to 2.6 (http://www.smorgasbordet.com/pellesc/changes_250_260.htm) change note and the MSDN (http://msdn.microsoft.com/en-us/library/ybk95axf%28VS.80%29.aspx), I understand that the objective in mapping _stprintf to snprintf when _UNICODE is not defined is to have it compatible wit the _UNICODE variant where it is mapped to swprintf.

Unfortunately, swprintf has a different signature for Windows and Windows CE. See wchar.h (from Pelles C 6.0):

#ifndef _WINCE
_CRTCHK(printf,3,4) int __cdecl swprintf(wchar_t * restrict, size_t, const wchar_t * restrict, ...);
//...
#else /* _WINCE */
int __cdecl swprintf(wchar_t *, const wchar_t *, ...);  /* WinCE */
//...
#endif /* _WINCE */

I.e. swprintf is lacking the size_t parameter for WinCE (see also here (http://msdn.microsoft.com/en-us/library/ms860374.aspx)).

So while
#define _stprintf  snprintf  /* 03-07-27: sprintf -> snprintf */
improves consistency for Win32, it reduces consistency for WinCE applications.

Since _stprintf is Microsoft specific and Microsoft sticks to
#define _stprintf       sprintf
I would prefer PellesC to go the same way.

Note: I came across this, because my use case is to compile my applications from the same source for Win32 (without _UNICODE) and WinCE (with _UNICODE) and I need a generic version for sprintf. If I had needed a generic version of snprintf, I would have chosen _sntprintf. So now I am limited in my choice which I don't like :'(. I guess I will change my code to use _sntprintf instead of _stprintf to work around that problem.

Nevertheless this is only a small issue in an otherwise great Compiler. Pelle: thank you!