Skip to content

Commit

Permalink
Fix #122.
Browse files Browse the repository at this point in the history
Refuse to divide a color by zero.
  • Loading branch information
kaj committed Oct 11, 2021
1 parent 357657f commit 1b8b687
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 3 deletions.
7 changes: 4 additions & 3 deletions src/value/operator.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use crate::css::{CssString, Value};
use crate::value::{ListSeparator, Numeric, Quotes};
use num_traits::Zero;
use std::fmt;

/// An operator that can be used in a sass value.
Expand Down Expand Up @@ -140,10 +141,10 @@ impl Operator {
Operator::Div if a.is_calculated() || b.is_calculated() => {
match (a, b) {
(Value::Color(a, _), Value::Numeric(b, _))
if b.is_no_unit() =>
if b.is_no_unit() && !b.value.is_zero() =>
{
let bn = b.as_ratio().ok()?;
Some((a.to_rgba().as_ref() / bn).into())
let bn = dbg!(b).as_ratio().ok()?;
Some((dbg!(a).to_rgba().as_ref() / bn).into())
}
(Value::Numeric(ref a, _), Value::Numeric(ref b, _)) => {
Some((a / b).into())
Expand Down
19 changes: 19 additions & 0 deletions tests/basic_manual.rs
Original file line number Diff line number Diff line change
Expand Up @@ -384,6 +384,25 @@ fn issue_116() {
);
}

/// https://github.com/kaj/rsass/issues/122
/// A division by zero that causes a panic
mod issue_122 {
use super::check_value;
// Note: The important thing here is not to panic, the exact
// output may be changed in the future, maybe to report an error.
#[test]
fn reduced() {
check_value("(#111 + #aaa)/0", "#bbbbbb/0")
}
#[test]
fn reported() {
check_value(
"54A444/0+-4444M4#444/-4444/0+-4444M4#444+44/0+444/0+.44444O#444+44/0+4/46",
"InfinityA444-4444M4 black/0-4444M4 #444InfinityInfinity0.44444O #444Infinity0.0869565217",
)
}
}

/// Test auto-converted from "sass-spec/spec/libsass/rel.hrx", except one failing unit calculation.
#[test]
fn rel() {
Expand Down

0 comments on commit 1b8b687

Please sign in to comment.