Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions .changes/fix-fs-seek-negative.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
fs: patch
fs-js: patch
---

`FileHandle.seek` now rejects a negative offset with `SeekMode.Start` instead of seeking to a huge position.
13 changes: 13 additions & 0 deletions packages/api-e2e/test/specs/fs.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -269,6 +269,19 @@ describePlugin('fs', () => {
})
})

it('FileHandle.seek rejects a negative offset from the start', async () => {
const message = await tauriError(async (api, path) => {
const baseDir = api.fs.BaseDirectory.AppData
const file = await api.fs.create(path, { baseDir })
try {
await file.seek(-1, api.fs.SeekMode.Start)
} finally {
await file.close()
}
}, `${dir}/seek.txt`)
expect(message).toMatch(/must not be negative/)
})

it('a closed FileHandle cannot be used again', async () => {
const message = await tauriError(async (api, path) => {
const baseDir = api.fs.BaseDirectory.AppData
Expand Down
19 changes: 10 additions & 9 deletions plugins/fs/src/commands.rs
Original file line number Diff line number Diff line change
Expand Up @@ -842,16 +842,17 @@ pub async fn seek<R: Runtime>(
whence: SeekMode,
) -> CommandResult<u64> {
use std::io::{Seek, SeekFrom};
let position = match whence {
SeekMode::Start => SeekFrom::Start(u64::try_from(offset).map_err(|_| {
format!("invalid seek offset {offset}: it must not be negative with SeekMode.Start")
})?),
SeekMode::Current => SeekFrom::Current(offset),
SeekMode::End => SeekFrom::End(offset),
};
let file: std::sync::Arc<StdFileResource<R>> = webview.resources_table().get(rid)?;
StdFileResource::with_lock(&file, |file| {
file.seek(match whence {
SeekMode::Start => SeekFrom::Start(offset as u64),
SeekMode::Current => SeekFrom::Current(offset),
SeekMode::End => SeekFrom::End(offset),
})
})
.map_err(|e| format!("failed to seek file with error: {e}"))
.map_err(Into::into)
StdFileResource::with_lock(&file, |file| file.seek(position))
.map_err(|e| format!("failed to seek file with error: {e}"))
.map_err(Into::into)
}

#[cfg(target_os = "android")]
Expand Down
Loading