Skip to content

Debloat any Android or Google TV over Wi-Fi and get an Apple TV-style home.

fix(tools): 🐛 keep flame charts from overwriting input traces

cixiangtao79ebf21c9f89
diff --git a/tests/test_flamechart_trace_path.py b/tests/test_flamechart_trace_path.py
index df6c4aacb..a78cbcd9e 100644
--- a/tests/test_flamechart_trace_path.py
+++ b/tests/test_flamechart_trace_path.py
@@ -23,6 +23,7 @@ Run from the repository root: python3 -m unittest discover -s tests -v
import contextlib
import importlib.util
import io
+import os
from pathlib import Path
import sys
import tempfile
@@ -65,6 +66,41 @@ class TracePathTest(unittest.TestCase):
path.write_bytes(b"")
return str(path)
+ def test_output_cannot_replace_a_trace_by_path_or_link(self):
+ for kind in ("direct", "symlink", "hardlink"):
+ with self.subTest(kind=kind):
+ source = Path(self.trace("input-" + kind + ".pftrace"))
+ source.write_bytes(b"keep this trace")
+ output = self.folder / (kind + ".png")
+ if kind == "direct":
+ source.rename(output)
+ source = output
+ else:
+ try:
+ (os.symlink if kind == "symlink" else os.link)(source, output)
+ except (OSError, NotImplementedError):
+ continue # the direct path control still runs without link support
+ with mock.patch.object(trace_report, "opener") as opener, \
+ contextlib.redirect_stderr(io.StringIO()):
+ self.assertEqual(flamechart.main([str(source), "--out", str(output)]), 2)
+ opener.assert_not_called()
+ self.assertEqual(source.read_bytes(), b"keep this trace")
+
+ def test_output_alias_of_any_folder_run_is_refused_before_median_selection(self):
+ source = Path(self.trace("nav-1.pftrace"))
+ source.write_bytes(b"keep the unselected run too")
+ self.trace("nav-2.pftrace")
+ output = self.folder / "chart.png"
+ try:
+ os.link(source, output)
+ except (OSError, NotImplementedError):
+ self.skipTest("hardlinks are not available here")
+ with mock.patch.object(trace_report, "opener") as opener, \
+ contextlib.redirect_stderr(io.StringIO()):
+ self.assertEqual(flamechart.main([str(self.folder), "--out", str(output)]), 2)
+ opener.assert_not_called()
+ self.assertEqual(source.read_bytes(), b"keep the unselected run too")
+
def test_a_path_that_is_not_a_file_is_named_before_the_engine_starts(self):
missing = str(self.folder / "nav-1.pftrace")
code, _, opener = self.run_main([missing, "--out", self.out])
diff --git a/tools/flamechart.md b/tools/flamechart.md
index 32bd050b1..49c499894 100644
--- a/tools/flamechart.md
+++ b/tools/flamechart.md
@@ -17,6 +17,8 @@ tools/venv/bin/python tools/flamechart.py traces/before traces/after \
before the traces are opened, so a typo cannot read as a chart that failed. A `TRACE` path is checked the
same way, before the trace engine is started: a path that is not there is `not a file: <path>`, the words
`tools/trace_report.py` uses, and a folder with no `.pftrace` in it names the folder.
+An output that is the same file as any input trace (including symbolic and hard links) is refused
+with exit 2 before the engine starts, so drawing the chart cannot overwrite a recording.
| Option | Default | Notes |
|---|---|---|
diff --git a/tools/flamechart.py b/tools/flamechart.py
index 10ba91442..94b9126fb 100644
--- a/tools/flamechart.py
+++ b/tools/flamechart.py
@@ -22,6 +22,7 @@ import argparse
import colorsys
import hashlib
import os
+from pathlib import Path
import re
import sys
@@ -272,6 +273,18 @@ def main(argv=None):
missing = [path for path in paths if not os.path.isfile(path)]
if missing:
sys.exit("not a file: " + ", ".join(missing))
+ # A .png name may alias a trace through a symlink or hardlink. Refuse before
+ # starting the engine, including every run a folder could choose as its median.
+ try:
+ target = Path(args.out)
+ for paths in sets:
+ for path in paths:
+ source = Path(path)
+ if target.resolve() == source.resolve() or (target.exists() and target.samefile(source)):
+ raise ValueError(f"--out {args.out} is the same file as trace {path}")
+ except (OSError, RuntimeError, ValueError) as e:
+ print(f"{e}. No trace was read or written.", file=sys.stderr)
+ return 2
try:
import PIL # noqa: F401
except ImportError:
@@ -301,4 +314,4 @@ def main(argv=None):
if __name__ == "__main__":
- main()
+ sys.exit(main())