From a8324669cc34afd15811a287decce350872b190d Mon Sep 17 00:00:00 2001 From: Nathan Vegdahl Date: Mon, 4 Jul 2016 01:26:28 -0700 Subject: [PATCH] Fixed possible out-of-bounds panic in XYZ -> Spectrum code. --- src/color/generate_spectra_rust.py | 5 +++++ src/color/spectra_xyz.rs | 5 +++++ 2 files changed, 10 insertions(+) diff --git a/src/color/generate_spectra_rust.py b/src/color/generate_spectra_rust.py index 5df31b5..245c983 100755 --- a/src/color/generate_spectra_rust.py +++ b/src/color/generate_spectra_rust.py @@ -79,6 +79,11 @@ pub fn spectrum_xyz_to_p(lambda: f32, xyz: (f32, f32, f32)) -> f32 let idx = &cell.idx; let num: i32 = cell.num_points; + // if the cell has no points, nothing we can do, so return 0 + if num == 0 { + return 0.0; + } + // get linearly interpolated spectral power for the corner vertices: // this clamping is only necessary if lambda is not sure to be >= SPECTRUM_SAMPLE_MIN and <= SPECTRUM_SAMPLE_MAX: let sb: f32 = /*(SPECTRUM_NUM_SAMPLES as f32 - 1e-4).min(0.0.max(*/ (lambda - SPECTRUM_SAMPLE_MIN) / (SPECTRUM_SAMPLE_MAX - SPECTRUM_SAMPLE_MIN) * (SPECTRUM_NUM_SAMPLES as f32 - 1.0)/*))*/; diff --git a/src/color/spectra_xyz.rs b/src/color/spectra_xyz.rs index 239592b..814385d 100644 --- a/src/color/spectra_xyz.rs +++ b/src/color/spectra_xyz.rs @@ -45,6 +45,11 @@ pub fn spectrum_xyz_to_p(lambda: f32, xyz: (f32, f32, f32)) -> f32 let idx = &cell.idx; let num: i32 = cell.num_points; + // if the cell has no points, nothing we can do, so return 0 + if num == 0 { + return 0.0; + } + // get linearly interpolated spectral power for the corner vertices: // this clamping is only necessary if lambda is not sure to be >= SPECTRUM_SAMPLE_MIN and <= SPECTRUM_SAMPLE_MAX: let sb: f32 = /*(SPECTRUM_NUM_SAMPLES as f32 - 1e-4).min(0.0.max(*/ (lambda - SPECTRUM_SAMPLE_MIN) / (SPECTRUM_SAMPLE_MAX - SPECTRUM_SAMPLE_MIN) * (SPECTRUM_NUM_SAMPLES as f32 - 1.0)/*))*/;