forge
Explore
Log in

Don't you dare go Hollow

edith_codecs

Clone
★ 0

0 watching0 forksRust370.3 MiB9b67700Tahinli3 days ago4021 commits

fuzz_regressions.rs70 lines, 2.6 KiBHistoryRawDownload
1//! Inputs the fuzz review and the read-through found to allocate without bound, before the fixes
2//! in `lib.rs`: an object or a canvas sized by a 16-bit field.
3
4use ec_pgs::{PgsDecoder, SEG_END, SEG_ODS, SEG_PCS, SEG_PDS};
5
6fn segment(kind: u8, body: &[u8]) -> Vec<u8> {
7 let mut out = b"PG".to_vec();
8 out.extend_from_slice(&[0; 8]);
9 out.push(kind);
10 out.extend_from_slice(&(body.len() as u16).to_be_bytes());
11 out.extend_from_slice(body);
12 out
13}
14
15/// A 1920x1080 canvas with one object placed at (0,0), epoch start.
16fn pcs() -> Vec<u8> {
17 segment(
18 SEG_PCS,
19 &[0x07, 0x80, 0x04, 0x38, 0x10, 0, 0, 0x80, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0],
20 )
21}
22
23/// Palette entry 1 opaque white.
24fn pds() -> Vec<u8> {
25 segment(SEG_PDS, &[0, 0, 1, 235, 128, 128, 255])
26}
27
28/// One ODS whose declared size is `width` x `height`, with `rle` as its data.
29fn ods(width: u16, height: u16, rle: &[u8]) -> Vec<u8> {
30 let mut body = vec![0, 0, 0, 0xC0];
31 body.extend_from_slice(&((rle.len() + 4) as u32).to_be_bytes()[1..]);
32 body.extend_from_slice(&width.to_be_bytes());
33 body.extend_from_slice(&height.to_be_bytes());
34 body.extend_from_slice(rle);
35 segment(SEG_ODS, &body)
36}
37
38/// A 65535 x 65535 object used to be allocated as a RLE bitmap (4 GB) and decoded at render time.
39/// It is now refused with `Error::corrupt`, the segments after it still decode, and nothing is
40/// allocated for it.
41#[test]
42fn an_object_past_the_dimension_limit_is_refused_and_the_stream_goes_on() {
43 let mut stream = pcs();
44 stream.extend(pds());
45 stream.extend(ods(65535, 65535, &[0x00, 0x88, 0x01]));
46 stream.extend(segment(SEG_END, &[]));
47 // A small, legal object after it, in its own display set.
48 stream.extend(pcs());
49 stream.extend(pds());
50 stream.extend(ods(8, 1, &[0x00, 0x88, 0x01]));
51 stream.extend(segment(SEG_END, &[]));
52
53 let mut decoder = PgsDecoder::new();
54 let error = decoder
55 .push(&stream)
56 .expect_err("oversized object is refused");
57 assert!(error.to_string().contains("65535"), "{error}");
58 let frame = decoder.take_frame().expect("the legal set still renders");
59 assert_eq!((frame.width, frame.height), (1920, 1080));
60}
61
62/// A canvas stated at 65535 x 65535 used to be allocated as RGBA (17 GB). It is refused.
63#[test]
64fn a_canvas_past_the_dimension_limit_is_refused() {
65 let mut stream = segment(SEG_PCS, &[0xFF, 0xFF, 0xFF, 0xFF, 0x10, 0, 0, 0x80, 0, 0, 0]);
66 stream.extend(segment(SEG_END, &[]));
67 let mut decoder = PgsDecoder::new();
68 assert!(decoder.push(&stream).is_err());
69 assert!(decoder.take_frame().is_none());
70}