emscripten-core / emscripten

Emscripten: An LLVM-to-WebAssembly Compiler
Other
25.92k stars 3.32k forks source link

Readlink incorrectly returns fully qualified path #22999

Closed hoodmane closed 1 week ago

hoodmane commented 1 week ago

Considering the following:

#include <fcntl.h>
#include <string.h>
#include <stdio.h>
#include <assert.h>
#include <unistd.h>

static void create_file(const char *path, const char *buffer, int mode) {
  int fd = open(path, O_WRONLY | O_CREAT | O_EXCL, mode);
  assert(fd >= 0);

  int err = write(fd, buffer, sizeof(char) * strlen(buffer));
  assert(err ==  (sizeof(char) * strlen(buffer)));

  close(fd);
}

int main() {
    char buf[20];

    create_file("symlink_target", "xyz", 0777);
    symlink("symlink_target", "symlink_src");
    readlink("symlink_src", buf, 20);
    printf("buf: '%s'\n", buf);
    unlink("symlink_src");
    unlink("symlink_target");
}

Running with gcc we just the contents of the link vs running with emcc we get a fully qualified path:

$ gcc a.c
$ ./a.out
buf: 'symlink_target'
$ emcc a.c
$ node a.out.js
buf: '/symlink_target'

Note the initial / in the emcc run.

hoodmane commented 1 week ago

Actually the one call site of readlink in the Emscripten source code doesn't seem to expect this behavior since it immediately calls resolve: https://github.com/emscripten-core/emscripten/blob/main/src/library_fs.js?plain=1#L217-L218