Skip to content

Commit 2eb2f7e

Browse files
Renyi Zhaosdroege
authored andcommitted
graphene: Fix UB in intersect_triangle by validating t_out before assume_init
1 parent e60ccb2 commit 2eb2f7e

File tree

3 files changed

+53
-41
lines changed

3 files changed

+53
-41
lines changed

graphene/Gir.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -375,6 +375,9 @@ boxed_inline = true
375375
name = "init_from_vec3"
376376
rename = "from_vec3"
377377
manual = true # manual proper constructor
378+
[[object.function]]
379+
pattern = "intersect_(box|sphere|triangle)"
380+
manual = true # Handle no intersection cases
378381

379382
[[object]]
380383
name = "Graphene.Rect"

graphene/src/auto/ray.rs

Lines changed: 1 addition & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
// from gir-files (https://github.com/gtk-rs/gir-files)
33
// DO NOT EDIT
44

5-
use crate::{ffi, Box, Plane, Point3D, RayIntersectionKind, Sphere, Triangle, Vec3};
5+
use crate::{ffi, Box, Plane, Point3D, Sphere, Triangle, Vec3};
66
use glib::translate::*;
77

88
glib::wrapper! {
@@ -85,45 +85,6 @@ impl Ray {
8585
}
8686
}
8787

88-
#[doc(alias = "graphene_ray_intersect_box")]
89-
pub fn intersect_box(&self, b: &Box) -> (RayIntersectionKind, f32) {
90-
unsafe {
91-
let mut t_out = std::mem::MaybeUninit::uninit();
92-
let ret = from_glib(ffi::graphene_ray_intersect_box(
93-
self.to_glib_none().0,
94-
b.to_glib_none().0,
95-
t_out.as_mut_ptr(),
96-
));
97-
(ret, t_out.assume_init())
98-
}
99-
}
100-
101-
#[doc(alias = "graphene_ray_intersect_sphere")]
102-
pub fn intersect_sphere(&self, s: &Sphere) -> (RayIntersectionKind, f32) {
103-
unsafe {
104-
let mut t_out = std::mem::MaybeUninit::uninit();
105-
let ret = from_glib(ffi::graphene_ray_intersect_sphere(
106-
self.to_glib_none().0,
107-
s.to_glib_none().0,
108-
t_out.as_mut_ptr(),
109-
));
110-
(ret, t_out.assume_init())
111-
}
112-
}
113-
114-
#[doc(alias = "graphene_ray_intersect_triangle")]
115-
pub fn intersect_triangle(&self, t: &Triangle) -> (RayIntersectionKind, f32) {
116-
unsafe {
117-
let mut t_out = std::mem::MaybeUninit::uninit();
118-
let ret = from_glib(ffi::graphene_ray_intersect_triangle(
119-
self.to_glib_none().0,
120-
t.to_glib_none().0,
121-
t_out.as_mut_ptr(),
122-
));
123-
(ret, t_out.assume_init())
124-
}
125-
}
126-
12788
#[doc(alias = "graphene_ray_intersects_box")]
12889
pub fn intersects_box(&self, b: &Box) -> bool {
12990
unsafe { ffi::graphene_ray_intersects_box(self.to_glib_none().0, b.to_glib_none().0) }

graphene/src/ray.rs

Lines changed: 49 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use std::fmt;
44

55
use glib::translate::*;
66

7-
use crate::{ffi, Point3D, Ray, Vec3};
7+
use crate::{ffi, Box, Point3D, Ray, RayIntersectionKind, Sphere, Triangle, Vec3};
88

99
impl Ray {
1010
#[doc(alias = "graphene_ray_init")]
@@ -35,6 +35,54 @@ impl Ray {
3535
ray
3636
}
3737
}
38+
39+
#[doc(alias = "graphene_ray_intersect_box")]
40+
pub fn intersect_box(&self, b: &Box) -> (RayIntersectionKind, Option<f32>) {
41+
unsafe {
42+
let mut t_out = std::mem::MaybeUninit::uninit();
43+
let ret = from_glib(ffi::graphene_ray_intersect_box(
44+
self.to_glib_none().0,
45+
b.to_glib_none().0,
46+
t_out.as_mut_ptr(),
47+
));
48+
match ret {
49+
RayIntersectionKind::None => (ret, None),
50+
_ => (ret, Some(t_out.assume_init())),
51+
}
52+
}
53+
}
54+
55+
#[doc(alias = "graphene_ray_intersect_sphere")]
56+
pub fn intersect_sphere(&self, s: &Sphere) -> (RayIntersectionKind, Option<f32>) {
57+
unsafe {
58+
let mut t_out = std::mem::MaybeUninit::uninit();
59+
let ret = from_glib(ffi::graphene_ray_intersect_sphere(
60+
self.to_glib_none().0,
61+
s.to_glib_none().0,
62+
t_out.as_mut_ptr(),
63+
));
64+
match ret {
65+
RayIntersectionKind::None => (ret, None),
66+
_ => (ret, Some(t_out.assume_init())),
67+
}
68+
}
69+
}
70+
71+
#[doc(alias = "graphene_ray_intersect_triangle")]
72+
pub fn intersect_triangle(&self, t: &Triangle) -> (RayIntersectionKind, Option<f32>) {
73+
unsafe {
74+
let mut t_out = std::mem::MaybeUninit::uninit();
75+
let ret = from_glib(ffi::graphene_ray_intersect_triangle(
76+
self.to_glib_none().0,
77+
t.to_glib_none().0,
78+
t_out.as_mut_ptr(),
79+
));
80+
match ret {
81+
RayIntersectionKind::None => (ret, None),
82+
_ => (ret, Some(t_out.assume_init())),
83+
}
84+
}
85+
}
3886
}
3987

4088
impl fmt::Debug for Ray {

0 commit comments

Comments
 (0)