Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Re: CW2A Broken.

37 views
Skip to first unread message

David Lowndes

unread,
Jun 7, 2005, 6:21:20 PM6/7/05
to
>Tracing through CW2AEX<>, it passes an output size to WideCharToMultiByte of
>(Unicode chars) * 2.
>
>This is obviously naive. Different encodings use different lengths (UTF8,
>for example, allows up to 4 bytes per character).

Tim,

Looking at the VS2005 B2 source code, it has been changed from the
VS2003 code. The relevant part of the Init method now looks like this:

int nLengthA = nLengthW*4;

AtlConvAllocMemory(&m_psz,nLengthA,m_szBuffer,t_nBufferLength);

BOOL bFailed=(0 == ::WideCharToMultiByte( nCodePage, 0, psz, nLengthW,
m_psz, nLengthA, NULL, NULL ));
if (bFailed)
{
if (GetLastError()==ERROR_INSUFFICIENT_BUFFER)
{
nLengthA = ::WideCharToMultiByte( nCodePage, 0, psz,
nLengthW, NULL, 0, NULL, NULL );

AtlConvAllocMemory(&m_psz,nLengthA,m_szBuffer,t_nBufferLength);
bFailed=(0 == ::WideCharToMultiByte( nCodePage, 0,
psz, nLengthW, m_psz, nLengthA, NULL, NULL ));
}
}

Dave
--
MVP VC++ FAQ: http://www.mvps.org/vcfaq

Igor Tandetnik

unread,
Jun 8, 2005, 12:07:07 AM6/8/05
to
"Tim Kientzle" <TimKi...@discussions.microsoft.com> wrote in message
news:9624E304-83C9-40DA...@microsoft.com

> Tracing through CW2AEX<>, it passes an output size to
> WideCharToMultiByte of (Unicode chars) * 2.
>
> This is obviously naive. Different encodings use different lengths
> (UTF8, for example, allows up to 4 bytes per character).

It's not that bad - codepoints that require 4 bytes in UTF-8 also
require 2 WCHARs in UTF-16 (which is what is meant by Unicode in Win32
API). Your point is still valid of course - some 16-bit codepoints
require 3 bytes in UTF-8, and those would overflow the buffer.
--
With best wishes,
Igor Tandetnik

With sufficient thrust, pigs fly just fine. However, this is not
necessarily a good idea. It is hard to be sure where they are going to
land, and it could be dangerous sitting under them as they fly
overhead. -- RFC 1925


Maciej Mróz

unread,
Jun 8, 2005, 3:19:02 AM6/8/05
to
Użytkownik Tim Kientzle napisał:

> Tracing through CW2AEX<>, it passes an output size to WideCharToMultiByte of
> (Unicode chars) * 2.


It's bug in ATL. I remember reporting it some time ago, in company where
I work we have been working on patched version for a long time now (our
code has hit that issue when converting Chinese text into utf-8). Nice
to hear (in some other mail) vs2005 finally fixed it.

Maciej Mróz

0 new messages