diff options
author | Yabin Cui <yabinc@google.com> | 2019-08-22 10:50:19 -0700 |
---|---|---|
committer | Yabin Cui <yabinc@google.com> | 2019-08-22 10:56:44 -0700 |
commit | 0e50ea422f283a277a598f9aa12a40c01c515a79 (patch) | |
tree | ef80d719e23e63ddb98b09f71d7ca0a92875727d | |
parent | 5ff5b91fdc0b07bebaaecd76e47d5c57571ca5ad (diff) | |
download | extras-0e50ea422f283a277a598f9aa12a40c01c515a79.tar.gz |
simpleperf: fix is_elf_file.
When running by python3, is_elf_file() decodes first 4 bytes of
a file, which may cause UnicodeDecodeError.
Bug: none
Test: run test.py TestTools.test_is_elf_file.
Change-Id: Ie19e4869f876bb92155d849c814c8f39989c443b
-rwxr-xr-x | simpleperf/scripts/test.py | 12 | ||||
-rw-r--r-- | simpleperf/scripts/utils.py | 4 |
2 files changed, 12 insertions, 4 deletions
diff --git a/simpleperf/scripts/test.py b/simpleperf/scripts/test.py index 24a04356..e24f58f1 100755 --- a/simpleperf/scripts/test.py +++ b/simpleperf/scripts/test.py @@ -56,7 +56,7 @@ from binary_cache_builder import BinaryCacheBuilder from simpleperf_report_lib import ReportLib from utils import log_exit, log_info, log_fatal from utils import AdbHelper, Addr2Nearestline, bytes_to_str, find_tool_path, get_script_dir -from utils import is_python3, is_windows, Objdump, ReadElf, remove, SourceFileSearcher +from utils import is_elf_file, is_python3, is_windows, Objdump, ReadElf, remove, SourceFileSearcher from utils import str_to_bytes try: @@ -1188,6 +1188,16 @@ class TestTools(unittest.TestCase): 'simpleperf/simpleperfexampleofkotlin/MainActivity.kt'), searcher.get_real_path('MainActivity.kt')) + def test_is_elf_file(self): + self.assertTrue(is_elf_file(os.path.join( + 'testdata', 'simpleperf_runtest_two_functions_arm'))) + with open('not_elf', 'wb') as fh: + fh.write(b'\x90123') + try: + self.assertFalse(is_elf_file('not_elf')) + finally: + remove('not_elf') + class TestNativeLibDownloader(unittest.TestCase): def setUp(self): diff --git a/simpleperf/scripts/utils.py b/simpleperf/scripts/utils.py index 4b942d8c..9891e606 100644 --- a/simpleperf/scripts/utils.py +++ b/simpleperf/scripts/utils.py @@ -374,9 +374,7 @@ def open_report_in_browser(report_path): def is_elf_file(path): if os.path.isfile(path): with open(path, 'rb') as fh: - data = fh.read(4) - if len(data) == 4 and bytes_to_str(data) == '\x7fELF': - return True + return fh.read(4) == b'\x7fELF' return False def find_real_dso_path(dso_path_in_record_file, binary_cache_path): |