BUG SPOTTING answers

by Michael S. Kaplan, published on 2010/06/26 07:01 -04:00, original URI: http://blogs.msdn.com/b/michkap/archive/2010/06/26/10028278.aspx


So.

It was several weeks ago, in BUG SPOTTING: Identify 1) what the code does, and 2) what they wanted it to do, that I challenged people to explain something about some code, as indicated by the title.

This code:

whcar_t* newParams = new wchar[MAX_PATH + MAX_PATH]; whcar_t* p = newParams; while (*params) {
    *p++ = *params;
    params = ::CharNext(params); 
}
*p = 0;
return newParams; 

in fact.

I actually cheated a little here. :-)

When I suggested The torrents of breaking CharNext/CharPrev as a refresher what I didn't say is that the bug in some code that caused Raymond Chen to in passing notice this other bug in NLS code looked almost identical to the current bug here. :-)

In essence the intent of this code was to be some kind of really smart allocating wmemcpy or wcscpy in C++.

But unfortunately, it was being so clever in its iteration that it had to use CharNextW rather than ++ for iterating the string, even though it clearly thought they were the same thing.

And they are not, whether there is a bug like the one in The torrents of breaking CharNext/CharPrev or not!

The reasoning for the separate function is unclear though, as is the need to allocate something of size MAX_PATH + MAX_PATH.

That is either twice the path you need or not even close to the amount of path you need, depending on who is using the function.

And none of which would perform better than an inlined wmemcpy of a function anyway. :-)


no comments

Please consider a donation to keep this archive running, maintained and free of advertising.
Donate €20 or more to receive an offline copy of the whole archive including all images.

referenced by

2010/06/27 Bugs hidden in plain sight, and commented that way too ANSWERS

go to newer or older post, or back to index or month or day