From 325ad381167163a14c1480b3fe3b4384e9f08b01 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Konstantin=20K=C3=A4fer?= Date: Tue, 22 May 2018 11:17:52 +0200 Subject: [PATCH 1/2] fix hang when using very large angles for hue in hsl colors --- csscolorparser.cpp | 5 +++-- test.cpp | 4 ++++ 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/csscolorparser.cpp b/csscolorparser.cpp index 5658f2b..e7a4e50 100644 --- a/csscolorparser.cpp +++ b/csscolorparser.cpp @@ -262,8 +262,9 @@ optional parse(const std::string& css_str) { } float h = parseFloat(params[0]) / 360.0f; - while (h < 0.0f) h++; - while (h > 1.0f) h--; + float i; + // Normalize the hue to [0..1[ + h = std::modf(h, &i); // NOTE(deanm): According to the CSS spec s/l should only be // percentages, but we don't bother and let float or percentage. diff --git a/test.cpp b/test.cpp index fc93644..25fa852 100644 --- a/test.cpp +++ b/test.cpp @@ -47,6 +47,10 @@ int main() { ASSERT_EQUAL({}, "hsla(900, 15%, 90%)"); ASSERT_EQUAL(Color{ 226, 233, 233, 1 }, "hsl(900, 15%, 90%)"); ASSERT_EQUAL(Color{ 226, 233, 233, 1 }, "hsl(900, 0.15, 90%)"); // NOTE: not spec compliamt. + ASSERT_EQUAL(Color{ 0, 0, 0, 1 }, "hsl(9999999999999999999, 0, 0)"); // NOTE: float precision loss + ASSERT_EQUAL(Color{ 255, 191, 0, 1 }, "hsl(45, 100%, 50%)"); + ASSERT_EQUAL(Color{ 255, 191, 0, 1 }, "hsl(-315, 100%, 50%)"); + ASSERT_EQUAL(Color{ 255, 191, 0, 1 }, "hsl(-675, 100%, 50%)"); // Out of range: ASSERT_EQUAL({}, "xxx"); From 33d0c489c0fb5e16ab7b3bc133af5a9d14bd4565 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Konstantin=20K=C3=A4fer?= Date: Tue, 22 May 2018 11:22:09 +0200 Subject: [PATCH 2/2] correctly iterate over named colors fixup to 4f51c64fbce0eae1b24ca9ddb252eeba04f548f3 --- csscolorparser.cpp | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/csscolorparser.cpp b/csscolorparser.cpp index e7a4e50..106dae6 100644 --- a/csscolorparser.cpp +++ b/csscolorparser.cpp @@ -185,10 +185,9 @@ optional parse(const std::string& css_str) { // Convert to lowercase. std::transform(str.begin(), str.end(), str.begin(), ::tolower); - - for (size_t i = 0; i < namedColorCount; i++) { - if (str == namedColors[i].name) { - return { namedColors[i].color }; + for (const auto& namedColor : namedColors) { + if (str == namedColor.name) { + return { namedColor.color }; } }