From 3d447dec3a91125de62750f842ec6b2c46027a16 Mon Sep 17 00:00:00 2001 From: ridiculousfish Date: Mon, 29 May 2023 13:22:46 -0700 Subject: [PATCH] Fix a multiplicative overflow in color.rs Also add a test. --- fish-rust/src/color.rs | 23 ++++++++++++++++++++--- 1 file changed, 20 insertions(+), 3 deletions(-) diff --git a/fish-rust/src/color.rs b/fish-rust/src/color.rs index 172bc19f3..7c48c8a2c 100644 --- a/fish-rust/src/color.rs +++ b/fish-rust/src/color.rs @@ -319,8 +319,10 @@ const NAMED_COLORS: &[NamedColor] = &[ assert_sorted_by_name!(NAMED_COLORS); fn convert_color(color: Color24, colors: &[u32]) -> usize { - fn squared_difference(a: u8, b: u8) -> u16 { - u16::from(a.abs_diff(b)).pow(2) + fn squared_difference(a: u8, b: u8) -> u32 { + let a = u32::from(a); + let b = u32::from(b); + a.abs_diff(b).pow(2) } colors @@ -402,7 +404,10 @@ fn term256_color_for_rgb(color: Color24) -> u8 { #[cfg(test)] mod tests { - use crate::{color::RgbColor, wchar::widestrs}; + use crate::{ + color::{Color24, Flags, RgbColor, Type}, + wchar::widestrs, + }; #[test] #[widestrs] @@ -419,4 +424,16 @@ mod tests { assert!(RgbColor::from_wstr("MaGeNTa"L).unwrap().is_named()); assert!(RgbColor::from_wstr("mooganta"L).is_none()); } + + // Regression test for multiplicative overflow in convert_color. + #[test] + fn test_term16_color_for_rgb() { + for c in 0..=u8::MAX { + let color = RgbColor { + typ: Type::Rgb(Color24 { r: c, g: c, b: c }), + flags: Flags::DEFAULT, + }; + let _ = color.to_name_index(); + } + } }