#586971 libc6-dev: REG_NOSUB prevents storing location of entire regex match

Package:
libc6-dev
Source:
glibc
Description:
GNU C Library: Development Libraries and Header Files
Submitter:
Frank Heckenbach
Date:
2010-06-24 03:15:04 UTC
Severity:
minor
#586971#5
Date:
2010-06-24 02:48:57 UTC
From:
To:
/usr/include/regex.h says:

/* If this bit is set, then report only success or fail in regexec.
   If not set, then returns differ between not matching and errors.  */
#define REG_NOSUB (REG_NEWLINE << 1)

It doesn't say anything about not storing the location of the match
of the entire regex in __pmatch[0]. However, when REG_NOSUB is set,
the location is not stored as the test program below shows.

This may be the expected behaviour. In this case, it should be made
clear in the comment above, since the name of the constant suggests
it only affects sub-matches.

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

int main ()
{
  regex_t r;
  regmatch_t m[1];
  int i;
  for (i = 0; i < 2; i++)
    {
      m[0].rm_so = m[0].rm_eo = -1;
      printf ("%s REG_NOSUB: ", i ? "with" : "without");
      if (regcomp (&r, "b", i * REG_NOSUB))
        printf ("error in regcomp\n");
      else if (regexec (&r, "abc", 1, m, 0))
        printf ("no match\n");
      else
        printf ("%i %i\n", m[0].rm_so, m[0].rm_eo);
    }
}

Output:

without REG_NOSUB: 1 2
with REG_NOSUB: -1 -1