From b9ad0cfad30c6fd1328658d8195f552f24df6ff9 Mon Sep 17 00:00:00 2001 From: Christian Duerr Date: Tue, 13 Mar 2018 07:07:40 +0100 Subject: Prevent negative cell dimensions (#1181) Prevent the cell dimensions from going below 1, this bug resulted in allocation of large amounts of memory in the scrollback PR but is also present on master. Currently the approach is to just `panic!`, however an `eprintln!` and `exit` could be an alternative too. I don't think it's realistic to check this at startup and it should have no performance impact since the failing method is only called once at startup. To make it a bit more clear what kind of values are accepted, the datatypes of offsets and paddings have also been changed so that these don't accept floats anymore and padding can never be negative. This should allow us to be a bit more strict with the config to make sure that errors are printed when invalid values are specified (like negative padding). This fixes #1167. --- src/config.rs | 35 +++++++++++++++-------------------- 1 file changed, 15 insertions(+), 20 deletions(-) (limited to 'src/config.rs') diff --git a/src/config.rs b/src/config.rs index 0eb7a0d7..f8e6490d 100644 --- a/src/config.rs +++ b/src/config.rs @@ -270,18 +270,18 @@ pub struct WindowConfig { /// Pixel padding #[serde(default="default_padding", deserialize_with = "deserialize_padding")] - padding: Delta, + padding: Delta, /// Draw the window with title bar / borders #[serde(default, deserialize_with = "failure_default")] decorations: bool, } -fn default_padding() -> Delta { - Delta { x: 2., y: 2. } +fn default_padding() -> Delta { + Delta { x: 2, y: 2 } } -fn deserialize_padding<'a, D>(deserializer: D) -> ::std::result::Result +fn deserialize_padding<'a, D>(deserializer: D) -> ::std::result::Result, D::Error> where D: de::Deserializer<'a> { match Delta::deserialize(deserializer) { @@ -318,7 +318,7 @@ pub struct Config { /// Pixel padding #[serde(default, deserialize_with = "failure_default")] - padding: Option, + padding: Option>, /// TERM env variable #[serde(default, deserialize_with = "failure_default")] @@ -1285,7 +1285,7 @@ impl Config { self.tabspaces } - pub fn padding(&self) -> &Delta { + pub fn padding(&self) -> &Delta { self.padding.as_ref() .unwrap_or(&self.window.padding) } @@ -1448,20 +1448,15 @@ impl Dimensions { } /// A delta for a point in a 2 dimensional plane -#[derive(Clone, Copy, Debug, Deserialize)] -pub struct Delta { +#[derive(Clone, Copy, Debug, Default, Deserialize)] +#[serde(bound(deserialize = "T: Deserialize<'de> + Default"))] +pub struct Delta { /// Horizontal change #[serde(default, deserialize_with = "failure_default")] - pub x: f32, + pub x: T, /// Vertical change #[serde(default, deserialize_with = "failure_default")] - pub y: f32, -} - -impl Default for Delta { - fn default() -> Delta { - Delta { x: 0.0, y: 0.0 } - } + pub y: T, } trait DeserializeSize : Sized { @@ -1539,11 +1534,11 @@ pub struct Font { /// Extra spacing per character #[serde(default, deserialize_with = "failure_default")] - offset: Delta, + offset: Delta, /// Glyph offset within character cell #[serde(default, deserialize_with = "failure_default")] - glyph_offset: Delta, + glyph_offset: Delta, #[serde(default="true_bool", deserialize_with = "default_true_bool")] use_thin_strokes: bool @@ -1582,13 +1577,13 @@ impl Font { /// Get offsets to font metrics #[inline] - pub fn offset(&self) -> &Delta { + pub fn offset(&self) -> &Delta { &self.offset } /// Get cell offsets for glyphs #[inline] - pub fn glyph_offset(&self) -> &Delta { + pub fn glyph_offset(&self) -> &Delta { &self.glyph_offset } -- cgit