Skip to content

NODERAWFS readdir cuts names past 255 bytes, which APFS holds (CJK names up to 765 bytes), to names that do not exist #28250

Open

Description

@jaredmbourgeois

Version: emsdk 6.0.11 (emcc 6.0.11, a001454) and its node 24.19.0, on macOS 27.0.1 with APFS, built with -sNODERAWFS=1.

emcc -v:

emcc (Emscripten gcc/clang-like replacement + linker emulating GNU ld) 6.0.11 (a0014542110d6078c3a1a7941fa1ddb3a2281f16)
clang version 24.0.0git (https:/github.com/llvm/llvm-project f718ebe873411a562ef6e5c27da36d75b7aaec98)
Target: wasm32-unknown-emscripten
Thread model: posix
InstalledDir: <emsdk>/upstream/bin

Such names exist in the wild: APFS, the file system of every Mac, bounds a name at 255 UTF-16 code units, not 255 bytes. A character of Chinese, Japanese or Korean is three bytes of UTF-8, so a name of 86 to 255 such characters, a long title used as a file name, is 258 to 765 bytes, and every Mac holds it. This follows the question in #26203 ("examples of files in the wild where one component of a path ... is longer than 256"), and is the read side of the silent cut at creation that #12076 describes.

What happens: under NODERAWFS, readdir answers such a name cut to its first 255 bytes. The cut name names nothing: a stat of it finds no file, an unlink of it fails with ENOENT, and so the directory that holds the file cannot be emptied, and its rmdir fails with ENOTEMPTY. Node's own fs.readdirSync answers the same names whole.

Why: __syscall_getdents64 (src/lib/libsyscall.js) writes each entry as one fixed-size struct dirent, and its name with stringToUTF8(name, dirp + pos + d_name, 256), which stops at the last whole character within 255 bytes.

The call is the same on main at e8b0dd9 (2026-11-13), its bound spelled NAME_MAX + 1.

To reproduce, on a Mac, in an empty directory on APFS:

emcc repro.c -sNODERAWFS=1 -o repro.js
node repro.js

Under Emscripten:

a name of 300 bytes made; readdir: 255 bytes, the name made: no; stat of it: not found
  unlink of the name readdir gave: No such file or directory
  rmdir of d300: Directory not empty
a name of 765 bytes made; readdir: 255 bytes, the name made: no; stat of it: not found
  unlink of the name readdir gave: No such file or directory
  rmdir of d765: Directory not empty

The same program built natively (cc repro.c) on the same Mac:

a name of 300 bytes made; readdir: 300 bytes, the name made: yes; stat of it: found
  unlink of the name readdir gave: removed
  rmdir of d300: removed
a name of 765 bytes made; readdir: 765 bytes, the name made: yes; stat of it: found
  unlink of the name readdir gave: removed
  rmdir of d765: removed

Node's own fs.readdirSync over the same two names (readdir.mjs below), under the same node:

a name of 300 bytes made; fs.readdirSync: 300 bytes, the name made: yes
a name of 765 bytes made; fs.readdirSync: 765 bytes, the name made: yes

repro.c:

#include <dirent.h>
#include <errno.h>
#include <fcntl.h>
#include <stdio.h>
#include <string.h>
#include <sys/stat.h>
#include <unistd.h>

/* Makes a file named by `characters` copies of U+65E5, three bytes of UTF-8 each, in a new directory `directory`, then
 * reads the directory back with readdir, stats and unlinks the name it answers, and removes the directory. APFS holds such a name up to 255 characters, 765
 * bytes: it bounds a name at 255 UTF-16 code units, not at 255 bytes. */
static int make_and_read(const char *directory, int characters) {
    char name[3 * 255 + 1] = {0};
    for (int index = 0; index < characters; ++index) {
        memcpy(name + (3 * index), "\xE6\x97\xA5", 3);
    }
    if (mkdir(directory, 0755) != 0) {
        perror("mkdir");
        return 1;
    }
    char path[1024];
    snprintf(path, sizeof path, "%s/%s", directory, name);
    const int descriptor = open(path, O_CREAT | O_WRONLY | O_EXCL, 0644);
    if (descriptor < 0) {
        perror("open");
        return 1;
    }
    close(descriptor);
    DIR *stream = opendir(directory);
    if (stream == NULL) {
        perror("opendir");
        return 1;
    }
    const struct dirent *entry;
    while ((entry = readdir(stream)) != NULL) {
        if (strcmp(entry->d_name, ".") == 0 || strcmp(entry->d_name, "..") == 0) {
            continue;
        }
        char read_path[1024];
        snprintf(read_path, sizeof read_path, "%s/%s", directory, entry->d_name);
        struct stat info;
        printf("a name of %zu bytes made; readdir: %zu bytes, the name made: %s; stat of it: %s\n", strlen(name),
               strlen(entry->d_name), strcmp(entry->d_name, name) == 0 ? "yes" : "no",
               stat(read_path, &info) == 0 ? "found" : "not found");
        printf("  unlink of the name readdir gave: %s\n", unlink(read_path) == 0 ? "removed" : strerror(errno));
    }
    closedir(stream);
    printf("  rmdir of %s: %s\n", directory, rmdir(directory) == 0 ? "removed" : strerror(errno));
    return 0;
}

int main(void) {
    return make_and_read("d300", 100) | make_and_read("d765", 255);
}

readdir.mjs:

// Makes a file named by 100 and by 255 copies of U+65E5 (300 and 765 bytes of UTF-8) and lists each directory with
// node's own fs.readdirSync, printing how many bytes each name it answers holds and whether it is the name made.
import fs from 'node:fs';
for (const characters of [100, 255]) {
    const directory = `n${characters}`;
    fs.mkdirSync(directory);
    const name = '日'.repeat(characters);
    fs.writeFileSync(`${directory}/${name}`, '');
    for (const listed of fs.readdirSync(directory)) {
        console.log(`a name of ${Buffer.byteLength(name)} bytes made; fs.readdirSync: ${Buffer.byteLength(listed)} bytes, the name made: ${listed === name ? 'yes' : 'no'}`);
    }
    fs.rmSync(directory, { recursive: true });
}

What would be expected:

  1. No silent cut, now. A name past 255 bytes is answered as an error (ENAMETOOLONG) in place of a cut name, or the entry is passed over with that error reported. The cut name is the worst answer: it looks valid and names nothing, so a caller's stat, unlink and rmdir each fail on it, where an explicit refusal fails visibly and keeps the platform's contract.
  2. Whole names, as the ABI question Allowing file path taking up more than 255 bytes #26203 left open, with the case above as its answer. It is an ABI change, not a record-size tweak: musl's readdir_r copies d_reclen bytes into the caller's struct dirent, whose d_name is 256 bytes (system/lib/libc/musl/src/dirent/readdir_r.c, arch/generic/bits/dirent.h), and NAME_MAX is 255 (include/limits.h), so programs size name buffers by it. A record sized to a 765-byte name would overrun them. Whole names safely mean a wider d_name, as macOS's struct dirent has (d_name[1024]).

Why a Linux host does not show it: its common file systems (ext4, XFS, ZFS, tmpfs) refuse a name past 255 bytes when it is made, so no such entry exists to be read.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions