emscripten-core / emscripten-core/emscripten

close fd before calling munmap will cause the write failure

Open
#15,140 1 comment 0 reactions 0 assignees View on GitHub
filesystem
Dominant language
C++
Stars
27.6k
Forks
3.6k
Avg merge
1d 6h
Merged PRs (30d)
109

Description

When using emcc to compile wasm, I try to use mmap to write the file, but if you close fd before calling munmap, the written content will be lost.

Here is the example.

```
int len = string.size();
int fd = open("test-shared.txt", O_RDWR | O_CREAT, 00777);
lseek(fd, len - 1, SEEK_END);
write(fd, " ", 1);
char* buffer = (char*) mmap(NULL, len, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
memcpy(buffer, string.data(), len);
// close(fd); // open it will lose all content, but it's ok in the linux environment.
munmap(buffer, len);
close(fd); // it's fine.
```

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.