Topic: Possible strcmp bug


Author: dreamers@netcom.com (Earl and Carmella Brown)
Date: Sat, 23 Apr 1994 03:30:52 GMT
Raw View
Can anyone please help me out?  I just realized this after 5 years of
programming in C and C++:

#define NUM_WORDS 2
char* paszWords[NUM_WORDS]= {"Read", "Reading"};
...
/*
        This function will find the index of a given word in the list
paszWords.  This list can be easily modified, and the index value could be
used as a command ID.
-1 indicates word not found.
*/

int GetWordIndex(char* pszWordToTest)
{
   int index = NUM_WORDS - 1;
   while ( (index >=0) &&
           (strcmp(paszWords[index], pszWordToTest) != 0x00) ) {
      index++;
   }
   return index;
}

...
// pszCommand = "Read"; hypothetically...
   GetWordIndex(pszCommand);

/* end code fragment!!! */

Given this situation, I will get index of 1 every time!  I've tried it, and
both BORLAND and MSVC yield the same results.  This indicates to me that the
check for end of string is being done before the check for matching pair:

int strcmp(char* szSource, char* szDest)
{
   int nDifference = 0;
   while ( bDoing == TRUE ) {
      if ( !szSource[count] || !szDest[count] ) {
         bDoing = FALSE;
      }
      else {
         nDifference = szSource[count] - szDest[count];
         bDoing = nDifference == 0;
      }
   return nDifference;
}

Which means that ("Read" == "Reading")!!

Am I really off-base in this?  It seems to me that I'll have to make a
stringcmp macro that actually returns:

(strcmp(s1,s2)==0x00) && (strlen(s1) == strlen(s2))

IMHO, a serious waste of time!  I'd rather edit the source code...

Am I really off-base in this?  If so, someone please tell me where!

Earl
--
The house of love                      Dreamers@netcom.com




Author: kevlin@wslint.demon.co.uk (Kevlin Henney)
Date: Thu, 5 May 1994 09:59:04 +0000
Raw View
In article <dreamersCop0FH.LqK@netcom.com>
           dreamers@netcom.com "Earl and Carmella Brown" writes:

>Can anyone please help me out?  I just realized this after 5 years of
>programming in C and C++:
>
>#define NUM_WORDS 2
>char* paszWords[NUM_WORDS]= {"Read", "Reading"};
>....
>/*
>        This function will find the index of a given word in the list
>paszWords.  This list can be easily modified, and the index value could be
>used as a command ID.
>-1 indicates word not found.
>*/
>
>int GetWordIndex(char* pszWordToTest)
>{
>   int index = NUM_WORDS - 1;
>   while ( (index >=0) &&
>           (strcmp(paszWords[index], pszWordToTest) != 0x00) ) {
>      index++;
>   }
>   return index;
>}
>
>....
>// pszCommand = "Read"; hypothetically...
>   GetWordIndex(pszCommand);
>
>/* end code fragment!!! */
>
>Given this situation, I will get index of 1 every time!  I've tried it, and
[stuff deleted]

Is there any reason that you are starting your array search from 1
rather than 0?
--
Kevlin Henney

 In DOS, I know not why I am so sad,
 It wearies me; you say it wearies you;
 But how I caught it, found it, or came by it,
 What stuff 'tis made of, whereof it is born,
 I am to learn;

  The Merchant of Venice (I.i)




Author: fred@genesis.demon.co.uk (Lawrence Kirby)
Date: Fri, 6 May 1994 18:35:31 +0000
Raw View
In article <768131944snz@wslint.demon.co.uk>
           Kevlin@wslint.demon.co.uk "Kevlin Henney" writes:

>>int GetWordIndex(char* pszWordToTest)
>>{
>>   int index = NUM_WORDS - 1;
>>   while ( (index >=0) &&
>>           (strcmp(paszWords[index], pszWordToTest) != 0x00) ) {
>>      index++;
>>   }
>>   return index;
>>}
>>
>>....
>>// pszCommand = "Read"; hypothetically...
>>   GetWordIndex(pszCommand);
>>
>>/* end code fragment!!! */
>>
>>Given this situation, I will get index of 1 every time!  I've tried it, and
>[stuff deleted]
>
>Is there any reason that you are starting your array search from 1
>rather than 0?

There was a bug in the code - it was meant to scan the array backwards i.e.
index++ should have been index--.

--
-----------------------------------------
Lawrence Kirby | fred@genesis.demon.co.uk
Wilts, England | 70734.126@compuserve.com
-----------------------------------------




Author: fred@genesis.demon.co.uk (Lawrence Kirby)
Date: Sat, 23 Apr 1994 14:41:41 +0000
Raw View
In article <dreamersCop0FH.LqK@netcom.com>
           dreamers@netcom.com "Earl and Carmella Brown" writes:

>Can anyone please help me out?  I just realized this after 5 years of
>programming in C and C++:
  .
  .
>int GetWordIndex(char* pszWordToTest)
>{
>   int index = NUM_WORDS - 1;
>   while ( (index >=0) &&
>           (strcmp(paszWords[index], pszWordToTest) != 0x00) ) {
>      index++;
>   }
>   return index;
>}

This works for me if I change index++ to index--. I don't know of that is
just a typo or not but I would be surprised if you come up with any result
the way you have it. However it seems very unlikely that such a gross
bug would last very long in a strcmp implementation.

I'm curious as to why you compare the result of strcmp() against 0x00. This
is just the same as 0 and there is nothing particularly 'hexaddecimal' or
'binary' about the flavour of the return code.

--
-----------------------------------------
Lawrence Kirby | fred@genesis.demon.co.uk
Wilts, England | 70734.126@compuserve.com
-----------------------------------------