From 091e0ab1ca0a48eead7f64792fa8b5eb1d67f4de Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C5=8Dshin?= Date: Sun, 17 Dec 2023 09:52:05 -0500 Subject: [PATCH] fix com test TryPath with com==1 tries both non-com and com, so the checks were redundant. With com==0, there is no reason to copy to the buffer if the initial access fails. --- libc/calls/getprogramexecutablename.greg.c | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/libc/calls/getprogramexecutablename.greg.c b/libc/calls/getprogramexecutablename.greg.c index e750152ed..d8d1fc93f 100644 --- a/libc/calls/getprogramexecutablename.greg.c +++ b/libc/calls/getprogramexecutablename.greg.c @@ -60,6 +60,10 @@ static int TryPath(const char *q, int com) { int c, f_ok; if ((f_ok = !sys_faccessat(AT_FDCWD, q, F_OK, 0))) { com = 0; + } else { + if (!com) { + return 0; + } } if (*q != '/') { if (q[0] == '.' && q[1] == '/') { @@ -80,7 +84,6 @@ static int TryPath(const char *q, int com) { *p = 0; return 1; } - if (!com) return 0; p = WRITE32LE(p, READ32LE(".com")); *p = 0; return !sys_faccessat(AT_FDCWD, g_prog.u.buf, F_OK, 0); @@ -159,9 +162,8 @@ static inline void InitProgramExecutableNameImpl(void) { // Try what the loader supplied first. Fall back to argv[0], // then argv[0].com, then $_, then $_.com. - if (TryPath(__program_executable_name, 0) || - TryPath(__argv[0], 0) || TryPath(__argv[0], 1) || - TryPath((q = __getenv(__envp, "_").s), 0) || TryPath(q, 1)) { + if (TryPath(__program_executable_name, 0) || TryPath(__argv[0], 1) || + TryPath(__getenv(__envp, "_").s, 1)) { goto UseBuf; }