Author: Zachary Henkel
Date: 2026-07-17T08:47:03-07:00
New Revision: bcb459015e12fb03e4ae3d56393883b2974f96fb

URL: 
https://github.com/llvm/llvm-project/commit/bcb459015e12fb03e4ae3d56393883b2974f96fb
DIFF: 
https://github.com/llvm/llvm-project/commit/bcb459015e12fb03e4ae3d56393883b2974f96fb.diff

LOG: Replace Python's deprecated tempfile.mktemp() with secure temp APIs 
(#210123)

Python's tempfile.mktemp() is deprecated and insecure: it returns a path
without creating the file, leaving a TOCTOU/symlink window before the
file is opened. Replace every use, choosing the temp API by lifetime:

- compiler-rt android_common.py adb(): tempfile.TemporaryFile
(anonymous, auto-deleted), read back via seek(0); drops manual
close()/unlink().
- compiler-rt android_common.py pull_from_device():
tempfile.TemporaryDirectory so "adb pull" writes into a private,
auto-cleaned directory.
- lldb examples delta.py / gdbremote.py start_gdb_log():
tempfile.NamedTemporaryFile(delete=False); the log must outlive the call
for a later stop_gdb_log, so it is intentionally not auto-deleted.

Reported by internal static analysis and contributed upstream for the
benefit of the wider community.

Assisted-by: GitHub Copilot CLI (Claude Opus 4.8)

I do not have commit access and will need someone to submit on my
behalf.

Added: 
    

Modified: 
    compiler-rt/test/sanitizer_common/android_commands/android_common.py
    lldb/examples/python/delta.py
    lldb/examples/python/gdbremote.py

Removed: 
    


################################################################################
diff  --git 
a/compiler-rt/test/sanitizer_common/android_commands/android_common.py 
b/compiler-rt/test/sanitizer_common/android_commands/android_common.py
index 4e7d15c9ebc23..6c7c410108a74 100644
--- a/compiler-rt/test/sanitizer_common/android_commands/android_common.py
+++ b/compiler-rt/test/sanitizer_common/android_commands/android_common.py
@@ -18,33 +18,28 @@ def host_to_device_path(path):
 def adb(args, attempts=1, timeout_sec=600):
     if verbose:
         print(args)
-    tmpname = tempfile.mktemp()
-    out = open(tmpname, "w")
-    ret = 255
-    while attempts > 0 and ret != 0:
-        attempts -= 1
-        ret = subprocess.call(
-            ["timeout", str(timeout_sec), ADB] + args,
-            stdout=out,
-            stderr=subprocess.STDOUT,
-        )
-    if ret != 0:
-        print("adb command failed", args)
-        print(tmpname)
-        out.close()
-        out = open(tmpname, "r")
-        print(out.read())
-    out.close()
-    os.unlink(tmpname)
+    with tempfile.TemporaryFile(mode="w+") as out:
+        ret = 255
+        while attempts > 0 and ret != 0:
+            attempts -= 1
+            ret = subprocess.call(
+                ["timeout", str(timeout_sec), ADB] + args,
+                stdout=out,
+                stderr=subprocess.STDOUT,
+            )
+        if ret != 0:
+            print("adb command failed", args)
+            out.seek(0)
+            print(out.read())
     return ret
 
 
 def pull_from_device(path):
-    tmp = tempfile.mktemp()
-    adb(["pull", path, tmp], 5, 60)
-    text = open(tmp, "r").read()
-    os.unlink(tmp)
-    return text
+    with tempfile.TemporaryDirectory() as tmp_dir:
+        tmp = os.path.join(tmp_dir, "pulled")
+        adb(["pull", path, tmp], 5, 60)
+        with open(tmp, "r") as f:
+            return f.read()
 
 
 def push_to_device(path):

diff  --git a/lldb/examples/python/delta.py b/lldb/examples/python/delta.py
old mode 100755
new mode 100644
index 35f155bdea100..e84185b1ae05a
--- a/lldb/examples/python/delta.py
+++ b/lldb/examples/python/delta.py
@@ -35,7 +35,8 @@ def start_gdb_log(debugger, command, result, dict):
     else:
         args_len = len(args)
         if args_len == 0:
-            log_file = tempfile.mktemp()
+            with tempfile.NamedTemporaryFile(delete=False) as tmp:
+                log_file = tmp.name
         elif len(args) == 1:
             log_file = args[0]
 

diff  --git a/lldb/examples/python/gdbremote.py 
b/lldb/examples/python/gdbremote.py
old mode 100755
new mode 100644
index 2f2d82c3d3d54..a215a878ef0a0
--- a/lldb/examples/python/gdbremote.py
+++ b/lldb/examples/python/gdbremote.py
@@ -229,7 +229,8 @@ def start_gdb_log(debugger, command, result, dict):
     else:
         args_len = len(args)
         if args_len == 0:
-            g_log_file = tempfile.mktemp()
+            with tempfile.NamedTemporaryFile(delete=False) as tmp:
+                g_log_file = tmp.name
         elif len(args) == 1:
             g_log_file = args[0]
 


        
_______________________________________________
lldb-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/lldb-commits

Reply via email to