This is an automated email from the ASF dual-hosted git repository.

Xuanwo pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/opendal.git


The following commit(s) were added to refs/heads/main by this push:
     new 3228dd35ab fix(bindings/python): reject negative SEEK_SET offsets 
(#8762)
3228dd35ab is described below

commit 3228dd35ab75c7a3b360cd330857d2969808b8b8
Author: Xuanwo <[email protected]>
AuthorDate: Mon Nov 23 08:10:07 2026 +0800

    fix(bindings/python): reject negative SEEK_SET offsets (#8762)
    
    Python File::seek and AsyncFile::seek cast a negative SEEK_SET offset 
directly to u64, wrapping -1 to u64::MAX and silently moving the cursor to an 
unusable position. Reject negative offsets with ValueError before calling the 
reader, matching io.BytesIO, and add sync and async regression tests that 
verify the cursor does not move.
---
 bindings/python/src/file.rs        | 10 ++++++++--
 bindings/python/tests/test_read.py | 41 ++++++++++++++++++++++++++++++++++++++
 2 files changed, 49 insertions(+), 2 deletions(-)

diff --git a/bindings/python/src/file.rs b/bindings/python/src/file.rs
index 0527da310d..38e013befb 100644
--- a/bindings/python/src/file.rs
+++ b/bindings/python/src/file.rs
@@ -337,7 +337,10 @@ impl File {
         };
 
         let whence = match whence {
-            0 => SeekFrom::Start(pos as u64),
+            0 => SeekFrom::Start(
+                u64::try_from(pos)
+                    .map_err(|_| PyValueError::new_err(format!("negative seek 
value {pos}")))?,
+            ),
             1 => SeekFrom::Current(pos),
             2 => SeekFrom::End(pos),
             _ => return Err(PyValueError::new_err("invalid whence")),
@@ -833,7 +836,10 @@ impl AsyncFile {
         let state = self.0.clone();
 
         let whence = match whence {
-            0 => SeekFrom::Start(pos as u64),
+            0 => SeekFrom::Start(
+                u64::try_from(pos)
+                    .map_err(|_| PyValueError::new_err(format!("negative seek 
value {pos}")))?,
+            ),
             1 => SeekFrom::Current(pos),
             2 => SeekFrom::End(pos),
             _ => return Err(PyValueError::new_err("invalid whence")),
diff --git a/bindings/python/tests/test_read.py 
b/bindings/python/tests/test_read.py
index 133207835e..8bd8d59289 100644
--- a/bindings/python/tests/test_read.py
+++ b/bindings/python/tests/test_read.py
@@ -113,6 +113,26 @@ def test_sync_reader_read_sizes(operator):
     operator.delete(filename)
 
 
[email protected]_capability("read", "write", "delete")
+def test_sync_reader_rejects_negative_seek_set(operator):
+    filename = f"random_file_{str(uuid4())}"
+    operator.write(filename, b"0123456789")
+
+    try:
+        with operator.open(filename, "rb") as reader:
+            assert reader.seek(3) == 3
+            with pytest.raises(ValueError, match="negative seek value -1"):
+                reader.seek(-1, 0)
+            assert reader.tell() == 3
+            with pytest.raises(ValueError, match="negative seek value -1"):
+                reader.seek(-1)
+            assert reader.tell() == 3
+
+            assert reader.read() == b"3456789"
+    finally:
+        operator.delete(filename)
+
+
 @pytest.mark.need_capability("read", "write", "delete")
 def test_sync_reader_readline(service_name, operator, async_operator):
     size = randint(1, 1024)
@@ -252,6 +272,27 @@ async def test_async_reader_read_sizes(async_operator):
     await async_operator.delete(filename)
 
 
[email protected]
[email protected]_capability("read", "write", "delete")
+async def test_async_reader_rejects_negative_seek_set(async_operator):
+    filename = f"random_file_{str(uuid4())}"
+    await async_operator.write(filename, b"0123456789")
+
+    try:
+        async with await async_operator.open(filename, "rb") as reader:
+            assert await reader.seek(3) == 3
+            with pytest.raises(ValueError, match="negative seek value -1"):
+                await reader.seek(-1, 0)
+            assert await reader.tell() == 3
+            with pytest.raises(ValueError, match="negative seek value -1"):
+                await reader.seek(-1)
+            assert await reader.tell() == 3
+
+            assert await reader.read() == b"3456789"
+    finally:
+        await async_operator.delete(filename)
+
+
 @pytest.mark.asyncio
 async def test_async_reader_read_buffered_coalesces_chunks():
     operator = opendal.AsyncOperator("memory")

Reply via email to