regex_t internals: can we use re_magic to tell whether a regex has been regcomp'd?

Fischer, Matthew L
Wed Aug 10 18:09:00 GMT 2011

We are porting code from Linux that is attempting to determine whether a regular expression has been properly regcomp'd and not freed.  The code from Linux is looking into the buffer inside regex_t.   On Cygwin, the "buffer" (not the same field name) is hidden inside re_guts which has a comment that dissuades us from using it for this purpose.  However, from looking at the Cygwin implementation, it looks like if re_magic is != 0 then the regexp is valid and has been regcomp'd and not regfree'd.  Is this interpretation correct?

The porting mechanism in the code below seems to work well, but we're not sure whether re_magic is the best solution for Cygwin.  Is method below the best and more importantly, safest option?

bool regexValid() 
#ifdef __CYGWIN__
                return (m_reg.re_magic != 0);
                //original Linux code
return m_Reg.buffer != NULL;  


Linux regex_t - typedef'd to struct re_pattern"

struct re_pattern_buffer
  /* Space that holds the compiled pattern.  It is declared as
     `unsigned char *' because its elements are sometimes used as
     array indexes.  */
  unsigned char *__REPB_PREFIX(buffer);
typedef struct re_pattern_buffer regex_t;


Cygwin regex_t:

On Cygwin, the malloc'd space is down in "re_guts" which has a great comment: 
typedef struct {
                int re_magic;
                size_t re_nsub;                 /* number of parenthesized subexpressions */
#ifdef __CYGWIN__
                const char *re_endp;     /* end pointer for REG_PEND */
                __const char *re_endp;               /* end pointer for REG_PEND */
                struct re_guts *re_g;     /* none of your business :-) */
} regex_t;

Problem reports:
Unsubscribe info:

More information about the Cygwin mailing list