hare

[hare] The Hare programming language
git clone https://git.torresjrjr.com/hare.git
Log | Files | Refs | README | LICENSE

commit dd8506172d1c4a5bbac9aca7166698fcbdb25b02
parent c55e262eb44be4916a9f3f452f8635d42c419565
Author: Willow Barraco <contact@willowbarraco.fr>
Date:   Thu,  5 Dec 2024 17:56:33 +0100

cmd/hare/build: Simplify binary path lookup

Using os::exists here is a bit wrong. Because the file could not have
the os::amode::X_OK.

exec::lookup already handle relative paths correctly ("./qbe"),
and absolute paths ("/usr/bin/qbe"). I don't think using "qbe" to mean a
"./qbe", present in the current directory is valid anyway.

TOCTOU can still happen. I'm not sure we can do much more at this point.

Signed-off-by: Willow Barraco <contact@willowbarraco.fr>

Diffstat:
Mcmd/hare/build.ha | 12++++--------
1 file changed, 4 insertions(+), 8 deletions(-)

diff --git a/cmd/hare/build.ha b/cmd/hare/build.ha @@ -159,14 +159,10 @@ fn build(name: str, cmd: *getopt::command) (void | error) = { defer hash::close(&h); for (let i = 1z; i < len(ctx.cmds); i += 1) { defer hash::reset(&h); - const cmd_path = if (os::exists(ctx.cmds[i])) { - yield ctx.cmds[i]; - } else { - yield match (exec::lookup(ctx.cmds[i])) { - case let cmd_path: str => yield cmd_path; - case void => - fmt::fatalf("Error: Command not found: {}", ctx.cmds[i]); - }; + const cmd_path = match (exec::lookup(ctx.cmds[i])) { + case let cmd_path: str => yield cmd_path; + case void => + fmt::fatalf("Error: Command not found: {}", ctx.cmds[i]); }; const cmd_file = match (os::open(cmd_path)) { case let file: io::file => yield file;