about summary refs log tree commit diff
path: root/library/std/src/io/tests.rs
diff options
context:
space:
mode:
authorFrank King <frankking1729@gmail.com>2022-03-17 23:11:49 +0800
committerFrank King <frankking1729@gmail.com>2022-05-29 11:44:30 +0800
commit64ac04567ba397f8600952e3875b9a197f7b2910 (patch)
tree3ac114b1fe8f881953e0149f9fac19ce6e45cd3d /library/std/src/io/tests.rs
parentbe52b4af5ec7e49572cb16519b7e144d6bcb051d (diff)
downloadrust-64ac04567ba397f8600952e3875b9a197f7b2910.tar.gz
rust-64ac04567ba397f8600952e3875b9a197f7b2910.zip
protect `std::io::Take::limit` from overflow in `read`
fixs #94981
Diffstat (limited to 'library/std/src/io/tests.rs')
-rw-r--r--library/std/src/io/tests.rs19
1 files changed, 19 insertions, 0 deletions
diff --git a/library/std/src/io/tests.rs b/library/std/src/io/tests.rs
index eb626348564..b11292ed82d 100644
--- a/library/std/src/io/tests.rs
+++ b/library/std/src/io/tests.rs
@@ -583,6 +583,25 @@ fn test_write_all_vectored() {
     }
 }
 
+// Issue 94981
+#[test]
+#[should_panic = "number of read bytes exceeds limit"]
+fn test_take_wrong_length() {
+    struct LieAboutSize(bool);
+
+    impl Read for LieAboutSize {
+        fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
+            // Lie about the read size at first time of read.
+            if core::mem::take(&mut self.0) { Ok(buf.len() + 1) } else { Ok(buf.len()) }
+        }
+    }
+
+    let mut buffer = vec![0; 4];
+    let mut reader = LieAboutSize(true).take(4);
+    // Primed the `Limit` by lying about the read size.
+    let _ = reader.read(&mut buffer[..]);
+}
+
 #[bench]
 fn bench_take_read(b: &mut test::Bencher) {
     b.iter(|| {