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 298492e350 fix(bindings/c): handle empty bytes and negative seek 
offsets (#8747)
298492e350 is described below

commit 298492e350b6b3d57e3d54eabeed2fe5bffe0b43
Author: Xuanwo <[email protected]>
AuthorDate: Mon Nov 23 06:57:59 2026 +0800

    fix(bindings/c): handle empty bytes and negative seek offsets (#8747)
    
    Return a zeroed opendal_bytes for empty buffers so data is NULL and the 
result can be written back. Reject negative OPENDAL_SEEK_SET offsets instead of 
wrapping them to u64::MAX.
---
 bindings/c/src/reader.rs    | 45 +++++++++++++++++++++++++++++++++++++++-
 bindings/c/src/types.rs     | 50 ++++++++++++++++++++++++++++++++++++++++++---
 bindings/c/tests/bdd.cpp    |  7 +++++++
 bindings/c/tests/reader.cpp |  8 ++++++++
 4 files changed, 106 insertions(+), 4 deletions(-)

diff --git a/bindings/c/src/reader.rs b/bindings/c/src/reader.rs
index ab8d3738de..d66c827ca8 100644
--- a/bindings/c/src/reader.rs
+++ b/bindings/c/src/reader.rs
@@ -93,7 +93,16 @@ impl opendal_reader {
     ) -> *mut opendal_error {
         assert!(!pos.is_null());
         let seek_from = match whence {
-            OPENDAL_SEEK_SET => SeekFrom::Start(offset as u64),
+            OPENDAL_SEEK_SET => match u64::try_from(offset) {
+                Ok(offset) => SeekFrom::Start(offset),
+                Err(_) => {
+                    std::ptr::write(pos, 0);
+                    return opendal_error::new(core::Error::new(
+                        core::ErrorKind::Unexpected,
+                        "negative seek offset is invalid with 
OPENDAL_SEEK_SET",
+                    ));
+                }
+            },
             OPENDAL_SEEK_CUR => SeekFrom::Current(offset),
             OPENDAL_SEEK_END => SeekFrom::End(offset),
             _ => {
@@ -133,3 +142,37 @@ impl opendal_reader {
         }
     }
 }
+
+#[cfg(test)]
+mod tests {
+    use super::*;
+
+    #[test]
+    fn test_seek_set_rejects_negative_offset() {
+        let runtime = tokio::runtime::Builder::new_multi_thread()
+            .enable_all()
+            .build()
+            .unwrap();
+        let op = 
core::Operator::new(core::services::Memory::default()).unwrap();
+        let blocking_op = {
+            let _guard = runtime.enter();
+            core::blocking::Operator::new(op).unwrap()
+        };
+        blocking_op.write("test", "hello").unwrap();
+
+        let reader = blocking_op
+            .reader("test")
+            .unwrap()
+            .into_std_read(core::BytesRange::default())
+            .unwrap();
+        let mut reader = opendal_reader::new(reader);
+
+        let mut pos = 42;
+        let err = unsafe {
+            opendal_reader::opendal_reader_seek(&mut reader, -1, 
OPENDAL_SEEK_SET, &mut pos)
+        };
+        assert!(!err.is_null());
+        assert_eq!(pos, 0);
+        unsafe { opendal_error::opendal_error_free(err) };
+    }
+}
diff --git a/bindings/c/src/types.rs b/bindings/c/src/types.rs
index a823b0f3b8..f3ce2bd92a 100644
--- a/bindings/c/src/types.rs
+++ b/bindings/c/src/types.rs
@@ -66,6 +66,10 @@ impl opendal_bytes {
 
     /// Construct a [`opendal_bytes`] from the Rust [`Vec`] of bytes
     pub(crate) fn new(b: Buffer) -> Self {
+        if b.is_empty() {
+            return Self::empty();
+        }
+
         let mut b = std::mem::ManuallyDrop::new(b.to_vec());
         Self {
             data: b.as_mut_ptr(),
@@ -82,10 +86,10 @@ impl opendal_bytes {
                 let bs = &mut *ptr;
                 if !bs.data.is_null() {
                     drop(Vec::from_raw_parts(bs.data, bs.len, bs.capacity));
-                    bs.data = std::ptr::null_mut();
-                    bs.len = 0;
-                    bs.capacity = 0;
                 }
+                bs.data = std::ptr::null_mut();
+                bs.len = 0;
+                bs.capacity = 0;
             }
         }
     }
@@ -1557,3 +1561,43 @@ impl opendal_operator_options {
         }
     }
 }
+
+#[cfg(test)]
+mod bytes_tests {
+    use super::*;
+
+    #[test]
+    fn test_empty_buffer_has_null_data() {
+        let mut bytes = opendal_bytes::new(Buffer::new());
+
+        assert!(bytes.data.is_null());
+        assert_eq!(bytes.len, 0);
+        assert_eq!(bytes.capacity, 0);
+
+        let buffer = bytes.to_buffer().expect("empty bytes should convert");
+        assert!(buffer.is_empty());
+
+        unsafe { opendal_bytes::opendal_bytes_free(&mut bytes) };
+        assert!(bytes.data.is_null());
+        assert_eq!(bytes.len, 0);
+        assert_eq!(bytes.capacity, 0);
+    }
+
+    #[test]
+    fn test_non_empty_buffer_round_trips() {
+        let mut bytes = opendal_bytes::new(Buffer::from("hello"));
+
+        assert!(!bytes.data.is_null());
+        assert_eq!(bytes.len, 5);
+        assert_eq!(
+            unsafe { std::slice::from_raw_parts(bytes.data, bytes.len) },
+            b"hello"
+        );
+        assert_eq!(bytes.to_buffer().unwrap().to_vec(), b"hello");
+
+        unsafe { opendal_bytes::opendal_bytes_free(&mut bytes) };
+        assert!(bytes.data.is_null());
+        assert_eq!(bytes.len, 0);
+        assert_eq!(bytes.capacity, 0);
+    }
+}
diff --git a/bindings/c/tests/bdd.cpp b/bindings/c/tests/bdd.cpp
index 9be19c1e9b..73630ef2c6 100644
--- a/bindings/c/tests/bdd.cpp
+++ b/bindings/c/tests/bdd.cpp
@@ -167,6 +167,12 @@ TEST_F(OpendalBddTest, WriteEmptyNullBytes)
     error = opendal_operator_read(this->p, "empty", &read_data);
     EXPECT_EQ(error, nullptr);
     EXPECT_EQ(read_data.len, 0);
+    EXPECT_EQ(read_data.data, nullptr);
+    EXPECT_EQ(read_data.capacity, 0);
+
+    // An empty read result must satisfy the write input contract.
+    error = opendal_operator_write(this->p, "empty-round-trip", &read_data);
+    EXPECT_EQ(error, nullptr);
     opendal_bytes_free(&read_data);
 
     // Cover the separate write_with entry point that shares this validation.
@@ -198,6 +204,7 @@ TEST_F(OpendalBddTest, WriteEmptyNullBytes)
     opendal_bytes_free(&read_data);
 
     EXPECT_EQ(opendal_operator_delete(this->p, "empty"), nullptr);
+    EXPECT_EQ(opendal_operator_delete(this->p, "empty-round-trip"), nullptr);
     EXPECT_EQ(opendal_operator_delete(this->p, "empty-with-options"), nullptr);
     EXPECT_EQ(opendal_operator_delete(this->p, "empty-writer"), nullptr);
 }
diff --git a/bindings/c/tests/reader.cpp b/bindings/c/tests/reader.cpp
index acc45d0a99..2e7d1e7699 100644
--- a/bindings/c/tests/reader.cpp
+++ b/bindings/c/tests/reader.cpp
@@ -105,5 +105,13 @@ TEST_F(OpendalReaderTest, SeekTest)
     EXPECT_EQ(pos, 0);
     opendal_error_free(err);
 
+    // A negative offset is invalid with OPENDAL_SEEK_SET and must not wrap to 
u64::MAX.
+    pos = 999;
+    err = opendal_reader_seek(reader, -1, OPENDAL_SEEK_SET, &pos);
+    ASSERT_NE(err, nullptr);
+    EXPECT_EQ(err->code, OPENDAL_UNEXPECTED);
+    EXPECT_EQ(pos, 0);
+    opendal_error_free(err);
+
     opendal_reader_free(reader);
 }

Reply via email to