BUG: Intrinsic Version of strcmp May Return Incorrect Result

Last reviewed: July 31, 1997
Article ID: Q167326
The information in this article applies to:
  • Microsoft Visual C++, 32-bit Editions, version 5.0

SYMPTOMS

Explicitly referencing the Null character in the second argument of strcmp causes the intrinsic version of strcmp to incorrectly report the two arguments are not equal. Please see the sample code below.

RESOLUTION

There are two workarounds:

  1. Do not explicitly reference the Null character.

  2. Do not use the intrinsic version of strcmp.

Please see the "MORE INFORMATION" section and sample code below.

STATUS

Microsoft has confirmed this to be a bug in the Microsoft products listed at the beginning of this article. We are researching this bug and will post new information here in the Microsoft Knowledge Base as it becomes available.

MORE INFORMATION

You can enable intrinsic functions either by using of the /Oi compiler switch or #pragma intrinsic. To disable intrinsic functions, remove the /Oi compiler switch, add /Oi-, or use #pragma function to force a function call on a function by function basis. Please see the sample code below for use of the #pragma function.

Sample Code

   /* Build Options: /Oi */
   #include <stdio.h>
   #include <string.h>
   // uncomment the following line for workaround #2
   //#pragma function(strcmp)

   int main(void)
   {
      char somestr [15]  = "Some String";

      somestr[1] = 0;
   // change the following to strcmp(somestr,"S") for workaround #1
      if (strcmp(somestr,"S\0") == 0)
         printf("match: correct\n");
      else
         printf("no match: incorrect\n");
      return 0;
   }

Note that a more common use of an embedded Null character may be to compare a string to "\0" to see if it is an empty string. For workaround #1, compare it to "" instead.
Keywords          : CRTIss vcbuglist500 kbcode kbprg kbbuglist
Version           : 5.0
Platform          : NT WINDOWS
Issue type        : kbbug


================================================================================


THE INFORMATION PROVIDED IN THE MICROSOFT KNOWLEDGE BASE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. MICROSOFT DISCLAIMS ALL WARRANTIES, EITHER EXPRESS OR IMPLIED, INCLUDING THE WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL MICROSOFT CORPORATION OR ITS SUPPLIERS BE LIABLE FOR ANY DAMAGES WHATSOEVER INCLUDING DIRECT, INDIRECT, INCIDENTAL, CONSEQUENTIAL, LOSS OF BUSINESS PROFITS OR SPECIAL DAMAGES, EVEN IF MICROSOFT CORPORATION OR ITS SUPPLIERS HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. SOME STATES DO NOT ALLOW THE EXCLUSION OR LIMITATION OF LIABILITY FOR CONSEQUENTIAL OR INCIDENTAL DAMAGES SO THE FOREGOING LIMITATION MAY NOT APPLY.

Last reviewed: July 31, 1997
© 1998 Microsoft Corporation. All rights reserved. Terms of Use.