This is the mail archive of the
gcc-patches@gcc.gnu.org
mailing list for the GCC project.
[PATCH v2] sanitize paths used in regular expression
- From: Zachary T Welch <zwelch at codesourcery dot com>
- To: <gcc-patches at gcc dot gnu dot org>, <dmalcolm at redhat dot com>, <mikestump at comcast dot net>
- Cc: Zachary T Welch <zwelch at codesourcery dot com>
- Date: Fri, 15 Jan 2016 10:40:31 -0800
- Subject: [PATCH v2] sanitize paths used in regular expression
- Authentication-results: sourceware.org; auth=none
- References: <D55AA5EF-518A-4945-ACAC-D027BB2A1A59 at comcast dot net>
Does this version look better? I am not sure if this the right place
to put the new helper, so let me know if there is a better spot for it.
gcc/testsuite/lib/
* prune.exp (prune_file_path): Sanitize path used in regex.
(escape_regex_chars): New.
Signed-off-by: Zachary T Welch <zwelch@codesourcery.com>
---
gcc/testsuite/lib/prune.exp | 25 +++++++++++++++++++++++--
1 file changed, 23 insertions(+), 2 deletions(-)
diff --git a/gcc/testsuite/lib/prune.exp b/gcc/testsuite/lib/prune.exp
index 8e4c203..fd3c4ea 100644
--- a/gcc/testsuite/lib/prune.exp
+++ b/gcc/testsuite/lib/prune.exp
@@ -73,12 +73,33 @@ proc prune_gcc_output { text } {
return $text
}
+# escape metacharacters in literal string, so it can be used in regex
+
+proc escape_regex_chars { line } {
+ return [string map {"^" "\\^"
+ "$" "\\$"
+ "(" "\\("
+ ")" "\\)"
+ "[" "\\["
+ "]" "\\]"
+ "{" "\\{"
+ "}" "\\}"
+ "." "\\."
+ "\\" "\\\\"
+ "?" "\\?"
+ "+" "\\+"
+ "*" "\\*"
+ "|" "\\|"} $line]
+}
+
proc prune_file_path { text } {
global srcdir
+ set safedir [escape_regex_chars $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
--
1.8.1.1