regexec from regex.h Segmentation fault

A

al.moorthi

the below program is working in Suse and not working on Cent 5:
can any body have the solution ?

#include <regex.h>
#include <stdlib.h>
#include <stdio.h>

int main(){

char cool[] = "http://www.cnn.com:80/wowsers.html";
regex_t test_reg;
regmatch_t matches[3];
int num_matches;


int success = regcomp(&test_reg, "http://([^/]*)(/{0,1}.*)",
REG_EXTENDED | REG_ICASE);
printf("regcomp success = %d\r\n", success);


success = regexec(&test_reg, cool, num_matches, matches, 0);
printf("regexec success = %d\r\n", success);


printf("overall match start index = %d\r\n", matches[0].rm_so);
printf("overall match end index = %d\r\n",
matches[0].rm_eo);
printf("first match start index = %d\r\n", matches[1].rm_so);
printf("first match end index = %d\r\n", matches[1].rm_eo);
printf("second match start index = %d\r\n", matches[2].rm_so);
printf("second match end index = %d\r\n", matches[2].rm_eo);

}

OUTPUT on SuSE(10.2):
../regex
regcomp success = 0
regexec success = 0
overall match start index = 0
overall match end index = 34
first match start index = 7
first match end index = 21
second match start index = 21
second match end index = 34
OUTPUT on Cent(5):
../regex
regcomp success = 0
Segmentation fault

MORE DETAILS:
regex.h are same on both machine.

ldd ./regex ( on Cent 5 )
linux-gate.so.1 => (0x00a43000)
libc.so.6 => /lib/libc.so.6 (0x00110000)
/lib/ld-linux.so.2 (0x0093c000)
glibc-devel-2.5-12 (rpm -qf /usr/include/regex.h)
ldd ./regex ( on SuSE 10.2 )
linux-gate.so.1 => (0xffffe000)
libc.so.6 => /lib/libc.so.6 (0xb7d8c000)
/lib/ld-linux.so.2 (0xb7ee9000)
glibc-devel-2.5-25 (rpm -qf /usr/include/regex.h)
 
M

Malcolm McLean

int num_matches;

success = regexec(&test_reg, cool, num_matches, matches, 0);
Looks suspicious to me. Surely you should pass &num_matches so that
regexec() can fill in the answer?
I don't know the prototype for regexec(), so this may be wrong.
 
B

Ben Bacarisse

the below program is working in Suse and not working on Cent 5:
can any body have the solution ?

#include <regex.h>
#include <stdlib.h>
#include <stdio.h>

int main(){

char cool[] = "http://www.cnn.com:80/wowsers.html";
regex_t test_reg;
regmatch_t matches[3];
int num_matches;

int num_matches = 3;

You are expected to say how big the matches array is. This is not
really on-topic here. Further problem should go to
comp.unix.programmer since this is a POSIX interface.
 
M

Mark Bluemel

make this "int num_matches = 3;" or (better) "size_t num_matches = 3;"
Looks suspicious to me. Surely you should pass &num_matches so that
regexec() can fill in the answer?
I don't know the prototype for regexec(), so this may be wrong.

Malcolm's instincts are right.

num_matches needs to be initialized to indicate how many
matches you can handle (loosely speaking).
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

No members online now.

Forum statistics

Threads
473,764
Messages
2,569,565
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top