12 octobers patches _vscwprintf( pszFormat, args ); difference!

  • Thread starter Thread starter Egbert Nierop \(MVP for IIS\)
  • Start date Start date
E

Egbert Nierop \(MVP for IIS\)

Am I the only one?

After having installed the patches as a brave MS citycen :),
this function suddenly returns one less then before or possible
vswprintf assumes the given length including the terminating zero.
 
Am I the only one?

After having installed the patches as a brave MS citycen :),
this function suddenly returns one less then before or possible
vswprintf assumes the given length including the terminating zero.

Can you demonstrate this in a little console program?
 
Doug Harrison said:
Can you demonstrate this in a little console program?

ps: I established this bug. It is not windows but VC 8 which behaves
different with VC 7.
I have mailed this to Michael M (Microsoft).
BSTR __cdecl Format(PCWSTR pszFormat, ...) throw()

{

va_list args;

HRESULT hr = S_OK;

BSTR retVal = NULL;
va_start( args, pszFormat );

int len = _vscwprintf( pszFormat, args );

retVal = SysAllocStringLen(NULL, len);

if(retval != NULL)

vswprintf( retVal, (SIZE_T)len, pszFormat, args );

va_end(args);


return retVal;

}


INT main()

{

CoInitialize(NULL);

{

BSTR test = Format(L"%s, %d,%s", L"one", 2, L"three");

wprintf(test); //"output one, 2, thre" <-- BUG this used to have the
correct length in VC 7?

SysFreeString(test);

}

CoUninitialize();

return 0;

}
 
Hi Egbert!
ps: I established this bug. It is not windows but VC 8 which behaves
different with VC 7.
I have mailed this to Michael M (Microsoft).
BSTR __cdecl Format(PCWSTR pszFormat, ...) throw()
{
va_list args;
HRESULT hr = S_OK;
BSTR retVal = NULL;
va_start( args, pszFormat );
int len = _vscwprintf( pszFormat, args );
retVal = SysAllocStringLen(NULL, len);
if(retval != NULL)
vswprintf( retVal, (SIZE_T)len, pszFormat, args );
va_end(args);
return retVal;
}

If it had worked in VC7(.1) then it was a bug in VC7(.1) and is now
corrected in VC8 :-)

The following line is wrong:
int len = _vscwprintf(pszFormat, args);

You should change it to
int len = _vscwprintf(pszFormat, args) + 1;

The reason is:
_vscprintf doesn't count the terminating '\0'!
but vswprintf requires the terminating NUL in in the count.

--
Greetings
Jochen

My blog about Win32 and .NET
http://blog.kalmbachnet.de/
 
Jochen Kalmbach said:
Hi Egbert!


If it had worked in VC7(.1) then it was a bug in VC7(.1) and is now
corrected in VC8 :-)

Great! :<

Counting on bugs helps one to improve his skills :)
 
Back
Top