faust icon indicating copy to clipboard operation
faust copied to clipboard

exepath::resolvelink does not correctly multilevel symlinks

Open umlaeute opened this issue 3 years ago • 1 comments

while investigating #676 i noticed that exepath::resolvelink fails to resolve multilevel symlinks.

consider this scenario:

$ ls -lha /usr/bin/faust
lrwxrwxrwx 1 root root 23 Aug 24 17:41 /usr/bin/faust -> /etc/alternatives/faust
$ ls -lha /etc/alternatives/faust
lrwxrwxrwx 1 root root 26 Aug 30 17:22 /etc/alternatives/faust -> /usr/bin/faust-2.37.3

then exepath::get will return /etc/alternatives as the path of the executable :-(

(note: this example is completely made up to illustrate the issue; i haven't seen anything like this in the real world)

umlaeute avatar Dec 02 '21 08:12 umlaeute

i guess the proper solution is to repeatedly call readlink() until the input is the same as the output:

void resolvelink(const char*path0) {
  char inbuf[BUFSIZE], outbuf[BUFSIZE];
  int count = 0;
  ssize_t len = 0;
  snprintf(inbuf, BUFSIZE-1, path0);
  inbuf[BUFSIZE-1] = 0;
  while(count < 50 && (len = readlink(inbuf, outbuf, BUFSIZE))) {
    if(len>= 0 && len<BUFSIZE)
      outbuf[len] = 0;
    if(!strncmp(inbuf, outbuf, BUFSIZE)) {
      printf("'%s' -> '%s'\n", path0, outbuf);
      return;
    }
    snprintf(inbuf, BUFSIZE-1, outbuf);
    inbuf[BUFSIZE-1] = 0;
    count++;
  }
}

umlaeute avatar Dec 02 '21 09:12 umlaeute