Skip to content

Commit 4804cee

Browse files
committed
2.0.3: check signature bytes before using them
1 parent 49d9dae commit 4804cee

1 file changed

Lines changed: 14 additions & 12 deletions

File tree

src/lib.rs

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -169,20 +169,22 @@ impl Decoder {
169169
}
170170

171171
fn decode_internal(contents: &[u8]) -> Result<Gif> {
172-
{
173-
let mut signature: String = String::new();
174-
match String::from_utf8(contents[0..3].to_vec()) {
175-
Ok(parsed_signature) => {
176-
signature = parsed_signature;
177-
}
172+
let signature = match contents.get(0..3) {
173+
Some(signature_bytes) => match String::from_utf8(signature_bytes.to_vec()) {
174+
Ok(parsed_signature) => parsed_signature,
178175
Err(err) => return Err(Error::from_reason(err.to_string())),
176+
},
177+
None => {
178+
return Err(Error::from_reason(
179+
"Unable to get file signature, the file is corrupted".to_string(),
180+
))
179181
}
180-
if signature != "GIF" {
181-
return Err(Error::from_reason(format!(
182-
"Invalid file signature, got {}",
183-
signature
184-
)));
185-
}
182+
};
183+
if signature != "GIF" {
184+
return Err(Error::from_reason(format!(
185+
"Invalid file signature, got {}",
186+
signature
187+
)));
186188
}
187189

188190
let mut gif = Gif::default();

0 commit comments

Comments
 (0)