Coverage Report

Created: 2026-10-17 16:40

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/root/doris/be/src/util/hyperscan_util.cpp
Line
Count
Source
1
// Licensed to the Apache Software Foundation (ASF) under one
2
// or more contributor license agreements.  See the NOTICE file
3
// distributed with this work for additional information
4
// regarding copyright ownership.  The ASF licenses this file
5
// to you under the Apache License, Version 2.0 (the
6
// "License"); you may not use this file except in compliance
7
// with the License.  You may obtain a copy of the License at
8
//
9
//   http://www.apache.org/licenses/LICENSE-2.0
10
//
11
// Unless required by applicable law or agreed to in writing,
12
// software distributed under the License is distributed on an
13
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14
// KIND, either express or implied.  See the License for the
15
// specific language governing permissions and limitations
16
// under the License.
17
18
#include "util/hyperscan_util.h"
19
20
#include <re2/re2.h>
21
#include <re2/stringpiece.h>
22
23
#include <charconv>
24
25
namespace doris {
26
namespace {
27
28
219
bool is_larger_than_fifty(std::string_view str) {
29
219
    int number = 0;
30
219
    auto [_, error] = std::from_chars(str.data(), str.data() + str.size(), number);
31
219
    return error == std::errc() && number > 50;
32
219
}
33
34
class SlowWithHyperscanChecker {
35
public:
36
    SlowWithHyperscanChecker()
37
1
            : _searcher_one_repeat(R"(\{\s*([\d]+)\s*,?\s*})"),
38
1
              _searcher_two_repeats(R"(\{\s*([\d]+)\s*,\s*([\d]+)\s*\})") {}
39
40
600
    bool is_slow(std::string_view regexp) const {
41
600
        return is_slow_one_repeat(regexp) || is_slow_two_repeats(regexp);
42
600
    }
43
44
private:
45
600
    bool is_slow_one_repeat(std::string_view regexp) const {
46
600
        re2::StringPiece haystack(regexp.data(), regexp.size());
47
600
        re2::StringPiece matches[2];
48
600
        size_t start_pos = 0;
49
633
        while (start_pos < haystack.size()) {
50
632
            if (!_searcher_one_repeat.Match(haystack, start_pos, haystack.size(),
51
632
                                            re2::RE2::Anchor::UNANCHORED, matches, 2)) {
52
593
                break;
53
593
            }
54
55
39
            start_pos = matches[0].data() - haystack.data() + matches[0].size();
56
39
            if (is_larger_than_fifty({matches[1].data(), matches[1].size()})) {
57
6
                return true;
58
6
            }
59
39
        }
60
594
        return false;
61
600
    }
62
63
594
    bool is_slow_two_repeats(std::string_view regexp) const {
64
594
        re2::StringPiece haystack(regexp.data(), regexp.size());
65
594
        re2::StringPiece matches[3];
66
594
        size_t start_pos = 0;
67
607
        while (start_pos < haystack.size()) {
68
606
            if (!_searcher_two_repeats.Match(haystack, start_pos, haystack.size(),
69
606
                                             re2::RE2::Anchor::UNANCHORED, matches, 3)) {
70
509
                break;
71
509
            }
72
73
97
            start_pos = matches[0].data() - haystack.data() + matches[0].size();
74
97
            if (is_larger_than_fifty({matches[1].data(), matches[1].size()}) ||
75
97
                is_larger_than_fifty({matches[2].data(), matches[2].size()})) {
76
84
                return true;
77
84
            }
78
97
        }
79
510
        return false;
80
594
    }
81
82
    re2::RE2 _searcher_one_repeat;
83
    re2::RE2 _searcher_two_repeats;
84
};
85
86
} // namespace
87
88
600
bool is_hyperscan_regexp_expensive(std::string_view regexp) {
89
600
    static const SlowWithHyperscanChecker slow_with_hyperscan_checker;
90
600
    return slow_with_hyperscan_checker.is_slow(regexp);
91
600
}
92
93
} // namespace doris