-
Notifications
You must be signed in to change notification settings - Fork 926
Closed
Description
fd_allocate should be grow only, never shrink. On WASIX, calling fd_allocate(fd, 0, smaller_len) after a larger allocation can truncate the file, risking data loss. POSIX/Linux fallocate(fd, 0, offset, len) never shrinks a file; shrinking requires ftruncate or hole‑punching with explicit flags.
Found via ongoing progress of porting POSIX compatibility tests in #6070, but I've decided to file the issue right away to fix it ASAP.
#include <assert.h>
#include <fcntl.h>
#include <stdio.h>
#include <sys/stat.h>
#include <unistd.h>
#include <wasi/api_wasi.h>
static off_t file_size(int fd) {
struct stat st;
assert(fstat(fd, &st) == 0);
return st.st_size;
}
int main(void) {
const char *path = "fd_allocate_basic";
unlink(path);
int fd = open(path, O_CREAT | O_TRUNC | O_RDWR, 0644);
assert(fd >= 0);
__wasi_errno_t err = __wasi_fd_allocate((__wasi_fd_t)fd, 0, 10);
assert(err == __WASI_ERRNO_SUCCESS);
assert(file_size(fd) == 10);
// BUG: should not shrink, but currently can
err = __wasi_fd_allocate((__wasi_fd_t)fd, 0, 5);
assert(err == __WASI_ERRNO_SUCCESS);
assert(file_size(fd) == 10); // fails on current WASIX
close(fd);
unlink(path);
return 0;
}Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
No labels