/proc/self/cwd/internal/re2_options.h
Line | Count | Source |
1 | | // Copyright 2026 Google LLC |
2 | | // |
3 | | // Licensed under the Apache License, Version 2.0 (the "License"); |
4 | | // you may not use this file except in compliance with the License. |
5 | | // You may obtain a copy of the License at |
6 | | // |
7 | | // https://www.apache.org/licenses/LICENSE-2.0 |
8 | | // |
9 | | // Unless required by applicable law or agreed to in writing, software |
10 | | // distributed under the License is distributed on an "AS IS" BASIS, |
11 | | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
12 | | // See the License for the specific language governing permissions and |
13 | | // limitations under the License. |
14 | | |
15 | | #ifndef THIRD_PARTY_CEL_CPP_INTERNAL_RE2_OPTIONS_H_ |
16 | | #define THIRD_PARTY_CEL_CPP_INTERNAL_RE2_OPTIONS_H_ |
17 | | |
18 | | #include "absl/status/status.h" |
19 | | #include "absl/strings/str_cat.h" |
20 | | #include "re2/re2.h" |
21 | | |
22 | | namespace cel::internal { |
23 | | |
24 | 0 | inline RE2::Options MakeRE2Options() { |
25 | 0 | RE2::Options options; |
26 | 0 | options.set_log_errors(false); |
27 | 0 | return options; |
28 | 0 | } |
29 | | |
30 | 0 | inline absl::Status CheckRE2(const RE2& re, int max_program_size) { |
31 | 0 | if (!re.ok()) { |
32 | 0 | switch (re.error_code()) { |
33 | 0 | case RE2::ErrorInternal: |
34 | 0 | return absl::InternalError( |
35 | 0 | absl::StrCat("internal RE2 error: ", re.error())); |
36 | 0 | case RE2::ErrorPatternTooLarge: |
37 | 0 | return absl::InvalidArgumentError( |
38 | 0 | absl::StrCat("regular expression too large: ", re.error())); |
39 | 0 | default: |
40 | 0 | return absl::InvalidArgumentError( |
41 | 0 | absl::StrCat("invalid regular expression: ", re.error())); |
42 | 0 | } |
43 | 0 | } |
44 | 0 | int program_size = re.ProgramSize(); |
45 | 0 | if (max_program_size > 0 && program_size > 0 && |
46 | 0 | program_size > max_program_size) { |
47 | 0 | return absl::InvalidArgumentError( |
48 | 0 | "regular expression exceeds max allowed size"); |
49 | 0 | } |
50 | 0 | int reverse_program_size = re.ReverseProgramSize(); |
51 | 0 | if (max_program_size > 0 && reverse_program_size > 0 && |
52 | 0 | reverse_program_size > max_program_size) { |
53 | 0 | return absl::InvalidArgumentError( |
54 | 0 | "regular expression exceeds max allowed size"); |
55 | 0 | } |
56 | 0 | return absl::OkStatus(); |
57 | 0 | } |
58 | | |
59 | | } // namespace cel::internal |
60 | | |
61 | | #endif // THIRD_PARTY_CEL_CPP_INTERNAL_RE2_OPTIONS_H_ |