diff mbox

sanitize paths used in regular expression

Message ID 1452819258-13754-1-git-send-email-zwelch@codesourcery.com
State New
Headers show

Commit Message

Zachary T Welch Jan. 15, 2016, 12:54 a.m. UTC
This patch fixes a small problem when running 'make check' from a path
that includes "++".  When such paths get used as a regular expression,
that sequence would cause a runtime error.  That is prevented here by
escaping that character.

	gcc/testsuite/lib/
	* prune.exp (prune_file_path): Sanitize path used in regex.

Signed-off-by: Zachary T Welch <zwelch@codesourcery.com>
---
 gcc/testsuite/lib/prune.exp | 7 +++++--
 1 file changed, 5 insertions(+), 2 deletions(-)

Comments

Mike Stump Jan. 15, 2016, 1:31 a.m. UTC | #1
On Jan 14, 2016, at 4:54 PM, Zachary T Welch <zwelch@codesourcery.com> wrote:
> This patch fixes a small problem when running 'make check' from a path
> that includes "++".  When such paths get used as a regular expression,
> that sequence would cause a runtime error.  That is prevented here by
> escaping that character.

You don’t ask Ok?, and you don’t say committed, so, I’m wondering if you want a review of it…

Anyway, seems reasonable.  I do wonder if there is a more complete set of meta characters to do with with beyond just a + however.
David Malcolm Jan. 15, 2016, 10:47 a.m. UTC | #2
On Thu, 2016-01-14 at 17:31 -0800, Mike Stump wrote:
> On Jan 14, 2016, at 4:54 PM, Zachary T Welch <zwelch@codesourcery.com> wrote:
> > This patch fixes a small problem when running 'make check' from a path
> > that includes "++".  When such paths get used as a regular expression,
> > that sequence would cause a runtime error.  That is prevented here by
> > escaping that character.
> 
> You don’t ask Ok?, and you don’t say committed, so, I’m wondering if you want a review of it…
> 
> Anyway, seems reasonable.  I do wonder if there is a more complete set of meta characters to do with with beyond just a + however.

FWIW, I do something similar in multiline.exp's _build_multiline_regex,
which attempts to have a complete list of metacharacters (though I
believe some of these are not valid for POSIX filenames); currently it
has:

	# We need to escape "^" and other regexp metacharacters.
	set line [string map {"^" "\\^"
	                      "(" "\\("
	                      ")" "\\)"
	                      "[" "\\["
	                      "]" "\\]"
	                      "{" "\\{"
	                      "}" "\\}"
	                      "." "\\."
	                      "\\" "\\\\"
	                      "?" "\\?"
	                      "+" "\\+"
	                      "*" "\\*"
	                      "|" "\\|"} $line]

Maybe this could be turned into a generic helper function?


Dave
Mike Stump Jan. 15, 2016, 5:34 p.m. UTC | #3
On Jan 15, 2016, at 2:47 AM, David Malcolm <dmalcolm@redhat.com> wrote:
> FWIW, I do something similar in multiline.exp's _build_multiline_regex,
> which attempts to have a complete list of metacharacters (though I
> believe some of these are not valid for POSIX filenames);

Only ‘\’ and ‘\0’ are invalid.  The rest are ok.  ‘/‘ is only invalid in a single component of a path, because / is used as a separator.

> 	# We need to escape "^" and other regexp metacharacters.
> 	set line [string map {"^" "\\^"
> 	                      "(" "\\("
> 	                      ")" "\\)"
> 	                      "[" "\\["
> 	                      "]" "\\]"
> 	                      "{" "\\{"
> 	                      "}" "\\}"
> 	                      "." "\\."
> 	                      "\\" "\\\\"
> 	                      "?" "\\?"
> 	                      "+" "\\+"
> 	                      "*" "\\*"
> 	                      "|" "\\|"} $line]

Some regexp systems that use ^, also use $.  TCL does does example.
diff mbox

Patch

diff --git a/gcc/testsuite/lib/prune.exp b/gcc/testsuite/lib/prune.exp
index 8e4c203..3bb477c 100644
--- a/gcc/testsuite/lib/prune.exp
+++ b/gcc/testsuite/lib/prune.exp
@@ -76,9 +76,12 @@  proc prune_gcc_output { text } {
 proc prune_file_path { text } {
     global srcdir
 
+    # escape '+' characters when using path as regex pattern
+    set safedir {[regsub -all {\+} "$srcdir" "\\+"]}
+    regsub -all "$safedir\/"  $text "" text
+
     # Truncate absolute file path into relative path.
-    set topdir "[file dirname [file dirname [file dirname $srcdir]]]"
-    regsub -all "$srcdir\/" $text "" text
+    set topdir "[file dirname [file dirname [file dirname $safedir]]]"
     regsub -all "$topdir\/" $text "" text
 
     return $text