Skip to content

Commit

Permalink
Merge pull request #473 from inrustwetrust/update_yuv_segfault_fix
Browse files Browse the repository at this point in the history
Fix segfault if destination rectangle in update_yuv is outside texture bounds
  • Loading branch information
AngryLawyer committed Nov 26, 2015
2 parents 3de2b77 + 6652dcf commit 4c4a651
Showing 1 changed file with 18 additions and 1 deletion.
19 changes: 18 additions & 1 deletion src/sdl2/render.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1070,11 +1070,28 @@ impl Texture {
return Err(ErrorMessage("The rectangle dimensions must be multiples-of-two for planar YUV 4:2:0 pixel formats".into()));
}

// If the destination rectangle lies outside the texture boundaries,
// SDL_UpdateYUVTexture will write outside allocated texture memory.
let tex_info = self.query();
let rect_inside_texture = match rect {
Some(r) => {
let tex_rect = Rect::new_unwrap(0, 0, tex_info.width, tex_info.height);
match r.intersect(&tex_rect) {
Some(intersection) => intersection == r,
None => false,
}
},
None => true,
};
if !rect_inside_texture {
return Err(ErrorMessage("The destination rectangle cannot lie outside the texture boundaries".into()));
}

// We need the height in order to check the array slice lengths.
// Checking the lengths can prevent buffer overruns in SDL_UpdateYUVTexture.
let height = match rect {
Some(r) => r.height(),
None => self.query().height
None => tex_info.height,
} as usize;

let wrong_length =
Expand Down

0 comments on commit 4c4a651

Please sign in to comment.