Coverage Report

Created: 2026-10-27 17:29

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
438
bool is_larger_than_fifty(std::string_view str) {
29
438
    int number = 0;
30
438
    auto [_, error] = std::from_chars(str.data(), str.data() + str.size(), number);
31
438
    return error == std::errc() && number > 50;
32
438
}
33
34
class SlowWithHyperscanChecker {
35
public:
36
    SlowWithHyperscanChecker()
37
2
            : _searcher_one_repeat(R"(\{\s*([\d]+)\s*,?\s*})"),
38
2
              _searcher_two_repeats(R"(\{\s*([\d]+)\s*,\s*([\d]+)\s*\})") {}
39
40
1.20k
    bool is_slow(std::string_view regexp) const {
41
1.20k
        return is_slow_one_repeat(regexp) || is_slow_two_repeats(regexp);
42
1.20k
    }
43
44
private:
45
1.20k
    bool is_slow_one_repeat(std::string_view regexp) const {
46
1.20k
        re2::StringPiece haystack(regexp.data(), regexp.size());
47
1.20k
        re2::StringPiece matches[2];
48
1.20k
        size_t start_pos = 0;
49
1.27k
        while (start_pos < haystack.size()) {
50
1.27k
            if (!_searcher_one_repeat.Match(haystack, start_pos, haystack.size(),
51
1.27k
                                            re2::RE2::Anchor::UNANCHORED, matches, 2)) {
52
1.19k
                break;
53
1.19k
            }
54
55
78
            start_pos = matches[0].data() - haystack.data() + matches[0].size();
56
78
            if (is_larger_than_fifty({matches[1].data(), matches[1].size()})) {
57
12
                return true;
58
12
            }
59
78
        }
60
1.19k
        return false;
61
1.20k
    }
62
63
1.19k
    bool is_slow_two_repeats(std::string_view regexp) const {
64
1.19k
        re2::StringPiece haystack(regexp.data(), regexp.size());
65
1.19k
        re2::StringPiece matches[3];
66
1.19k
        size_t start_pos = 0;
67
1.22k
        while (start_pos < haystack.size()) {
68
1.21k
            if (!_searcher_two_repeats.Match(haystack, start_pos, haystack.size(),
69
1.21k
                                             re2::RE2::Anchor::UNANCHORED, matches, 3)) {
70
1.02k
                break;
71
1.02k
            }
72
73
194
            start_pos = matches[0].data() - haystack.data() + matches[0].size();
74
194
            if (is_larger_than_fifty({matches[1].data(), matches[1].size()}) ||
75
194
                is_larger_than_fifty({matches[2].data(), matches[2].size()})) {
76
168
                return true;
77
168
            }
78
194
        }
79
1.02k
        return false;
80
1.19k
    }
81
82
    re2::RE2 _searcher_one_repeat;
83
    re2::RE2 _searcher_two_repeats;
84
};
85
86
} // namespace
87
88
1.20k
bool is_hyperscan_regexp_expensive(std::string_view regexp) {
89
1.20k
    static const SlowWithHyperscanChecker slow_with_hyperscan_checker;
90
1.20k
    return slow_with_hyperscan_checker.is_slow(regexp);
91
1.20k
}
92
93
} // namespace doris