xref: /aosp_15_r20/external/pcre/src/pcre2posix.c (revision 22dc650d8ae982c6770746019a6f94af92b0f024)
1 /*************************************************
2 *      Perl-Compatible Regular Expressions       *
3 *************************************************/
4 
5 /* PCRE is a library of functions to support regular expressions whose syntax
6 and semantics are as close as possible to those of the Perl 5 language.
7 
8                        Written by Philip Hazel
9      Original API code Copyright (c) 1997-2012 University of Cambridge
10           New API code Copyright (c) 2016-2022 University of Cambridge
11 
12 -----------------------------------------------------------------------------
13 Redistribution and use in source and binary forms, with or without
14 modification, are permitted provided that the following conditions are met:
15 
16     * Redistributions of source code must retain the above copyright notice,
17       this list of conditions and the following disclaimer.
18 
19     * Redistributions in binary form must reproduce the above copyright
20       notice, this list of conditions and the following disclaimer in the
21       documentation and/or other materials provided with the distribution.
22 
23     * Neither the name of the University of Cambridge nor the names of its
24       contributors may be used to endorse or promote products derived from
25       this software without specific prior written permission.
26 
27 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
28 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
29 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
30 ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
31 LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
32 CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
33 SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
34 INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
35 CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
36 ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
37 POSSIBILITY OF SUCH DAMAGE.
38 -----------------------------------------------------------------------------
39 */
40 
41 
42 /* This module is a wrapper that provides a POSIX API to the underlying PCRE2
43 functions. The functions are called pcre2_regcomp(), pcre2_regexec(), etc.
44 pcre2posix.h defines the POSIX names as macros for the corresonding pcre2_xxx
45 functions, so any program that includes it and uses the POSIX names will call
46 the PCRE2 implementations instead. */
47 
48 
49 #ifdef HAVE_CONFIG_H
50 #include "config.h"
51 #endif
52 
53 #ifdef PCRE2POSIX_SHARED
54 #undef PCRE2_STATIC
55 #endif
56 
57 
58 /* Ensure that the PCRE2POSIX_EXP_xxx macros are set appropriately for
59 compiling these functions. This must come before including pcre2posix.h, where
60 they are set for an application (using these functions) if they have not
61 previously been set. */
62 
63 #if defined(_WIN32) && (defined(PCRE2POSIX_SHARED) || !defined(PCRE2_STATIC))
64 #  define PCRE2POSIX_EXP_DECL extern __declspec(dllexport)
65 #  define PCRE2POSIX_EXP_DEFN __declspec(dllexport)
66 #endif
67 
68 /* Older versions of MSVC lack snprintf(). This define allows for
69 warning/error-free compilation and testing with MSVC compilers back to at least
70 MSVC 10/2010. Except for VC6 (which is missing some fundamentals and fails). */
71 
72 #if defined(_MSC_VER) && (_MSC_VER < 1900)
73 #define snprintf _snprintf
74 #define BROKEN_SNPRINTF
75 #endif
76 
77 
78 /* Compile-time error numbers start at this value. It should probably never be
79 changed. This #define is a copy of the one in pcre2_internal.h. */
80 
81 #define COMPILE_ERROR_BASE 100
82 
83 
84 /* Standard C headers */
85 
86 #include <ctype.h>
87 #include <limits.h>
88 #include <stddef.h>
89 #include <stdio.h>
90 #include <stdlib.h>
91 #include <string.h>
92 
93 /* PCRE2 headers */
94 
95 #include "pcre2.h"
96 #include "pcre2posix.h"
97 
98 /* Table to translate PCRE2 compile time error codes into POSIX error codes.
99 Only a few PCRE2 errors with a value greater than 23 turn into special POSIX
100 codes: most go to REG_BADPAT. The second table lists, in pairs, those that
101 don't. */
102 
103 static const int eint1[] = {
104   0,           /* No error */
105   REG_EESCAPE, /* \ at end of pattern */
106   REG_EESCAPE, /* \c at end of pattern */
107   REG_EESCAPE, /* unrecognized character follows \ */
108   REG_BADBR,   /* numbers out of order in {} quantifier */
109   /* 5 */
110   REG_BADBR,   /* number too big in {} quantifier */
111   REG_EBRACK,  /* missing terminating ] for character class */
112   REG_ECTYPE,  /* invalid escape sequence in character class */
113   REG_ERANGE,  /* range out of order in character class */
114   REG_BADRPT,  /* nothing to repeat */
115   /* 10 */
116   REG_ASSERT,  /* internal error: unexpected repeat */
117   REG_BADPAT,  /* unrecognized character after (? or (?- */
118   REG_BADPAT,  /* POSIX named classes are supported only within a class */
119   REG_BADPAT,  /* POSIX collating elements are not supported */
120   REG_EPAREN,  /* missing ) */
121   /* 15 */
122   REG_ESUBREG, /* reference to non-existent subpattern */
123   REG_INVARG,  /* pattern passed as NULL */
124   REG_INVARG,  /* unknown compile-time option bit(s) */
125   REG_EPAREN,  /* missing ) after (?# comment */
126   REG_ESIZE,   /* parentheses nested too deeply */
127   /* 20 */
128   REG_ESIZE,   /* regular expression too large */
129   REG_ESPACE,  /* failed to get memory */
130   REG_EPAREN,  /* unmatched closing parenthesis */
131   REG_ASSERT   /* internal error: code overflow */
132   };
133 
134 static const int eint2[] = {
135   30, REG_ECTYPE,  /* unknown POSIX class name */
136   32, REG_INVARG,  /* this version of PCRE2 does not have Unicode support */
137   37, REG_EESCAPE, /* PCRE2 does not support \L, \l, \N{name}, \U, or \u */
138   56, REG_INVARG,  /* internal error: unknown newline setting */
139   92, REG_INVARG,  /* invalid option bits with PCRE2_LITERAL */
140   99, REG_EESCAPE  /* \K in lookaround */
141 };
142 
143 /* Table of texts corresponding to POSIX error codes */
144 
145 static const char *const pstring[] = {
146   "",                                /* Dummy for value 0 */
147   "internal error",                  /* REG_ASSERT */
148   "invalid repeat counts in {}",     /* BADBR      */
149   "pattern error",                   /* BADPAT     */
150   "? * + invalid",                   /* BADRPT     */
151   "unbalanced {}",                   /* EBRACE     */
152   "unbalanced []",                   /* EBRACK     */
153   "collation error - not relevant",  /* ECOLLATE   */
154   "bad class",                       /* ECTYPE     */
155   "bad escape sequence",             /* EESCAPE    */
156   "empty expression",                /* EMPTY      */
157   "unbalanced ()",                   /* EPAREN     */
158   "bad range inside []",             /* ERANGE     */
159   "expression too big",              /* ESIZE      */
160   "failed to get memory",            /* ESPACE     */
161   "bad back reference",              /* ESUBREG    */
162   "bad argument",                    /* INVARG     */
163   "match failed"                     /* NOMATCH    */
164 };
165 
message_len(const char * message,int offset)166 static int message_len(const char *message, int offset)
167 {
168 char buf[12];
169 
170 /* 11 magic number comes from the format below */
171 return (int)strlen(message) + 11 + snprintf(buf, sizeof(buf), "%d", offset);
172 }
173 
174 /*************************************************
175 *          Translate error code to string        *
176 *************************************************/
177 
178 PCRE2POSIX_EXP_DEFN size_t PCRE2_CALL_CONVENTION
pcre2_regerror(int errcode,const regex_t * preg,char * errbuf,size_t errbuf_size)179 pcre2_regerror(int errcode, const regex_t *preg, char *errbuf,
180   size_t errbuf_size)
181 {
182 int ret;
183 const char *message;
184 size_t len = 0; /* keeps 0 if snprintf is used */
185 
186 message = (errcode <= 0 || errcode >= (int)(sizeof(pstring)/sizeof(char *)))?
187   "unknown error code" : pstring[errcode];
188 
189 if (preg != NULL && (int)preg->re_erroffset != -1)
190   {
191   /* no need to deal with UB in snprintf */
192   if (errbuf_size > INT_MAX) errbuf_size = INT_MAX;
193 
194   /* there are 11 charactes between message and offset,
195      update message_len() if changed */
196   ret = snprintf(errbuf, errbuf_size, "%s at offset %d", message,
197                  (int)preg->re_erroffset);
198   }
199 else
200   {
201   len = strlen(message);
202   if (errbuf_size != 0)
203     {
204     strncpy(errbuf, message, errbuf_size);
205     if (errbuf_size <= len) errbuf[errbuf_size - 1] = '\0';
206     }
207   ret = (int)len;
208   }
209 
210 do {
211   if (ret < 0)
212     {
213 #ifdef BROKEN_SNPRINTF
214     /* _snprintf returns -1 on overflow and doesn't zero terminate */
215     if (!len)
216       {
217       if (ret == -1 && errbuf_size != 0) errbuf[errbuf_size - 1] = '\0';
218 
219       ret = message_len(message, (int)preg->re_erroffset);
220       break;
221       }
222 #endif
223     /* snprintf failed, will use a 14 char long message if possible */
224     ret = 14;
225     if (errbuf_size != 0)
226       {
227       strncpy(errbuf, "internal error", errbuf_size);
228       if ((int)errbuf_size <= ret) errbuf[errbuf_size - 1] = '\0';
229       }
230     }
231   else if (ret == (int)errbuf_size && !len)
232     {
233       /* pre C99 snprintf returns used, so redo ret to fix that */
234 
235       ret = message_len(message, (int)preg->re_erroffset);
236     }
237 } while (0);
238 
239 return ret + 1;
240 }
241 
242 
243 
244 /*************************************************
245 *           Free store held by a regex           *
246 *************************************************/
247 
248 PCRE2POSIX_EXP_DEFN void PCRE2_CALL_CONVENTION
pcre2_regfree(regex_t * preg)249 pcre2_regfree(regex_t *preg)
250 {
251 pcre2_match_data_free(preg->re_match_data);
252 pcre2_code_free(preg->re_pcre2_code);
253 }
254 
255 
256 
257 /*************************************************
258 *            Compile a regular expression        *
259 *************************************************/
260 
261 /*
262 Arguments:
263   preg        points to a structure for recording the compiled expression
264   pattern     the pattern to compile
265   cflags      compilation flags
266 
267 Returns:      0 on success
268               various non-zero codes on failure
269 */
270 
271 PCRE2POSIX_EXP_DEFN int PCRE2_CALL_CONVENTION
pcre2_regcomp(regex_t * preg,const char * pattern,int cflags)272 pcre2_regcomp(regex_t *preg, const char *pattern, int cflags)
273 {
274 PCRE2_SIZE erroffset;
275 PCRE2_SIZE patlen;
276 int errorcode;
277 int options = 0;
278 int re_nsub = 0;
279 
280 patlen = ((cflags & REG_PEND) != 0)? (PCRE2_SIZE)(preg->re_endp - pattern) :
281   PCRE2_ZERO_TERMINATED;
282 
283 if ((cflags & REG_ICASE) != 0)    options |= PCRE2_CASELESS;
284 if ((cflags & REG_NEWLINE) != 0)  options |= PCRE2_MULTILINE;
285 if ((cflags & REG_DOTALL) != 0)   options |= PCRE2_DOTALL;
286 if ((cflags & REG_NOSPEC) != 0)   options |= PCRE2_LITERAL;
287 if ((cflags & REG_UTF) != 0)      options |= PCRE2_UTF;
288 if ((cflags & REG_UCP) != 0)      options |= PCRE2_UCP;
289 if ((cflags & REG_UNGREEDY) != 0) options |= PCRE2_UNGREEDY;
290 
291 preg->re_cflags = cflags;
292 preg->re_pcre2_code = pcre2_compile((PCRE2_SPTR)pattern, patlen, options,
293   &errorcode, &erroffset, NULL);
294 preg->re_erroffset = erroffset;
295 
296 if (preg->re_pcre2_code == NULL)
297   {
298   unsigned int i;
299 
300   /* A negative value is a UTF error; otherwise all error codes are greater
301   than COMPILE_ERROR_BASE, but check, just in case. */
302 
303   if (errorcode < COMPILE_ERROR_BASE) return REG_BADPAT;
304   errorcode -= COMPILE_ERROR_BASE;
305 
306   if (errorcode < (int)(sizeof(eint1)/sizeof(const int)))
307     return eint1[errorcode];
308   for (i = 0; i < sizeof(eint2)/sizeof(const int); i += 2)
309     if (errorcode == eint2[i]) return eint2[i+1];
310   return REG_BADPAT;
311   }
312 
313 (void)pcre2_pattern_info((const pcre2_code *)preg->re_pcre2_code,
314   PCRE2_INFO_CAPTURECOUNT, &re_nsub);
315 preg->re_nsub = (size_t)re_nsub;
316 preg->re_match_data = pcre2_match_data_create(re_nsub + 1, NULL);
317 preg->re_erroffset = (size_t)(-1);  /* No meaning after successful compile */
318 
319 if (preg->re_match_data == NULL)
320   {
321   /* LCOV_EXCL_START */
322   pcre2_code_free(preg->re_pcre2_code);
323   return REG_ESPACE;
324   /* LCOV_EXCL_STOP */
325   }
326 
327 return 0;
328 }
329 
330 
331 
332 /*************************************************
333 *              Match a regular expression        *
334 *************************************************/
335 
336 /* A suitable match_data block, large enough to hold all possible captures, was
337 obtained when the pattern was compiled, to save having to allocate and free it
338 for each match. If REG_NOSUB was specified at compile time, the nmatch and
339 pmatch arguments are ignored, and the only result is yes/no/error. */
340 
341 PCRE2POSIX_EXP_DEFN int PCRE2_CALL_CONVENTION
pcre2_regexec(const regex_t * preg,const char * string,size_t nmatch,regmatch_t pmatch[],int eflags)342 pcre2_regexec(const regex_t *preg, const char *string, size_t nmatch,
343   regmatch_t pmatch[], int eflags)
344 {
345 int rc, so, eo;
346 int options = 0;
347 pcre2_match_data *md = (pcre2_match_data *)preg->re_match_data;
348 
349 if (string == NULL) return REG_INVARG;
350 
351 if ((eflags & REG_NOTBOL) != 0) options |= PCRE2_NOTBOL;
352 if ((eflags & REG_NOTEOL) != 0) options |= PCRE2_NOTEOL;
353 if ((eflags & REG_NOTEMPTY) != 0) options |= PCRE2_NOTEMPTY;
354 
355 /* When REG_NOSUB was specified, or if no vector has been passed in which to
356 put captured strings, ensure that nmatch is zero. This will stop any attempt to
357 write to pmatch. */
358 
359 if ((preg->re_cflags & REG_NOSUB) != 0 || pmatch == NULL) nmatch = 0;
360 
361 /* REG_STARTEND is a BSD extension, to allow for non-NUL-terminated strings.
362 The man page from OS X says "REG_STARTEND affects only the location of the
363 string, not how it is matched". That is why the "so" value is used to bump the
364 start location rather than being passed as a PCRE2 "starting offset". */
365 
366 if ((eflags & REG_STARTEND) != 0)
367   {
368   if (pmatch == NULL) return REG_INVARG;
369   so = pmatch[0].rm_so;
370   eo = pmatch[0].rm_eo;
371   }
372 else
373   {
374   so = 0;
375   eo = (int)strlen(string);
376   }
377 
378 rc = pcre2_match((const pcre2_code *)preg->re_pcre2_code,
379   (PCRE2_SPTR)string + so, (eo - so), 0, options, md, NULL);
380 
381 /* Successful match */
382 
383 if (rc >= 0)
384   {
385   size_t i;
386   PCRE2_SIZE *ovector = pcre2_get_ovector_pointer(md);
387   if ((size_t)rc > nmatch) rc = (int)nmatch;
388   for (i = 0; i < (size_t)rc; i++)
389     {
390     pmatch[i].rm_so = (ovector[i*2] == PCRE2_UNSET)? -1 :
391       (int)(ovector[i*2] + so);
392     pmatch[i].rm_eo = (ovector[i*2+1] == PCRE2_UNSET)? -1 :
393       (int)(ovector[i*2+1] + so);
394     }
395   for (; i < nmatch; i++) pmatch[i].rm_so = pmatch[i].rm_eo = -1;
396   return 0;
397   }
398 
399 /* Unsuccessful match */
400 
401 if (rc <= PCRE2_ERROR_UTF8_ERR1 && rc >= PCRE2_ERROR_UTF8_ERR21)
402   return REG_INVARG;
403 
404 /* Most of these are events that won't occur during testing, so exclude them
405 from coverage. */
406 
407 switch(rc)
408   {
409   case PCRE2_ERROR_HEAPLIMIT: return REG_ESPACE;
410   case PCRE2_ERROR_NOMATCH: return REG_NOMATCH;
411 
412   /* LCOV_EXCL_START */
413   case PCRE2_ERROR_BADMODE: return REG_INVARG;
414   case PCRE2_ERROR_BADMAGIC: return REG_INVARG;
415   case PCRE2_ERROR_BADOPTION: return REG_INVARG;
416   case PCRE2_ERROR_BADUTFOFFSET: return REG_INVARG;
417   case PCRE2_ERROR_MATCHLIMIT: return REG_ESPACE;
418   case PCRE2_ERROR_NOMEMORY: return REG_ESPACE;
419   case PCRE2_ERROR_NULL: return REG_INVARG;
420   default: return REG_ASSERT;
421   /* LCOV_EXCL_STOP */
422   }
423 }
424 
425 /* End of pcre2posix.c */
426