Segmentation error when using regexec / strtok_r in C

I am having trouble figuring out where and why I am getting a segmentation error.

I am writing C code that prompts the user to enter a regular expression and compile it, and then enter a line with several sentences:

int main(void){ char RegExp[50]; regex_t CompiledRegExp; char *para; char delim[] = ".!?,"; char *sentence; char *ptr1; printf("Enter regular expression: "); fgets(RegExp, 50, stdin); if (regcomp(&CompiledRegExp,RegExp,REG_EXTENDED|REG_NOSUB) != 0) { printf("ERROR: Something wrong in the regular expression\n"); exit(EXIT_FAILURE); } printf("\nEnter string: "); 

strtok_r is used to split the string into any of the following delimiters.,?! and then the received token (sentence) is used as a string parameter in the regexec function, which searches for it to see if the previously expressed regular expression contains the token:

 if( fgets(para, 1000, stdin)){ char *ptr = para; sentence = strtok_r(ptr, delim, &ptr1); while(sentence != NULL){ printf("\n%s", sentence); if (regexec(&CompiledRegExp,sentence,(size_t)0,NULL,0) == 0) { printf("\nYes"); } else { printf("\nNo"); } ptr = ptr1; sentence = strtok_r(ptr, delim, &ptr1); } } regfree(&CompiledRegExp); } 

This is probably the stupid mistake I am making, but any help in identifying the reasons for segfaul would be greatly appreciated!

EDIT: Moved regfree to a more appropriate location. However, segfault is still happening. I am sure this is due to how the regular expression is read or how it is compared in regexec . Incredible, however.

+6
source share
3 answers

Instead of this:

 char *para; fgets(para, 1000, stdin); 

Write this:

 char para[1000]; fgets(para, 1000, stdin); 

In the first version, para is a pointer that points somewhere in memory, and a string entered by the user is written somewhere to this. Most likely, para points to an invalid address that immediately flushes your program.

+3
source

You called regfree inside a loop. The second time in a loop, you call regexec on freed memory with undefined behavior.

+2
source

You are using strtok_r() incorrectly.

To strtok_r() string using strtok_r() , in the first call, the first argument is a pointer to the string you want to parse. Subsequent calls to strtok_r() to parse the same line must have NULL as the first argument. What are you doing:

 ptr = ptr1; sentence = strtok_r(ptr, delim, &ptr1); 

doesn't make sense.

0
source

All Articles