]> source.dussan.org Git - rspamd.git/commitdiff
[Minor] Css: Slightly fix error propagation
authorVsevolod Stakhov <vsevolod@highsecure.ru>
Fri, 23 Jul 2021 16:31:54 +0000 (17:31 +0100)
committerVsevolod Stakhov <vsevolod@highsecure.ru>
Fri, 23 Jul 2021 16:31:54 +0000 (17:31 +0100)
src/libserver/css/css_parser.cxx
src/libserver/css/parse_error.hxx

index 1ab46a4ecb4f550847be5fe8e692cc86be50d8bb..4a545db4e51ab21eba67eeeb0b69b3263ee5f1d7 100644 (file)
@@ -166,7 +166,7 @@ public:
         * Process a single css rule
         */
        std::unique_ptr<css_consumed_block> consume_css_rule(const std::string_view &sv);
-       bool consume_input(const std::string_view &sv);
+       std::optional<css_parse_error> consume_input(const std::string_view &sv);
 
        auto get_object_maybe(void) -> tl::expected<std::shared_ptr<css_style_sheet>, css_parse_error> {
                if (style_object) {
@@ -599,13 +599,20 @@ css_parser::consume_css_rule(const std::string_view &sv) -> std::unique_ptr<css_
        return rule_block;
 }
 
-bool css_parser::consume_input(const std::string_view &sv)
+std::optional<css_parse_error>
+css_parser::consume_input(const std::string_view &sv)
 {
        auto &&consumed_blocks = consume_css_blocks(sv);
        const auto &rules = consumed_blocks->get_blocks_or_empty();
 
        if (rules.empty()) {
-               return false;
+               if (error.type == css_parse_error_type::PARSE_ERROR_UNKNOWN_ERROR) {
+                       return css_parse_error(css_parse_error_type::PARSE_ERROR_EMPTY,
+                                       "no css rules consumed");
+               }
+               else {
+                       return error;
+               }
        }
 
        if (!style_object) {
@@ -691,7 +698,7 @@ bool css_parser::consume_input(const std::string_view &sv)
        auto debug_str = consumed_blocks->debug_str();
        msg_debug_css("consumed css: {%*s}", (int)debug_str.size(), debug_str.data());
 
-       return true;
+       return std::nullopt;
 }
 
 auto
@@ -779,12 +786,12 @@ auto parse_css(rspamd_mempool_t *pool, const std::string_view &st,
                processed_input = std::string_view{nspace, st.size()};
        }
 
-       if (parser.consume_input(processed_input)) {
+       auto maybe_error = parser.consume_input(processed_input);
+       if (!maybe_error) {
                return parser.get_object_maybe();
        }
 
-       return tl::make_unexpected(css_parse_error{css_parse_error_type::PARSE_ERROR_INVALID_SYNTAX,
-                                                                                          "cannot parse input"});
+       return tl::make_unexpected(maybe_error.value());
 }
 
 auto
index 450c49d684d5e76a68b9765271efa6dcbe05a200..a493e993fdf842f92d656a05b40e81fbf44c596a 100644 (file)
@@ -31,6 +31,7 @@ enum class css_parse_error_type {
        PARSE_ERROR_UNKNOWN_OPTION,
        PARSE_ERROR_INVALID_SYNTAX,
        PARSE_ERROR_BAD_NESTING,
+       PARSE_ERROR_EMPTY,
        PARSE_ERROR_NYI,
        PARSE_ERROR_UNKNOWN_ERROR,
        PARSE_ERROR_NO_ERROR,