Solved

regcomp giving SIGSEGV, Segmentation fault.

Posted on 2004-08-03
17
965 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
  • 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
 

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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
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 500 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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

An Outlet in Cocoa is a persistent reference to a GUI control; it connects a property (a variable) to a control.  For example, it is common to create an Outlet for the text field GUI control and change the text that appears in this field via that Ou…
Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
The goal of this video is to provide viewers with basic examples to understand how to use strings and some functions related to them 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.

707 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now