[PATCH v2 07/10] sandbox/sandbox: use slicing rather than re.sub in copyfile()

Stephen Smalley <[email protected]>
Newsgroups org.kernel.vger.selinux
Message-ID <[email protected]>
copyfile() rewrites the leading srcdir with dest via re.sub(),
which treats srcdir as a regular expression and dest as a
replacement template. A home directory containing regex
metacharacters or a destination containing a backslash
will mangle the result. Replace the use of re.sub() with
simple slicing and string concatenation.

Signed-off-by: Stephen Smalley <[email protected]>
---
 sandbox/sandbox | 3 +--
 1 file changed, 1 insertion(+), 2 deletions(-)

diff --git a/sandbox/sandbox b/sandbox/sandbox
index 4e7d56fd..3368a95b 100644
--- a/sandbox/sandbox
+++ b/sandbox/sandbox
@@ -84,14 +84,13 @@ def error_exit(msg):
 
 
 def copyfile(file, srcdir, dest):
-    import re
     if file.startswith(srcdir):
         dname = os.path.dirname(file)
         bname = os.path.basename(file)
         if dname == srcdir:
             dest = dest + "/" + bname
         else:
-            newdir = re.sub(srcdir, dest, dname)
+            newdir = dest + dname[len(srcdir):]
             if not os.path.exists(newdir):
                 os.makedirs(newdir)
             dest = newdir + "/" + bname
-- 
2.55.0
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.