Skip to content

Commit

Permalink
Fix fd_seek for boundary parameters
Browse files Browse the repository at this point in the history
  • Loading branch information
kateinoigakukun committed Mar 27, 2022
1 parent cb5c6bc commit 5c524ff
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 2 deletions.
4 changes: 2 additions & 2 deletions src/wasi_snapshot_preview1.rs
Original file line number Diff line number Diff line change
Expand Up @@ -488,11 +488,11 @@ pub(crate) unsafe fn fd_seek<S: Storage>(
) -> Result<Filesize, Error> {
let fd = fs.get_backing_fd(fd)?;
fn compute_new_offset(base: usize, offset: Filedelta) -> Result<usize, Error> {
let new_offset = if offset > 0 {
let new_offset = if offset >= 0 {
base + (offset as usize)
} else {
let neg_offset = (-offset) as usize;
if neg_offset < base {
if neg_offset <= base {
base - neg_offset
} else {
return Err(wasi::ERRNO_INVAL.into());
Expand Down
12 changes: 12 additions & 0 deletions tests/run-make/seek/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
-include ../tools.mk

objs = $(TMPDIR)/main.c.o

check: $(objs)
$(CC) $(LDFLAGS) $(objs) $(LIB_WASI_VFS) -o $(TMPDIR)/main.wasm
wasmtime --mapdir /fixtures::./fixtures $(TMPDIR)/main.wasm
$(WASI_VFS_CLI) pack $(TMPDIR)/main.wasm --mapdir /fixtures::./fixtures -o $(TMPDIR)/main.packed.wasm
$(WASI_RUN) $(TMPDIR)/main.packed.wasm

clean:
rm -rf $(PROG) $(objs)
2 changes: 2 additions & 0 deletions tests/run-make/seek/fixtures/file1.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
file1.1
file1.2
59 changes: 59 additions & 0 deletions tests/run-make/seek/main.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
#include "../check.h"
#include <sys/stat.h>
#include <fcntl.h>
#include <wasi/api.h>
#include <errno.h>
#include <assert.h>

int main(int argc, char *argv[]) {
int err;
int file1 = open("/fixtures/file1.txt", O_RDONLY);
assert(file1 != -1);

struct stat st;
err = fstat(file1, &st);
assert(err == 0);

char buffer[st.st_size];

__wasi_filesize_t off;

// reset the cursor
assert(__wasi_fd_seek(file1, 0, SEEK_SET, &off) == 0);
assert(off == 0);

// keep the initial cursor position
assert(__wasi_fd_seek(file1, 0, SEEK_CUR, &off) == 0);
assert(off == 0);
assert(read(file1, buffer, st.st_size) == st.st_size);

assert(__wasi_fd_seek(file1, 2, SEEK_SET, &off) == 0);
assert(off == 2);
assert(read(file1, buffer, st.st_size) == st.st_size - 2);

assert(__wasi_fd_seek(file1, st.st_size, SEEK_SET, &off) == 0);
assert(off == st.st_size);
assert(read(file1, buffer, 1) == 0);

assert(__wasi_fd_seek(file1, st.st_size + 1, SEEK_SET, &off) == 0);
assert(err == 0);
assert(read(file1, buffer, 1) == 0);

// reset the cursor
assert(__wasi_fd_seek(file1, 0, SEEK_SET, &off) == 0);

assert(__wasi_fd_seek(file1, 0, SEEK_END, &off) == 0);
assert(err == 0);
assert(read(file1, buffer, 1) == 0);

assert(__wasi_fd_seek(file1, -1, SEEK_END, &off) == 0);
assert(err == 0);
assert(read(file1, buffer, 1) == 1);

assert(__wasi_fd_seek(file1, -st.st_size, SEEK_END, &off) == 0);
assert(err == 0);
assert(off == 0);
assert(read(file1, buffer, st.st_size) == st.st_size);

return 0;
}

0 comments on commit 5c524ff

Please sign in to comment.