Be extra careful to quote $abs_top_builddir-derived names.
authorJim Meyering <meyering@redhat.com>
Fri, 30 Nov 2007 12:45:38 +0000 (13:45 +0100)
committerJim Meyering <meyering@redhat.com>
Fri, 30 Nov 2007 12:45:38 +0000 (13:45 +0100)
* tests/misc/ls-misc (shell_quote): New function.
Use it to quote file names derived from $abs_top_builddir,
in case it contains shell meta-characters.  This is not currently
needed, since CuTmpdir detects the fishy name and skips the test.
But it's important enough to add the extra protection.
Reported by Ralf Wildenhues.

ChangeLog
tests/misc/ls-misc

index 2013a25..005cf8f 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,5 +1,13 @@
 2007-11-30  Jim Meyering  <meyering@redhat.com>
 
+       Be extra careful to quote $abs_top_builddir-derived names.
+       * tests/misc/ls-misc (shell_quote): New function.
+       Use it to quote file names derived from $abs_top_builddir,
+       in case it contains shell meta-characters.  This is not currently
+       needed, since CuTmpdir detects the fishy name and skips the test.
+       But it's important enough to add the extra protection.
+       Reported by Ralf Wildenhues.
+
        Include test name in the "unsafe working directory name" diagnostic.
        * tests/CuTmpdir.pm (import): If $ME is '-', use $prefix.
 
index 520c503..1e4f327 100755 (executable)
@@ -35,9 +35,24 @@ use strict;
 # Turn off localisation of executable's ouput.
 @ENV{qw(LANGUAGE LANG LC_ALL)} = ('C') x 3;
 
+# If the string $S is a well-behaved file name, simply return it.
+# If it contains white space, quotes, etc., quote it, and return the new string.
+sub shell_quote($)
+{
+  my ($s) = @_;
+  if ($s =~ m![^\w+/.,-]!)
+    {
+      # Convert each single quote to '\''
+      $s =~ s/\'/\'\\\'\'/g;
+      # Then single quote the string.
+      $s = "'$s'";
+    }
+  return $s;
+}
+
 # Set up files used by the setuid-etc tests; skip this entire test if
 # that cannot be done.
-my $test = "$ENV{abs_top_builddir}/src/test";
+my $test = shell_quote "$ENV{abs_top_builddir}/src/test";
 system (qq(touch setuid && chmod u+s setuid && $test -u setuid &&
           touch setgid && chmod g+s setgid && $test -g setgid &&
           mkdir sticky && chmod +t sticky  && $test -k sticky &&