0

I'm relatively new to C and I have a problem with the following:

 regex_t re;
 regmatch_t matches[2];
 int start;
 int end;
 int count = 0;

 int reti = regcomp(&re, "^(?:[a-zA-Z]|[a-zA-Z](?:[a-zA-Z0-9\\-])*[a-zA-Z0-9])(?:\\.(?:[a-zA-Z]|[a-zA-Z](?:[a-zA-Z0-9\\-])*[a-zA-Z0-9]))*$", REG_EXTENDED);

 while (1) {

   printf("Local = %s\n", local);
   reti = regexec(&re, local, 2, matches, 0);

  // More code here

 }

When I run this, I get a segmentation fault.

local was defined as char *local and is being printed out correctly.

I ran the code using GDB and it turns out the issue is arising at the line:

reti = regexec(&re, local, 2, matches, 0);

I can't seem to figure out why.

This is the output from gdb:

Program received signal SIGSEGV, Segmentation fault.
0x00007ffff7af8d42 in regexec () from /lib/x86_64-linux-gnu/libc.so.6
(gdb) backtrace
#0  0x00007ffff7af8d42 in regexec () from /lib/x86_64-linux-gnu/libc.so.6
#1  0x000000000040092d in email_in (str=0x400aa8 "giri@gmail.com") attestdriver.c:70
#2  0x0000000000400780 in main () at testdriver.c:15

Would anyone have any ideas as to what might be the issue?

Thanks for your help.

1 Answers1

1

The problem is actually here:

int reti = regcomp(&re, "^(?:[a............

From here:

You need to check the return of regcomp, it will tell you that your regex is invalid.

You will get Invalid preceding regular expression

(?: is not supported by POSIX regexes.


It's throwing this error because:

A common way to get a segfault is to dereference a null pointer

(From here)

The regex cannot initialize because of the illegal (?:

The original error is shown in the return of regcomp, and does not halt execution.

Community
  • 1
  • 1
Laurel
  • 5,965
  • 14
  • 31
  • 57