Skip to content

WASMFS write() on an O_RDONLY fd silently succeeds #26712

@Ryan2026R

Description

@Ryan2026R

Version of emscripten/emsdk:

5.0.6

POSIX: write() on a file descriptor opened O_RDONLY should return -1. It seems that the default JS-FS does this correctly; but under WASMFS (-sWASMFS=1) the write succeeds.

Failing command line in full:

$ emcc -O0 -Wall -sWASMFS=1 write_rdonly.c -o a.out.js
$ node a.out.js
write(RDONLY fd): ret=1 errno=0

The same program on native Linux prints write(RDONLY fd): ret=-1 errno=9.

Example code snippet:

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

int main(void) {
    mkdir("/tmp/xsv_test", 0755);
    int fd = open("/tmp/xsv_test/f.txt", O_WRONLY | O_CREAT | O_TRUNC, 0644);
    write(fd, "x", 1); close(fd);

    fd = open("/tmp/xsv_test/f.txt", O_RDONLY);
    errno = 0;
    ssize_t r = write(fd, "y", 1);
    printf("write(RDONLY fd): ret=%zd errno=%d\n", r, errno);
    close(fd);
    return (r < 0) ? 0 : 1;
}

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