Subject: bug#36148: Debian Bug#930247: grep: does not handle backreferences correctly, violating POSIX

From: Thorsten Glaser <[email-masked]>
Date: Fri, 2 Dec 2022 01:21:01 +0000 (UTC)

Please fix this bug, it’s really bad and embarrassing.

It looks like instead of matching “the same[…]string of characters as
was matched by a subexpression[…]preceding”, it matches with the same
as the previous subexpression used?

---------- Forwarded message ----------
Message-ID: <[email-masked]>
Date: Fri, 02 Dec 2022 02:12:20 +0100
Subject: Bug#930247: grep: inconsistent behaviour with anchored regex containing
     back-references

Package: grep
Version: 3.6-1
Followup-For: Bug #930247
X-Debbugs-Cc: [email-masked]
Control: found 930247 3.8-3
Control: severity 930247 serious
Control: retitle 930247 grep: does not handle backreferences correctly, violating POSIX

I’m running into this, in stable and unstable both:

(sid-amd64)tglase@tglase:/tmp $ cat x
Total failed: 0
Total failed: 1 (1 ignored)
Total failed: 2 (1 ignored)
Total failed: 1 (2 ignored)
Total failed: 1
Total failed: 111
(sid-amd64)tglase@tglase:/tmp $ grep -e '^Total failed: 0$' -e '^Total failed: \([0-9]*\) (\1 ignored)$' x
Total failed: 0
Total failed: 1 (1 ignored)
Total failed: 2 (1 ignored)
Total failed: 1 (2 ignored)

By contrast, BSD handles it correctly:

tg@tglase-bsd:/tmp $ grep -e '^Total failed: 0$' -e '^Total failed: \([0-9]*\) (\1 ignored)$' x
Total failed: 0
Total failed: 1 (1 ignored)

POSIX:

    3. The back-reference expression '\n' shall match the same (possibly
       empty) string of characters as was matched by a subexpression
       enclosed between "\(" and "\)" preceding the '\n'. The character

via https://pubs.opengroup.org/onlinepubs/9699919799/basedefs/V1_chap09.html#tag_09_03
from https://pubs.opengroup.org/onlinepubs/9699919799/utilities/grep.html

Please fix this clear standards violation; it makes grep
virtually unusable.

-- System Information:
Debian Release: 11.5
  APT prefers stable-updates
  APT policy: (500, 'stable-updates'), (500, 'stable-security'), (500, 'stable')
Architecture: amd64 (x86_64)

Kernel: Linux 5.10.0-19-amd64 (SMP w/2 CPU threads)
Locale: LANG=C, LC_CTYPE=C (charmap=UTF-8) (ignored: LC_ALL set to C.UTF-8), LANGUAGE not set
Shell: /bin/sh linked to /bin/lksh
Init: sysvinit (via /sbin/init)

Versions of packages grep depends on:
ii  dpkg          1.20.12
ii  install-info  6.7.0.dfsg.2-6
ii  libc6         2.31-13+deb11u5
ii  libpcre3      2:8.39-13

grep recommends no packages.

Versions of packages grep suggests:
ii  libpcre3  2:8.39-13

-- no debconf information