Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

replace_regex: remove bash word boundary when detecting gnused #686

Open
wants to merge 1 commit into
base: staging
Choose a base branch
from

Conversation

Aleksanaa
Copy link

Bash linked against C libraries other than GLibc may not support GNU extensions of POSIX Extended Regular Regex. For example,

re='\bx'; [[ 'x' =~ $re ]] && echo "1"

does not output the same result on Linux/GLibc and macOS.

Bash linked against C libraries other than GLibc may not support GNU
extensions of POSIX Extended Regular Regex. For example,

> re='\bx'; [[ 'x' =~ $re ]] && echo "1"

does not output the same result on Linux/GLibc and macOS.
@Aleksanaa
Copy link
Author

This fixes tests::stdlib::test_stdlib_src_tests_stdlib_text_replace_regex_ext_ab on macOS with gnused.

@b1ek b1ek changed the base branch from main to staging March 9, 2025 15:37
Copy link
Member

@b1ek b1ek left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

tests pass in CI so i guess its ok.

they dont on my archlinux machine, but it could be a me problem since it is a new install, so i ask the next reviewer to check if it runs on their machine and block this if it doesn't

never mind, bc wasn't installed on my system. you really should check the tests on your machine however

@b1ek
Copy link
Member

b1ek commented Mar 9, 2025

This fixes tests::stdlib::test_stdlib_src_tests_stdlib_text_replace_regex_ext_ab on macOS with gnused.

is there any difference with bsdsed? i think i heard something about macos being built on top of bsd

@Aleksanaa
Copy link
Author

never mind, bc wasn't installed on my system. you really should check the tests on your machine however

It's okay on our Nixpkgs CI with both Linux and macOS now.

is there any difference with bsdsed? i think i heard something about macos being built on top of bsd

It's not related to sed, but the regex of bash itself. The command should be able to detect gnused, but it straightly fails on macOS, so it jumps to "this is not gnused", and try to pass an unsupported argument to it.

The biggest mistake here is \b which doesn't belong to POSIX Extended Regular Regex (ERE).Bash is said to be using ERE, but it actually depends on system libc for that. GLibc goes way more than ERE and implements a set of rules called GNU extensions. I cannot find the most related source but you can refer to findutils manual. For example, the following code will give different result on GNU/Linux and other platforms:

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

  int main() {
      regex_t re;
      int ret = regcomp(&re, "\\bx\\b", REG_EXTENDED);
      if (ret) { printf("regcomp error\n"); return 1; }

      ret = regexec(&re, "x", 0, NULL, 0);
      regfree(&re);
      printf(ret == 0 ? "Match\n" : "No match\n");
      return 0;
  }

As you can see, there's no equivalent of word boundary in standard POSIX ERE. So the best option is simply not using them, and it's not necessary here, too.

@Mte90 Mte90 requested review from hdwalters and Ph0enixKM March 10, 2025 11:06
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants