Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

regcomp giving SIGSEGV, Segmentation fault.

Posted on 2004-08-03
17
Medium Priority
?
1,032 Views
Last Modified: 2007-12-19

I have a common file, used in 2 programs. There is a regular expression function in there. It runs like this:

#define      MAIN_REGEXP_COUNT 6
const char      *main_regexp[MAIN_REGEXP_COUNT] = {
            "[0-9]*$",
            "others..." };
static regex_t      compiled_main_regexp[MAIN_REGEXP_COUNT];

            for (i = 0; i < MAIN_REGEXP_COUNT; i++)
            {
                  if (regcomp(&compiled_main_regexp[i], main_regexp[i], 0) != 0)
                  {
                        fprintf(stderr, "main_regexp[%d] error\n", i);
                        exit(12);
                  }
            }

In one of the two programs it works fine. In the other I get a segfault from the regcomp line:

#0  0x4207adc0 in chunk_free () from /lib/i686/libc.so.6
#1  0x4207b4bf in chunk_realloc () from /lib/i686/libc.so.6
#2  0x4207b2f8 in realloc () from /lib/i686/libc.so.6
#3  0x4207e011 in realloc_hook_ini () from /lib/i686/libc.so.6
#4  0x4207b0a1 in realloc () from /lib/i686/libc.so.6
#5  0x420cb29e in byte_regex_compile () from /lib/i686/libc.so.6
#6  0x420d0d6d in regcomp () from /lib/i686/libc.so.6

I've looked at header files. I'm using the same .o file for both programs.

System is: Red Hat Linux release 7.3 (Valhalla) - with no option to upgrade.

Any ideas why the two behave differently?
0
Comment
Question by:Gyles
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 8
  • 7
  • 2
17 Comments
 
LVL 45

Expert Comment

by:sunnycoder
ID: 11705156
const char     *main_regexp[MAIN_REGEXP_COUNT] = {
          "[0-9]*$",
          "others..." };


this array has only two valid indices 0 and 1 and you are accessing locations 0 to 5 in the loop
0
 
LVL 45

Expert Comment

by:sunnycoder
ID: 11705162
>#define     MAIN_REGEXP_COUNT 6
#define     MAIN_REGEXP_COUNT 2
0
 

Author Comment

by:Gyles
ID: 11705194

There are, obviously, 5 other expressions too - I wrote "others..." just for brevity.
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:Gyles
ID: 11705224

The problem remains if I completely remove the array stuff. The first call to regcomp triggers the bug.
0
 
LVL 45

Expert Comment

by:sunnycoder
ID: 11705276
char errbuf[256];
          for (i = 0; i < MAIN_REGEXP_COUNT; i++)
          {
               if ((temp= regcomp(&compiled_main_regexp[i], main_regexp[i], 0)) != 0)
               {
                    regerror(temp, &compiled_main_regexp[i], errbuf, 256);
                    fprintf(stderr, "main_regexp[%d] error\n", i);
                    exit(12);
               }
          }
0
 
LVL 45

Expert Comment

by:sunnycoder
ID: 11705292
oops submitted too soon ... print errbuf after call to regerror

char errbuf[256];
          for (i = 0; i < MAIN_REGEXP_COUNT; i++)
          {
               if ((temp= regcomp(&compiled_main_regexp[i], main_regexp[i], 0)) != 0)
               {
                    regerror(temp, &compiled_main_regexp[i], errbuf, 256);
                    fprintf(stderr, "main_regexp[%d] error %s\n", i, errbuf);
                    exit(12);
               }
          }
0
 

Author Comment

by:Gyles
ID: 11705366
No change - the segfault is within the regcomp, so the regerror never gets called.
0
 
LVL 45

Expert Comment

by:sunnycoder
ID: 11705702
Is there multithreading of some kind ... this piece of code looks fine to me
0
 
LVL 45

Expert Comment

by:sunnycoder
ID: 11706288
seems that regcomp writes to the memory passed as regexp_main ... since you have declared constant string as "blah", it is unable to write to that memory and fails ... took quite a while to figure out but I think this should solve the problem

#include<stdio.h>

#include <sys/types.h>
#include <regex.h>
#define     MAIN_REGEXP_COUNT 6

int main ()
{
   int i;
const char     *main_regexp[MAIN_REGEXP_COUNT];
        //  "[0-9]*$",
         // "others..." };

regex_t     compiled_main_regexp[MAIN_REGEXP_COUNT];

          for (i = 0; i < MAIN_REGEXP_COUNT; i++)
          {
               main_regexp[i]=(char *) malloc(10);
               if (main_regexp[i])
                      strcpy (main_regexp[i],"[0-9]");
               else
                      printf ("calloc failed\n");
               if (regcomp(&compiled_main_regexp[i], main_regexp[i], 0) != 0)
               {
                    fprintf(stderr, "main_regexp[%d] error\n", i);
                    exit(12);
               }
              else
                    printf ("success\n");
              free (main_regexp[i]);
          }
}
0
 

Author Comment

by:Gyles
ID: 11706483

Tried it this way:

        char    *expression;

        expression = malloc(80);

        for (i = 0; i < MAIN_REGEXP_COUNT; i++)
        {
            strcpy(expression, main_regexp[i]);

            if (regcomp(&compiled_main_regexp[i], expression, 0) != 0)
            {
                fprintf(stderr, "main_regexp[%d] error\n", i);
                exit(12);

Still segfaults!

Stranger still - the segfault comes and goes depending on whether or not I call another (completely unrelated) function from the main code into the common code. Might be some miserable linker issue...
0
 
LVL 46

Expert Comment

by:Sjef Bosman
ID: 11706712
So the "other" function/code may have overwritten some reg. expressions in main_regexp?
0
 
LVL 46

Expert Comment

by:Sjef Bosman
ID: 11706723
printf the regexp's before you start...
0
 
LVL 45

Expert Comment

by:sunnycoder
ID: 11712237
may be compiler is doing som optimizations which it should not be doing ... try eliminating the constants array all together and replacing it with writeable malloced memory ...

Also check if the new segfault is in strcpy or regcomp ... Invoking regcomp in the function often caused my malloc to fail, so check the return value from malloc too.

I have no idea why malloc failed but the particular code I posted here always worked ... it were the other permutations which failed
0
 

Author Comment

by:Gyles
ID: 11713196
@sjef(1) - At this stage I can't see how. I've only got local variables in the two functions.

The other function just writes out a file for me, from the parameters it is given, checking permissions etc. as appropriate. I can't see why running (or not) that first makes a difference to a completely separate function.

@sjef(2) - I actually already do this (omitted to keep code section short). They are OK.

@sunnycoder - The segfault remains in regcomp. malloc seems to be OK.

I'm stumped!
0
 
LVL 45

Accepted Solution

by:
sunnycoder earned 1500 total points
ID: 11713206
does the above code which I posted work fine? If it didn't , its time to sift through bug reports and mailing lists
0
 

Author Comment

by:Gyles
ID: 11714026
If I run the other (seemingly unrelated) function it works fine. If I don't it segfaults. Same execuatable.
0
 

Author Comment

by:Gyles
ID: 11715319
So - make a nasty hack which enters the "other" function, checks permissions etc., but does not perform the actual write, and all is OK.
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were small…
Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address. This address might be address of another variable/address of devices/address of fu…
The goal of this video is to provide viewers with basic examples to understand recursion in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use while-loops in the C programming language.

705 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question