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

Xuanwo pushed a commit to branch xuanwo/fix-stream-reader-suffix-range
in repository https://gitbox.apache.org/repos/asf/opendal.git

commit 4121cb35711bc820ea15947490bf76a13b2fffea
Author: Xuanwo <[email protected]>
AuthorDate: Mon Nov 23 04:27:15 2026 +0100

    fix(core): allow short suffix reads in StreamReader
    
    StreamReader::read required the returned buffer to match BytesRange::size()
    exactly. For a suffix range this rejects valid short reads when the object
    is smaller than the requested suffix, for example suffix(100) on a 5-byte
    object.
    
    Use RpRead metadata to resolve the exact suffix length when the full object
    size is known. Without metadata, only reject data beyond the requested
    suffix. Bounded forward ranges keep their exact-length validation.
---
 core/core/src/raw/oio/read/stream_read.rs | 129 +++++++++++++++++++++++++++++-
 1 file changed, 126 insertions(+), 3 deletions(-)

diff --git a/core/core/src/raw/oio/read/stream_read.rs 
b/core/core/src/raw/oio/read/stream_read.rs
index 3ab2f99481..c9a7433bd0 100644
--- a/core/core/src/raw/oio/read/stream_read.rs
+++ b/core/core/src/raw/oio/read/stream_read.rs
@@ -63,14 +63,137 @@ impl<R: StreamRead> oio::Read for StreamReader<R> {
 
         let (rp, mut stream) = self.inner.open(range).await?;
         let buffer = stream.read_all().await?;
-        if buffer.len() as u64 != expected {
+        let actual = buffer.len() as u64;
+
+        let (bound, exact) = if range.is_suffix() {
+            match rp.metadata().filter(|metadata| metadata.is_file()) {
+                Some(metadata) => (expected.min(metadata.content_length()), 
true),
+                // Without the object size, a suffix range can validly return
+                // fewer bytes when the object is shorter than the suffix.
+                None => (expected, false),
+            }
+        } else {
+            (expected, true)
+        };
+
+        let mismatched = if exact {
+            actual != bound
+        } else {
+            actual > bound
+        };
+        if mismatched {
             return Err(
                 Error::new(ErrorKind::Unexpected, "reader got unexpected data 
size")
-                    .with_context("expect", expected)
-                    .with_context("actual", buffer.len() as u64),
+                    .with_context("expect", bound)
+                    .with_context("actual", actual),
             );
         }
 
         Ok((rp, buffer))
     }
 }
+
+#[cfg(test)]
+mod tests {
+    use super::*;
+
+    /// Serves fixed content and honors the requested range.
+    struct RangeStreamRead {
+        content: Buffer,
+    }
+
+    impl StreamRead for RangeStreamRead {
+        async fn open(&self, range: BytesRange) -> Result<(RpRead, Box<dyn 
oio::ReadStreamDyn>)> {
+            let content = self
+                .content
+                .slice(range.to_content_range(self.content.len())?);
+            Ok((RpRead::default(), Box::new(content)))
+        }
+    }
+
+    /// Serves fixed content regardless of the requested range.
+    struct FixedStreamRead {
+        content: Buffer,
+        content_length: Option<u64>,
+    }
+
+    impl StreamRead for FixedStreamRead {
+        async fn open(&self, _: BytesRange) -> Result<(RpRead, Box<dyn 
oio::ReadStreamDyn>)> {
+            let rp = self.content_length.map_or_else(RpRead::default, |length| 
{
+                RpRead::new(MetadataBuilder::file(length).build())
+            });
+            Ok((rp, Box::new(self.content.clone())))
+        }
+    }
+
+    #[tokio::test]
+    async fn test_read_suffix_larger_than_content() -> Result<()> {
+        let reader = StreamReader::new(RangeStreamRead {
+            content: Buffer::from("hello"),
+        });
+
+        let (_, buffer) = oio::Read::read(&reader, 
BytesRange::suffix(100)).await?;
+        assert_eq!(buffer.to_bytes(), "hello");
+        Ok(())
+    }
+
+    #[tokio::test]
+    async fn test_read_suffix_within_content() -> Result<()> {
+        let reader = StreamReader::new(RangeStreamRead {
+            content: Buffer::from("hello"),
+        });
+
+        let (_, buffer) = oio::Read::read(&reader, 
BytesRange::suffix(3)).await?;
+        assert_eq!(buffer.to_bytes(), "llo");
+        Ok(())
+    }
+
+    #[tokio::test]
+    async fn test_read_suffix_zero() -> Result<()> {
+        let reader = StreamReader::new(RangeStreamRead {
+            content: Buffer::from("hello"),
+        });
+
+        let (_, buffer) = oio::Read::read(&reader, 
BytesRange::suffix(0)).await?;
+        assert!(buffer.is_empty());
+        Ok(())
+    }
+
+    #[tokio::test]
+    async fn test_read_suffix_rejects_extra_data() {
+        let reader = StreamReader::new(FixedStreamRead {
+            content: Buffer::from("hello!"),
+            content_length: None,
+        });
+
+        let err = oio::Read::read(&reader, BytesRange::suffix(3))
+            .await
+            .expect_err("suffix read should reject data beyond the requested 
suffix");
+        assert_eq!(err.kind(), ErrorKind::Unexpected);
+    }
+
+    #[tokio::test]
+    async fn test_read_suffix_rejects_short_data_with_known_content_length() {
+        let reader = StreamReader::new(FixedStreamRead {
+            content: Buffer::from("hell"),
+            content_length: Some(5),
+        });
+
+        let err = oio::Read::read(&reader, BytesRange::suffix(100))
+            .await
+            .expect_err("suffix read should reject data shorter than the 
object");
+        assert_eq!(err.kind(), ErrorKind::Unexpected);
+    }
+
+    #[tokio::test]
+    async fn test_read_bounded_rejects_short_data() {
+        let reader = StreamReader::new(RangeStreamRead {
+            content: Buffer::from("hello"),
+        });
+
+        let err = oio::Read::read(&reader, BytesRange::from(0..10))
+            .await
+            .expect_err("bounded read should reject a short buffer");
+        assert_eq!(err.kind(), ErrorKind::Unexpected);
+    }
+}

Reply via email to