Reynard 0 Posted June 11, 2012 Report Share Posted June 11, 2012 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 Quote Link to post Share on other sites
Reynard 0 Posted June 13, 2012 Author Report Share Posted June 13, 2012 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 Quote Link to post Share on other sites
Dave 0 Posted June 16, 2012 Report Share Posted June 16, 2012 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 Quote Link to post Share on other sites
JorgeF 0 Posted June 16, 2012 Report Share Posted June 16, 2012 Hi Dave Should we use the code you posted to patch up the include/libs in the SourceBoost 7.05 instalation? Thank you. Best regards Jorge Quote Link to post Share on other sites
Pavel 0 Posted June 17, 2012 Report Share Posted June 17, 2012 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 Quote Link to post Share on other sites
Reynard 0 Posted June 27, 2012 Author Report Share Posted June 27, 2012 Dave, Pavel, Thanks for the fix for this function. Project ticking away nicely using 7.10RC Regards Reynard Quote Link to post Share on other sites
Recommended Posts
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.