[Webinar] Streamline your web hosting managementRegister Today

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1068
  • Last Modified:

regcomp giving SIGSEGV, Segmentation fault.


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
Gyles
Asked:
Gyles
  • 8
  • 7
  • 2
1 Solution
 
sunnycoderCommented:
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
 
sunnycoderCommented:
>#define     MAIN_REGEXP_COUNT 6
#define     MAIN_REGEXP_COUNT 2
0
 
GylesAuthor Commented:

There are, obviously, 5 other expressions too - I wrote "others..." just for brevity.
0
Problems using Powershell and Active Directory?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

 
GylesAuthor Commented:

The problem remains if I completely remove the array stuff. The first call to regcomp triggers the bug.
0
 
sunnycoderCommented:
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
 
sunnycoderCommented:
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
 
GylesAuthor Commented:
No change - the segfault is within the regcomp, so the regerror never gets called.
0
 
sunnycoderCommented:
Is there multithreading of some kind ... this piece of code looks fine to me
0
 
sunnycoderCommented:
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
 
GylesAuthor Commented:

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
 
Sjef BosmanGroupware ConsultantCommented:
So the "other" function/code may have overwritten some reg. expressions in main_regexp?
0
 
Sjef BosmanGroupware ConsultantCommented:
printf the regexp's before you start...
0
 
sunnycoderCommented:
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
 
GylesAuthor Commented:
@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
 
sunnycoderCommented:
does the above code which I posted work fine? If it didn't , its time to sift through bug reports and mailing lists
0
 
GylesAuthor Commented:
If I run the other (seemingly unrelated) function it works fine. If I don't it segfaults. Same execuatable.
0
 
GylesAuthor Commented:
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

The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

  • 8
  • 7
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now