Skip to content

Commit

Permalink
Slightly improve the code of search-pattern (#862)
Browse files Browse the repository at this point in the history
Make `search-pattern` rely on native `gdb` exception to catch invalid memory access instead of relying on the string of the error.
  • Loading branch information
therealdreg committed Jul 2, 2022
1 parent 35c115a commit 366237c
Showing 1 changed file with 3 additions and 15 deletions.
18 changes: 3 additions & 15 deletions gef.py
Expand Up @@ -5714,7 +5714,7 @@ def print_loc(self, loc: Tuple[int, int, str]) -> None:
def search_pattern_by_address(self, pattern: str, start_address: int, end_address: int) -> List[Tuple[int, int, Optional[str]]]:
"""Search a pattern within a range defined by arguments."""
_pattern = gef_pybytes(pattern)
step = 0x400 * 0x1000
step = self["nr_pages_chunk"] * gef.session.pagesize
locations = []

for chunk_addr in range(start_address, end_address, step):
Expand All @@ -5725,20 +5725,8 @@ def search_pattern_by_address(self, pattern: str, start_address: int, end_addres

try:
mem = gef.memory.read(chunk_addr, chunk_size)
except gdb.error as e:
estr = str(e)
if estr.startswith("Cannot access memory "):
#
# This is a special case where /proc/$pid/maps
# shows virtual memory address with a read bit,
# but it cannot be read directly from userspace.
#
# See: https://github.com/hugsy/gef/issues/674
#
err(estr)
return []
else:
raise e
except gdb.MemoryError as e:
return []

for match in re.finditer(_pattern, mem):
start = chunk_addr + match.start()
Expand Down

0 comments on commit 366237c

Please sign in to comment.