-
Notifications
You must be signed in to change notification settings - Fork 2.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Auto merge of #11089 - pietroalbini:pa-cves-nightly, r=weihanglo
[master] Fix for CVE-2022-36113 and CVE-2022-36114 This PR includes the fixes for CVE-2022-36113 and CVE-2022-36114 targeting the master branch. See [the advisory](https://blog.rust-lang.org/2022/09/14/cargo-cves.html) for more information about the vulnerabilities.
- Loading branch information
Showing
5 changed files
with
214 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
use std::io::{self, Read, Take}; | ||
|
||
#[derive(Debug)] | ||
pub struct LimitErrorReader<R> { | ||
inner: Take<R>, | ||
} | ||
|
||
impl<R: Read> LimitErrorReader<R> { | ||
pub fn new(r: R, limit: u64) -> LimitErrorReader<R> { | ||
LimitErrorReader { | ||
inner: r.take(limit), | ||
} | ||
} | ||
} | ||
|
||
impl<R: Read> Read for LimitErrorReader<R> { | ||
fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> { | ||
match self.inner.read(buf) { | ||
Ok(0) if self.inner.limit() == 0 => Err(io::Error::new( | ||
io::ErrorKind::Other, | ||
"maximum limit reached when reading", | ||
)), | ||
e => e, | ||
} | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use super::LimitErrorReader; | ||
|
||
use std::io::Read; | ||
|
||
#[test] | ||
fn under_the_limit() { | ||
let buf = &[1; 7][..]; | ||
let mut r = LimitErrorReader::new(buf, 8); | ||
let mut out = Vec::new(); | ||
assert!(matches!(r.read_to_end(&mut out), Ok(7))); | ||
assert_eq!(buf, out.as_slice()); | ||
} | ||
|
||
#[test] | ||
#[should_panic = "maximum limit reached when reading"] | ||
fn over_the_limit() { | ||
let buf = &[1; 8][..]; | ||
let mut r = LimitErrorReader::new(buf, 8); | ||
let mut out = Vec::new(); | ||
r.read_to_end(&mut out).unwrap(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters