Hungarian notation causing buffer overruns?

Rob Earhardt had some interesting notes on Hungarian notation, and I thought I'll chime in. By the way, I am not against Hungarian, and I actually use it in my C++ code. It was harder to get rid of this habit when when I started programming in C# :-)

Anyway, back to the original point. Say that you have to use a fictive API which looks like this:

BOOL CompressStringIntoBuffer(
LPWSTR pwszString, // [in] string to be copied
INT cbMinData, // [in] minimum data that must be compressed
INT cbOutputBufferSize, // [in] size of the output buffer
      BYTE * pbOutputBuffer, // [out]
);

Description: The first parameter represents the string to be compressed. The second parameter, the minimum number of characters to be compressed, and the last two describing the output buffer.

This code attempts to compress the given string into the given buffer. It must compress at least the given number of characters. You write the code using this API (also documented in the same way) and everyting is nice.

But you discover later that you just introduced a buffer overrun. How come? The original intention of the developer that wrote the API was to write a different parameter name:

BOOL CompressStringIntoBuffer(
LPWSTR pwszString, // [in] string to be copied
INT cchMinCharacters, // [in] minimum data that must be compressed
      INT cbOutputBufferSize, // [in] size of the output buffer
      BYTE * pbOutputBuffer, // [out]
);

Now it's clear that the second parameter represents a number of wide characters, not of bytes. Ouch! In your code you might have passed, say, a size of 200 bytes instead of a count of 100 wide characters, therefore causing an internal buffer overrun.

What's worse, the documentation writer (the person who wrote the documentation for this fictive API) most likely copy-pasted the API definition directly in the help, propagating further this bogus parameter name. Note, however that the API description correctly identifies the second parameter as being a count of characters. 

In the end, as a developer, you must never assume that parameter names (and their hungarian accent) are 100% accurate...