// If this happens, I am going to quit and become a barista

Raymond Chen

Raymond

While chasing down a bug, I ran across this comment:

// Arbitrary cap on message length.
// If you change the format string, then update this to match.
// (Although, if we ever need to place a million icons on the desktop,
// I am going to quit and become a barista.)

But the buffer was not big enough. Should I suggest to the developer that they check if Starbucks is hiring?

The buffer wasn’t big enough because the format string was incorrect.

constexpr wchar_t formatString[] = L"(%f3.3,%f3.3)";
constexpr size_t worstCaseFormat = ARRAYSIZE(L"(000.000,000.000)");

I guess they never looked at their log file, because the format string is wrong. For an icon at position (10, 10), the resulting log message is (10.0000003.3,10.0000003.3), which is longer than the allotted worst-case string.

That’s because the format string %f3.3 is interpreted as

  • %f: A formatted floating point number, with a default of six places after the decimal.
  • 3.3: The literal characters 3.3.

The format string was intended to be %3.3f, which means a formatted floating point number with a minimum of three characters of output and exactly three places after the decimal.

That would result in (10.000,10.000).

Note that if you ask for three places after the decimal, then you’re going to get at least four characters of output anyway, so the first 3 in %3.3f is meaningless. It doesn’t cause any harm, but it doesn’t do anything either. If they wanted a fixed-width format, then they could have used %7.3f, which would space-pad the value on the left to ensure three characters before the decimal.

Raymond Chen
Raymond Chen

Follow Raymond   

0 comments

Comments are closed.