public inbox for gdb-patches@sourceware.org
 help / color / mirror / Atom feed
* [PATCH] GDB testsuite: Escape paths used in regular expressions
@ 2016-02-10 16:40 Zachary T Welch
  2016-02-10 16:46 ` Pedro Alves
  0 siblings, 1 reply; 7+ messages in thread
From: Zachary T Welch @ 2016-02-10 16:40 UTC (permalink / raw)
  To: gdb-patches; +Cc: Zachary T Welch

This patch fixes problems with a few GDB testsuites when executing in a
path that contains special characters (e.g. "++").  When such paths are
used as a regular expression, the regular expression parser will choke
and cause the tests to fail.  This patch introduces a new function to
escape strings that will be used as regular expressions, using it to
sanitize path names used in expect scripts.

If it looks good, please commit.  Thanks!

	gdb/testsuite/:
	* gdb.base/maint.exp: Escape paths used in regular expressions.
	* gdb.stabs/weird.exp: Likewise.
	* gdb.exp (escape_regex_chars): New.


Signed-off-by: Zachary T Welch <zwelch@codesourcery.com>
---
 gdb/testsuite/gdb.base/maint.exp  | 15 +++++++++------
 gdb/testsuite/gdb.stabs/weird.exp |  3 ++-
 gdb/testsuite/lib/gdb.exp         | 21 +++++++++++++++++++++
 3 files changed, 32 insertions(+), 7 deletions(-)

diff --git a/gdb/testsuite/gdb.base/maint.exp b/gdb/testsuite/gdb.base/maint.exp
index f444018..cf60230 100644
--- a/gdb/testsuite/gdb.base/maint.exp
+++ b/gdb/testsuite/gdb.base/maint.exp
@@ -213,12 +213,13 @@ gdb_test "maint print psymbols" \
 
 if { ! $have_gdb_index } {
     set psymbols_output [standard_output_file psymbols_output]
+    set psymbols_output_re [escape_regex_chars $psymbols_output]
     send_gdb "maint print psymbols $psymbols_output ${srcdir}/${subdir}/${srcfile}\n"
     gdb_expect  {
-	-re "^maint print psymbols $psymbols_output \[^\n\]*\r\n$gdb_prompt $" {
+	-re "^maint print psymbols $psymbols_output_re \[^\n\]*\r\n$gdb_prompt $" {
 	    send_gdb "shell ls $psymbols_output\n"
 	    gdb_expect {
-		-re "$psymbols_output\r\n$gdb_prompt $" {
+		-re "$psymbols_output_re\r\n$gdb_prompt $" {
 		    # We want this grep to be as specific as possible,
 		    # so it's less likely to match symbol file names in
 		    # psymbols_output.  Yes, this actually happened;
@@ -253,12 +254,13 @@ gdb_test "maint print msymbols" \
 
 
 set msymbols_output [standard_output_file msymbols_output]
+set msymbols_output_re [escape_regex_chars $msymbols_output]
 send_gdb "maint print msymbols $msymbols_output ${binfile}\n"
 gdb_expect  {
-    -re "^maint print msymbols $msymbols_output \[^\n\]*\r\n$gdb_prompt $" {
+    -re "^maint print msymbols $msymbols_output_re \[^\n\]*\r\n$gdb_prompt $" {
 	send_gdb "shell ls $msymbols_output\n"
 	gdb_expect {
-	    -re "$msymbols_output\r\n$gdb_prompt $" {
+	    -re "$msymbols_output_re\r\n$gdb_prompt $" {
 		send_gdb "shell grep factorial $msymbols_output\n"
 		gdb_expect {
 		    -re "\\\[ *$decimal\\\] \[tT\]\[ \t\]+$hex \\.?factorial.*$gdb_prompt $" {
@@ -331,12 +333,13 @@ gdb_test "maint print symbols" \
 # for GNU libc.
 
 set symbols_output [standard_output_file symbols_output]
+set symbols_output_re [escape_regex_chars $symbols_output]
 send_gdb "maint print symbols $symbols_output ${srcdir}/${subdir}/${srcfile}\n"
 gdb_expect  {
-    -re "^maint print symbols $symbols_output \[^\n\]*\r\n$gdb_prompt $" {
+    -re "^maint print symbols $symbols_output_re \[^\n\]*\r\n$gdb_prompt $" {
 	send_gdb "shell ls $symbols_output\n"
 	gdb_expect {
-	    -re "$symbols_output\r\n$gdb_prompt $" {
+	    -re "$symbols_output_re\r\n$gdb_prompt $" {
 		# See comments for `maint print psymbols'.
 		send_gdb "shell grep 'main(.*block' $symbols_output\n"
 		gdb_expect {
diff --git a/gdb/testsuite/gdb.stabs/weird.exp b/gdb/testsuite/gdb.stabs/weird.exp
index 73f8b52..2e0ceaf 100644
--- a/gdb/testsuite/gdb.stabs/weird.exp
+++ b/gdb/testsuite/gdb.stabs/weird.exp
@@ -294,6 +294,7 @@ gdb_reinitialize_dir $srcdir/$subdir
 
 set binfile [gdb_remote_download host ${binfile} \
 		 [standard_output_file object.o]]
+set binfile_re [escape_regex_chars $binfile]
 send_gdb "file $binfile\n"
 # If $binfile is very long, a \r (but not a \n) will echo in the
 # middle of the echo of the command.  So to match the echo, we
@@ -310,7 +311,7 @@ gdb_expect 60 {
 	send_gdb "y\n"
 	exp_continue
     }
-    -re "^Reading symbols from .*$binfile\\.\\.\\.done\.(|\r\nUsing host libthread_db library .*libthread_db.so.*\\.)\r\n$gdb_prompt $" {
+    -re "^Reading symbols from .*$binfile_re\\.\\.\\.done\.(|\r\nUsing host libthread_db library .*libthread_db.so.*\\.)\r\n$gdb_prompt $" {
 	pass "weirdx.o read without error"
     }
     -re ".*$gdb_prompt $" {
diff --git a/gdb/testsuite/lib/gdb.exp b/gdb/testsuite/lib/gdb.exp
index 19a4ea8..0f11147 100644
--- a/gdb/testsuite/lib/gdb.exp
+++ b/gdb/testsuite/lib/gdb.exp
@@ -123,6 +123,27 @@ set inferior_exited_re "(\\\[Inferior \[0-9\]+ \\(.*\\) exited)"
 ### Only procedures should come after this point.
 
 #
+# escape_regex_chars -- escape metacharacters in literal string,
+# so it can be used safely in a regular expression.
+#
+proc escape_regex_chars { line } {
+    return [string map {"^" "\\^"
+			"$" "\\$"
+			"(" "\\("
+			")" "\\)"
+			"[" "\\["
+			"]" "\\]"
+			"{" "\\{"
+			"}" "\\}"
+			"." "\\."
+			"\\" "\\\\"
+			"?" "\\?"
+			"+" "\\+"
+			"*" "\\*"
+			"|" "\\|"} $line]
+}
+
+#
 # gdb_version -- extract and print the version number of GDB
 #
 proc default_gdb_version {} {
-- 
1.8.1.1

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [PATCH] GDB testsuite: Escape paths used in regular expressions
  2016-02-10 16:40 [PATCH] GDB testsuite: Escape paths used in regular expressions Zachary T Welch
@ 2016-02-10 16:46 ` Pedro Alves
  2016-02-10 17:47   ` [PATCH v2] " Zachary T Welch
  0 siblings, 1 reply; 7+ messages in thread
From: Pedro Alves @ 2016-02-10 16:46 UTC (permalink / raw)
  To: Zachary T Welch, gdb-patches

On 02/10/2016 04:39 PM, Zachary T Welch wrote:
> This patch fixes problems with a few GDB testsuites when executing in a
> path that contains special characters (e.g. "++").  When such paths are
> used as a regular expression, the regular expression parser will choke
> and cause the tests to fail.  This patch introduces a new function to
> escape strings that will be used as regular expressions, using it to
> sanitize path names used in expect scripts.
> 
> If it looks good, please commit.  Thanks!
> 
> 	gdb/testsuite/:
> 	* gdb.base/maint.exp: Escape paths used in regular expressions.
> 	* gdb.stabs/weird.exp: Likewise.
> 	* gdb.exp (escape_regex_chars): New.

Seems like you reinvented string_to_regexp.

Thanks,
Pedro Alves

^ permalink raw reply	[flat|nested] 7+ messages in thread

* [PATCH v2] Escape paths used in regular expressions
  2016-02-10 16:46 ` Pedro Alves
@ 2016-02-10 17:47   ` Zachary T Welch
  2016-02-10 17:53     ` Pedro Alves
  0 siblings, 1 reply; 7+ messages in thread
From: Zachary T Welch @ 2016-02-10 17:47 UTC (permalink / raw)
  To: Pedro Alves, gdb-patches; +Cc: Zachary T Welch

On 02/10/2016 08:46 AM, Pedro Alves wrote:
...
>> 	* gdb.exp (escape_regex_chars): New.
> 
> Seems like you reinvented string_to_regexp.

How does this version look?

	 *gdb.base/maint.exp: Escape paths used in regular expressions.
	* gdb.stabs/weird.exp: Likewise.

Signed-off-by: Zachary T Welch <zwelch@codesourcery.com>
---
 gdb/testsuite/gdb.base/maint.exp  | 15 +++++++++------
 gdb/testsuite/gdb.stabs/weird.exp |  3 ++-
 2 files changed, 11 insertions(+), 7 deletions(-)

diff --git a/gdb/testsuite/gdb.base/maint.exp b/gdb/testsuite/gdb.base/maint.exp
index f444018..b33ae26 100644
--- a/gdb/testsuite/gdb.base/maint.exp
+++ b/gdb/testsuite/gdb.base/maint.exp
@@ -213,12 +213,13 @@ gdb_test "maint print psymbols" \
 
 if { ! $have_gdb_index } {
     set psymbols_output [standard_output_file psymbols_output]
+    set psymbols_output_re [string_to_regex $psymbols_output]
     send_gdb "maint print psymbols $psymbols_output ${srcdir}/${subdir}/${srcfile}\n"
     gdb_expect  {
-	-re "^maint print psymbols $psymbols_output \[^\n\]*\r\n$gdb_prompt $" {
+	-re "^maint print psymbols $psymbols_output_re \[^\n\]*\r\n$gdb_prompt $" {
 	    send_gdb "shell ls $psymbols_output\n"
 	    gdb_expect {
-		-re "$psymbols_output\r\n$gdb_prompt $" {
+		-re "$psymbols_output_re\r\n$gdb_prompt $" {
 		    # We want this grep to be as specific as possible,
 		    # so it's less likely to match symbol file names in
 		    # psymbols_output.  Yes, this actually happened;
@@ -253,12 +254,13 @@ gdb_test "maint print msymbols" \
 
 
 set msymbols_output [standard_output_file msymbols_output]
+set msymbols_output_re [string_to_regex $msymbols_output]
 send_gdb "maint print msymbols $msymbols_output ${binfile}\n"
 gdb_expect  {
-    -re "^maint print msymbols $msymbols_output \[^\n\]*\r\n$gdb_prompt $" {
+    -re "^maint print msymbols $msymbols_output_re \[^\n\]*\r\n$gdb_prompt $" {
 	send_gdb "shell ls $msymbols_output\n"
 	gdb_expect {
-	    -re "$msymbols_output\r\n$gdb_prompt $" {
+	    -re "$msymbols_output_re\r\n$gdb_prompt $" {
 		send_gdb "shell grep factorial $msymbols_output\n"
 		gdb_expect {
 		    -re "\\\[ *$decimal\\\] \[tT\]\[ \t\]+$hex \\.?factorial.*$gdb_prompt $" {
@@ -331,12 +333,13 @@ gdb_test "maint print symbols" \
 # for GNU libc.
 
 set symbols_output [standard_output_file symbols_output]
+set symbols_output_re [string_to_regex $symbols_output]
 send_gdb "maint print symbols $symbols_output ${srcdir}/${subdir}/${srcfile}\n"
 gdb_expect  {
-    -re "^maint print symbols $symbols_output \[^\n\]*\r\n$gdb_prompt $" {
+    -re "^maint print symbols $symbols_output_re \[^\n\]*\r\n$gdb_prompt $" {
 	send_gdb "shell ls $symbols_output\n"
 	gdb_expect {
-	    -re "$symbols_output\r\n$gdb_prompt $" {
+	    -re "$symbols_output_re\r\n$gdb_prompt $" {
 		# See comments for `maint print psymbols'.
 		send_gdb "shell grep 'main(.*block' $symbols_output\n"
 		gdb_expect {
diff --git a/gdb/testsuite/gdb.stabs/weird.exp b/gdb/testsuite/gdb.stabs/weird.exp
index 73f8b52..4f1f117 100644
--- a/gdb/testsuite/gdb.stabs/weird.exp
+++ b/gdb/testsuite/gdb.stabs/weird.exp
@@ -294,6 +294,7 @@ gdb_reinitialize_dir $srcdir/$subdir
 
 set binfile [gdb_remote_download host ${binfile} \
 		 [standard_output_file object.o]]
+set binfile_re [string_to_regex $binfile]
 send_gdb "file $binfile\n"
 # If $binfile is very long, a \r (but not a \n) will echo in the
 # middle of the echo of the command.  So to match the echo, we
@@ -310,7 +311,7 @@ gdb_expect 60 {
 	send_gdb "y\n"
 	exp_continue
     }
-    -re "^Reading symbols from .*$binfile\\.\\.\\.done\.(|\r\nUsing host libthread_db library .*libthread_db.so.*\\.)\r\n$gdb_prompt $" {
+    -re "^Reading symbols from .*$binfile_re\\.\\.\\.done\.(|\r\nUsing host libthread_db library .*libthread_db.so.*\\.)\r\n$gdb_prompt $" {
 	pass "weirdx.o read without error"
     }
     -re ".*$gdb_prompt $" {
-- 
1.8.1.1

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [PATCH v2] Escape paths used in regular expressions
  2016-02-10 17:47   ` [PATCH v2] " Zachary T Welch
@ 2016-02-10 17:53     ` Pedro Alves
  2016-07-12 22:28       ` Don Breazeal
  0 siblings, 1 reply; 7+ messages in thread
From: Pedro Alves @ 2016-02-10 17:53 UTC (permalink / raw)
  To: Zachary T Welch, gdb-patches

On 02/10/2016 05:47 PM, Zachary T Welch wrote:
> On 02/10/2016 08:46 AM, Pedro Alves wrote:
> ...
>>> 	* gdb.exp (escape_regex_chars): New.
>>
>> Seems like you reinvented string_to_regexp.
> 
> How does this version look?
> 
> 	 *gdb.base/maint.exp: Escape paths used in regular expressions.
> 	* gdb.stabs/weird.exp: Likewise.
> 
> Signed-off-by: Zachary T Welch <zwelch@codesourcery.com>

Looks good.

Thanks,
Pedro Alves

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [PATCH v2] Escape paths used in regular expressions
  2016-02-10 17:53     ` Pedro Alves
@ 2016-07-12 22:28       ` Don Breazeal
  2016-07-15 13:34         ` Yao Qi
  0 siblings, 1 reply; 7+ messages in thread
From: Don Breazeal @ 2016-07-12 22:28 UTC (permalink / raw)
  To: gdb-patches, palves

Zach had submitted this patch back in February, and Pedro had approved it,
but it was never pushed in.  I've taken over responsibility for this item.

Neither of the affected expect scripts have been changed since this was
approved.  However, I fixed a typo in the original patch, changing
string_to_regex to string_to_regexp.

Verified that this patch fixes the tests when run in a directory with
a name containing "++" on native Linux x86_64.

OK to push this in?

Thanks
--Don

---
This patch fixes problems with a few GDB testsuites when executing in a
path that contains special characters (e.g. "++").  When such paths are
used as a regular expression, the regular expression parser will choke
and cause the tests to fail.  This patch uses string_to_regexp to
escape strings that will be used as regular expressions, in order to
sanitize path names used in expect scripts.

2016-07-12  Zachary Welch  <zwelch@codesourcery.com>
	    Don Breazeal <donb@codesourcery.com>

	gdb/testsuite/ChangeLog:
	* gdb.base/maint.exp: Escape paths used in regular expressions.
	* gdb.stabs/weird.exp: Likewise.

---
 gdb/testsuite/gdb.base/maint.exp  | 15 +++++++++------
 gdb/testsuite/gdb.stabs/weird.exp |  3 ++-
 2 files changed, 11 insertions(+), 7 deletions(-)

diff --git a/gdb/testsuite/gdb.base/maint.exp b/gdb/testsuite/gdb.base/maint.exp
index f926c8b..e66f566 100644
--- a/gdb/testsuite/gdb.base/maint.exp
+++ b/gdb/testsuite/gdb.base/maint.exp
@@ -213,12 +213,13 @@ gdb_test "maint print psymbols" \
 
 if { ! $have_gdb_index } {
     set psymbols_output [standard_output_file psymbols_output]
+    set psymbols_output_re [string_to_regexp $psymbols_output]
     send_gdb "maint print psymbols $psymbols_output ${srcdir}/${subdir}/${srcfile}\n"
     gdb_expect  {
-	-re "^maint print psymbols $psymbols_output \[^\n\]*\r\n$gdb_prompt $" {
+	-re "^maint print psymbols $psymbols_output_re \[^\n\]*\r\n$gdb_prompt $" {
 	    send_gdb "shell ls $psymbols_output\n"
 	    gdb_expect {
-		-re "$psymbols_output\r\n$gdb_prompt $" {
+		-re "$psymbols_output_re\r\n$gdb_prompt $" {
 		    # We want this grep to be as specific as possible,
 		    # so it's less likely to match symbol file names in
 		    # psymbols_output.  Yes, this actually happened;
@@ -253,12 +254,13 @@ gdb_test "maint print msymbols" \
 
 
 set msymbols_output [standard_output_file msymbols_output]
+set msymbols_output_re [string_to_regexp $msymbols_output]
 send_gdb "maint print msymbols $msymbols_output ${binfile}\n"
 gdb_expect  {
-    -re "^maint print msymbols $msymbols_output \[^\n\]*\r\n$gdb_prompt $" {
+    -re "^maint print msymbols $msymbols_output_re \[^\n\]*\r\n$gdb_prompt $" {
 	send_gdb "shell ls $msymbols_output\n"
 	gdb_expect {
-	    -re "$msymbols_output\r\n$gdb_prompt $" {
+	    -re "$msymbols_output_re\r\n$gdb_prompt $" {
 		send_gdb "shell grep factorial $msymbols_output\n"
 		gdb_expect {
 		    -re "\\\[ *$decimal\\\] \[tT\]\[ \t\]+$hex \\.?factorial.*$gdb_prompt $" {
@@ -331,12 +333,13 @@ gdb_test "maint print symbols" \
 # for GNU libc.
 
 set symbols_output [standard_output_file symbols_output]
+set symbols_output_re [string_to_regexp $symbols_output]
 send_gdb "maint print symbols $symbols_output ${srcdir}/${subdir}/${srcfile}\n"
 gdb_expect  {
-    -re "^maint print symbols $symbols_output \[^\n\]*\r\n$gdb_prompt $" {
+    -re "^maint print symbols $symbols_output_re \[^\n\]*\r\n$gdb_prompt $" {
 	send_gdb "shell ls $symbols_output\n"
 	gdb_expect {
-	    -re "$symbols_output\r\n$gdb_prompt $" {
+	    -re "$symbols_output_re\r\n$gdb_prompt $" {
 		# See comments for `maint print psymbols'.
 		send_gdb "shell grep 'main(.*block' $symbols_output\n"
 		gdb_expect {
diff --git a/gdb/testsuite/gdb.stabs/weird.exp b/gdb/testsuite/gdb.stabs/weird.exp
index fa418ed..2a8ef3e 100644
--- a/gdb/testsuite/gdb.stabs/weird.exp
+++ b/gdb/testsuite/gdb.stabs/weird.exp
@@ -284,6 +284,7 @@ gdb_reinitialize_dir $srcdir/$subdir
 
 set binfile [gdb_remote_download host ${binfile} \
 		 [standard_output_file object.o]]
+set binfile_re [string_to_regexp $binfile]
 send_gdb "file $binfile\n"
 # If $binfile is very long, a \r (but not a \n) will echo in the
 # middle of the echo of the command.  So to match the echo, we
@@ -300,7 +301,7 @@ gdb_expect 60 {
 	send_gdb "y\n"
 	exp_continue
     }
-    -re "^Reading symbols from .*$binfile\\.\\.\\.done\.(|\r\nUsing host libthread_db library .*libthread_db.so.*\\.)\r\n$gdb_prompt $" {
+    -re "^Reading symbols from .*$binfile_re\\.\\.\\.done\.(|\r\nUsing host libthread_db library .*libthread_db.so.*\\.)\r\n$gdb_prompt $" {
 	pass "weirdx.o read without error"
     }
     -re ".*$gdb_prompt $" {
-- 
2.8.1

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [PATCH v2] Escape paths used in regular expressions
  2016-07-12 22:28       ` Don Breazeal
@ 2016-07-15 13:34         ` Yao Qi
  2016-07-15 18:24           ` [pushed] " Don Breazeal
  0 siblings, 1 reply; 7+ messages in thread
From: Yao Qi @ 2016-07-15 13:34 UTC (permalink / raw)
  To: Don Breazeal; +Cc: gdb-patches, Pedro Alves

On Tue, Jul 12, 2016 at 11:28 PM, Don Breazeal <donb@codesourcery.com> wrote:
> Zach had submitted this patch back in February, and Pedro had approved it,
> but it was never pushed in.  I've taken over responsibility for this item.
>
> Neither of the affected expect scripts have been changed since this was
> approved.  However, I fixed a typo in the original patch, changing
> string_to_regex to string_to_regexp.
>
> Verified that this patch fixes the tests when run in a directory with
> a name containing "++" on native Linux x86_64.
>
> OK to push this in?

Yes, it is OK.

-- 
Yao (齐尧)

^ permalink raw reply	[flat|nested] 7+ messages in thread

* [pushed] Re: [PATCH v2] Escape paths used in regular expressions
  2016-07-15 13:34         ` Yao Qi
@ 2016-07-15 18:24           ` Don Breazeal
  0 siblings, 0 replies; 7+ messages in thread
From: Don Breazeal @ 2016-07-15 18:24 UTC (permalink / raw)
  To: Yao Qi, Breazeal, Don; +Cc: gdb-patches, Pedro Alves

On 7/15/2016 6:34 AM, Yao Qi wrote:
> On Tue, Jul 12, 2016 at 11:28 PM, Don Breazeal <donb@codesourcery.com> wrote:
>> Zach had submitted this patch back in February, and Pedro had approved it,
>> but it was never pushed in.  I've taken over responsibility for this item.
>>
>> Neither of the affected expect scripts have been changed since this was
>> approved.  However, I fixed a typo in the original patch, changing
>> string_to_regex to string_to_regexp.
>>
>> Verified that this patch fixes the tests when run in a directory with
>> a name containing "++" on native Linux x86_64.
>>
>> OK to push this in?
> 
> Yes, it is OK.
> 
Thanks Yao.
I've pushed this in.
--Don

^ permalink raw reply	[flat|nested] 7+ messages in thread

end of thread, other threads:[~2016-07-15 18:24 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2016-02-10 16:40 [PATCH] GDB testsuite: Escape paths used in regular expressions Zachary T Welch
2016-02-10 16:46 ` Pedro Alves
2016-02-10 17:47   ` [PATCH v2] " Zachary T Welch
2016-02-10 17:53     ` Pedro Alves
2016-07-12 22:28       ` Don Breazeal
2016-07-15 13:34         ` Yao Qi
2016-07-15 18:24           ` [pushed] " Don Breazeal

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).