[hackers] [sbase] [PATCH] which: absolute path not handled

From: Pieter Kockx <pieterkockx_AT_gmail.com>
Date: Tue, 10 Oct 2017 22:56:04 +0000

Hello hackers

$ which /usr/bin/env
/usr/bin//usr/bin/env

which should probably be idempotent when applied to its own output as
in which $(which $(which env)).

The underlying reason is that fstatat ignores dirfd if the the given
filename is an absolute path.
The following fix is possible:

if ((dirfd = open(p, O_RDONLY, 0)) >= 0) {
  if (!fstatat(dirfd, name, &st, 0) &&
      S_ISREG(st.st_mode) &&
      !faccessat(dirfd, name, X_OK, 0)) {
    found = 1;
+ if (name[0] == '/') {
+ puts(name);
+ close(dirfd);
+ break;
+ }
    fputs(p, stdout);
    if (i && ptr[i - 1] != '/')
      fputc('/', stdout);
    puts(name);
    if (!aflag) {
      close(dirfd);
      break;
    }
  }
}

This fix is not perfect though, since which will (still) return
a different result if PATH contains no valid directories:

$ PATH=/foo which /usr/bin/env
Error
$ PATH=/ which /usr/bin/env
/usr/bin/env

Patch incoming.

Something else, utilities like which are often used used to check for the
existence of a file, so printing an error message seems undesirable.

Thoughts?

-- Pieter
Received on Wed Oct 11 2017 - 00:56:04 CEST

This archive was generated by hypermail 2.3.0 : Wed Oct 11 2017 - 01:00:43 CEST