From 31d6af0d2f480bc122649764670150559e759429 Mon Sep 17 00:00:00 2001 From: Kevin Phoenix Date: Mon, 14 Aug 2023 14:42:34 -0700 Subject: [PATCH] Fix ruff complaints --- cle/backends/elf/elf.py | 4 ++-- cle/backends/elf/metaelf.py | 12 +++--------- cle/backends/elf/regions.py | 5 +---- cle/backends/elf/symbol.py | 7 ++----- cle/loader.py | 14 +++++++------- cle/memory.py | 4 ++-- cle/utils.py | 6 ++++++ 7 files changed, 23 insertions(+), 29 deletions(-) diff --git a/cle/backends/elf/elf.py b/cle/backends/elf/elf.py index 06390909..e540123b 100644 --- a/cle/backends/elf/elf.py +++ b/cle/backends/elf/elf.py @@ -383,7 +383,7 @@ def get_symbol(self, symid, symbol_table=None): # pylint: disable=arguments-dif :param symid: Either an index into .dynsym or the name of a symbol. """ version = None - if type(symid) is int: + if isinstance(symid, int): if symid == 0: # special case the null symbol, this is important for static binaries return self._nullsymbol @@ -400,7 +400,7 @@ def get_symbol(self, symid, symbol_table=None): # pylint: disable=arguments-dif return cached if self.hashtable is not None and symbol_table is self.hashtable.symtab and self._vertable is not None: version = self._vertable.get_symbol(symid).entry.ndx - elif type(symid) is str: + elif isinstance(symid, str): if not symid: log.warning("Trying to resolve a symbol by its empty name") return None diff --git a/cle/backends/elf/metaelf.py b/cle/backends/elf/metaelf.py index 2c90069a..e36aafab 100644 --- a/cle/backends/elf/metaelf.py +++ b/cle/backends/elf/metaelf.py @@ -12,9 +12,9 @@ from cle.address_translator import AT from cle.backends.backend import Backend from cle.backends.symbol import SymbolType -from cle.utils import stream_or_path +from cle.utils import maybedecode, stream_or_path -__all__ = ("MetaELF", "Relro", "maybedecode") +__all__ = ("MetaELF", "Relro") log = logging.getLogger(name=__name__) @@ -25,12 +25,6 @@ class Relro(Enum): FULL = 2 -def maybedecode(string): - # so... it turns out that pyelftools is garbage and will transparently give you either strings or bytestrings - # based on pretty much nothing whatsoever - return string if type(string) is str else string.decode() - - def _get_relro(elf): # The tests for partial and full RELRO have been taken from # checksec.sh v1.5 (https://www.trapkit.de/tools/checksec/): @@ -479,7 +473,7 @@ def extract_soname(path): for tag in seg.iter_tags(): if tag.entry.d_tag == "DT_SONAME": return maybedecode(tag.soname) - if type(path) is str: + if isinstance(path, str): return os.path.basename(path) except elftools.common.exceptions.ELFError: diff --git a/cle/backends/elf/regions.py b/cle/backends/elf/regions.py index c8234e49..a020f2ba 100644 --- a/cle/backends/elf/regions.py +++ b/cle/backends/elf/regions.py @@ -1,8 +1,5 @@ from cle.backends.region import Section, Segment - - -def maybedecode(string): - return string if type(string) is str else string.decode() +from cle.utils import maybedecode class ELFSegment(Segment): diff --git a/cle/backends/elf/symbol.py b/cle/backends/elf/symbol.py index d7e14a15..49fa72fc 100644 --- a/cle/backends/elf/symbol.py +++ b/cle/backends/elf/symbol.py @@ -2,14 +2,11 @@ from cle.address_translator import AT from cle.backends.symbol import Symbol, SymbolType +from cle.utils import maybedecode from .symbol_type import ELFSymbolType -def maybedecode(string): - return string if type(string) is str else string.decode() - - class ELFSymbol(Symbol): """ Represents a symbol for the ELF format. @@ -49,7 +46,7 @@ def __init__(self, owner, symb): self.version = None self.binding = symb.entry.st_info.bind self.is_hidden = symb.entry["st_other"]["visibility"] == "STV_HIDDEN" - self.section = sec_ndx if type(sec_ndx) is not str else None + self.section = sec_ndx if not isinstance(sec_ndx, str) else None self.is_static = self._type == SymbolType.TYPE_SECTION or sec_ndx == "SHN_ABS" self.is_common = sec_ndx == "SHN_COMMON" self.is_weak = self.binding == "STB_WEAK" diff --git a/cle/loader.py b/cle/loader.py index 8babd2ae..d77e509f 100644 --- a/cle/loader.py +++ b/cle/loader.py @@ -150,9 +150,9 @@ def __init__( self._satisfied_deps: Dict[str, Union[Literal[False], Backend]] = {x: False for x in skip_libs} self._main_opts = {} if main_opts is None else main_opts self._lib_opts = {} if lib_opts is None else lib_opts - self._custom_ld_path = [ld_path] if type(ld_path) is str else ld_path - force_load_libs = [force_load_libs] if type(force_load_libs) is str else force_load_libs - preload_libs = [preload_libs] if type(preload_libs) is str else preload_libs + self._custom_ld_path = [ld_path] if isinstance(ld_path, str) else ld_path + force_load_libs = [force_load_libs] if isinstance(force_load_libs, str) else force_load_libs + preload_libs = [preload_libs] if isinstance(preload_libs, str) else preload_libs self._use_system_libs = use_system_libs self._ignore_import_version_numbers = ignore_import_version_numbers self._case_insensitive = case_insensitive @@ -165,7 +165,7 @@ def __init__( if sys.platform == "win32": # TODO: a real check for case insensitive filesystems if self._main_binary_path: self._main_binary_path = self._main_binary_path.lower() - force_load_libs = [x.lower() if type(x) is str else x for x in force_load_libs] + force_load_libs = [x.lower() if isinstance(x, str) else x for x in force_load_libs] for x in list(self._satisfied_deps): self._satisfied_deps[x.lower()] = self._satisfied_deps[x] for x in list(self._lib_opts): @@ -436,7 +436,7 @@ def _check_object_memory(obj_): self._last_object = obj_ return obj_ return None - elif type(obj_.memory) is str: + elif isinstance(obj_.memory, str): self._last_object = obj_ return obj_ else: @@ -564,11 +564,11 @@ def find_symbol(self, thing, fuzzy=False) -> Optional[Symbol]: :returns: A :class:`cle.backends.Symbol` object if found, None otherwise. """ - if type(thing) is archinfo.arch_soot.SootAddressDescriptor: + if isinstance(thing, archinfo.arch_soot.SootAddressDescriptor): # Soot address # TODO launch this shit into the sun return thing.method.fullname # type: ignore - elif type(thing) is int: + elif isinstance(thing, int): # address if fuzzy: so = self.find_object_containing(thing) diff --git a/cle/memory.py b/cle/memory.py index 051b948c..13ddd2ae 100644 --- a/cle/memory.py +++ b/cle/memory.py @@ -213,7 +213,7 @@ def add_backer(self, start, data, overwrite=False): raise ValueError("Address %#x is already backed!" % start) if isinstance(data, Clemory) and data._root: raise ValueError("Cannot add a root clemory as a backer!") - if type(data) is bytes: + if isinstance(data, bytes): data = bytearray(data) bisect.insort(self._backers, (start, data)) self._update_min_max() @@ -243,7 +243,7 @@ def __repr__(self) -> str: def update_backer(self, start, data): if not isinstance(data, (bytes, list, Clemory)): raise TypeError("Data must be a bytes, list, or Clemory object.") - if type(data) is bytes: + if isinstance(data, bytes): data = bytearray(data) for i, (oldstart, _) in enumerate(self._backers): if oldstart == start: diff --git a/cle/utils.py b/cle/utils.py index 2a668290..ceeedfc8 100644 --- a/cle/utils.py +++ b/cle/utils.py @@ -1,5 +1,6 @@ import contextlib import os +from typing import Union import elftools @@ -146,3 +147,8 @@ def get_text_offset(path): with stream_or_path(path) as f: e = elftools.elf.elffile.ELFFile(f) return e.get_section_by_name(".text").header.sh_offset + + +def maybedecode(string: Union[str, bytes]) -> str: + """maybedecode will return a decoded string if the input is bytes, otherwise it will return the input.""" + return string if isinstance(string, str) else string.decode()