Microsoft KB Archive/167326: Difference between revisions

From BetaArchive Wiki
(importing KB archive)
 
m (Text replacement - """ to """)
 
(2 intermediate revisions by the same user not shown)
Line 61: Line 61:
* Do not use the intrinsic version of strcmp.
* Do not use the intrinsic version of strcmp.


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


</div>
</div>
Line 84: Line 84:


<pre class="codesample">  /* Build Options: /Oi */  
<pre class="codesample">  /* Build Options: /Oi */  
   #include &lt;stdio.h&gt;
   #include <stdio.h>
   #include &lt;string.h&gt;
   #include <string.h>
   // uncomment the following line for workaround #2
   // uncomment the following line for workaround #2
   //#pragma function(strcmp)
   //#pragma function(strcmp)
Line 91: Line 91:
   int main(void)
   int main(void)
   {
   {
       char somestr [15]  = &quot;Some String&quot;;
       char somestr [15]  = "Some String";


       somestr[1] = 0;
       somestr[1] = 0;


   // change the following to strcmp(somestr,&quot;S&quot;) for workaround #1
   // change the following to strcmp(somestr,"S") for workaround #1
       if (strcmp(somestr,&quot;S\0&quot;) == 0)
       if (strcmp(somestr,"S\0") == 0)
         printf(&quot;match: correct\n&quot;);
         printf("match: correct\n");
       else
       else
         printf(&quot;no match: incorrect\n&quot;);
         printf("no match: incorrect\n");
       return 0;
       return 0;
   }
   }
                 </pre>
                 </pre>
Note that a more common use of an embedded Null character may be to compare a string to &quot;\0&quot; to see if it is an empty string. For the first workaround above, compare it to &quot;&quot; instead.
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 the first workaround above, compare it to "" instead.


</div>
</div>

Latest revision as of 11:03, 21 July 2020

Knowledge Base


Article ID: 167326

Article Last Modified on 12/10/2003



APPLIES TO

  • Microsoft Visual C++ 5.0 Enterprise Edition
  • Microsoft Visual C++ 6.0 Enterprise Edition
  • Microsoft Visual C++ 5.0 Professional Edition
  • Microsoft Visual C++ 6.0 Professional Edition
  • Microsoft Visual C++ 6.0 Standard Edition



This article was previously published under Q167326

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

Use one of the following two workarounds:

  • Do not explicitly reference the Null character.
  • Do not use the intrinsic version of strcmp.

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

STATUS

Microsoft has confirmed that this is a bug in the Microsoft products that are listed at the beginning of this article.

This problem was corrected in Microsoft Visual C++ .NET.


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 the first workaround above, compare it to "" instead.

Keywords: kbbug kbfix kbnoupdate kbprogramming kbcode KB167326