From 3361db7b5db0f93bd91458275c79b9a3a60be745 Mon Sep 17 00:00:00 2001 From: Artem Pavlenko Date: Tue, 20 Apr 2021 12:31:10 +0100 Subject: [PATCH] SVG parse_svg_value: restore result to default value on parsing failure + update `is_percent` only if parsing is successful --- src/svg/svg_parser.cpp | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/src/svg/svg_parser.cpp b/src/svg/svg_parser.cpp index 1edf00607..ab87e90ae 100644 --- a/src/svg/svg_parser.cpp +++ b/src/svg/svg_parser.cpp @@ -265,10 +265,10 @@ double parse_svg_value(T & err_handler, const char* str, bool & is_percent) css_unit_value units; const char* cur = str; // phrase_parse mutates the first iterator const char* end = str + std::strlen(str); - - auto apply_value = [&](auto const& ctx) { val = _attr(ctx); is_percent = false; }; + bool is_percent_; + auto apply_value = [&](auto const& ctx) { val = _attr(ctx); is_percent_ = false; }; auto apply_units = [&](auto const& ctx) { val *= _attr(ctx); }; - auto apply_percent = [&](auto const& ctx) { val *= 0.01; is_percent = true; }; + auto apply_percent = [&](auto const& ctx) { val *= 0.01; is_percent_ = true; }; if (!x3::phrase_parse(cur, end, x3::double_[apply_value] @@ -277,8 +277,13 @@ double parse_svg_value(T & err_handler, const char* str, bool & is_percent) x3::lit('%')[apply_percent]), x3::space) || (cur != end)) { + val = 0.0; // restore to default on parsing failure err_handler.on_error("SVG parse error: failed to parse with value \"" + std::string(str) + "\""); } + else + { + is_percent = is_percent_; // update only on success + } return val; }