pkgsrc-Changes-HG archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

[pkgsrc/trunk]: pkgsrc mk/subst.mk: avoid undefined behavior in regular expre...



details:   https://anonhg.NetBSD.org/pkgsrc/rev/2bf67e72accb
branches:  trunk
changeset: 427595:2bf67e72accb
user:      rillig <rillig%pkgsrc.org@localhost>
date:      Sat Apr 18 12:21:10 2020 +0000

description:
mk/subst.mk: avoid undefined behavior in regular expressions

https://pubs.opengroup.org/onlinepubs/9699919799/basedefs/V1_chap09.html
says in section 9.3.2 BRE Ordinary Characters that only very few
characters may be preceded with a backslash.

As a side effect, this change allows parentheses in the variable names
listed in SUBST_VARS (even if that will never happen in practice).

The reason that the regression test had not replaced VAR.[] before was
simply that this variable had not been listed in SUBST_VARS.

diffstat:

 mk/subst.mk                      |  4 ++--
 regress/infra-unittests/subst.sh |  9 ++++-----
 2 files changed, 6 insertions(+), 7 deletions(-)

diffs (54 lines):

diff -r 08774ccab43a -r 2bf67e72accb mk/subst.mk
--- a/mk/subst.mk       Sat Apr 18 12:17:18 2020 +0000
+++ b/mk/subst.mk       Sat Apr 18 12:21:10 2020 +0000
@@ -1,4 +1,4 @@
-# $NetBSD: subst.mk,v 1.76 2020/04/18 11:42:34 rillig Exp $
+# $NetBSD: subst.mk,v 1.77 2020/04/18 12:21:10 rillig Exp $
 #
 # The subst framework replaces text in one or more files in the WRKSRC
 # directory. Packages can define several ``classes'' of replacements.
@@ -128,7 +128,7 @@
 SUBST_VARS.${class}?=          # none
 SUBST_MESSAGE.${class}?=       Substituting "${class}" in ${SUBST_FILES.${class}}
 .  for v in ${SUBST_VARS.${class}}
-SUBST_FILTER_CMD.${class}+=    -e s,@${v:C|[^A-Za-z0-9_]|\\\\&|gW:Q}@,${${v}:S|\\|\\\\|gW:S|,|\\,|gW:S|&|\\\&|gW:S|${.newline}|\\${.newline}|gW:Q},g
+SUBST_FILTER_CMD.${class}+=    -e s,@${v:C|[.[\\*^${$}]|\\\\&|gW:Q}@,${${v}:S|\\|\\\\|gW:S|,|\\,|gW:S|&|\\\&|gW:S|${.newline}|\\${.newline}|gW:Q},g
 .  endfor
 .  if ${SUBST_SHOW_DIFF.${class}:U${SUBST_SHOW_DIFF}:tl} == yes
 _SUBST_KEEP.${class}?=         LC_ALL=C ${DIFF} -u "$$file" "$$tmpfile" || true
diff -r 08774ccab43a -r 2bf67e72accb regress/infra-unittests/subst.sh
--- a/regress/infra-unittests/subst.sh  Sat Apr 18 12:17:18 2020 +0000
+++ b/regress/infra-unittests/subst.sh  Sat Apr 18 12:21:10 2020 +0000
@@ -801,7 +801,7 @@
                'SUBST_CLASSES+=        vars' \
                'SUBST_STAGE.vars=      pre-configure' \
                'SUBST_FILES.vars=      vars.txt' \
-               'SUBST_VARS.vars=       VAR...... VAR.abcde VAR.() VAR.<>' \
+               'SUBST_VARS.vars=       VAR...... VAR.abcde VAR.() VAR.<> VAR.[]' \
                '' \
                'VAR......=     dots' \
                'VAR.abcde=     letters' \
@@ -817,20 +817,19 @@
                "@VAR.()@" \
                "@VAR.<>@" \
                "@VAR.[]@"
+cp "$tmpdir/testcase.mk" /tmp
 
        run_bmake "testcase.mk" "pre-configure" \
                1> "$tmpdir/stdout" \
                2> "$tmpdir/stderr" \
        && exitcode=0 || exitcode=$?
 
-       # TODO: Why are the angle brackets replaced, but not the parentheses
-       # and square brackets?
        assert_that "vars.txt" --file-is-lines \
                "dots" \
                "letters" \
-               "@VAR.()@" \
+               "parentheses" \
                "angle brackets" \
-               "@VAR.[]@"
+               "square brackets"
        assert_that "stdout" --file-is-lines \
                "=> Substituting \"vars\" in vars.txt"
        assert_that "stderr" --file-is-empty



Home | Main Index | Thread Index | Old Index