Free as in Freedom: Codeberg.org. Create your repos!
Browse Source

Fix casting (after integer overflow) in hash(...) function.

tags/v0.5.1
Jan Walter 2 months ago
parent
commit
fe5ae69281
3 changed files with 26 additions and 4 deletions
  1. 2
    0
      Makefile
  2. 19
    0
      examples/integrators_sspm_hash.rs
  3. 5
    4
      src/integrators/sppm.rs

+ 2
- 0
Makefile View File

@@ -52,10 +52,12 @@ examples: release
52 52
 	./target/release/examples/geometry_spherical_direction_vec3
53 53
 	./target/release/examples/geometry_vector2_null
54 54
 	./target/release/examples/geometry_vector3_null
55
+	./target/release/examples/integrators_sspm_hash.rs
55 56
 	./target/release/examples/lights_diffuse_area_light_new
56 57
 	./target/release/examples/lights_distant_light_new
57 58
 	./target/release/examples/lights_infinite_area_light_new
58 59
 	./target/release/examples/lights_point_light_new
60
+	./target/release/examples/parse_ass_file.rs
59 61
 	./target/release/examples/pbrt_spheres_differentials_texfilt
60 62
 	./target/release/examples/pbrt_teapot_area_light
61 63
 	./target/release/examples/pest_test

+ 19
- 0
examples/integrators_sspm_hash.rs View File

@@ -0,0 +1,19 @@
1
+extern crate pbrt;
2
+
3
+use pbrt::core::geometry::Point3i;
4
+
5
+fn hash(p: &Point3i, hash_size: i32) -> usize {
6
+    (((p.x * 73856093) ^ (p.y * 19349663) ^ (p.z * 83492791)) as u32 % hash_size as u32) as usize
7
+}
8
+
9
+fn main() {
10
+    let photon_grid_index: Point3i = Point3i {
11
+        x: 225,
12
+        y: 0,
13
+        z: 267,
14
+    };
15
+    let hash_size: usize = 700000;
16
+    let h: usize = hash(&photon_grid_index, hash_size as i32);
17
+    assert!(h < hash_size, "hash({:?}, {:?})", photon_grid_index, hash_size);
18
+    println!("hash({:?}, {:?}) = {}", photon_grid_index, hash_size, h);
19
+}

+ 5
- 4
src/integrators/sppm.rs View File

@@ -108,8 +108,8 @@ fn to_grid(p: &Point3f, bounds: &Bounds3f, grid_res: &[i32; 3], pi: &mut Point3i
108 108
     in_bounds
109 109
 }
110 110
 
111
-fn hash(p: &Point3i, hash_size: usize) -> usize {
112
-    (((p.x * 73856093) ^ (p.y * 19349663) ^ (p.z * 83492791)) % hash_size as i32) as usize
111
+fn hash(p: &Point3i, hash_size: i32) -> usize {
112
+    (((p.x * 73856093) ^ (p.y * 19349663) ^ (p.z * 83492791)) as u32 % hash_size as u32) as usize
113 113
 }
114 114
 
115 115
 /// **Main function** to **render** a scene multi-threaded (using all
@@ -382,7 +382,7 @@ pub fn render_sppm(
382 382
                             for y in p_min.y..p_max.y {
383 383
                                 for x in p_min.x..p_max.x {
384 384
                                     // add visible point to grid cell $(x, y, z)$
385
-                                    let h: usize = hash(&Point3i { x: x, y: y, z: z }, hash_size);
385
+                                    let h: usize = hash(&Point3i { x: x, y: y, z: z }, hash_size as i32);
386 386
                                     let mut node_arc: Arc<SPPMPixelListNode> =
387 387
                                         Arc::new(SPPMPixelListNode::default());
388 388
                                     let pixel_clone: Arc<SPPMPixel> = pixel.clone();
@@ -471,8 +471,9 @@ pub fn render_sppm(
471 471
                                         &grid_res,
472 472
                                         &mut photon_grid_index,
473 473
                                     ) {
474
-                                        let h: usize = hash(&photon_grid_index, hash_size);
474
+                                        let h: usize = hash(&photon_grid_index, hash_size as i32);
475 475
                                         // add photon contribution to visible points in _grid[h]_
476
+                                        assert!(h < hash_size, "hash({:?}, {:?})", photon_grid_index, hash_size);
476 477
                                         if let Some(root) = grid[h].get() {
477 478
                                             let mut node: &SPPMPixelListNode = root;
478 479
                                             loop {

Loading…
Cancel
Save