mailing list of musl libc
 help / color / mirror / code / Atom feed
* [musl] Different results with regex.h between Musl and Libc
@ 2024-06-10  5:38 Nigel Kukard
  2024-06-10 11:47 ` [musl] " Valery Ushakov
  0 siblings, 1 reply; 4+ messages in thread
From: Nigel Kukard @ 2024-06-10  5:38 UTC (permalink / raw)
  To: musl


[-- Attachment #1.1: Type: text/plain, Size: 2036 bytes --]

Hi there,

I'm wondering if someone could possibly point out what I'm doing wrong here.

I tried searching the mailing list for similar topics, I found some 
relating to POSIX regex but none that really dealt with this issue with 
extended regex. I also didn't find anything really addressing this on 
https://wiki.musl-libc.org/functional-differences-from-glibc.html.

If I missed something I'm terribly sorry.


Musl output (Alpine 3.20), musl-1.2.5-r1...

The input '37' matches the pattern '^([0-9]*)?\.?([0-9]*)?$'
Match 0: 37
Match 1:
Match 2: 37

Glibc output (ArchLinux), glibc 2.39+r52+gf8e4623421-1...

The input '37' matches the pattern '^([0-9]*)?\.?([0-9]*)?$'
Match 0: 37
Match 1: 37
Match 2:


Test code...
/*--------------------*/
#include <stdio.h>
#include <regex.h>

int main() {
     const char *pattern = "^([0-9]*)?\\.?([0-9]*)?$";
     const char *input = "37";
     regex_t regex;
     regmatch_t matches[3];
     int ret;

     ret = regcomp(&regex, pattern, REG_EXTENDED);
     if (ret) {
         fprintf(stderr, "Could not compile regex\n");
         return 1;
     }

     ret = regexec(&regex, input, 3, matches, 0);
     if (!ret) {
         printf("The input '%s' matches the pattern '%s'\n", input, 
pattern);
         for (int i = 0; i < 3; i++) {
             if (matches[i].rm_so != -1) {
                 printf("Match %d: %.*s\n", i, matches[i].rm_eo - 
matches[i].rm_so, input + matches[i].rm_so);
             }
         }
     } else if (ret == REG_NOMATCH) {
         printf("The input '%s' does not match the pattern '%s'\n", 
input, pattern);
     } else {
         char msgbuf[100];
         regerror(ret, &regex, msgbuf, sizeof(msgbuf));
         fprintf(stderr, "Regex match failed: %s\n", msgbuf);
         return 1;
     }

     regfree(&regex);

     return 0;
}


[-- Attachment #2: OpenPGP digital signature --]
[-- Type: application/pgp-signature, Size: 236 bytes --]

^ permalink raw reply	[flat|nested] 4+ messages in thread

* [musl] Re: Different results with regex.h between Musl and Libc
  2024-06-10  5:38 [musl] Different results with regex.h between Musl and Libc Nigel Kukard
@ 2024-06-10 11:47 ` Valery Ushakov
  2024-06-10 15:56   ` Rich Felker
  0 siblings, 1 reply; 4+ messages in thread
From: Valery Ushakov @ 2024-06-10 11:47 UTC (permalink / raw)
  To: musl

On Mon, Jun 10, 2024 at 05:38:36 +0000, Nigel Kukard wrote:

> Musl output (Alpine 3.20), musl-1.2.5-r1...
> 
> The input '37' matches the pattern '^([0-9]*)?\.?([0-9]*)?$'
> Match 0: 37
> Match 1:
> Match 2: 37
> 
> Glibc output (ArchLinux), glibc 2.39+r52+gf8e4623421-1...
> 
> The input '37' matches the pattern '^([0-9]*)?\.?([0-9]*)?$'
> Match 0: 37
> Match 1: 37
> Match 2:

I'm not sure what POSIX requires here.  The closest I can find after
skimming through "9. Regular Expressions" is 9.4.6 that ends with:

  An ERE matching a single character repeated by an '*', '?', or an
  interval expression shall not match a null expression unless this is
  the only match for the repetition or it is necessary to satisfy the
  exact or minimum number of occurrences for the interval expression.

    https://pubs.opengroup.org/onlinepubs/9699919799/basedefs/V1_chap09.html#tag_09_04_06

I'm not sure what to read into the absense of the usual "or an ERE
enclosed in parentheses" chorus here.


> printf("Match %d: %.*s\n", i, matches[i].rm_eo - matches[i].rm_so, input + matches[i].rm_so);

Nit-pick: regoff_t may be wider than int (expected by '*').  E.g. your
test program prints nothing for all those %.* on NetBSD/macppc (with
the appropriate cast it prints 37/37/<empty>), as regoff_t is 64-bit
(very old posix required regoff_t to be at least as wide as off_t).
It will probably crash on a little-endian 32-bit NetBSD system, b/c
the zero MSW of a 64-bit regoff_t will be interpreted as the argument
for %s.


-uwe

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [musl] Re: Different results with regex.h between Musl and Libc
  2024-06-10 11:47 ` [musl] " Valery Ushakov
@ 2024-06-10 15:56   ` Rich Felker
  2024-06-10 17:57     ` Valery Ushakov
  0 siblings, 1 reply; 4+ messages in thread
From: Rich Felker @ 2024-06-10 15:56 UTC (permalink / raw)
  To: Valery Ushakov; +Cc: musl, Nigel Kukard

On Mon, Jun 10, 2024 at 02:47:04PM +0300, Valery Ushakov wrote:
> On Mon, Jun 10, 2024 at 05:38:36 +0000, Nigel Kukard wrote:
> 
> > Musl output (Alpine 3.20), musl-1.2.5-r1...
> > 
> > The input '37' matches the pattern '^([0-9]*)?\.?([0-9]*)?$'
> > Match 0: 37
> > Match 1:
> > Match 2: 37
> > 
> > Glibc output (ArchLinux), glibc 2.39+r52+gf8e4623421-1...
> > 
> > The input '37' matches the pattern '^([0-9]*)?\.?([0-9]*)?$'
> > Match 0: 37
> > Match 1: 37
> > Match 2:
> 
> I'm not sure what POSIX requires here.  The closest I can find after
> skimming through "9. Regular Expressions" is 9.4.6 that ends with:
> 
>   An ERE matching a single character repeated by an '*', '?', or an
>   interval expression shall not match a null expression unless this is
>   the only match for the repetition or it is necessary to satisfy the
>   exact or minimum number of occurrences for the interval expression.
> 
>     https://pubs.opengroup.org/onlinepubs/9699919799/basedefs/V1_chap09.html#tag_09_04_06
> 
> I'm not sure what to read into the absense of the usual "or an ERE
> enclosed in parentheses" chorus here.

This looks like a bug. The general requirement (from memory; I don't
have the spec in front of me now) is that each subexpression, in order
from the beginning of the regex, matches the maximal-length input it
can, subject to the overall constraint that the entire regex match the
earliest (first priority) and maximal length (second priority) input
it can.

I guess we need to dig into why this is happening, ensure it's
actually incorrect, and figure out how to fix it...

Rich

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [musl] Re: Different results with regex.h between Musl and Libc
  2024-06-10 15:56   ` Rich Felker
@ 2024-06-10 17:57     ` Valery Ushakov
  0 siblings, 0 replies; 4+ messages in thread
From: Valery Ushakov @ 2024-06-10 17:57 UTC (permalink / raw)
  To: Rich Felker; +Cc: musl, Nigel Kukard

On Mon, Jun 10, 2024 at 11:56:14 -0400, Rich Felker wrote:

> On Mon, Jun 10, 2024 at 02:47:04PM +0300, Valery Ushakov wrote:
> > On Mon, Jun 10, 2024 at 05:38:36 +0000, Nigel Kukard wrote:
> > 
> > > Musl output (Alpine 3.20), musl-1.2.5-r1...
> > > 
> > > The input '37' matches the pattern '^([0-9]*)?\.?([0-9]*)?$'
> > > Match 0: 37
> > > Match 1:
> > > Match 2: 37
> > > 
> > > Glibc output (ArchLinux), glibc 2.39+r52+gf8e4623421-1...
> > > 
> > > The input '37' matches the pattern '^([0-9]*)?\.?([0-9]*)?$'
> > > Match 0: 37
> > > Match 1: 37
> > > Match 2:
> > 
> > I'm not sure what POSIX requires here.  The closest I can find after
> > skimming through "9. Regular Expressions" is 9.4.6 that ends with:
> > 
> >   An ERE matching a single character repeated by an '*', '?', or an
> >   interval expression shall not match a null expression unless this is
> >   the only match for the repetition or it is necessary to satisfy the
> >   exact or minimum number of occurrences for the interval expression.
> > 
> >     https://pubs.opengroup.org/onlinepubs/9699919799/basedefs/V1_chap09.html#tag_09_04_06
> > 
> > I'm not sure what to read into the absense of the usual "or an ERE
> > enclosed in parentheses" chorus here.
> 
> This looks like a bug. The general requirement (from memory; I don't
> have the spec in front of me now) is that each subexpression, in order
> from the beginning of the regex, matches the maximal-length input it
> can, subject to the overall constraint that the entire regex match the
> earliest (first priority) and maximal length (second priority) input
> it can.
> 
> I guess we need to dig into why this is happening, ensure it's
> actually incorrect, and figure out how to fix it...

You are right, 9.1 Regular Expression Definitions has under "matched":

  Consistent with the whole match being the longest of the leftmost
  matches, each subpattern, from left to right, shall match the
  longest possible string. For this purpose, a null string shall be
  considered to be longer than no match at all.  For example,
  matching the BRE "\(.*\).*" against "abcdef", the subexpression
  "(\1)" is "abcdef", and matching the BRE "\(a*\)*" against "bc",
  the subexpression "(\1)" is the null string.

-uwe

^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2024-06-10 18:49 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2024-06-10  5:38 [musl] Different results with regex.h between Musl and Libc Nigel Kukard
2024-06-10 11:47 ` [musl] " Valery Ushakov
2024-06-10 15:56   ` Rich Felker
2024-06-10 17:57     ` Valery Ushakov

Code repositories for project(s) associated with this public inbox

	https://git.vuxu.org/mirror/musl/

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).