Skip to content

Commit

Permalink
Merge pull request #87 from emielbeinema/85-read-only-header-for-check
Browse files Browse the repository at this point in the history
Check header of file before loading it into memory
  • Loading branch information
shssoichiro authored Sep 27, 2017
2 parents 37deccd + 4711cd6 commit 2b60d3a
Showing 1 changed file with 13 additions and 2 deletions.
15 changes: 13 additions & 2 deletions src/png.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use itertools::Itertools;
use reduction::*;
use std::collections::{HashMap, HashSet};
use std::fs::File;
use std::io::Read;
use std::io::{Read, Seek, SeekFrom};
use std::iter::Iterator;
use std::path::Path;

Expand Down Expand Up @@ -195,8 +195,19 @@ impl PngData {
Ok(f) => f,
Err(_) => return Err(PngError::new("Failed to open file for reading")),
};
let mut byte_data: Vec<u8> = Vec::new();
// Check file for PNG header
let mut header = [0; 8];
if file.read_exact(&mut header).is_err() {
return Err(PngError::new("Not a PNG file: too small"));
}
if !file_header_is_valid(&header) {
return Err(PngError::new("Invalid PNG header detected"));
}
if file.seek(SeekFrom::Start(0)).is_err() {
return Err(PngError::new("Failed to read from file"));
}
// Read raw png data into memory
let mut byte_data: Vec<u8> = Vec::new();
match file.read_to_end(&mut byte_data) {
Ok(_) => (),
Err(_) => return Err(PngError::new("Failed to read from file")),
Expand Down

0 comments on commit 2b60d3a

Please sign in to comment.