Bug Summary

File:build/llvm-toolchain-snapshot-16~++20221003111214+1fa2019828ca/llvm/lib/Support/regcomp.c
Warning:line 1664, column 10
Dereference of null pointer

Annotated Source Code

Press '?' to see keyboard shortcuts

clang -cc1 -cc1 -triple x86_64-pc-linux-gnu -analyze -disable-free -clear-ast-before-backend -disable-llvm-verifier -discard-value-names -main-file-name regcomp.c -analyzer-checker=core -analyzer-checker=apiModeling -analyzer-checker=unix -analyzer-checker=deadcode -analyzer-checker=security.insecureAPI.UncheckedReturn -analyzer-checker=security.insecureAPI.getpw -analyzer-checker=security.insecureAPI.gets -analyzer-checker=security.insecureAPI.mktemp -analyzer-checker=security.insecureAPI.mkstemp -analyzer-checker=security.insecureAPI.vfork -analyzer-checker=nullability.NullPassedToNonnull -analyzer-checker=nullability.NullReturnedFromNonnull -analyzer-output plist -w -setup-static-analyzer -analyzer-config-compatibility-mode=true -mrelocation-model pic -pic-level 2 -mframe-pointer=none -fmath-errno -ffp-contract=on -fno-rounding-math -mconstructor-aliases -funwind-tables=2 -target-cpu x86-64 -tune-cpu generic -debugger-tuning=gdb -ffunction-sections -fdata-sections -fcoverage-compilation-dir=/build/llvm-toolchain-snapshot-16~++20221003111214+1fa2019828ca/build-llvm -resource-dir /usr/lib/llvm-16/lib/clang/16.0.0 -D _DEBUG -D _GNU_SOURCE -D __STDC_CONSTANT_MACROS -D __STDC_FORMAT_MACROS -D __STDC_LIMIT_MACROS -I lib/Support -I /build/llvm-toolchain-snapshot-16~++20221003111214+1fa2019828ca/llvm/lib/Support -I include -I /build/llvm-toolchain-snapshot-16~++20221003111214+1fa2019828ca/llvm/include -D _FORTIFY_SOURCE=2 -D NDEBUG -U NDEBUG -internal-isystem /usr/lib/llvm-16/lib/clang/16.0.0/include -internal-isystem /usr/local/include -internal-isystem /usr/lib/gcc/x86_64-linux-gnu/10/../../../../x86_64-linux-gnu/include -internal-externc-isystem /usr/include/x86_64-linux-gnu -internal-externc-isystem /include -internal-externc-isystem /usr/include -fmacro-prefix-map=/build/llvm-toolchain-snapshot-16~++20221003111214+1fa2019828ca/build-llvm=build-llvm -fmacro-prefix-map=/build/llvm-toolchain-snapshot-16~++20221003111214+1fa2019828ca/= -fcoverage-prefix-map=/build/llvm-toolchain-snapshot-16~++20221003111214+1fa2019828ca/build-llvm=build-llvm -fcoverage-prefix-map=/build/llvm-toolchain-snapshot-16~++20221003111214+1fa2019828ca/= -O3 -Wno-unused-command-line-argument -Wno-unused-parameter -Wwrite-strings -Wno-missing-field-initializers -Wno-long-long -Wno-comment -fconst-strings -fdebug-compilation-dir=/build/llvm-toolchain-snapshot-16~++20221003111214+1fa2019828ca/build-llvm -fdebug-prefix-map=/build/llvm-toolchain-snapshot-16~++20221003111214+1fa2019828ca/build-llvm=build-llvm -fdebug-prefix-map=/build/llvm-toolchain-snapshot-16~++20221003111214+1fa2019828ca/= -ferror-limit 19 -stack-protector 2 -fgnuc-version=4.2.1 -fcolor-diagnostics -vectorize-loops -vectorize-slp -analyzer-output=html -analyzer-config stable-report-filename=true -faddrsig -D__GCC_HAVE_DWARF2_CFI_ASM=1 -o /tmp/scan-build-2022-10-03-140002-15933-1 -x c /build/llvm-toolchain-snapshot-16~++20221003111214+1fa2019828ca/llvm/lib/Support/regcomp.c
1/*-
2 * This code is derived from OpenBSD's libc/regex, original license follows:
3 *
4 * Copyright (c) 1992, 1993, 1994 Henry Spencer.
5 * Copyright (c) 1992, 1993, 1994
6 * The Regents of the University of California. All rights reserved.
7 *
8 * This code is derived from software contributed to Berkeley by
9 * Henry Spencer.
10 *
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
13 * are met:
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
19 * 3. Neither the name of the University nor the names of its contributors
20 * may be used to endorse or promote products derived from this software
21 * without specific prior written permission.
22 *
23 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 * SUCH DAMAGE.
34 *
35 * @(#)regcomp.c 8.5 (Berkeley) 3/20/94
36 */
37
38#include <sys/types.h>
39#include <stdint.h>
40#include <stdio.h>
41#include <string.h>
42#include <ctype.h>
43#include <limits.h>
44#include <stdlib.h>
45#include "regex_impl.h"
46
47#include "regutils.h"
48#include "regex2.h"
49
50#include "llvm/Config/config.h"
51#include "llvm/Support/Compiler.h"
52
53/* character-class table */
54static struct cclass {
55 const char *name;
56 const char *chars;
57 const char *multis;
58} cclasses[] = {
59 { "alnum", "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz\
600123456789", ""} ,
61 { "alpha", "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz",
62 ""} ,
63 { "blank", " \t", ""} ,
64 { "cntrl", "\007\b\t\n\v\f\r\1\2\3\4\5\6\16\17\20\21\22\23\24\
65\25\26\27\30\31\32\33\34\35\36\37\177", ""} ,
66 { "digit", "0123456789", ""} ,
67 { "graph", "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz\
680123456789!\"#$%&'()*+,-./:;<=>?@[\\]^_`{|}~",
69 ""} ,
70 { "lower", "abcdefghijklmnopqrstuvwxyz",
71 ""} ,
72 { "print", "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz\
730123456789!\"#$%&'()*+,-./:;<=>?@[\\]^_`{|}~ ",
74 ""} ,
75 { "punct", "!\"#$%&'()*+,-./:;<=>?@[\\]^_`{|}~",
76 ""} ,
77 { "space", "\t\n\v\f\r ", ""} ,
78 { "upper", "ABCDEFGHIJKLMNOPQRSTUVWXYZ",
79 ""} ,
80 { "xdigit", "0123456789ABCDEFabcdef",
81 ""} ,
82 { NULL((void*)0), 0, "" }
83};
84
85/* character-name table */
86static struct cname {
87 const char *name;
88 char code;
89} cnames[] = {
90 { "NUL", '\0' },
91 { "SOH", '\001' },
92 { "STX", '\002' },
93 { "ETX", '\003' },
94 { "EOT", '\004' },
95 { "ENQ", '\005' },
96 { "ACK", '\006' },
97 { "BEL", '\007' },
98 { "alert", '\007' },
99 { "BS", '\010' },
100 { "backspace", '\b' },
101 { "HT", '\011' },
102 { "tab", '\t' },
103 { "LF", '\012' },
104 { "newline", '\n' },
105 { "VT", '\013' },
106 { "vertical-tab", '\v' },
107 { "FF", '\014' },
108 { "form-feed", '\f' },
109 { "CR", '\015' },
110 { "carriage-return", '\r' },
111 { "SO", '\016' },
112 { "SI", '\017' },
113 { "DLE", '\020' },
114 { "DC1", '\021' },
115 { "DC2", '\022' },
116 { "DC3", '\023' },
117 { "DC4", '\024' },
118 { "NAK", '\025' },
119 { "SYN", '\026' },
120 { "ETB", '\027' },
121 { "CAN", '\030' },
122 { "EM", '\031' },
123 { "SUB", '\032' },
124 { "ESC", '\033' },
125 { "IS4", '\034' },
126 { "FS", '\034' },
127 { "IS3", '\035' },
128 { "GS", '\035' },
129 { "IS2", '\036' },
130 { "RS", '\036' },
131 { "IS1", '\037' },
132 { "US", '\037' },
133 { "space", ' ' },
134 { "exclamation-mark", '!' },
135 { "quotation-mark", '"' },
136 { "number-sign", '#' },
137 { "dollar-sign", '$' },
138 { "percent-sign", '%' },
139 { "ampersand", '&' },
140 { "apostrophe", '\'' },
141 { "left-parenthesis", '(' },
142 { "right-parenthesis", ')' },
143 { "asterisk", '*' },
144 { "plus-sign", '+' },
145 { "comma", ',' },
146 { "hyphen", '-' },
147 { "hyphen-minus", '-' },
148 { "period", '.' },
149 { "full-stop", '.' },
150 { "slash", '/' },
151 { "solidus", '/' },
152 { "zero", '0' },
153 { "one", '1' },
154 { "two", '2' },
155 { "three", '3' },
156 { "four", '4' },
157 { "five", '5' },
158 { "six", '6' },
159 { "seven", '7' },
160 { "eight", '8' },
161 { "nine", '9' },
162 { "colon", ':' },
163 { "semicolon", ';' },
164 { "less-than-sign", '<' },
165 { "equals-sign", '=' },
166 { "greater-than-sign", '>' },
167 { "question-mark", '?' },
168 { "commercial-at", '@' },
169 { "left-square-bracket", '[' },
170 { "backslash", '\\' },
171 { "reverse-solidus", '\\' },
172 { "right-square-bracket", ']' },
173 { "circumflex", '^' },
174 { "circumflex-accent", '^' },
175 { "underscore", '_' },
176 { "low-line", '_' },
177 { "grave-accent", '`' },
178 { "left-brace", '{' },
179 { "left-curly-bracket", '{' },
180 { "vertical-line", '|' },
181 { "right-brace", '}' },
182 { "right-curly-bracket", '}' },
183 { "tilde", '~' },
184 { "DEL", '\177' },
185 { NULL((void*)0), 0 }
186};
187
188/*
189 * parse structure, passed up and down to avoid global variables and
190 * other clumsinesses
191 */
192struct parse {
193 char *next; /* next character in RE */
194 char *end; /* end of string (-> NUL normally) */
195 int error; /* has an error been seen? */
196 sop *strip; /* malloced strip */
197 sopno ssize; /* malloced strip size (allocated) */
198 sopno slen; /* malloced strip length (used) */
199 int ncsalloc; /* number of csets allocated */
200 struct re_guts *g;
201# define NPAREN10 10 /* we need to remember () 1-9 for back refs */
202 sopno pbegin[NPAREN10]; /* -> ( ([0] unused) */
203 sopno pend[NPAREN10]; /* -> ) ([0] unused) */
204};
205
206static void p_ere(struct parse *, int);
207static void p_ere_exp(struct parse *);
208static void p_str(struct parse *);
209static void p_bre(struct parse *, int, int);
210static int p_simp_re(struct parse *, int);
211static int p_count(struct parse *);
212static void p_bracket(struct parse *);
213static void p_b_term(struct parse *, cset *);
214static void p_b_cclass(struct parse *, cset *);
215static void p_b_eclass(struct parse *, cset *);
216static char p_b_symbol(struct parse *);
217static char p_b_coll_elem(struct parse *, int);
218static char othercase(int);
219static void bothcases(struct parse *, int);
220static void ordinary(struct parse *, int);
221static void nonnewline(struct parse *);
222static void repeat(struct parse *, sopno, int, int);
223static int seterr(struct parse *, int);
224static cset *allocset(struct parse *);
225static void freeset(struct parse *, cset *);
226static int freezeset(struct parse *, cset *);
227static int firstch(struct parse *, cset *);
228static int nch(struct parse *, cset *);
229static void mcadd(struct parse *, cset *, const char *);
230static void mcinvert(struct parse *, cset *);
231static void mccase(struct parse *, cset *);
232static int isinsets(struct re_guts *, int);
233static int samesets(struct re_guts *, int, int);
234static void categorize(struct parse *, struct re_guts *);
235static sopno dupl(struct parse *, sopno, sopno);
236static void doemit(struct parse *, sop, size_t);
237static void doinsert(struct parse *, sop, size_t, sopno);
238static void dofwd(struct parse *, sopno, sop);
239static void enlarge(struct parse *, sopno);
240static void stripsnug(struct parse *, struct re_guts *);
241static void findmust(struct parse *, struct re_guts *);
242static sopno pluscount(struct parse *, struct re_guts *);
243
244static char nuls[10]; /* place to point scanner in event of error */
245
246/*
247 * macros for use with parse structure
248 * BEWARE: these know that the parse structure is named `p' !!!
249 */
250#define PEEK()(*p->next) (*p->next)
251#define PEEK2()(*(p->next+1)) (*(p->next+1))
252#define MORE()(p->end - p->next > 0) (p->end - p->next > 0)
253#define MORE2()(p->end - p->next > 1) (p->end - p->next > 1)
254#define SEE(c)((p->end - p->next > 0) && (*p->next) == (
c))
(MORE()(p->end - p->next > 0) && PEEK()(*p->next) == (c))
255#define SEETWO(a, b)((p->end - p->next > 1) && (*p->next) == (
a) && (*(p->next+1)) == (b))
(MORE2()(p->end - p->next > 1) && PEEK()(*p->next) == (a) && PEEK2()(*(p->next+1)) == (b))
256#define EAT(c)((((p->end - p->next > 0) && (*p->next) ==
(c))) ? ((p->next++), 1) : 0)
((SEE(c)((p->end - p->next > 0) && (*p->next) == (
c))
) ? (NEXT()(p->next++), 1) : 0)
257#define EATTWO(a, b)((((p->end - p->next > 1) && (*p->next) ==
(a) && (*(p->next+1)) == (b))) ? ((p->next += 2
), 1) : 0)
((SEETWO(a, b)((p->end - p->next > 1) && (*p->next) == (
a) && (*(p->next+1)) == (b))
) ? (NEXT2()(p->next += 2), 1) : 0)
258#define NEXT()(p->next++) (p->next++)
259#define NEXT2()(p->next += 2) (p->next += 2)
260#define NEXTn(n)(p->next += (n)) (p->next += (n))
261#define GETNEXT()(*p->next++) (*p->next++)
262#define SETERROR(e)seterr(p, (e)) seterr(p, (e))
263#define REQUIRE(co, e)(void)((co) || seterr(p, (e))) (void)((co) || SETERROR(e)seterr(p, (e)))
264#define MUSTSEE(c, e)((void)(((p->end - p->next > 0) && (*p->next
) == (c)) || seterr(p, (e))))
(REQUIRE(MORE() && PEEK() == (c), e)(void)(((p->end - p->next > 0) && (*p->next
) == (c)) || seterr(p, (e)))
)
265#define MUSTEAT(c, e)((void)(((p->end - p->next > 0) && (*p->next
++) == (c)) || seterr(p, (e))))
(REQUIRE(MORE() && GETNEXT() == (c), e)(void)(((p->end - p->next > 0) && (*p->next
++) == (c)) || seterr(p, (e)))
)
266#define MUSTNOTSEE(c, e)((void)((!(p->end - p->next > 0) || (*p->next) !=
(c)) || seterr(p, (e))))
(REQUIRE(!MORE() || PEEK() != (c), e)(void)((!(p->end - p->next > 0) || (*p->next) != (
c)) || seterr(p, (e)))
)
267#define EMIT(op, sopnd)doemit(p, (sop)(op), (size_t)(sopnd)) doemit(p, (sop)(op), (size_t)(sopnd))
268#define INSERT(op, pos)doinsert(p, (sop)(op), (p->slen)-(pos)+1, pos) doinsert(p, (sop)(op), HERE()(p->slen)-(pos)+1, pos)
269#define AHEAD(pos)dofwd(p, pos, (p->slen)-(pos)) dofwd(p, pos, HERE()(p->slen)-(pos))
270#define ASTERN(sop, pos)doemit(p, (sop)(sop), (size_t)((p->slen)-pos)) EMIT(sop, HERE()-pos)doemit(p, (sop)(sop), (size_t)((p->slen)-pos))
271#define HERE()(p->slen) (p->slen)
272#define THERE()(p->slen - 1) (p->slen - 1)
273#define THERETHERE()(p->slen - 2) (p->slen - 2)
274#define DROP(n)(p->slen -= (n)) (p->slen -= (n))
275
276#ifdef _POSIX2_RE_DUP_MAX255
277#define DUPMAX255 _POSIX2_RE_DUP_MAX255
278#else
279#define DUPMAX255 255
280#endif
281#define INFINITY(255 + 1) (DUPMAX255 + 1)
282
283#ifndef NDEBUG
284static int never0 = 0; /* for use in asserts; shuts lint up */
285#else
286#define never0 0 /* some <assert.h>s have bugs too */
287#endif
288
289/*
290 - llvm_regcomp - interface for parser and compilation
291 */
292int /* 0 success, otherwise REG_something */
293llvm_regcomp(llvm_regex_t *preg, const char *pattern, int cflags)
294{
295 struct parse pa;
296 struct re_guts *g;
297 struct parse *p = &pa;
298 int i;
299 size_t len;
300#ifdef REDEBUG
301# define GOODFLAGS(f)((f)&~0200) (f)
302#else
303# define GOODFLAGS(f)((f)&~0200) ((f)&~REG_DUMP0200)
304#endif
305
306 cflags = GOODFLAGS(cflags)((cflags)&~0200);
307 if ((cflags&REG_EXTENDED0001) && (cflags&REG_NOSPEC0020))
308 return(REG_INVARG16);
309
310 if (cflags&REG_PEND0040) {
311 if (preg->re_endp < pattern)
312 return(REG_INVARG16);
313 len = preg->re_endp - pattern;
314 } else
315 len = strlen((const char *)pattern);
316
317 /* do the mallocs early so failure handling is easy */
318 g = (struct re_guts *)malloc(sizeof(struct re_guts) +
319 (NC(127 - (-127 -1) + 1)-1)*sizeof(cat_t));
320 if (g == NULL((void*)0))
321 return(REG_ESPACE12);
322 p->ssize = len/(size_t)2*(size_t)3 + (size_t)1; /* ugh */
323 p->strip = (sop *)calloc(p->ssize, sizeof(sop));
324 p->slen = 0;
325 if (p->strip == NULL((void*)0)) {
326 free((char *)g);
327 return(REG_ESPACE12);
328 }
329
330 /* set things up */
331 p->g = g;
332 p->next = (char *)pattern; /* convenience; we do not modify it */
333 p->end = p->next + len;
334 p->error = 0;
335 p->ncsalloc = 0;
336 for (i = 0; i < NPAREN10; i++) {
337 p->pbegin[i] = 0;
338 p->pend[i] = 0;
339 }
340 g->csetsize = NC(127 - (-127 -1) + 1);
341 g->sets = NULL((void*)0);
342 g->setbits = NULL((void*)0);
343 g->ncsets = 0;
344 g->cflags = cflags;
345 g->iflags = 0;
346 g->nbol = 0;
347 g->neol = 0;
348 g->must = NULL((void*)0);
349 g->mlen = 0;
350 g->nsub = 0;
351 g->ncategories = 1; /* category 0 is "everything else" */
352 g->categories = &g->catspace[-(CHAR_MIN(-127 -1))];
353 (void) memset((char *)g->catspace, 0, NC(127 - (-127 -1) + 1)*sizeof(cat_t));
354 g->backrefs = 0;
355
356 /* do it */
357 EMIT(OEND, 0)doemit(p, (sop)((1LU<<((unsigned)27))), (size_t)(0));
358 g->firststate = THERE()(p->slen - 1);
359 if (cflags&REG_EXTENDED0001)
360 p_ere(p, OUT(127 +1));
361 else if (cflags&REG_NOSPEC0020)
362 p_str(p);
363 else
364 p_bre(p, OUT(127 +1), OUT(127 +1));
365 EMIT(OEND, 0)doemit(p, (sop)((1LU<<((unsigned)27))), (size_t)(0));
366 g->laststate = THERE()(p->slen - 1);
367
368 /* tidy up loose ends and fill things in */
369 categorize(p, g);
370 stripsnug(p, g);
371 findmust(p, g);
372 g->nplus = pluscount(p, g);
373 g->magic = MAGIC2((('R'^0200)<<8)|'E');
374 preg->re_nsub = g->nsub;
375 preg->re_g = g;
376 preg->re_magic = MAGIC1((('r'^0200)<<8) | 'e');
377#ifndef REDEBUG
378 /* not debugging, so can't rely on the assert() in llvm_regexec() */
379 if (g->iflags&REGEX_BAD04)
380 SETERROR(REG_ASSERT)seterr(p, (15));
381#endif
382
383 /* win or lose, we're done */
384 if (p->error != 0) /* lose */
385 llvm_regfree(preg);
386 return(p->error);
387}
388
389/*
390 - p_ere - ERE parser top level, concatenation and alternation
391 */
392static void
393p_ere(struct parse *p, int stop) /* character this ERE should end at */
394{
395 char c;
396 sopno prevback = 0;
397 sopno prevfwd = 0;
398 sopno conc;
399 int first = 1; /* is this the first alternative? */
400
401 for (;;) {
402 /* do a bunch of concatenated expressions */
403 conc = HERE()(p->slen);
404 while (MORE()(p->end - p->next > 0) && (c = PEEK()(*p->next)) != '|' && c != stop)
405 p_ere_exp(p);
406 REQUIRE(HERE() != conc, REG_EMPTY)(void)(((p->slen) != conc) || seterr(p, (14))); /* require nonempty */
407
408 if (!EAT('|')((((p->end - p->next > 0) && (*p->next) ==
('|'))) ? ((p->next++), 1) : 0)
)
409 break; /* NOTE BREAK OUT */
410
411 if (first) {
412 INSERT(OCH_, conc)doinsert(p, (sop)((15LU<<((unsigned)27))), (p->slen)
-(conc)+1, conc)
; /* offset is wrong */
413 prevfwd = conc;
414 prevback = conc;
415 first = 0;
416 }
417 ASTERN(OOR1, prevback)doemit(p, (sop)((16LU<<((unsigned)27))), (size_t)((p->
slen)-prevback))
;
418 prevback = THERE()(p->slen - 1);
419 AHEAD(prevfwd)dofwd(p, prevfwd, (p->slen)-(prevfwd)); /* fix previous offset */
420 prevfwd = HERE()(p->slen);
421 EMIT(OOR2, 0)doemit(p, (sop)((17LU<<((unsigned)27))), (size_t)(0)); /* offset is very wrong */
422 }
423
424 if (!first) { /* tail-end fixups */
425 AHEAD(prevfwd)dofwd(p, prevfwd, (p->slen)-(prevfwd));
426 ASTERN(O_CH, prevback)doemit(p, (sop)((18LU<<((unsigned)27))), (size_t)((p->
slen)-prevback))
;
427 }
428
429 assert(!MORE() || SEE(stop))((void) (0));
430}
431
432/*
433 - p_ere_exp - parse one subERE, an atom possibly followed by a repetition op
434 */
435static void
436p_ere_exp(struct parse *p)
437{
438 char c;
439 sopno pos;
440 int count;
441 int count2;
442 int backrefnum;
443 sopno subno;
444 int wascaret = 0;
445
446 assert(MORE())((void) (0)); /* caller should have ensured this */
447 c = GETNEXT()(*p->next++);
448
449 pos = HERE()(p->slen);
450 switch (c) {
451 case '(':
452 REQUIRE(MORE(), REG_EPAREN)(void)(((p->end - p->next > 0)) || seterr(p, (8)));
453 p->g->nsub++;
454 subno = p->g->nsub;
455 if (subno < NPAREN10)
456 p->pbegin[subno] = HERE()(p->slen);
457 EMIT(OLPAREN, subno)doemit(p, (sop)((13LU<<((unsigned)27))), (size_t)(subno
))
;
458 if (!SEE(')')((p->end - p->next > 0) && (*p->next) == (
')'))
)
459 p_ere(p, ')');
460 if (subno < NPAREN10) {
461 p->pend[subno] = HERE()(p->slen);
462 assert(p->pend[subno] != 0)((void) (0));
463 }
464 EMIT(ORPAREN, subno)doemit(p, (sop)((14LU<<((unsigned)27))), (size_t)(subno
))
;
465 MUSTEAT(')', REG_EPAREN)((void)(((p->end - p->next > 0) && (*p->next
++) == (')')) || seterr(p, (8))))
;
466 break;
467#ifndef POSIX_MISTAKE
468 case ')': /* happens only if no current unmatched ( */
469 /*
470 * You may ask, why the ifndef? Because I didn't notice
471 * this until slightly too late for 1003.2, and none of the
472 * other 1003.2 regular-expression reviewers noticed it at
473 * all. So an unmatched ) is legal POSIX, at least until
474 * we can get it fixed.
475 */
476 SETERROR(REG_EPAREN)seterr(p, (8));
477 break;
478#endif
479 case '^':
480 EMIT(OBOL, 0)doemit(p, (sop)((3LU<<((unsigned)27))), (size_t)(0));
481 p->g->iflags |= USEBOL01;
482 p->g->nbol++;
483 wascaret = 1;
484 break;
485 case '$':
486 EMIT(OEOL, 0)doemit(p, (sop)((4LU<<((unsigned)27))), (size_t)(0));
487 p->g->iflags |= USEEOL02;
488 p->g->neol++;
489 break;
490 case '|':
491 SETERROR(REG_EMPTY)seterr(p, (14));
492 break;
493 case '*':
494 case '+':
495 case '?':
496 SETERROR(REG_BADRPT)seterr(p, (13));
497 break;
498 case '.':
499 if (p->g->cflags&REG_NEWLINE0010)
500 nonnewline(p);
501 else
502 EMIT(OANY, 0)doemit(p, (sop)((5LU<<((unsigned)27))), (size_t)(0));
503 break;
504 case '[':
505 p_bracket(p);
506 break;
507 case '\\':
508 REQUIRE(MORE(), REG_EESCAPE)(void)(((p->end - p->next > 0)) || seterr(p, (5)));
509 c = GETNEXT()(*p->next++);
510 if (c >= '1' && c <= '9') {
511 /* \[0-9] is taken to be a back-reference to a previously specified
512 * matching group. backrefnum will hold the number. The matching
513 * group must exist (i.e. if \4 is found there must have been at
514 * least 4 matching groups specified in the pattern previously).
515 */
516 backrefnum = c - '0';
517 if (p->pend[backrefnum] == 0) {
518 SETERROR(REG_ESUBREG)seterr(p, (6));
519 break;
520 }
521
522 /* Make sure everything checks out and emit the sequence
523 * that marks a back-reference to the parse structure.
524 */
525 assert(backrefnum <= p->g->nsub)((void) (0));
526 EMIT(OBACK_, backrefnum)doemit(p, (sop)((7LU<<((unsigned)27))), (size_t)(backrefnum
))
;
527 assert(p->pbegin[backrefnum] != 0)((void) (0));
528 assert(OP(p->strip[p->pbegin[backrefnum]]) != OLPAREN)((void) (0));
529 assert(OP(p->strip[p->pend[backrefnum]]) != ORPAREN)((void) (0));
530 (void) dupl(p, p->pbegin[backrefnum]+1, p->pend[backrefnum]);
531 EMIT(O_BACK, backrefnum)doemit(p, (sop)((8LU<<((unsigned)27))), (size_t)(backrefnum
))
;
532 p->g->backrefs = 1;
533 } else {
534 /* Other chars are simply themselves when escaped with a backslash.
535 */
536 ordinary(p, c);
537 }
538 break;
539 case '{': /* okay as ordinary except if digit follows */
540 REQUIRE(!MORE() || !isdigit((uch)PEEK()), REG_BADRPT)(void)((!(p->end - p->next > 0) || !((*__ctype_b_loc
())[(int) (((uch)(*p->next)))] & (unsigned short int)
_ISdigit)) || seterr(p, (13)))
;
541 LLVM_FALLTHROUGH__attribute__((fallthrough));
542 default:
543 ordinary(p, c);
544 break;
545 }
546
547 if (!MORE()(p->end - p->next > 0))
548 return;
549 c = PEEK()(*p->next);
550 /* we call { a repetition if followed by a digit */
551 if (!( c == '*' || c == '+' || c == '?' ||
552 (c == '{' && MORE2()(p->end - p->next > 1) && isdigit((uch)PEEK2())((*__ctype_b_loc ())[(int) (((uch)(*(p->next+1))))] & (
unsigned short int) _ISdigit)
) ))
553 return; /* no repetition, we're done */
554 NEXT()(p->next++);
555
556 REQUIRE(!wascaret, REG_BADRPT)(void)((!wascaret) || seterr(p, (13)));
557 switch (c) {
558 case '*': /* implemented as +? */
559 /* this case does not require the (y|) trick, noKLUDGE */
560 INSERT(OPLUS_, pos)doinsert(p, (sop)((9LU<<((unsigned)27))), (p->slen)-
(pos)+1, pos)
;
561 ASTERN(O_PLUS, pos)doemit(p, (sop)((10LU<<((unsigned)27))), (size_t)((p->
slen)-pos))
;
562 INSERT(OQUEST_, pos)doinsert(p, (sop)((11LU<<((unsigned)27))), (p->slen)
-(pos)+1, pos)
;
563 ASTERN(O_QUEST, pos)doemit(p, (sop)((12LU<<((unsigned)27))), (size_t)((p->
slen)-pos))
;
564 break;
565 case '+':
566 INSERT(OPLUS_, pos)doinsert(p, (sop)((9LU<<((unsigned)27))), (p->slen)-
(pos)+1, pos)
;
567 ASTERN(O_PLUS, pos)doemit(p, (sop)((10LU<<((unsigned)27))), (size_t)((p->
slen)-pos))
;
568 break;
569 case '?':
570 /* KLUDGE: emit y? as (y|) until subtle bug gets fixed */
571 INSERT(OCH_, pos)doinsert(p, (sop)((15LU<<((unsigned)27))), (p->slen)
-(pos)+1, pos)
; /* offset slightly wrong */
572 ASTERN(OOR1, pos)doemit(p, (sop)((16LU<<((unsigned)27))), (size_t)((p->
slen)-pos))
; /* this one's right */
573 AHEAD(pos)dofwd(p, pos, (p->slen)-(pos)); /* fix the OCH_ */
574 EMIT(OOR2, 0)doemit(p, (sop)((17LU<<((unsigned)27))), (size_t)(0)); /* offset very wrong... */
575 AHEAD(THERE())dofwd(p, (p->slen - 1), (p->slen)-((p->slen - 1))); /* ...so fix it */
576 ASTERN(O_CH, THERETHERE())doemit(p, (sop)((18LU<<((unsigned)27))), (size_t)((p->
slen)-(p->slen - 2)))
;
577 break;
578 case '{':
579 count = p_count(p);
580 if (EAT(',')((((p->end - p->next > 0) && (*p->next) ==
(','))) ? ((p->next++), 1) : 0)
) {
581 if (isdigit((uch)PEEK())((*__ctype_b_loc ())[(int) (((uch)(*p->next)))] & (unsigned
short int) _ISdigit)
) {
582 count2 = p_count(p);
583 REQUIRE(count <= count2, REG_BADBR)(void)((count <= count2) || seterr(p, (10)));
584 } else /* single number with comma */
585 count2 = INFINITY(255 + 1);
586 } else /* just a single number */
587 count2 = count;
588 repeat(p, pos, count, count2);
589 if (!EAT('}')((((p->end - p->next > 0) && (*p->next) ==
('}'))) ? ((p->next++), 1) : 0)
) { /* error heuristics */
590 while (MORE()(p->end - p->next > 0) && PEEK()(*p->next) != '}')
591 NEXT()(p->next++);
592 REQUIRE(MORE(), REG_EBRACE)(void)(((p->end - p->next > 0)) || seterr(p, (9)));
593 SETERROR(REG_BADBR)seterr(p, (10));
594 }
595 break;
596 }
597
598 if (!MORE()(p->end - p->next > 0))
599 return;
600 c = PEEK()(*p->next);
601 if (!( c == '*' || c == '+' || c == '?' ||
602 (c == '{' && MORE2()(p->end - p->next > 1) && isdigit((uch)PEEK2())((*__ctype_b_loc ())[(int) (((uch)(*(p->next+1))))] & (
unsigned short int) _ISdigit)
) ) )
603 return;
604 SETERROR(REG_BADRPT)seterr(p, (13));
605}
606
607/*
608 - p_str - string (no metacharacters) "parser"
609 */
610static void
611p_str(struct parse *p)
612{
613 REQUIRE(MORE(), REG_EMPTY)(void)(((p->end - p->next > 0)) || seterr(p, (14)));
614 while (MORE()(p->end - p->next > 0))
615 ordinary(p, GETNEXT()(*p->next++));
616}
617
618/*
619 - p_bre - BRE parser top level, anchoring and concatenation
620 * Giving end1 as OUT essentially eliminates the end1/end2 check.
621 *
622 * This implementation is a bit of a kludge, in that a trailing $ is first
623 * taken as an ordinary character and then revised to be an anchor. The
624 * only undesirable side effect is that '$' gets included as a character
625 * category in such cases. This is fairly harmless; not worth fixing.
626 * The amount of lookahead needed to avoid this kludge is excessive.
627 */
628static void
629p_bre(struct parse *p,
630 int end1, /* first terminating character */
631 int end2) /* second terminating character */
632{
633 sopno start = HERE()(p->slen);
634 int first = 1; /* first subexpression? */
635 int wasdollar = 0;
636
637 if (EAT('^')((((p->end - p->next > 0) && (*p->next) ==
('^'))) ? ((p->next++), 1) : 0)
) {
638 EMIT(OBOL, 0)doemit(p, (sop)((3LU<<((unsigned)27))), (size_t)(0));
639 p->g->iflags |= USEBOL01;
640 p->g->nbol++;
641 }
642 while (MORE()(p->end - p->next > 0) && !SEETWO(end1, end2)((p->end - p->next > 1) && (*p->next) == (
end1) && (*(p->next+1)) == (end2))
) {
643 wasdollar = p_simp_re(p, first);
644 first = 0;
645 }
646 if (wasdollar) { /* oops, that was a trailing anchor */
647 DROP(1)(p->slen -= (1));
648 EMIT(OEOL, 0)doemit(p, (sop)((4LU<<((unsigned)27))), (size_t)(0));
649 p->g->iflags |= USEEOL02;
650 p->g->neol++;
651 }
652
653 REQUIRE(HERE() != start, REG_EMPTY)(void)(((p->slen) != start) || seterr(p, (14))); /* require nonempty */
654}
655
656/*
657 - p_simp_re - parse a simple RE, an atom possibly followed by a repetition
658 */
659static int /* was the simple RE an unbackslashed $? */
660p_simp_re(struct parse *p,
661 int starordinary) /* is a leading * an ordinary character? */
662{
663 int c;
664 int count;
665 int count2;
666 sopno pos;
667 int i;
668 sopno subno;
669# define BACKSL(1<<8) (1<<CHAR_BIT8)
670
671 pos = HERE()(p->slen); /* repetition op, if any, covers from here */
672
673 assert(MORE())((void) (0)); /* caller should have ensured this */
674 c = GETNEXT()(*p->next++);
675 if (c == '\\') {
676 REQUIRE(MORE(), REG_EESCAPE)(void)(((p->end - p->next > 0)) || seterr(p, (5)));
677 c = BACKSL(1<<8) | GETNEXT()(*p->next++);
678 }
679 switch (c) {
680 case '.':
681 if (p->g->cflags&REG_NEWLINE0010)
682 nonnewline(p);
683 else
684 EMIT(OANY, 0)doemit(p, (sop)((5LU<<((unsigned)27))), (size_t)(0));
685 break;
686 case '[':
687 p_bracket(p);
688 break;
689 case BACKSL(1<<8)|'{':
690 SETERROR(REG_BADRPT)seterr(p, (13));
691 break;
692 case BACKSL(1<<8)|'(':
693 p->g->nsub++;
694 subno = p->g->nsub;
695 if (subno < NPAREN10)
696 p->pbegin[subno] = HERE()(p->slen);
697 EMIT(OLPAREN, subno)doemit(p, (sop)((13LU<<((unsigned)27))), (size_t)(subno
))
;
698 /* the MORE here is an error heuristic */
699 if (MORE()(p->end - p->next > 0) && !SEETWO('\\', ')')((p->end - p->next > 1) && (*p->next) == (
'\\') && (*(p->next+1)) == (')'))
)
700 p_bre(p, '\\', ')');
701 if (subno < NPAREN10) {
702 p->pend[subno] = HERE()(p->slen);
703 assert(p->pend[subno] != 0)((void) (0));
704 }
705 EMIT(ORPAREN, subno)doemit(p, (sop)((14LU<<((unsigned)27))), (size_t)(subno
))
;
706 REQUIRE(EATTWO('\\', ')'), REG_EPAREN)(void)((((((p->end - p->next > 1) && (*p->
next) == ('\\') && (*(p->next+1)) == (')'))) ? ((p
->next += 2), 1) : 0)) || seterr(p, (8)))
;
707 break;
708 case BACKSL(1<<8)|')': /* should not get here -- must be user */
709 case BACKSL(1<<8)|'}':
710 SETERROR(REG_EPAREN)seterr(p, (8));
711 break;
712 case BACKSL(1<<8)|'1':
713 case BACKSL(1<<8)|'2':
714 case BACKSL(1<<8)|'3':
715 case BACKSL(1<<8)|'4':
716 case BACKSL(1<<8)|'5':
717 case BACKSL(1<<8)|'6':
718 case BACKSL(1<<8)|'7':
719 case BACKSL(1<<8)|'8':
720 case BACKSL(1<<8)|'9':
721 i = (c&~BACKSL(1<<8)) - '0';
722 assert(i < NPAREN)((void) (0));
723 if (p->pend[i] != 0) {
724 assert(i <= p->g->nsub)((void) (0));
725 EMIT(OBACK_, i)doemit(p, (sop)((7LU<<((unsigned)27))), (size_t)(i));
726 assert(p->pbegin[i] != 0)((void) (0));
727 assert(OP(p->strip[p->pbegin[i]]) == OLPAREN)((void) (0));
728 assert(OP(p->strip[p->pend[i]]) == ORPAREN)((void) (0));
729 (void) dupl(p, p->pbegin[i]+1, p->pend[i]);
730 EMIT(O_BACK, i)doemit(p, (sop)((8LU<<((unsigned)27))), (size_t)(i));
731 } else
732 SETERROR(REG_ESUBREG)seterr(p, (6));
733 p->g->backrefs = 1;
734 break;
735 case '*':
736 REQUIRE(starordinary, REG_BADRPT)(void)((starordinary) || seterr(p, (13)));
737 LLVM_FALLTHROUGH__attribute__((fallthrough));
738 default:
739 ordinary(p, (char)c);
740 break;
741 }
742
743 if (EAT('*')((((p->end - p->next > 0) && (*p->next) ==
('*'))) ? ((p->next++), 1) : 0)
) { /* implemented as +? */
744 /* this case does not require the (y|) trick, noKLUDGE */
745 INSERT(OPLUS_, pos)doinsert(p, (sop)((9LU<<((unsigned)27))), (p->slen)-
(pos)+1, pos)
;
746 ASTERN(O_PLUS, pos)doemit(p, (sop)((10LU<<((unsigned)27))), (size_t)((p->
slen)-pos))
;
747 INSERT(OQUEST_, pos)doinsert(p, (sop)((11LU<<((unsigned)27))), (p->slen)
-(pos)+1, pos)
;
748 ASTERN(O_QUEST, pos)doemit(p, (sop)((12LU<<((unsigned)27))), (size_t)((p->
slen)-pos))
;
749 } else if (EATTWO('\\', '{')((((p->end - p->next > 1) && (*p->next) ==
('\\') && (*(p->next+1)) == ('{'))) ? ((p->next
+= 2), 1) : 0)
) {
750 count = p_count(p);
751 if (EAT(',')((((p->end - p->next > 0) && (*p->next) ==
(','))) ? ((p->next++), 1) : 0)
) {
752 if (MORE()(p->end - p->next > 0) && isdigit((uch)PEEK())((*__ctype_b_loc ())[(int) (((uch)(*p->next)))] & (unsigned
short int) _ISdigit)
) {
753 count2 = p_count(p);
754 REQUIRE(count <= count2, REG_BADBR)(void)((count <= count2) || seterr(p, (10)));
755 } else /* single number with comma */
756 count2 = INFINITY(255 + 1);
757 } else /* just a single number */
758 count2 = count;
759 repeat(p, pos, count, count2);
760 if (!EATTWO('\\', '}')((((p->end - p->next > 1) && (*p->next) ==
('\\') && (*(p->next+1)) == ('}'))) ? ((p->next
+= 2), 1) : 0)
) { /* error heuristics */
761 while (MORE()(p->end - p->next > 0) && !SEETWO('\\', '}')((p->end - p->next > 1) && (*p->next) == (
'\\') && (*(p->next+1)) == ('}'))
)
762 NEXT()(p->next++);
763 REQUIRE(MORE(), REG_EBRACE)(void)(((p->end - p->next > 0)) || seterr(p, (9)));
764 SETERROR(REG_BADBR)seterr(p, (10));
765 }
766 } else if (c == '$') /* $ (but not \$) ends it */
767 return(1);
768
769 return(0);
770}
771
772/*
773 - p_count - parse a repetition count
774 */
775static int /* the value */
776p_count(struct parse *p)
777{
778 int count = 0;
779 int ndigits = 0;
780
781 while (MORE()(p->end - p->next > 0) && isdigit((uch)PEEK())((*__ctype_b_loc ())[(int) (((uch)(*p->next)))] & (unsigned
short int) _ISdigit)
&& count <= DUPMAX255) {
782 count = count*10 + (GETNEXT()(*p->next++) - '0');
783 ndigits++;
784 }
785
786 REQUIRE(ndigits > 0 && count <= DUPMAX, REG_BADBR)(void)((ndigits > 0 && count <= 255) || seterr(
p, (10)))
;
787 return(count);
788}
789
790/*
791 - p_bracket - parse a bracketed character list
792 *
793 * Note a significant property of this code: if the allocset() did SETERROR,
794 * no set operations are done.
795 */
796static void
797p_bracket(struct parse *p)
798{
799 cset *cs;
800 int invert = 0;
801
802 /* Dept of Truly Sickening Special-Case Kludges */
803 if (p->end - p->next > 5) {
804 if (strncmp(p->next, "[:<:]]", 6) == 0) {
805 EMIT(OBOW, 0)doemit(p, (sop)((19LU<<((unsigned)27))), (size_t)(0));
806 NEXTn(6)(p->next += (6));
807 return;
808 }
809 if (strncmp(p->next, "[:>:]]", 6) == 0) {
810 EMIT(OEOW, 0)doemit(p, (sop)((20LU<<((unsigned)27))), (size_t)(0));
811 NEXTn(6)(p->next += (6));
812 return;
813 }
814 }
815
816 if ((cs = allocset(p)) == NULL((void*)0)) {
817 /* allocset did set error status in p */
818 return;
819 }
820
821 if (EAT('^')((((p->end - p->next > 0) && (*p->next) ==
('^'))) ? ((p->next++), 1) : 0)
)
822 invert++; /* make note to invert set at end */
823 if (EAT(']')((((p->end - p->next > 0) && (*p->next) ==
(']'))) ? ((p->next++), 1) : 0)
)
824 CHadd(cs, ']')((cs)->ptr[(uch)(']')] |= (cs)->mask, (cs)->hash += (
']'))
;
825 else if (EAT('-')((((p->end - p->next > 0) && (*p->next) ==
('-'))) ? ((p->next++), 1) : 0)
)
826 CHadd(cs, '-')((cs)->ptr[(uch)('-')] |= (cs)->mask, (cs)->hash += (
'-'))
;
827 while (MORE()(p->end - p->next > 0) && PEEK()(*p->next) != ']' && !SEETWO('-', ']')((p->end - p->next > 1) && (*p->next) == (
'-') && (*(p->next+1)) == (']'))
)
828 p_b_term(p, cs);
829 if (EAT('-')((((p->end - p->next > 0) && (*p->next) ==
('-'))) ? ((p->next++), 1) : 0)
)
830 CHadd(cs, '-')((cs)->ptr[(uch)('-')] |= (cs)->mask, (cs)->hash += (
'-'))
;
831 MUSTEAT(']', REG_EBRACK)((void)(((p->end - p->next > 0) && (*p->next
++) == (']')) || seterr(p, (7))))
;
832
833 if (p->error != 0) { /* don't mess things up further */
834 freeset(p, cs);
835 return;
836 }
837
838 if (p->g->cflags&REG_ICASE0002) {
839 int i;
840 int ci;
841
842 for (i = p->g->csetsize - 1; i >= 0; i--)
843 if (CHIN(cs, i)((cs)->ptr[(uch)(i)] & (cs)->mask) && isalpha(i)((*__ctype_b_loc ())[(int) ((i))] & (unsigned short int) _ISalpha
)
) {
844 ci = othercase(i);
845 if (ci != i)
846 CHadd(cs, ci)((cs)->ptr[(uch)(ci)] |= (cs)->mask, (cs)->hash += (
ci))
;
847 }
848 if (cs->multis != NULL((void*)0))
849 mccase(p, cs);
850 }
851 if (invert) {
852 int i;
853
854 for (i = p->g->csetsize - 1; i >= 0; i--)
855 if (CHIN(cs, i)((cs)->ptr[(uch)(i)] & (cs)->mask))
856 CHsub(cs, i)((cs)->ptr[(uch)(i)] &= ~(cs)->mask, (cs)->hash -=
(i))
;
857 else
858 CHadd(cs, i)((cs)->ptr[(uch)(i)] |= (cs)->mask, (cs)->hash += (i
))
;
859 if (p->g->cflags&REG_NEWLINE0010)
860 CHsub(cs, '\n')((cs)->ptr[(uch)('\n')] &= ~(cs)->mask, (cs)->hash
-= ('\n'))
;
861 if (cs->multis != NULL((void*)0))
862 mcinvert(p, cs);
863 }
864
865 assert(cs->multis == NULL)((void) (0)); /* xxx */
866
867 if (nch(p, cs) == 1) { /* optimize singleton sets */
868 ordinary(p, firstch(p, cs));
869 freeset(p, cs);
870 } else
871 EMIT(OANYOF, freezeset(p, cs))doemit(p, (sop)((6LU<<((unsigned)27))), (size_t)(freezeset
(p, cs)))
;
872}
873
874/*
875 - p_b_term - parse one term of a bracketed character list
876 */
877static void
878p_b_term(struct parse *p, cset *cs)
879{
880 char c;
881 char start, finish;
882 int i;
883
884 /* classify what we've got */
885 switch ((MORE()(p->end - p->next > 0)) ? PEEK()(*p->next) : '\0') {
886 case '[':
887 c = (MORE2()(p->end - p->next > 1)) ? PEEK2()(*(p->next+1)) : '\0';
888 break;
889 case '-':
890 SETERROR(REG_ERANGE)seterr(p, (11));
891 return; /* NOTE RETURN */
892 break;
893 default:
894 c = '\0';
895 break;
896 }
897
898 switch (c) {
899 case ':': /* character class */
900 NEXT2()(p->next += 2);
901 REQUIRE(MORE(), REG_EBRACK)(void)(((p->end - p->next > 0)) || seterr(p, (7)));
902 c = PEEK()(*p->next);
903 REQUIRE(c != '-' && c != ']', REG_ECTYPE)(void)((c != '-' && c != ']') || seterr(p, (4)));
904 p_b_cclass(p, cs);
905 REQUIRE(MORE(), REG_EBRACK)(void)(((p->end - p->next > 0)) || seterr(p, (7)));
906 REQUIRE(EATTWO(':', ']'), REG_ECTYPE)(void)((((((p->end - p->next > 1) && (*p->
next) == (':') && (*(p->next+1)) == (']'))) ? ((p->
next += 2), 1) : 0)) || seterr(p, (4)))
;
907 break;
908 case '=': /* equivalence class */
909 NEXT2()(p->next += 2);
910 REQUIRE(MORE(), REG_EBRACK)(void)(((p->end - p->next > 0)) || seterr(p, (7)));
911 c = PEEK()(*p->next);
912 REQUIRE(c != '-' && c != ']', REG_ECOLLATE)(void)((c != '-' && c != ']') || seterr(p, (3)));
913 p_b_eclass(p, cs);
914 REQUIRE(MORE(), REG_EBRACK)(void)(((p->end - p->next > 0)) || seterr(p, (7)));
915 REQUIRE(EATTWO('=', ']'), REG_ECOLLATE)(void)((((((p->end - p->next > 1) && (*p->
next) == ('=') && (*(p->next+1)) == (']'))) ? ((p->
next += 2), 1) : 0)) || seterr(p, (3)))
;
916 break;
917 default: /* symbol, ordinary character, or range */
918/* xxx revision needed for multichar stuff */
919 start = p_b_symbol(p);
920 if (SEE('-')((p->end - p->next > 0) && (*p->next) == (
'-'))
&& MORE2()(p->end - p->next > 1) && PEEK2()(*(p->next+1)) != ']') {
921 /* range */
922 NEXT()(p->next++);
923 if (EAT('-')((((p->end - p->next > 0) && (*p->next) ==
('-'))) ? ((p->next++), 1) : 0)
)
924 finish = '-';
925 else
926 finish = p_b_symbol(p);
927 } else
928 finish = start;
929/* xxx what about signed chars here... */
930 REQUIRE(start <= finish, REG_ERANGE)(void)((start <= finish) || seterr(p, (11)));
931 for (i = start; i <= finish; i++)
932 CHadd(cs, i)((cs)->ptr[(uch)(i)] |= (cs)->mask, (cs)->hash += (i
))
;
933 break;
934 }
935}
936
937/*
938 - p_b_cclass - parse a character-class name and deal with it
939 */
940static void
941p_b_cclass(struct parse *p, cset *cs)
942{
943 char *sp = p->next;
944 struct cclass *cp;
945 size_t len;
946 const char *u;
947 char c;
948
949 while (MORE()(p->end - p->next > 0) && isalpha((uch)PEEK())((*__ctype_b_loc ())[(int) (((uch)(*p->next)))] & (unsigned
short int) _ISalpha)
)
950 NEXT()(p->next++);
951 len = p->next - sp;
952 for (cp = cclasses; cp->name != NULL((void*)0); cp++)
953 if (strncmp(cp->name, sp, len) == 0 && cp->name[len] == '\0')
954 break;
955 if (cp->name == NULL((void*)0)) {
956 /* oops, didn't find it */
957 SETERROR(REG_ECTYPE)seterr(p, (4));
958 return;
959 }
960
961 u = cp->chars;
962 while ((c = *u++) != '\0')
963 CHadd(cs, c)((cs)->ptr[(uch)(c)] |= (cs)->mask, (cs)->hash += (c
))
;
964 for (u = cp->multis; *u != '\0'; u += strlen(u) + 1)
965 MCadd(p, cs, u)mcadd(p, cs, u);
966}
967
968/*
969 - p_b_eclass - parse an equivalence-class name and deal with it
970 *
971 * This implementation is incomplete. xxx
972 */
973static void
974p_b_eclass(struct parse *p, cset *cs)
975{
976 char c;
977
978 c = p_b_coll_elem(p, '=');
979 CHadd(cs, c)((cs)->ptr[(uch)(c)] |= (cs)->mask, (cs)->hash += (c
))
;
980}
981
982/*
983 - p_b_symbol - parse a character or [..]ed multicharacter collating symbol
984 */
985static char /* value of symbol */
986p_b_symbol(struct parse *p)
987{
988 char value;
989
990 REQUIRE(MORE(), REG_EBRACK)(void)(((p->end - p->next > 0)) || seterr(p, (7)));
991 if (!EATTWO('[', '.')((((p->end - p->next > 1) && (*p->next) ==
('[') && (*(p->next+1)) == ('.'))) ? ((p->next
+= 2), 1) : 0)
)
992 return(GETNEXT()(*p->next++));
993
994 /* collating symbol */
995 value = p_b_coll_elem(p, '.');
996 REQUIRE(EATTWO('.', ']'), REG_ECOLLATE)(void)((((((p->end - p->next > 1) && (*p->
next) == ('.') && (*(p->next+1)) == (']'))) ? ((p->
next += 2), 1) : 0)) || seterr(p, (3)))
;
997 return(value);
998}
999
1000/*
1001 - p_b_coll_elem - parse a collating-element name and look it up
1002 */
1003static char /* value of collating element */
1004p_b_coll_elem(struct parse *p,
1005 int endc) /* name ended by endc,']' */
1006{
1007 char *sp = p->next;
1008 struct cname *cp;
1009 size_t len;
1010
1011 while (MORE()(p->end - p->next > 0) && !SEETWO(endc, ']')((p->end - p->next > 1) && (*p->next) == (
endc) && (*(p->next+1)) == (']'))
)
1012 NEXT()(p->next++);
1013 if (!MORE()(p->end - p->next > 0)) {
1014 SETERROR(REG_EBRACK)seterr(p, (7));
1015 return(0);
1016 }
1017 len = p->next - sp;
1018 for (cp = cnames; cp->name != NULL((void*)0); cp++)
1019 if (strncmp(cp->name, sp, len) == 0 && strlen(cp->name) == len)
1020 return(cp->code); /* known name */
1021 if (len == 1)
1022 return(*sp); /* single character */
1023 SETERROR(REG_ECOLLATE)seterr(p, (3)); /* neither */
1024 return(0);
1025}
1026
1027/*
1028 - othercase - return the case counterpart of an alphabetic
1029 */
1030static char /* if no counterpart, return ch */
1031othercase(int ch)
1032{
1033 ch = (uch)ch;
1034 assert(isalpha(ch))((void) (0));
1035 if (isupper(ch)((*__ctype_b_loc ())[(int) ((ch))] & (unsigned short int)
_ISupper)
)
1036 return ((uch)tolower(ch)(__extension__ ({ int __res; if (sizeof (ch) > 1) { if (__builtin_constant_p
(ch)) { int __c = (ch); __res = __c < -128 || __c > 255
? __c : (*__ctype_tolower_loc ())[__c]; } else __res = tolower
(ch); } else __res = (*__ctype_tolower_loc ())[(int) (ch)]; __res
; }))
);
1037 else if (islower(ch)((*__ctype_b_loc ())[(int) ((ch))] & (unsigned short int)
_ISlower)
)
1038 return ((uch)toupper(ch)(__extension__ ({ int __res; if (sizeof (ch) > 1) { if (__builtin_constant_p
(ch)) { int __c = (ch); __res = __c < -128 || __c > 255
? __c : (*__ctype_toupper_loc ())[__c]; } else __res = toupper
(ch); } else __res = (*__ctype_toupper_loc ())[(int) (ch)]; __res
; }))
);
1039 else /* peculiar, but could happen */
1040 return(ch);
1041}
1042
1043/*
1044 - bothcases - emit a dualcase version of a two-case character
1045 *
1046 * Boy, is this implementation ever a kludge...
1047 */
1048static void
1049bothcases(struct parse *p, int ch)
1050{
1051 char *oldnext = p->next;
1052 char *oldend = p->end;
1053 char bracket[3];
1054
1055 ch = (uch)ch;
1056 assert(othercase(ch) != ch)((void) (0)); /* p_bracket() would recurse */
1057 p->next = bracket;
1058 p->end = bracket+2;
1059 bracket[0] = ch;
1060 bracket[1] = ']';
1061 bracket[2] = '\0';
1062 p_bracket(p);
1063 assert(p->next == bracket+2)((void) (0));
1064 p->next = oldnext;
1065 p->end = oldend;
1066}
1067
1068/*
1069 - ordinary - emit an ordinary character
1070 */
1071static void
1072ordinary(struct parse *p, int ch)
1073{
1074 cat_t *cap = p->g->categories;
1075
1076 if ((p->g->cflags&REG_ICASE0002) && isalpha((uch)ch)((*__ctype_b_loc ())[(int) (((uch)ch))] & (unsigned short
int) _ISalpha)
&& othercase(ch) != ch)
1077 bothcases(p, ch);
1078 else {
1079 EMIT(OCHAR, (uch)ch)doemit(p, (sop)((2LU<<((unsigned)27))), (size_t)((uch)ch
))
;
1080 if (cap[ch] == 0)
1081 cap[ch] = p->g->ncategories++;
1082 }
1083}
1084
1085/*
1086 - nonnewline - emit REG_NEWLINE version of OANY
1087 *
1088 * Boy, is this implementation ever a kludge...
1089 */
1090static void
1091nonnewline(struct parse *p)
1092{
1093 char *oldnext = p->next;
1094 char *oldend = p->end;
1095 char bracket[4];
1096
1097 p->next = bracket;
1098 p->end = bracket+3;
1099 bracket[0] = '^';
1100 bracket[1] = '\n';
1101 bracket[2] = ']';
1102 bracket[3] = '\0';
1103 p_bracket(p);
1104 assert(p->next == bracket+3)((void) (0));
1105 p->next = oldnext;
1106 p->end = oldend;
1107}
1108
1109/*
1110 - repeat - generate code for a bounded repetition, recursively if needed
1111 */
1112static void
1113repeat(struct parse *p,
1114 sopno start, /* operand from here to end of strip */
1115 int from, /* repeated from this number */
1116 int to) /* to this number of times (maybe INFINITY) */
1117{
1118 sopno finish = HERE()(p->slen);
1119# define N2 2
1120# define INF3 3
1121# define REP(f, t)((f)*8 + (t)) ((f)*8 + (t))
1122# define MAP(n)(((n) <= 1) ? (n) : ((n) == (255 + 1)) ? 3 : 2) (((n) <= 1) ? (n) : ((n) == INFINITY(255 + 1)) ? INF3 : N2)
1123 sopno copy;
1124
1125 if (p->error != 0) /* head off possible runaway recursion */
1126 return;
1127
1128 assert(from <= to)((void) (0));
1129
1130 switch (REP(MAP(from), MAP(to))(((((from) <= 1) ? (from) : ((from) == (255 + 1)) ? 3 : 2)
)*8 + ((((to) <= 1) ? (to) : ((to) == (255 + 1)) ? 3 : 2))
)
) {
1131 case REP(0, 0)((0)*8 + (0)): /* must be user doing this */
1132 DROP(finish-start)(p->slen -= (finish-start)); /* drop the operand */
1133 break;
1134 case REP(0, 1)((0)*8 + (1)): /* as x{1,1}? */
1135 case REP(0, N)((0)*8 + (2)): /* as x{1,n}? */
1136 case REP(0, INF)((0)*8 + (3)): /* as x{1,}? */
1137 /* KLUDGE: emit y? as (y|) until subtle bug gets fixed */
1138 INSERT(OCH_, start)doinsert(p, (sop)((15LU<<((unsigned)27))), (p->slen)
-(start)+1, start)
; /* offset is wrong... */
1139 repeat(p, start+1, 1, to);
1140 ASTERN(OOR1, start)doemit(p, (sop)((16LU<<((unsigned)27))), (size_t)((p->
slen)-start))
;
1141 AHEAD(start)dofwd(p, start, (p->slen)-(start)); /* ... fix it */
1142 EMIT(OOR2, 0)doemit(p, (sop)((17LU<<((unsigned)27))), (size_t)(0));
1143 AHEAD(THERE())dofwd(p, (p->slen - 1), (p->slen)-((p->slen - 1)));
1144 ASTERN(O_CH, THERETHERE())doemit(p, (sop)((18LU<<((unsigned)27))), (size_t)((p->
slen)-(p->slen - 2)))
;
1145 break;
1146 case REP(1, 1)((1)*8 + (1)): /* trivial case */
1147 /* done */
1148 break;
1149 case REP(1, N)((1)*8 + (2)): /* as x?x{1,n-1} */
1150 /* KLUDGE: emit y? as (y|) until subtle bug gets fixed */
1151 INSERT(OCH_, start)doinsert(p, (sop)((15LU<<((unsigned)27))), (p->slen)
-(start)+1, start)
;
1152 ASTERN(OOR1, start)doemit(p, (sop)((16LU<<((unsigned)27))), (size_t)((p->
slen)-start))
;
1153 AHEAD(start)dofwd(p, start, (p->slen)-(start));
1154 EMIT(OOR2, 0)doemit(p, (sop)((17LU<<((unsigned)27))), (size_t)(0)); /* offset very wrong... */
1155 AHEAD(THERE())dofwd(p, (p->slen - 1), (p->slen)-((p->slen - 1))); /* ...so fix it */
1156 ASTERN(O_CH, THERETHERE())doemit(p, (sop)((18LU<<((unsigned)27))), (size_t)((p->
slen)-(p->slen - 2)))
;
1157 copy = dupl(p, start+1, finish+1);
1158 assert(copy == finish+4)((void) (0));
1159 repeat(p, copy, 1, to-1);
1160 break;
1161 case REP(1, INF)((1)*8 + (3)): /* as x+ */
1162 INSERT(OPLUS_, start)doinsert(p, (sop)((9LU<<((unsigned)27))), (p->slen)-
(start)+1, start)
;
1163 ASTERN(O_PLUS, start)doemit(p, (sop)((10LU<<((unsigned)27))), (size_t)((p->
slen)-start))
;
1164 break;
1165 case REP(N, N)((2)*8 + (2)): /* as xx{m-1,n-1} */
1166 copy = dupl(p, start, finish);
1167 repeat(p, copy, from-1, to-1);
1168 break;
1169 case REP(N, INF)((2)*8 + (3)): /* as xx{n-1,INF} */
1170 copy = dupl(p, start, finish);
1171 repeat(p, copy, from-1, to);
1172 break;
1173 default: /* "can't happen" */
1174 SETERROR(REG_ASSERT)seterr(p, (15)); /* just in case */
1175 break;
1176 }
1177}
1178
1179/*
1180 - seterr - set an error condition
1181 */
1182static int /* useless but makes type checking happy */
1183seterr(struct parse *p, int e)
1184{
1185 if (p->error == 0) /* keep earliest error condition */
1186 p->error = e;
1187 p->next = nuls; /* try to bring things to a halt */
1188 p->end = nuls;
1189 return(0); /* make the return value well-defined */
1190}
1191
1192/*
1193 - allocset - allocate a set of characters for []
1194 */
1195static cset *
1196allocset(struct parse *p)
1197{
1198 int no = p->g->ncsets++;
1199 size_t nc;
1200 size_t nbytes;
1201 cset *cs;
1202 size_t css = (size_t)p->g->csetsize;
1203 int i;
1204
1205 if (no >= p->ncsalloc) { /* need another column of space */
1206 void *ptr;
1207
1208 p->ncsalloc += CHAR_BIT8;
1209 nc = p->ncsalloc;
1210 if (nc > SIZE_MAX(18446744073709551615UL) / sizeof(cset))
1211 goto nomem;
1212 assert(nc % CHAR_BIT == 0)((void) (0));
1213 nbytes = nc / CHAR_BIT8 * css;
1214
1215 ptr = (cset *)realloc((char *)p->g->sets, nc * sizeof(cset));
1216 if (ptr == NULL((void*)0))
1217 goto nomem;
1218 p->g->sets = ptr;
1219
1220 ptr = (uch *)realloc((char *)p->g->setbits, nbytes);
1221 if (ptr == NULL((void*)0))
1222 goto nomem;
1223 p->g->setbits = ptr;
1224
1225 for (i = 0; i < no; i++)
1226 p->g->sets[i].ptr = p->g->setbits + css*(i/CHAR_BIT8);
1227
1228 (void) memset((char *)p->g->setbits + (nbytes - css), 0, css);
1229 }
1230 /* XXX should not happen */
1231 if (p->g->sets == NULL((void*)0) || p->g->setbits == NULL((void*)0))
1232 goto nomem;
1233
1234 cs = &p->g->sets[no];
1235 cs->ptr = p->g->setbits + css*((no)/CHAR_BIT8);
1236 cs->mask = 1 << ((no) % CHAR_BIT8);
1237 cs->hash = 0;
1238 cs->smultis = 0;
1239 cs->multis = NULL((void*)0);
1240
1241 return(cs);
1242nomem:
1243 free(p->g->sets);
1244 p->g->sets = NULL((void*)0);
1245 free(p->g->setbits);
1246 p->g->setbits = NULL((void*)0);
1247
1248 SETERROR(REG_ESPACE)seterr(p, (12));
1249 /* caller's responsibility not to do set ops */
1250 return(NULL((void*)0));
1251}
1252
1253/*
1254 - freeset - free a now-unused set
1255 */
1256static void
1257freeset(struct parse *p, cset *cs)
1258{
1259 size_t i;
1260 cset *top = &p->g->sets[p->g->ncsets];
1261 size_t css = (size_t)p->g->csetsize;
1262
1263 for (i = 0; i < css; i++)
1264 CHsub(cs, i)((cs)->ptr[(uch)(i)] &= ~(cs)->mask, (cs)->hash -=
(i))
;
1265 if (cs == top-1) /* recover only the easy case */
1266 p->g->ncsets--;
1267}
1268
1269/*
1270 - freezeset - final processing on a set of characters
1271 *
1272 * The main task here is merging identical sets. This is usually a waste
1273 * of time (although the hash code minimizes the overhead), but can win
1274 * big if REG_ICASE is being used. REG_ICASE, by the way, is why the hash
1275 * is done using addition rather than xor -- all ASCII [aA] sets xor to
1276 * the same value!
1277 */
1278static int /* set number */
1279freezeset(struct parse *p, cset *cs)
1280{
1281 uch h = cs->hash;
1282 size_t i;
1283 cset *top = &p->g->sets[p->g->ncsets];
1284 cset *cs2;
1285 size_t css = (size_t)p->g->csetsize;
1286
1287 /* look for an earlier one which is the same */
1288 for (cs2 = &p->g->sets[0]; cs2 < top; cs2++)
1289 if (cs2->hash == h && cs2 != cs) {
1290 /* maybe */
1291 for (i = 0; i < css; i++)
1292 if (!!CHIN(cs2, i)((cs2)->ptr[(uch)(i)] & (cs2)->mask) != !!CHIN(cs, i)((cs)->ptr[(uch)(i)] & (cs)->mask))
1293 break; /* no */
1294 if (i == css)
1295 break; /* yes */
1296 }
1297
1298 if (cs2 < top) { /* found one */
1299 freeset(p, cs);
1300 cs = cs2;
1301 }
1302
1303 return((int)(cs - p->g->sets));
1304}
1305
1306/*
1307 - firstch - return first character in a set (which must have at least one)
1308 */
1309static int /* character; there is no "none" value */
1310firstch(struct parse *p, cset *cs)
1311{
1312 size_t i;
1313 size_t css = (size_t)p->g->csetsize;
1314
1315 for (i = 0; i < css; i++)
1316 if (CHIN(cs, i)((cs)->ptr[(uch)(i)] & (cs)->mask))
1317 return((char)i);
1318 assert(never)((void) (0));
1319 return(0); /* arbitrary */
1320}
1321
1322/*
1323 - nch - number of characters in a set
1324 */
1325static int
1326nch(struct parse *p, cset *cs)
1327{
1328 size_t i;
1329 size_t css = (size_t)p->g->csetsize;
1330 int n = 0;
1331
1332 for (i = 0; i < css; i++)
1333 if (CHIN(cs, i)((cs)->ptr[(uch)(i)] & (cs)->mask))
1334 n++;
1335 return(n);
1336}
1337
1338/*
1339 - mcadd - add a collating element to a cset
1340 */
1341static void
1342mcadd( struct parse *p, cset *cs, const char *cp)
1343{
1344 size_t oldend = cs->smultis;
1345 void *np;
1346
1347 cs->smultis += strlen(cp) + 1;
1348 np = realloc(cs->multis, cs->smultis);
1349 if (np == NULL((void*)0)) {
1350 if (cs->multis)
1351 free(cs->multis);
1352 cs->multis = NULL((void*)0);
1353 SETERROR(REG_ESPACE)seterr(p, (12));
1354 return;
1355 }
1356 cs->multis = np;
1357
1358 llvm_strlcpy(cs->multis + oldend - 1, cp, cs->smultis - oldend + 1);
1359}
1360
1361/*
1362 - mcinvert - invert the list of collating elements in a cset
1363 *
1364 * This would have to know the set of possibilities. Implementation
1365 * is deferred.
1366 */
1367/* ARGSUSED */
1368static void
1369mcinvert(struct parse *p, cset *cs)
1370{
1371 assert(cs->multis == NULL)((void) (0)); /* xxx */
1372}
1373
1374/*
1375 - mccase - add case counterparts of the list of collating elements in a cset
1376 *
1377 * This would have to know the set of possibilities. Implementation
1378 * is deferred.
1379 */
1380/* ARGSUSED */
1381static void
1382mccase(struct parse *p, cset *cs)
1383{
1384 assert(cs->multis == NULL)((void) (0)); /* xxx */
1385}
1386
1387/*
1388 - isinsets - is this character in any sets?
1389 */
1390static int /* predicate */
1391isinsets(struct re_guts *g, int c)
1392{
1393 uch *col;
1394 int i;
1395 int ncols = (g->ncsets+(CHAR_BIT8-1)) / CHAR_BIT8;
1396 unsigned uc = (uch)c;
1397
1398 for (i = 0, col = g->setbits; i < ncols; i++, col += g->csetsize)
1399 if (col[uc] != 0)
1400 return(1);
1401 return(0);
1402}
1403
1404/*
1405 - samesets - are these two characters in exactly the same sets?
1406 */
1407static int /* predicate */
1408samesets(struct re_guts *g, int c1, int c2)
1409{
1410 uch *col;
1411 int i;
1412 int ncols = (g->ncsets+(CHAR_BIT8-1)) / CHAR_BIT8;
1413 unsigned uc1 = (uch)c1;
1414 unsigned uc2 = (uch)c2;
1415
1416 for (i = 0, col = g->setbits; i < ncols; i++, col += g->csetsize)
1417 if (col[uc1] != col[uc2])
1418 return(0);
1419 return(1);
1420}
1421
1422/*
1423 - categorize - sort out character categories
1424 */
1425static void
1426categorize(struct parse *p, struct re_guts *g)
1427{
1428 cat_t *cats = g->categories;
1429 int c;
1430 int c2;
1431 cat_t cat;
1432
1433 /* avoid making error situations worse */
1434 if (p->error != 0)
1435 return;
1436
1437 for (c = CHAR_MIN(-127 -1); c <= CHAR_MAX127; c++)
1438 if (cats[c] == 0 && isinsets(g, c)) {
1439 cat = g->ncategories++;
1440 cats[c] = cat;
1441 for (c2 = c+1; c2 <= CHAR_MAX127; c2++)
1442 if (cats[c2] == 0 && samesets(g, c, c2))
1443 cats[c2] = cat;
1444 }
1445}
1446
1447/*
1448 - dupl - emit a duplicate of a bunch of sops
1449 */
1450static sopno /* start of duplicate */
1451dupl(struct parse *p,
1452 sopno start, /* from here */
1453 sopno finish) /* to this less one */
1454{
1455 sopno ret = HERE()(p->slen);
1456 sopno len = finish - start;
1457
1458 assert(finish >= start)((void) (0));
1459 if (len == 0)
1460 return(ret);
1461 enlarge(p, p->ssize + len); /* this many unexpected additions */
1462 assert(p->ssize >= p->slen + len)((void) (0));
1463 (void) memmove((char *)(p->strip + p->slen),
1464 (char *)(p->strip + start), (size_t)len*sizeof(sop));
1465 p->slen += len;
1466 return(ret);
1467}
1468
1469/*
1470 - doemit - emit a strip operator
1471 *
1472 * It might seem better to implement this as a macro with a function as
1473 * hard-case backup, but it's just too big and messy unless there are
1474 * some changes to the data structures. Maybe later.
1475 */
1476static void
1477doemit(struct parse *p, sop op, size_t opnd)
1478{
1479 /* avoid making error situations worse */
1480 if (p->error != 0)
1481 return;
1482
1483 /* deal with oversize operands ("can't happen", more or less) */
1484 assert(opnd < 1<<OPSHIFT)((void) (0));
1485
1486 /* deal with undersized strip */
1487 if (p->slen >= p->ssize)
1488 enlarge(p, (p->ssize+1) / 2 * 3); /* +50% */
1489 assert(p->slen < p->ssize)((void) (0));
1490
1491 /* finally, it's all reduced to the easy case */
1492 p->strip[p->slen++] = SOP(op, opnd)((op)|(opnd));
1493}
1494
1495/*
1496 - doinsert - insert a sop into the strip
1497 */
1498static void
1499doinsert(struct parse *p, sop op, size_t opnd, sopno pos)
1500{
1501 sopno sn;
1502 sop s;
1503 int i;
1504
1505 /* avoid making error situations worse */
1506 if (p->error != 0)
1507 return;
1508
1509 sn = HERE()(p->slen);
1510 EMIT(op, opnd)doemit(p, (sop)(op), (size_t)(opnd)); /* do checks, ensure space */
1511 assert(HERE() == sn+1)((void) (0));
1512 s = p->strip[sn];
1513
1514 /* adjust paren pointers */
1515 assert(pos > 0)((void) (0));
1516 for (i = 1; i < NPAREN10; i++) {
1517 if (p->pbegin[i] >= pos) {
1518 p->pbegin[i]++;
1519 }
1520 if (p->pend[i] >= pos) {
1521 p->pend[i]++;
1522 }
1523 }
1524
1525 memmove((char *)&p->strip[pos+1], (char *)&p->strip[pos],
1526 (HERE()(p->slen)-pos-1)*sizeof(sop));
1527 p->strip[pos] = s;
1528}
1529
1530/*
1531 - dofwd - complete a forward reference
1532 */
1533static void
1534dofwd(struct parse *p, sopno pos, sop value)
1535{
1536 /* avoid making error situations worse */
1537 if (p->error != 0)
1538 return;
1539
1540 assert(value < 1<<OPSHIFT)((void) (0));
1541 p->strip[pos] = OP(p->strip[pos])((p->strip[pos])&0xf8000000LU) | value;
1542}
1543
1544/*
1545 - enlarge - enlarge the strip
1546 */
1547static void
1548enlarge(struct parse *p, sopno size)
1549{
1550 sop *sp;
1551
1552 if (p->ssize >= size)
1553 return;
1554
1555 if ((uintptr_t)size > SIZE_MAX(18446744073709551615UL) / sizeof(sop)) {
1556 SETERROR(REG_ESPACE)seterr(p, (12));
1557 return;
1558 }
1559
1560 sp = (sop *)realloc(p->strip, size*sizeof(sop));
1561 if (sp == NULL((void*)0)) {
1562 SETERROR(REG_ESPACE)seterr(p, (12));
1563 return;
1564 }
1565 p->strip = sp;
1566 p->ssize = size;
1567}
1568
1569/*
1570 - stripsnug - compact the strip
1571 */
1572static void
1573stripsnug(struct parse *p, struct re_guts *g)
1574{
1575 g->nstates = p->slen;
1576 if ((uintptr_t)p->slen > SIZE_MAX(18446744073709551615UL) / sizeof(sop)) {
1577 g->strip = p->strip;
1578 SETERROR(REG_ESPACE)seterr(p, (12));
1579 return;
1580 }
1581
1582 g->strip = (sop *)realloc((char *)p->strip, p->slen * sizeof(sop));
1583 if (g->strip == NULL((void*)0)) {
1584 SETERROR(REG_ESPACE)seterr(p, (12));
1585 g->strip = p->strip;
1586 }
1587}
1588
1589/*
1590 - findmust - fill in must and mlen with longest mandatory literal string
1591 *
1592 * This algorithm could do fancy things like analyzing the operands of |
1593 * for common subsequences. Someday. This code is simple and finds most
1594 * of the interesting cases.
1595 *
1596 * Note that must and mlen got initialized during setup.
1597 */
1598static void
1599findmust(struct parse *p, struct re_guts *g)
1600{
1601 sop *scan;
1602 sop *start = 0; /* start initialized in the default case, after that */
1603 sop *newstart = 0; /* newstart was initialized in the OCHAR case */
1604 sopno newlen;
1605 sop s;
1606 char *cp;
1607 sopno i;
1608
1609 /* avoid making error situations worse */
1610 if (p->error != 0)
1
Assuming field 'error' is equal to 0
2
Taking false branch
1611 return;
1612
1613 /* find the longest OCHAR sequence in strip */
1614 newlen = 0;
1615 scan = g->strip + 1;
1616 do {
8
Loop condition is false. Exiting loop
1617 s = *scan++;
1618 switch (OP(s)((s)&0xf8000000LU)) {
3
Control jumps to the 'default' case at line 1642
1619 case OCHAR(2LU<<((unsigned)27)): /* sequence member */
1620 if (newlen == 0) /* new sequence */
1621 newstart = scan - 1;
1622 newlen++;
1623 break;
1624 case OPLUS_(9LU<<((unsigned)27)): /* things that don't break one */
1625 case OLPAREN(13LU<<((unsigned)27)):
1626 case ORPAREN(14LU<<((unsigned)27)):
1627 break;
1628 case OQUEST_(11LU<<((unsigned)27)): /* things that must be skipped */
1629 case OCH_(15LU<<((unsigned)27)):
1630 scan--;
1631 do {
1632 scan += OPND(s)((s)&0x07ffffffLU);
1633 s = *scan;
1634 /* assert() interferes w debug printouts */
1635 if (OP(s)((s)&0xf8000000LU) != O_QUEST(12LU<<((unsigned)27)) && OP(s)((s)&0xf8000000LU) != O_CH(18LU<<((unsigned)27)) &&
1636 OP(s)((s)&0xf8000000LU) != OOR2(17LU<<((unsigned)27))) {
1637 g->iflags |= REGEX_BAD04;
1638 return;
1639 }
1640 } while (OP(s)((s)&0xf8000000LU) != O_QUEST(12LU<<((unsigned)27)) && OP(s)((s)&0xf8000000LU) != O_CH(18LU<<((unsigned)27)));
1641 LLVM_FALLTHROUGH__attribute__((fallthrough));
1642 default: /* things that break a sequence */
1643 if (newlen > g->mlen) { /* ends one */
4
Assuming 'newlen' is <= field 'mlen'
5
Taking false branch
1644 start = newstart;
1645 g->mlen = newlen;
1646 }
1647 newlen = 0;
1648 break;
6
Execution continues on line 1650
1649 }
1650 } while (OP(s)((s)&0xf8000000LU) != OEND(1LU<<((unsigned)27)));
7
Assuming the condition is false
1651
1652 if (g->mlen == 0) /* there isn't one */
9
Assuming field 'mlen' is not equal to 0
10
Taking false branch
1653 return;
1654
1655 /* turn it into a character string */
1656 g->must = malloc((size_t)g->mlen + 1);
1657 if (g->must == NULL((void*)0)) { /* argh; just forget it */
11
Assuming field 'must' is not equal to NULL
12
Taking false branch
1658 g->mlen = 0;
1659 return;
1660 }
1661 cp = g->must;
1662 scan = start;
1663 for (i = g->mlen; i
12.1
'i' is > 0
> 0; i--) {
13
Loop condition is true. Entering loop body
1664 while (OP(s = *scan++)((s = *scan++)&0xf8000000LU) != OCHAR(2LU<<((unsigned)27)))
14
Null pointer value stored to 'scan'
15
Dereference of null pointer
1665 continue;
1666 assert(cp < g->must + g->mlen)((void) (0));
1667 *cp++ = (char)OPND(s)((s)&0x07ffffffLU);
1668 }
1669 assert(cp == g->must + g->mlen)((void) (0));
1670 *cp++ = '\0'; /* just on general principles */
1671}
1672
1673/*
1674 - pluscount - count + nesting
1675 */
1676static sopno /* nesting depth */
1677pluscount(struct parse *p, struct re_guts *g)
1678{
1679 sop *scan;
1680 sop s;
1681 sopno plusnest = 0;
1682 sopno maxnest = 0;
1683
1684 if (p->error != 0)
1685 return(0); /* there may not be an OEND */
1686
1687 scan = g->strip + 1;
1688 do {
1689 s = *scan++;
1690 switch (OP(s)((s)&0xf8000000LU)) {
1691 case OPLUS_(9LU<<((unsigned)27)):
1692 plusnest++;
1693 break;
1694 case O_PLUS(10LU<<((unsigned)27)):
1695 if (plusnest > maxnest)
1696 maxnest = plusnest;
1697 plusnest--;
1698 break;
1699 }
1700 } while (OP(s)((s)&0xf8000000LU) != OEND(1LU<<((unsigned)27)));
1701 if (plusnest != 0)
1702 g->iflags |= REGEX_BAD04;
1703 return(maxnest);
1704}