Debian Codegraph @

src/common/oscap_pcre.c

raw symbols SCIP
1 /*
2 * Copyright 2023 Red Hat Inc., Durham, North Carolina.
3 * All Rights Reserved.
4 *
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2.1 of the License, or (at your option) any later version.
9 *
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
14 *
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18 *
19 * Authors:
20 * Evgenii Kolesnikov <[email protected]>
21 */
22
23 #ifdef HAVE_CONFIG_Hcxx . . $ `<built-in>:409:9`!
24 #include <config.h>cxx . . $ `<file>/build/config.h`/
25 #endif
26
27 #include <memory.h>
28
29 #define OSCAP_PCRE_EXEC_RECURSION_LIMIT_DEFAULTcxx . . $ `src/common/oscap_pcre.c:29:9`! 3500
30
31 #define PCRE2_CODE_UNIT_WIDTHcxx . . $ `src/common/oscap_pcre.c:31:9`! 8
32 #define PCRE2_ERR_BUF_SIZEcxx . . $ `src/common/oscap_pcre.c:32:9`! 127
33 #include <pcre2.h>
34
35
36 #include "debug_priv.h"cxx . . $ `<file>/src/common/debug_priv.h`/
37 #include "oscap_pcre.h"cxx . . $ `<file>/src/common/oscap_pcre.h`/
38
39
40 struct oscap_pcrecxx . . $ oscap_pcre# {
41 pcre2_code_8cxx . . $ pcre2_code_8# *recxx . . $ oscap_pcre#re.;
42 pcre2_match_context_8cxx . . $ pcre2_match_context_8# *re_ctxcxx . . $ oscap_pcre#re_ctx.;
43 };
44
45
46 static inline int _oscap_pcre_opts_to_pcrecxx . . $ _oscap_pcre_opts_to_pcre(396d139e3bb0bc80).(oscap_pcre_options_tcxx . . $ oscap_pcre_options_t# opts)
47 {
48 int res = 0;
49 if (opts & OSCAP_PCRE_OPTS_UTF8cxx . . $ OSCAP_PCRE_OPTS_UTF8.)
50 res |= PCRE2_UTFcxx . . $ `<external>/9988290459910441303/pcre2.h:138:9`!;
51 if (opts & OSCAP_PCRE_OPTS_MULTILINEcxx . . $ OSCAP_PCRE_OPTS_MULTILINE.)
52 res |= PCRE2_MULTILINEcxx . . $ `<external>/9988290459910441303/pcre2.h:129:9`!;
53 if (opts & OSCAP_PCRE_OPTS_DOTALLcxx . . $ OSCAP_PCRE_OPTS_DOTALL.)
54 res |= PCRE2_DOTALLcxx . . $ `<external>/9988290459910441303/pcre2.h:124:9`!;
55 if (opts & OSCAP_PCRE_OPTS_CASELESScxx . . $ OSCAP_PCRE_OPTS_CASELESS.)
56 res |= PCRE2_CASELESScxx . . $ `<external>/9988290459910441303/pcre2.h:122:9`!;
57 if (opts & OSCAP_PCRE_OPTS_NO_UTF8_CHECKcxx . . $ OSCAP_PCRE_OPTS_NO_UTF8_CHECK.)
58 res |= PCRE2_NO_UTF_CHECKcxx . . $ `<external>/9988290459910441303/pcre2.h:106:9`!;
59 if (opts & OSCAP_PCRE_OPTS_PARTIALcxx . . $ OSCAP_PCRE_OPTS_PARTIAL.)
60 res |= PCRE2_PARTIAL_SOFTcxx . . $ `<external>/9988290459910441303/pcre2.h:186:9`!;
61
62 return res;
63 };
64
65 static inline oscap_pcre_error_tcxx . . $ oscap_pcre_error_t# _pcre_error_to_oscap_pcrecxx . . $ _pcre_error_to_oscap_pcre(85e69a8f33198fd).(int res)
66 {
67 switch (res) {
68 case PCRE2_ERROR_NOMATCHcxx . . $ `<external>/9988290459910441303/pcre2.h:349:9`!:
69 return OSCAP_PCRE_ERR_NOMATCHcxx . . $ OSCAP_PCRE_ERR_NOMATCH.;
70 case PCRE2_ERROR_PARTIALcxx . . $ `<external>/9988290459910441303/pcre2.h:350:9`!:
71 return OSCAP_PCRE_ERR_PARTIALcxx . . $ OSCAP_PCRE_ERR_PARTIAL.;
72 case PCRE2_ERROR_UTF8_ERR1cxx . . $ `<external>/9988290459910441303/pcre2.h:354:9`!:
73 case PCRE2_ERROR_UTF8_ERR2cxx . . $ `<external>/9988290459910441303/pcre2.h:355:9`!:
74 case PCRE2_ERROR_UTF8_ERR3cxx . . $ `<external>/9988290459910441303/pcre2.h:356:9`!:
75 case PCRE2_ERROR_UTF8_ERR4cxx . . $ `<external>/9988290459910441303/pcre2.h:357:9`!:
76 case PCRE2_ERROR_UTF8_ERR5cxx . . $ `<external>/9988290459910441303/pcre2.h:358:9`!:
77 case PCRE2_ERROR_UTF8_ERR6cxx . . $ `<external>/9988290459910441303/pcre2.h:359:9`!:
78 case PCRE2_ERROR_UTF8_ERR7cxx . . $ `<external>/9988290459910441303/pcre2.h:360:9`!:
79 case PCRE2_ERROR_UTF8_ERR8cxx . . $ `<external>/9988290459910441303/pcre2.h:361:9`!:
80 case PCRE2_ERROR_UTF8_ERR9cxx . . $ `<external>/9988290459910441303/pcre2.h:362:9`!:
81 case PCRE2_ERROR_UTF8_ERR10cxx . . $ `<external>/9988290459910441303/pcre2.h:363:9`!:
82 case PCRE2_ERROR_UTF8_ERR11cxx . . $ `<external>/9988290459910441303/pcre2.h:364:9`!:
83 case PCRE2_ERROR_UTF8_ERR12cxx . . $ `<external>/9988290459910441303/pcre2.h:365:9`!:
84 case PCRE2_ERROR_UTF8_ERR13cxx . . $ `<external>/9988290459910441303/pcre2.h:366:9`!:
85 case PCRE2_ERROR_UTF8_ERR14cxx . . $ `<external>/9988290459910441303/pcre2.h:367:9`!:
86 case PCRE2_ERROR_UTF8_ERR15cxx . . $ `<external>/9988290459910441303/pcre2.h:368:9`!:
87 case PCRE2_ERROR_UTF8_ERR16cxx . . $ `<external>/9988290459910441303/pcre2.h:369:9`!:
88 case PCRE2_ERROR_UTF8_ERR17cxx . . $ `<external>/9988290459910441303/pcre2.h:370:9`!:
89 case PCRE2_ERROR_UTF8_ERR18cxx . . $ `<external>/9988290459910441303/pcre2.h:371:9`!:
90 case PCRE2_ERROR_UTF8_ERR19cxx . . $ `<external>/9988290459910441303/pcre2.h:372:9`!:
91 case PCRE2_ERROR_UTF8_ERR20cxx . . $ `<external>/9988290459910441303/pcre2.h:373:9`!:
92 case PCRE2_ERROR_UTF8_ERR21cxx . . $ `<external>/9988290459910441303/pcre2.h:374:9`!:
93 return OSCAP_PCRE_ERR_BADUTF8cxx . . $ OSCAP_PCRE_ERR_BADUTF8.;
94 case PCRE2_ERROR_RECURSIONLIMITcxx . . $ `<external>/9988290459910441303/pcre2.h:417:9`!:
95 return OSCAP_PCRE_ERR_RECURSIONLIMITcxx . . $ OSCAP_PCRE_ERR_RECURSIONLIMIT.;
96 default:
97 dWcxx . . $ `src/common/debug_priv.h:62:9`!("Unknown PCRE error code: %d", res);
98 return OSCAP_PCRE_ERR_UNKNOWNcxx . . $ OSCAP_PCRE_ERR_UNKNOWN.;
99 }
100 }
101
102 oscap_pcre_tcxx . . $ oscap_pcre_t# *oscap_pcre_compilecxx . . $ oscap_pcre_compile(29ca11f2d36a957a).(const char *pattern, oscap_pcre_options_tcxx . . $ oscap_pcre_options_t# options,
103 char **errptr, int *erroffset)
104 {
105 oscap_pcre_tcxx . . $ oscap_pcre_t# *res = malloccxx . . $ malloc(bfcfa4d6b7f7ef64).(sizeof(oscap_pcre_tcxx . . $ oscap_pcre_t#));
106 int errno;
107 PCRE2_SIZEcxx . . $ `<external>/9988290459910441303/pcre2.h:528:9`! erroffset2;
108 res->re_ctxcxx . . $ oscap_pcre#re_ctx. = NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!;
109 res->recxx . . $ oscap_pcre#re. = pcre2_compile_8cxx . . $ pcre2_compile_8(90b44b9159016012).((PCRE2_SPTRcxx . . $ `<external>/9988290459910441303/pcre2.h:877:9`!)pattern, PCRE2_ZERO_TERMINATEDcxx . . $ `<external>/9988290459910441303/pcre2.h:530:9`!, _oscap_pcre_opts_to_pcrecxx . . $ _oscap_pcre_opts_to_pcre(396d139e3bb0bc80).(options), &errno, &erroffset2, NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!);
110 if (res->recxx . . $ oscap_pcre#re. == NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!) {
111 PCRE2_UCHAR8cxx . . $ PCRE2_UCHAR8# errmsg[PCRE2_ERR_BUF_SIZEcxx . . $ `src/common/oscap_pcre.c:32:9`!];
112 pcre2_get_error_message_8cxx . . $ pcre2_get_error_message_8(f8d5d22569f0f594).(errno, errmsg, PCRE2_ERR_BUF_SIZEcxx . . $ `src/common/oscap_pcre.c:32:9`!);
113 dWcxx . . $ `src/common/debug_priv.h:62:9`!("pcre2_compile_8: error (at offset %d): %s", erroffset2, errmsg);
114 *erroffset = erroffset2;
115 *errptr = strdupcxx . . $ strdup(a8b8e32347182212).((const char*)errmsg);
116 }
117
118 if (res->recxx . . $ oscap_pcre#re. == NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!) {
119 freecxx . . $ free(5d22bdacc48458e8).(res);
120 res = NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!;
121 }
122 return res;
123 }
124
125 void oscap_pcre_set_match_limit_recursioncxx . . $ oscap_pcre_set_match_limit_recursion(c2fa3d2ab1823f32).(oscap_pcre_tcxx . . $ oscap_pcre_t# *opcre, unsigned long limit)
126 {
127 if (opcre->re_ctxcxx . . $ oscap_pcre#re_ctx. == NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!) {
128 opcre->re_ctxcxx . . $ oscap_pcre#re_ctx. = pcre2_match_context_create_8cxx . . $ pcre2_match_context_create_8(a5c4a7fc42bac176).(NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!);
129 }
130 pcre2_set_depth_limit_8cxx . . $ pcre2_set_depth_limit_8(d937f51a527b8e4b).(opcre->re_ctxcxx . . $ oscap_pcre#re_ctx., limit);
131 }
132
133 int oscap_pcre_execcxx . . $ oscap_pcre_exec(7630dfd25fd504ec).(const oscap_pcre_tcxx . . $ oscap_pcre_t# *opcre, const char *subject,
134 int length, int startoffset, oscap_pcre_options_tcxx . . $ oscap_pcre_options_t# options,
135 int *ovector, int ovecsize)
136 {
137 int rc = 0;
138 // The ovecsize is multiplied by 3 in the code for compatibility with PCRE1
139 int ovecsize2 = ovecsize/3;
140 pcre2_match_data_8cxx . . $ pcre2_match_data_8# *mdata = pcre2_match_data_create_8cxx . . $ pcre2_match_data_create_8(ab40438157f82d90).(ovecsize2, NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!);
141 rc = pcre2_match_8cxx . . $ pcre2_match_8(53469719d8181bf4).(opcre->recxx . . $ oscap_pcre#re., (PCRE2_SPTR8cxx . . $ PCRE2_SPTR8#)subject, length, startoffset, _oscap_pcre_opts_to_pcrecxx . . $ _oscap_pcre_opts_to_pcre(396d139e3bb0bc80).(options), mdata, opcre->re_ctxcxx . . $ oscap_pcre#re_ctx.);
142 if (rc > PCRE2_ERROR_NOMATCHcxx . . $ `<external>/9988290459910441303/pcre2.h:349:9`!) {
143 PCRE2_SIZEcxx . . $ `<external>/9988290459910441303/pcre2.h:528:9`! *ovecp = pcre2_get_ovector_pointer_8cxx . . $ pcre2_get_ovector_pointer_8(2374b54008ed18b3).(mdata);
144 for (int i = 0; i < rc; i++) {
145 if (i < ovecsize2) {
146 ovector[i*2] = ovecp[i*2];
147 ovector[i*2+1] = ovecp[i*2+1];
148 }
149 }
150 }
151 pcre2_match_data_free_8cxx . . $ pcre2_match_data_free_8(f75948ac8632553).(mdata);
152
153 return rc >= 0 ? rc : _pcre_error_to_oscap_pcrecxx . . $ _pcre_error_to_oscap_pcre(85e69a8f33198fd).(rc);
154 }
155
156 void oscap_pcre_freecxx . . $ oscap_pcre_free(804bb72472b0846e).(oscap_pcre_tcxx . . $ oscap_pcre_t# *opcre)
157 {
158 if (opcre != NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!) {
159 if (opcre->re_ctxcxx . . $ oscap_pcre#re_ctx. != NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!)
160 pcre2_match_context_free_8cxx . . $ pcre2_match_context_free_8(ec338c98d9f55c18).(opcre->re_ctxcxx . . $ oscap_pcre#re_ctx.);
161 pcre2_code_free_8cxx . . $ pcre2_code_free_8(e74e306c389dfeb6).(opcre->recxx . . $ oscap_pcre#re.);
162 freecxx . . $ free(5d22bdacc48458e8).(opcre);
163 }
164 }
165
166 int oscap_pcre_get_substringscxx . . $ oscap_pcre_get_substrings(60fafe1d399fec14).(char *str, int *ofs, oscap_pcre_tcxx . . $ oscap_pcre_t# *re, int want_substrs, char ***substrings) {
167 int i, ret, rc;
168 int ovector[60], ovector_len = sizeof (ovector) / sizeof (ovector[0]);
169 char **substrs;
170
171 // todo: max match count check
172
173 for (i = 0; i < ovector_len; ++i) {
174 ovector[i] = -1;
175 }
176
177 unsigned long limit = OSCAP_PCRE_EXEC_RECURSION_LIMIT_DEFAULTcxx . . $ `src/common/oscap_pcre.c:29:9`!;
178 char *limit_str = getenvcxx . . $ getenv(a8b8e32347182212).("OSCAP_PCRE_EXEC_RECURSION_LIMIT");
179 if (limit_str != NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!)
180 if (sscanfcxx . . $ sscanf(5e8a2c12db2ea735).(limit_str, "%lu", &limit) <= 0)
181 dWcxx . . $ `src/common/debug_priv.h:62:9`!("Unable to parse OSCAP_PCRE_EXEC_RECURSION_LIMIT value");
182 oscap_pcre_set_match_limit_recursioncxx . . $ oscap_pcre_set_match_limit_recursion(c2fa3d2ab1823f32).(re, limit);
183 size_t str_len = strlencxx . . $ strlen(751346f8406fd082).(str);
184 #if defined(OS_SOLARIS)
185 rc = oscap_pcre_exec(re, str, str_len, *ofs, OSCAP_PCRE_OPTS_NO_UTF8_CHECK, ovector, ovector_len);
186 #else
187 rc = oscap_pcre_execcxx . . $ oscap_pcre_exec(7630dfd25fd504ec).(re, str, str_len, *ofs, 0, ovector, ovector_len);
188 #endif
189
190 if (rc < OSCAP_PCRE_ERR_NOMATCHcxx . . $ OSCAP_PCRE_ERR_NOMATCH.) {
191 if (str_len < 100)
192 dEcxx . . $ `src/common/debug_priv.h:63:9`!("Function oscap_pcre_exec() failed to match a regular expression with return code %d on string '%s'.", rc, str);
193 else
194 dEcxx . . $ `src/common/debug_priv.h:63:9`!("Function oscap_pcre_exec() failed to match a regular expression with return code %d on string '%.100s' (truncated, showing first 100 characters).", rc, str);
195 return rc;
196 } else if (rc == OSCAP_PCRE_ERR_NOMATCHcxx . . $ OSCAP_PCRE_ERR_NOMATCH.) {
197 /* no match */
198 return 0;
199 }
200
201 *ofs = (*ofs == ovector[1]) ? ovector[1] + 1 : ovector[1];
202
203 if (!want_substrs) {
204 /* just report successful match */
205 return 1;
206 }
207
208 ret = 0;
209 if (rc == 0) {
210 /* vector too small */
211 // todo: report partial results
212 rc = ovector_len / 3;
213 }
214
215 substrs = malloccxx . . $ malloc(bfcfa4d6b7f7ef64).(rc * sizeof (char *));
216 for (i = 0; i < rc; ++i) {
217 int len;
218 char *buf;
219
220 if (ovector[2 * i] == -1) {
221 continue;
222 }
223 len = ovector[2 * i + 1] - ovector[2 * i];
224 buf = malloccxx . . $ malloc(bfcfa4d6b7f7ef64).(len + 1);
225 memcpycxx . . $ memcpy(cc817cb175f282d).(buf, str + ovector[2 * i], len);
226 buf[len] = '\0';
227 substrs[ret] = buf;
228 ++ret;
229 }
230
231 *substrings = substrs;
232
233 return ret;
234 }
235
236 void oscap_pcre_err_freecxx . . $ oscap_pcre_err_free(fda57c8427c60377).(char *err)
237 {
238 if (err != NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!) {
239 freecxx . . $ free(5d22bdacc48458e8).(err);
240 }
241 }
242