]> git.lizzy.rs Git - rust.git/blobdiff - library/std/src/io/tests.rs
Rollup merge of #99415 - ferrocene:pa-reuse-initial, r=Mark-Simulacrum
[rust.git] / library / std / src / io / tests.rs
index d5a8c93b0ce9f35cbc48a2b9300813be558e8582..f357f33ec52c54b771d2e89ec45df7633a3c353c 100644 (file)
@@ -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(|| {