Skip to content

Commit

Permalink
Use shutil when looking for executables on the host system (#1146)
Browse files Browse the repository at this point in the history
## Description

The implementation of the `which` function only checks for the
executable flag on paths, which means it can return a directory (at
least on POSIX), causing weird permission errors down the line. It
should instead defer to `shutil.which`; that function contains a
platform-agnostic implementation that should hopefully match the
behavior of the GNU `which` utility.

Co-authored-by: Grazfather <[email protected]>
Co-authored-by: crazy hugsy <[email protected]>
  • Loading branch information
3 people authored Oct 29, 2024
1 parent ba70548 commit 5376d78
Showing 1 changed file with 4 additions and 7 deletions.
11 changes: 4 additions & 7 deletions gef.py
Original file line number Diff line number Diff line change
Expand Up @@ -1936,13 +1936,10 @@ def gef_pybytes(x: str) -> bytes:
@lru_cache()
def which(program: str) -> pathlib.Path:
"""Locate a command on the filesystem."""
for path in os.environ["PATH"].split(os.pathsep):
dirname = pathlib.Path(path)
fpath = dirname / program
if os.access(fpath, os.X_OK):
return fpath

raise FileNotFoundError(f"Missing file `{program}`")
res = shutil.which(program)
if not res:
raise FileNotFoundError(f"Missing file `{program}`")
return pathlib.Path(res)


def style_byte(b: int, color: bool = True) -> str:
Expand Down

0 comments on commit 5376d78

Please sign in to comment.