Debian Codegraph @

debian/missing-sources/gdk-pixbuf/tests/overflow.rs

raw
1 use gdk_pixbuf::*;
2
3 #[test]
4 #[cfg(target_pointer_width = "64")]
5 fn put_pixel_doesnt_overflow() {
6 // Only test this on 64-bit boxes; otherwise we can't even
7 // allocate a pixbuf this big.
8
9 let pixbuf = Pixbuf::new(Colorspace::Rgb, true, 8, 21000, 29700).unwrap();
10
11 // debug build: thread 'put_pixel_doesnt_overflow' panicked at
12 // 'attempt to multiply with overflow', src/pixbuf.rs:274:24
13 //
14 // release build: thread 'put_pixel_doesnt_overflow' panicked at
15 // 'index out of bounds: the len is 2494800000, but the index is
16 // 18446744071598664320', src/pixbuf.rs:276:13
17
18 pixbuf.put_pixel(20000, 26000, 255, 255, 255, 255);
19 }
20
21 #[test]
22 #[cfg(target_pointer_width = "64")]
23 fn new_from_mut_slice_doesnt_overflow() {
24 // Only test this on 64-bit boxes; otherwise we can't even
25 // allocate a pixbuf this big.
26
27 // Plus 5 to test that new_from_mut_slice() can ignore trailing data past the last row
28 let data = vec![0u8; 21000 * 4 * 29700 + 5];
29
30 // debug build: thread 'new_from_mut_slice_doesnt_overflow'
31 // panicked at 'attempt to multiply with overflow',
32 // /home/federico/src/gtk-rs/gdk-pixbuf/src/pixbuf.rs:50:36
33 //
34 // release build: thread 'new_from_mut_slice_doesnt_overflow'
35 // panicked at 'assertion failed: data.len() == ((height - 1) *
36 // row_stride + last_row_len) as usize', src/pixbuf.rs:50:13
37
38 let _pixbuf = Pixbuf::from_mut_slice(data, Colorspace::Rgb, true, 8, 21000, 29700, 21000 * 4);
39 }
40
41 #[test]
42 #[should_panic]
43 fn put_pixel_out_of_bounds_x_should_panic() {
44 let pixbuf = Pixbuf::new(Colorspace::Rgb, true, 8, 100, 200).unwrap();
45
46 pixbuf.put_pixel(100, 0, 0, 0, 0, 0);
47 }
48
49 #[test]
50 #[should_panic]
51 fn put_pixel_out_of_bounds_y_should_panic() {
52 let pixbuf = Pixbuf::new(Colorspace::Rgb, true, 8, 100, 200).unwrap();
53
54 pixbuf.put_pixel(0, 200, 0, 0, 0, 0);
55 }
56
57 #[test]
58 #[should_panic]
59 fn too_small_slice_should_panic() {
60 let data = vec![0u8; 100 * 99 * 4];
61
62 Pixbuf::from_mut_slice(data, Colorspace::Rgb, true, 8, 100, 100, 100 * 4);
63 }
64
65 #[test]
66 fn last_row_with_incomplete_rowstride_works() {
67 // 1-pixel wide, RGB, 3 32-bit rows, no extra padding byte on the fourth row
68 let data = vec![0u8; 4 * 3 + 3];
69
70 Pixbuf::from_mut_slice(data, Colorspace::Rgb, false, 8, 1, 4, 4);
71 }
72
73 #[test]
74 fn last_row_with_full_rowstride_works() {
75 // 1-pixel wide, RGB, 4 32-bit rows
76 let data = vec![0u8; 4 * 4];
77
78 Pixbuf::from_mut_slice(data, Colorspace::Rgb, false, 8, 1, 4, 4);
79 }
80
81 #[test]
82 fn extra_data_after_last_row_works() {
83 // 1-pixel wide, RGB, 4 32-bit rows, plus some extra space
84 let data = vec![0u8; 4 * 4 + 42];
85
86 Pixbuf::from_mut_slice(data, Colorspace::Rgb, false, 8, 1, 4, 4);
87 }