'right' does not always equal 'smart' #1

by Michael S. Kaplan, published on 2006/01/25 15:46 -05:00, original URI: http://blogs.msdn.com/b/michkap/archive/2006/01/25/517586.aspx


I can think of one crucial scenario where the right behavior is not always what is conventionally considered the smart behavior by reasonable people.

That scenario is appcompat/backcompat.

The code may be doing something that is completely wrong and weird, but someone may be relying on it. So we keep in the wrong behavior.

This of course happens often, which is why the title has a #1 there -- I anticipate that I'll be giving more examples in the future. :-)

So, today's example takes us into the new FindNLSString function in Vista, and its ancestors (the managed IsPrefixIndexOf, LastIndexOf methods and particularly the IsSuffix method).

You see, somebody decided a long time ago that the empty string is the suffix of every other string. I guess it is hard to argue that the empty string is not in fact a suffix since it is not not visible....

But when you apply that to FindNLSString with the FIND_ENDSWITH flag (which returns an index), it gets weirder. In order to meet this requirement, it has to return an index value that is one greater than the last character of the string (a length defined in the cchSource parameter).

Now I suppose this is somewhat mitigated by the fact that the pcchFound parameter will indicate that the length of the string that was found was also zero. So that people who are using the function attentively will not be creating their own personal buffer overrun.

Unfortunately, there are apparently managed applications that rely on this functionality (there was a large customer app which reportedly would not boot when the behavior was changed). And IsSuffix depends on FindNLSString with the FIND_ENDSWITH flag when supporting Windows-only cultures in Whidbey running on Vista (it is the real reason why the function was added!). So even though it is not very 'smart', it is in some sense the 'right' thing to do.

But it definitely makes paying attention to that pcchFound parameter worthwhile!

 

This post brought to you by "e" (U+0065, a.k.a. LATIN SMALL LETTER E)


# Michael S. Kaplan on 25 Jan 2006 3:51 PM:

Note that the strstr behavior actually always assumes it is a prefix so the suffix issue never comes up....

# Gabe on 26 Jan 2006 3:43 AM:

I'm confused. Why would IsSuffix use the FIND_STARTSWITH flag? The docs you link to state the flag "Tests whether lpStringValue is a prefix of lpStringSource".

# Michael S. Kaplan on 26 Jan 2006 4:41 AM:

Sorry Gabe -- a mistyped. It is the FIND_ENDSWITH flag that IsSuffix uses....

# Phylyp on 26 Jan 2006 9:32 AM:

Reasons you're similar to Raymond:
1. Your blogs on erroneous APIs being carried forward for backward-compat.
2. Most of what you blog about goes right over my head.
3. What little I understand just exemplifies the fact that I have a long way to go before reaching the professional standards you set.
4. Reading the various gotchas ( http://blogs.msdn.com/michkap/archive/2005/10/15/481314.aspx ) on your blogs makes me remember my earlier programs that implement the erroneous method described

~Phylyp

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