Skip to content

Commit

Permalink
Fix ruff complaints
Browse files Browse the repository at this point in the history
  • Loading branch information
twizmwazin committed Aug 14, 2023
1 parent 330ad1e commit 31d6af0
Show file tree
Hide file tree
Showing 7 changed files with 23 additions and 29 deletions.
4 changes: 2 additions & 2 deletions cle/backends/elf/elf.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
12 changes: 3 additions & 9 deletions cle/backends/elf/metaelf.py
Original file line number Diff line number Diff line change
Expand Up @@ -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__)

Expand All @@ -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/):
Expand Down Expand Up @@ -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:
Expand Down
5 changes: 1 addition & 4 deletions cle/backends/elf/regions.py
Original file line number Diff line number Diff line change
@@ -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):
Expand Down
7 changes: 2 additions & 5 deletions cle/backends/elf/symbol.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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"
Expand Down
14 changes: 7 additions & 7 deletions cle/loader.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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):
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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)
Expand Down
4 changes: 2 additions & 2 deletions cle/memory.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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:
Expand Down
6 changes: 6 additions & 0 deletions cle/utils.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import contextlib
import os
from typing import Union

import elftools

Expand Down Expand Up @@ -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()

0 comments on commit 31d6af0

Please sign in to comment.