Jump to content

Strstr Giving False Result


Recommended Posts

Searching a string within a string fails if sub-string is in rom.

 

This code incorrectly finds a match. Sub-string in rom.

rom char *MSG_REC_READ = "REC READ";

strcpy(receiveBuffer,"+CMGR: 'REC UNREAD','+441234567890',,'12/06/11,13:37:48+04");
pBuff = strstr(receiveBuffer, MSG_REC_READ);

 

This code does not find a match, as it shouldn't. Sub-string in ram.

strcpy(receiveBuffer,"+CMGR: 'REC UNREAD','+441234567890',,'12/06/11,13:37:48+04");
pBuff = strstr(receiveBuffer, "REC_READ");

 

A pointer is not being reset on an unmatched character somewhere after a match has started.

Unmatched characters are just being skipped.

 

SB 7.05.1

Windows XP SP3

PIC18F26K22

 

Cheers

 

Reynard

Link to post
Share on other sites

For those who need an un-official fix and with source code for libc, modify the code for strstr with sub-string in rom to:

for( ; ( ptr1 = strchr( ptr1, ptr2[0] ) ) != NULL; ptr1++ )
{
 for( i=0, j=0 ; ; )

Both for loops modified.

 

Cheers

 

Reynard

Link to post
Share on other sites

Reynard,

 

For those who need an un-official fix and with source code for libc, modify the code for strstr with sub-string in rom to:
 for( ; ( ptr1 = strchr( ptr1, ptr2[0] ) ) != NULL; ptr1++ ) { for( i=0, j=0 ; ; ) 

Both for loops modified. Cheers Reynard

 

Nice simple fix.

I didn't quite see all your changes so thought it was still broken.

I ended up with a completely new version that uses less code and executes faster - see below:

 

char* strstr( const char *ptr1, rom char *ptr2 )
{
char *uPtr1;
char c1, c2_1st, c2;

// to save constant lookup of first character of string we are trying
// to find take a copy of it
c2_1st = ptr2[ 0 ];

// Nothing to find
if( c2_1st == '\0' )
 return (char *)ptr1; // casting constness away - not really a good idea!

for( ; c1 = *ptr1, c1 != '\0'; ptr1++ )
{
 // when first character matches, we need to examine further to check all match
 if( c1 == c2_1st )
 {  
  uPtr1 = ptr1; // take copy of pointer, so search can continue from this point if needed

  // rom char idexes are only 8 bit
  unsigned char romCharIdx = 0;
  while( 1 )
  {
// get next character of string to find
c2 = ptr2[ ++romCharIdx ];

// see if match completed successfully,
// ie no more characters left string we are trying to match
if( c2 == '\0' )  return (char* )ptr1; // casting constness away - not really a good idea!

// get next character of we are searching in
uPtr1++;
c1 = *uPtr1;

// if characters are not the same, matching has failed.
if( c1 != c2 ) break;

  }
 }
}

// finding string failed
return NULL;
}

 

Regards

Dave

Link to post
Share on other sites

Should we use the code you posted to patch up the include/libs in the SourceBoost 7.05 instalation?

Thank you.

 

Please do. This fix will be included into the coming 7.10 release.

 

Regards,

Pavel

Link to post
Share on other sites
  • 2 weeks later...

Join the conversation

You are posting as a guest. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
×
×
  • Create New...