emscripten-core / emscripten-core/emscripten

WASMFS write() on an `O_RDONLY` fd silently succeeds

Open
#26,712 0 comments 0 reactions 0 assignees View on GitHub
Dominant language
C++
Stars
27.6k
Forks
3.6k
Avg merge
1d 1h
Merged PRs (30d)
105

Description

**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:**
```c
#include
#include
#include
#include
#include

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;
}
```

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.