My Function...
Code:
PHP Code:
int findpattern( char * source, char * start, char * end)
{
char * start_ptr = strstr(source, start);
if (start_ptr != NULL)
{
char * end_ptr = strstr(start_ptr, end);
if (end_ptr != NULL)
{
start_ptr += strlen(start);
end_ptr -= 1;
printf("start_ptr = %c\n", *start_ptr);
printf("end_ptr= %c\n", *end_ptr);
printf("findpattern: start = %d\n", start_ptr);
printf("findpattern: end= %d\n", end_ptr);
printf("= %d", (end_ptr-start_ptr));
/* I must write one more byte to avoid this heap corruption.? Why is this. I included the terminating character at the end. */
//char * textinpattern = (char*) malloc((end_ptr-start_ptr)+1);
/* the fix... */
char * textinpattern = (char*) malloc((end_ptr-start_ptr)+2);
memcpy(textinpattern, start_ptr, (end_ptr-start_ptr)+1);
memcpy(textinpattern+(int)(end_ptr-start_ptr)+1, "\0", 1);
printf("%s", textinpattern);
free (textinpattern);
return 1;
}
}else{
return 0;
}
}
So I have to write one more byte of memory? I don't see why.. here is the problem code
Code:
PHP Code:
char * textinpattern = (char*) malloc((end_ptr-start_ptr)+1);
I included the terminating character to end the string. ? OR.. did I miss count