]> git.ipfire.org Git - thirdparty/glibc.git/blame - posix/regexec.c
elf: Use nocancel pread64() instead of lseek()+read()
[thirdparty/glibc.git] / posix / regexec.c
CommitLineData
3b0bdc72 1/* Extended regular expression matching and search library.
04277e02 2 Copyright (C) 2002-2019 Free Software Foundation, Inc.
3b0bdc72
UD
3 This file is part of the GNU C Library.
4 Contributed by Isamu Hasegawa <isamu@yamato.ibm.com>.
5
6 The GNU C Library is free software; you can redistribute it and/or
7 modify it under the terms of the GNU Lesser General Public
8 License as published by the Free Software Foundation; either
9 version 2.1 of the License, or (at your option) any later version.
10
11 The GNU C Library is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Lesser General Public License for more details.
15
16 You should have received a copy of the GNU Lesser General Public
59ba27a6 17 License along with the GNU C Library; if not, see
eb04c213 18 <https://www.gnu.org/licenses/>. */
e054f494 19
a9388965 20static reg_errcode_t match_ctx_init (re_match_context_t *cache, int eflags,
eb04c213 21 Idx n);
b41bd5bc
FW
22static void match_ctx_clean (re_match_context_t *mctx);
23static void match_ctx_free (re_match_context_t *cache);
eb04c213
AZ
24static reg_errcode_t match_ctx_add_entry (re_match_context_t *cache, Idx node,
25 Idx str_idx, Idx from, Idx to);
26static Idx search_cur_bkref_entry (const re_match_context_t *mctx, Idx str_idx);
27static reg_errcode_t match_ctx_add_subtop (re_match_context_t *mctx, Idx node,
28 Idx str_idx);
6291ee3c 29static re_sub_match_last_t * match_ctx_add_sublast (re_sub_match_top_t *subtop,
eb04c213 30 Idx node, Idx str_idx);
0742e48e 31static void sift_ctx_init (re_sift_context_t *sctx, re_dfastate_t **sifted_sts,
eb04c213
AZ
32 re_dfastate_t **limited_sts, Idx last_node,
33 Idx last_str_idx);
a9388965 34static reg_errcode_t re_search_internal (const regex_t *preg,
eb04c213
AZ
35 const char *string, Idx length,
36 Idx start, Idx last_start, Idx stop,
15a7d175 37 size_t nmatch, regmatch_t pmatch[],
b41bd5bc 38 int eflags);
eb04c213
AZ
39static regoff_t re_search_2_stub (struct re_pattern_buffer *bufp,
40 const char *string1, Idx length1,
41 const char *string2, Idx length2,
42 Idx start, regoff_t range,
43 struct re_registers *regs,
44 Idx stop, bool ret_len);
45static regoff_t re_search_stub (struct re_pattern_buffer *bufp,
46 const char *string, Idx length, Idx start,
47 regoff_t range, Idx stop,
48 struct re_registers *regs,
49 bool ret_len);
ac3d553b 50static unsigned re_copy_regs (struct re_registers *regs, regmatch_t *pmatch,
eb04c213 51 Idx nregs, int regs_allocated);
b41bd5bc 52static reg_errcode_t prune_impossible_nodes (re_match_context_t *mctx);
eb04c213
AZ
53static Idx check_matching (re_match_context_t *mctx, bool fl_longest_match,
54 Idx *p_match_first);
55static Idx check_halt_state_context (const re_match_context_t *mctx,
56 const re_dfastate_t *state, Idx idx);
76b864c8 57static void update_regs (const re_dfa_t *dfa, regmatch_t *pmatch,
eb04c213
AZ
58 regmatch_t *prev_idx_match, Idx cur_node,
59 Idx cur_idx, Idx nmatch);
1b2c2628 60static reg_errcode_t push_fail_stack (struct re_fail_stack_t *fs,
eb04c213 61 Idx str_idx, Idx dest_node, Idx nregs,
15a7d175 62 regmatch_t *regs,
b41bd5bc 63 re_node_set *eps_via_nodes);
612546c6 64static reg_errcode_t set_regs (const regex_t *preg,
15a7d175
UD
65 const re_match_context_t *mctx,
66 size_t nmatch, regmatch_t *pmatch,
eb04c213 67 bool fl_backtrack);
b41bd5bc 68static reg_errcode_t free_fail_stack_return (struct re_fail_stack_t *fs);
485d775d 69
434d3784 70#ifdef RE_ENABLE_I18N
e3a87852 71static int sift_states_iter_mb (const re_match_context_t *mctx,
15a7d175 72 re_sift_context_t *sctx,
eb04c213 73 Idx node_idx, Idx str_idx, Idx max_str_idx);
434d3784 74#endif /* RE_ENABLE_I18N */
76b864c8 75static reg_errcode_t sift_states_backward (const re_match_context_t *mctx,
b41bd5bc 76 re_sift_context_t *sctx);
76b864c8 77static reg_errcode_t build_sifted_states (const re_match_context_t *mctx,
eb04c213 78 re_sift_context_t *sctx, Idx str_idx,
b41bd5bc 79 re_node_set *cur_dest);
76b864c8 80static reg_errcode_t update_cur_sifted_state (const re_match_context_t *mctx,
15a7d175 81 re_sift_context_t *sctx,
eb04c213 82 Idx str_idx,
b41bd5bc 83 re_node_set *dest_nodes);
76b864c8 84static reg_errcode_t add_epsilon_src_nodes (const re_dfa_t *dfa,
15a7d175 85 re_node_set *dest_nodes,
b41bd5bc 86 const re_node_set *candidates);
eb04c213
AZ
87static bool check_dst_limits (const re_match_context_t *mctx,
88 const re_node_set *limits,
89 Idx dst_node, Idx dst_idx, Idx src_node,
90 Idx src_idx);
01ed6ceb 91static int check_dst_limits_calc_pos_1 (const re_match_context_t *mctx,
eb04c213
AZ
92 int boundaries, Idx subexp_idx,
93 Idx from_node, Idx bkref_idx);
01ed6ceb 94static int check_dst_limits_calc_pos (const re_match_context_t *mctx,
eb04c213
AZ
95 Idx limit, Idx subexp_idx,
96 Idx node, Idx str_idx,
97 Idx bkref_idx);
76b864c8 98static reg_errcode_t check_subexp_limits (const re_dfa_t *dfa,
15a7d175
UD
99 re_node_set *dest_nodes,
100 const re_node_set *candidates,
101 re_node_set *limits,
102 struct re_backref_cache_entry *bkref_ents,
eb04c213 103 Idx str_idx);
76b864c8 104static reg_errcode_t sift_states_bkref (const re_match_context_t *mctx,
15a7d175 105 re_sift_context_t *sctx,
eb04c213 106 Idx str_idx, const re_node_set *candidates);
76b864c8
UD
107static reg_errcode_t merge_state_array (const re_dfa_t *dfa,
108 re_dfastate_t **dst,
eb04c213 109 re_dfastate_t **src, Idx num);
4c595adb 110static re_dfastate_t *find_recover_state (reg_errcode_t *err,
b41bd5bc 111 re_match_context_t *mctx);
e3a87852 112static re_dfastate_t *transit_state (reg_errcode_t *err,
15a7d175 113 re_match_context_t *mctx,
b41bd5bc 114 re_dfastate_t *state);
4c595adb
UD
115static re_dfastate_t *merge_state_with_log (reg_errcode_t *err,
116 re_match_context_t *mctx,
b41bd5bc 117 re_dfastate_t *next_state);
e3a87852 118static reg_errcode_t check_subexp_matching_top (re_match_context_t *mctx,
6291ee3c 119 re_node_set *cur_nodes,
eb04c213 120 Idx str_idx);
c13c99fa 121#if 0
e3a87852
UD
122static re_dfastate_t *transit_state_sb (reg_errcode_t *err,
123 re_match_context_t *mctx,
b41bd5bc 124 re_dfastate_t *pstate);
c13c99fa 125#endif
434d3784 126#ifdef RE_ENABLE_I18N
e3a87852 127static reg_errcode_t transit_state_mb (re_match_context_t *mctx,
b41bd5bc 128 re_dfastate_t *pstate);
434d3784 129#endif /* RE_ENABLE_I18N */
e3a87852 130static reg_errcode_t transit_state_bkref (re_match_context_t *mctx,
b41bd5bc 131 const re_node_set *nodes);
e3a87852 132static reg_errcode_t get_subexp (re_match_context_t *mctx,
eb04c213 133 Idx bkref_node, Idx bkref_str_idx);
e3a87852 134static reg_errcode_t get_subexp_sub (re_match_context_t *mctx,
fe9434bb 135 const re_sub_match_top_t *sub_top,
6291ee3c 136 re_sub_match_last_t *sub_last,
eb04c213
AZ
137 Idx bkref_node, Idx bkref_str);
138static Idx find_subexp_node (const re_dfa_t *dfa, const re_node_set *nodes,
139 Idx subexp_idx, int type);
e3a87852 140static reg_errcode_t check_arrival (re_match_context_t *mctx,
eb04c213
AZ
141 state_array_t *path, Idx top_node,
142 Idx top_str, Idx last_node, Idx last_str,
b41bd5bc 143 int type);
e3a87852 144static reg_errcode_t check_arrival_add_next_nodes (re_match_context_t *mctx,
eb04c213 145 Idx str_idx,
6291ee3c 146 re_node_set *cur_nodes,
b41bd5bc 147 re_node_set *next_nodes);
6efbd82c 148static reg_errcode_t check_arrival_expand_ecl (const re_dfa_t *dfa,
6291ee3c 149 re_node_set *cur_nodes,
eb04c213 150 Idx ex_subexp, int type);
6efbd82c 151static reg_errcode_t check_arrival_expand_ecl_sub (const re_dfa_t *dfa,
6291ee3c 152 re_node_set *dst_nodes,
eb04c213 153 Idx target, Idx ex_subexp,
b41bd5bc 154 int type);
e3a87852 155static reg_errcode_t expand_bkref_cache (re_match_context_t *mctx,
eb04c213
AZ
156 re_node_set *cur_nodes, Idx cur_str,
157 Idx subexp_num, int type);
158static bool build_trtable (const re_dfa_t *dfa, re_dfastate_t *state);
434d3784 159#ifdef RE_ENABLE_I18N
eb04c213
AZ
160static int check_node_accept_bytes (const re_dfa_t *dfa, Idx node_idx,
161 const re_string_t *input, Idx idx);
434d3784 162# ifdef _LIBC
c202c2c5 163static unsigned int find_collation_sequence_value (const unsigned char *mbs,
b41bd5bc 164 size_t name_len);
434d3784
UD
165# endif /* _LIBC */
166#endif /* RE_ENABLE_I18N */
eb04c213 167static Idx group_nodes_into_DFAstates (const re_dfa_t *dfa,
15a7d175
UD
168 const re_dfastate_t *state,
169 re_node_set *states_node,
b41bd5bc 170 bitset_t *states_ch);
eb04c213
AZ
171static bool check_node_accept (const re_match_context_t *mctx,
172 const re_token_t *node, Idx idx);
b41bd5bc 173static reg_errcode_t extend_buffers (re_match_context_t *mctx, int min_len);
3b0bdc72
UD
174\f
175/* Entry point for POSIX code. */
176
177/* regexec searches for a given pattern, specified by PREG, in the
178 string STRING.
179
180 If NMATCH is zero or REG_NOSUB was set in the cflags argument to
eb04c213 181 'regcomp', we ignore PMATCH. Otherwise, we assume PMATCH has at
3b0bdc72
UD
182 least NMATCH elements, and we set them to the offsets of the
183 corresponding matched substrings.
184
eb04c213 185 EFLAGS specifies "execution flags" which affect matching: if
3b0bdc72
UD
186 REG_NOTBOL is set, then ^ does not match at the beginning of the
187 string; if REG_NOTEOL is set, then $ does not match at the end.
188
189 We return 0 if we find a match and REG_NOMATCH if not. */
190
191int
c0feb731 192regexec (const regex_t *__restrict preg, const char *__restrict string,
9dd346ff 193 size_t nmatch, regmatch_t pmatch[], int eflags)
3b0bdc72 194{
a9388965 195 reg_errcode_t err;
eb04c213
AZ
196 Idx start, length;
197 re_dfa_t *dfa = preg->buffer;
3f2fb223
UD
198
199 if (eflags & ~(REG_NOTBOL | REG_NOTEOL | REG_STARTEND))
200 return REG_BADPAT;
201
6fefb4e0
UD
202 if (eflags & REG_STARTEND)
203 {
204 start = pmatch[0].rm_so;
205 length = pmatch[0].rm_eo;
206 }
207 else
208 {
209 start = 0;
210 length = strlen (string);
211 }
7b918993 212
eb04c213 213 lock_lock (dfa->lock);
3b0bdc72 214 if (preg->no_sub)
eb04c213 215 err = re_search_internal (preg, string, length, start, length,
6fefb4e0 216 length, 0, NULL, eflags);
3b0bdc72 217 else
eb04c213 218 err = re_search_internal (preg, string, length, start, length,
6fefb4e0 219 length, nmatch, pmatch, eflags);
eb04c213 220 lock_unlock (dfa->lock);
a9388965 221 return err != REG_NOERROR;
3b0bdc72 222}
3f2fb223 223
3b0bdc72 224#ifdef _LIBC
b68f8620
L
225libc_hidden_def (__regexec)
226
3f2fb223
UD
227# include <shlib-compat.h>
228versioned_symbol (libc, __regexec, regexec, GLIBC_2_3_4);
229
230# if SHLIB_COMPAT (libc, GLIBC_2_0, GLIBC_2_3_4)
231__typeof__ (__regexec) __compat_regexec;
232
233int
78678039 234attribute_compat_text_section
c0feb731
PE
235__compat_regexec (const regex_t *__restrict preg,
236 const char *__restrict string, size_t nmatch,
3f2fb223
UD
237 regmatch_t pmatch[], int eflags)
238{
239 return regexec (preg, string, nmatch, pmatch,
240 eflags & (REG_NOTBOL | REG_NOTEOL));
241}
242compat_symbol (libc, __compat_regexec, regexec, GLIBC_2_0);
243# endif
3b0bdc72
UD
244#endif
245
246/* Entry points for GNU code. */
247
ac3d553b
UD
248/* re_match, re_search, re_match_2, re_search_2
249
250 The former two functions operate on STRING with length LENGTH,
251 while the later two operate on concatenation of STRING1 and STRING2
252 with lengths LENGTH1 and LENGTH2, respectively.
253
254 re_match() matches the compiled pattern in BUFP against the string,
255 starting at index START.
256
257 re_search() first tries matching at index START, then it tries to match
258 starting from index START + 1, and so on. The last start position tried
259 is START + RANGE. (Thus RANGE = 0 forces re_search to operate the same
260 way as re_match().)
261
262 The parameter STOP of re_{match,search}_2 specifies that no match exceeding
263 the first STOP characters of the concatenation of the strings should be
264 concerned.
265
266 If REGS is not NULL, and BUFP->no_sub is not set, the offsets of the match
5069ff32 267 and all groups is stored in REGS. (For the "_2" variants, the offsets are
ac3d553b
UD
268 computed relative to the concatenation, not relative to the individual
269 strings.)
270
271 On success, re_match* functions return the length of the match, re_search*
272 return the position of the start of the match. Return value -1 means no
273 match was found and -2 indicates an internal error. */
3b0bdc72 274
eb04c213
AZ
275regoff_t
276re_match (struct re_pattern_buffer *bufp, const char *string, Idx length,
277 Idx start, struct re_registers *regs)
3b0bdc72 278{
eb04c213 279 return re_search_stub (bufp, string, length, start, 0, length, regs, true);
3b0bdc72
UD
280}
281#ifdef _LIBC
282weak_alias (__re_match, re_match)
283#endif
284
eb04c213
AZ
285regoff_t
286re_search (struct re_pattern_buffer *bufp, const char *string, Idx length,
287 Idx start, regoff_t range, struct re_registers *regs)
ac3d553b 288{
eb04c213
AZ
289 return re_search_stub (bufp, string, length, start, range, length, regs,
290 false);
ac3d553b
UD
291}
292#ifdef _LIBC
293weak_alias (__re_search, re_search)
294#endif
3b0bdc72 295
eb04c213
AZ
296regoff_t
297re_match_2 (struct re_pattern_buffer *bufp, const char *string1, Idx length1,
298 const char *string2, Idx length2, Idx start,
299 struct re_registers *regs, Idx stop)
3b0bdc72 300{
ac3d553b 301 return re_search_2_stub (bufp, string1, length1, string2, length2,
eb04c213 302 start, 0, regs, stop, true);
3b0bdc72
UD
303}
304#ifdef _LIBC
305weak_alias (__re_match_2, re_match_2)
306#endif
307
eb04c213
AZ
308regoff_t
309re_search_2 (struct re_pattern_buffer *bufp, const char *string1, Idx length1,
310 const char *string2, Idx length2, Idx start, regoff_t range,
311 struct re_registers *regs, Idx stop)
ac3d553b
UD
312{
313 return re_search_2_stub (bufp, string1, length1, string2, length2,
eb04c213 314 start, range, regs, stop, false);
ac3d553b
UD
315}
316#ifdef _LIBC
317weak_alias (__re_search_2, re_search_2)
318#endif
319
eb04c213 320static regoff_t
85231522 321re_search_2_stub (struct re_pattern_buffer *bufp, const char *string1,
eb04c213
AZ
322 Idx length1, const char *string2, Idx length2, Idx start,
323 regoff_t range, struct re_registers *regs,
324 Idx stop, bool ret_len)
ac3d553b
UD
325{
326 const char *str;
eb04c213
AZ
327 regoff_t rval;
328 Idx len;
d044d844 329 char *s = NULL;
ac3d553b 330
f4efbdfb
PE
331 if (__glibc_unlikely ((length1 < 0 || length2 < 0 || stop < 0
332 || INT_ADD_WRAPV (length1, length2, &len))))
ac3d553b
UD
333 return -2;
334
335 /* Concatenate the strings. */
336 if (length2 > 0)
337 if (length1 > 0)
338 {
d044d844 339 s = re_malloc (char, len);
15a7d175 340
f4efbdfb 341 if (__glibc_unlikely (s == NULL))
15a7d175 342 return -2;
c42b4152
UD
343#ifdef _LIBC
344 memcpy (__mempcpy (s, string1, length1), string2, length2);
345#else
15a7d175
UD
346 memcpy (s, string1, length1);
347 memcpy (s + length1, string2, length2);
c42b4152 348#endif
15a7d175 349 str = s;
ac3d553b
UD
350 }
351 else
352 str = string2;
353 else
354 str = string1;
355
eb04c213
AZ
356 rval = re_search_stub (bufp, str, len, start, range, stop, regs,
357 ret_len);
d044d844 358 re_free (s);
ac3d553b
UD
359 return rval;
360}
361
362/* The parameters have the same meaning as those of re_search.
363 Additional parameters:
eb04c213 364 If RET_LEN is true the length of the match is returned (re_match style);
ac3d553b
UD
365 otherwise the position of the match is returned. */
366
eb04c213
AZ
367static regoff_t
368re_search_stub (struct re_pattern_buffer *bufp, const char *string, Idx length,
369 Idx start, regoff_t range, Idx stop, struct re_registers *regs,
370 bool ret_len)
3b0bdc72 371{
a9388965 372 reg_errcode_t result;
3b0bdc72 373 regmatch_t *pmatch;
eb04c213
AZ
374 Idx nregs;
375 regoff_t rval;
ac3d553b 376 int eflags = 0;
eb04c213
AZ
377 re_dfa_t *dfa = bufp->buffer;
378 Idx last_start = start + range;
ac3d553b
UD
379
380 /* Check for out-of-range. */
f4efbdfb 381 if (__glibc_unlikely (start < 0 || start > length))
ac3d553b 382 return -1;
f4efbdfb
PE
383 if (__glibc_unlikely (length < last_start
384 || (0 <= range && last_start < start)))
eb04c213 385 last_start = length;
f4efbdfb
PE
386 else if (__glibc_unlikely (last_start < 0
387 || (range < 0 && start <= last_start)))
eb04c213 388 last_start = 0;
3b0bdc72 389
eb04c213 390 lock_lock (dfa->lock);
7b918993 391
3b0bdc72
UD
392 eflags |= (bufp->not_bol) ? REG_NOTBOL : 0;
393 eflags |= (bufp->not_eol) ? REG_NOTEOL : 0;
394
ac3d553b 395 /* Compile fastmap if we haven't yet. */
eb04c213 396 if (start < last_start && bufp->fastmap != NULL && !bufp->fastmap_accurate)
ac3d553b
UD
397 re_compile_fastmap (bufp);
398
f4efbdfb 399 if (__glibc_unlikely (bufp->no_sub))
ac3d553b 400 regs = NULL;
3b0bdc72
UD
401
402 /* We need at least 1 register. */
ac3d553b
UD
403 if (regs == NULL)
404 nregs = 1;
f4efbdfb
PE
405 else if (__glibc_unlikely (bufp->regs_allocated == REGS_FIXED
406 && regs->num_regs <= bufp->re_nsub))
ac3d553b
UD
407 {
408 nregs = regs->num_regs;
f4efbdfb 409 if (__glibc_unlikely (nregs < 1))
15a7d175
UD
410 {
411 /* Nothing can be copied to regs. */
412 regs = NULL;
413 nregs = 1;
414 }
ac3d553b
UD
415 }
416 else
417 nregs = bufp->re_nsub + 1;
3b0bdc72 418 pmatch = re_malloc (regmatch_t, nregs);
f4efbdfb 419 if (__glibc_unlikely (pmatch == NULL))
7b918993
UD
420 {
421 rval = -2;
422 goto out;
423 }
3b0bdc72 424
eb04c213 425 result = re_search_internal (bufp, string, length, start, last_start, stop,
15a7d175 426 nregs, pmatch, eflags);
3b0bdc72 427
ac3d553b
UD
428 rval = 0;
429
eb04c213 430 /* I hope we needn't fill their regs with -1's when no match was found. */
ac3d553b 431 if (result != REG_NOERROR)
eb04c213 432 rval = result == REG_NOMATCH ? -1 : -2;
ac3d553b 433 else if (regs != NULL)
3b0bdc72 434 {
ac3d553b
UD
435 /* If caller wants register contents data back, copy them. */
436 bufp->regs_allocated = re_copy_regs (regs, pmatch, nregs,
15a7d175 437 bufp->regs_allocated);
f4efbdfb 438 if (__glibc_unlikely (bufp->regs_allocated == REGS_UNALLOCATED))
15a7d175 439 rval = -2;
3b0bdc72
UD
440 }
441
f4efbdfb 442 if (__glibc_likely (rval == 0))
3b0bdc72 443 {
ac3d553b 444 if (ret_len)
15a7d175
UD
445 {
446 assert (pmatch[0].rm_so == start);
447 rval = pmatch[0].rm_eo - start;
448 }
ac3d553b 449 else
15a7d175 450 rval = pmatch[0].rm_so;
3b0bdc72 451 }
3b0bdc72 452 re_free (pmatch);
7b918993 453 out:
eb04c213 454 lock_unlock (dfa->lock);
3b0bdc72
UD
455 return rval;
456}
3b0bdc72 457
ac3d553b 458static unsigned
eb04c213 459re_copy_regs (struct re_registers *regs, regmatch_t *pmatch, Idx nregs,
9dd346ff 460 int regs_allocated)
3b0bdc72 461{
ac3d553b 462 int rval = REGS_REALLOCATE;
eb04c213
AZ
463 Idx i;
464 Idx need_regs = nregs + 1;
465 /* We need one extra element beyond 'num_regs' for the '-1' marker GNU code
ac3d553b
UD
466 uses. */
467
468 /* Have the register data arrays been allocated? */
469 if (regs_allocated == REGS_UNALLOCATED)
44777771 470 { /* No. So allocate them with malloc. */
a96c63ed 471 regs->start = re_malloc (regoff_t, need_regs);
f4efbdfb 472 if (__glibc_unlikely (regs->start == NULL))
15a7d175 473 return REGS_UNALLOCATED;
74bc9f14 474 regs->end = re_malloc (regoff_t, need_regs);
f4efbdfb 475 if (__glibc_unlikely (regs->end == NULL))
74bc9f14
PE
476 {
477 re_free (regs->start);
478 return REGS_UNALLOCATED;
479 }
ac3d553b
UD
480 regs->num_regs = need_regs;
481 }
482 else if (regs_allocated == REGS_REALLOCATE)
483 { /* Yes. If we need more elements than were already
15a7d175
UD
484 allocated, reallocate them. If we need fewer, just
485 leave it alone. */
f4efbdfb 486 if (__glibc_unlikely (need_regs > regs->num_regs))
15a7d175 487 {
44777771 488 regoff_t *new_start = re_realloc (regs->start, regoff_t, need_regs);
74bc9f14 489 regoff_t *new_end;
f4efbdfb 490 if (__glibc_unlikely (new_start == NULL))
a96c63ed 491 return REGS_UNALLOCATED;
74bc9f14 492 new_end = re_realloc (regs->end, regoff_t, need_regs);
f4efbdfb 493 if (__glibc_unlikely (new_end == NULL))
74bc9f14
PE
494 {
495 re_free (new_start);
496 return REGS_UNALLOCATED;
497 }
44777771
UD
498 regs->start = new_start;
499 regs->end = new_end;
15a7d175
UD
500 regs->num_regs = need_regs;
501 }
ac3d553b
UD
502 }
503 else
504 {
505 assert (regs_allocated == REGS_FIXED);
506 /* This function may not be called with REGS_FIXED and nregs too big. */
507 assert (regs->num_regs >= nregs);
508 rval = REGS_FIXED;
509 }
510
511 /* Copy the regs. */
512 for (i = 0; i < nregs; ++i)
513 {
514 regs->start[i] = pmatch[i].rm_so;
515 regs->end[i] = pmatch[i].rm_eo;
516 }
517 for ( ; i < regs->num_regs; ++i)
518 regs->start[i] = regs->end[i] = -1;
519
520 return rval;
3b0bdc72 521}
3b0bdc72
UD
522
523/* Set REGS to hold NUM_REGS registers, storing them in STARTS and
524 ENDS. Subsequent matches using PATTERN_BUFFER and REGS will use
525 this memory for recording register information. STARTS and ENDS
526 must be allocated using the malloc library routine, and must each
527 be at least NUM_REGS * sizeof (regoff_t) bytes long.
528
529 If NUM_REGS == 0, then subsequent matches should allocate their own
530 register data.
531
532 Unless this function is called, the first search or match using
533 PATTERN_BUFFER will allocate its own register data, without
534 freeing the old data. */
535
536void
9dd346ff 537re_set_registers (struct re_pattern_buffer *bufp, struct re_registers *regs,
eb04c213 538 __re_size_t num_regs, regoff_t *starts, regoff_t *ends)
3b0bdc72
UD
539{
540 if (num_regs)
541 {
542 bufp->regs_allocated = REGS_REALLOCATE;
543 regs->num_regs = num_regs;
544 regs->start = starts;
545 regs->end = ends;
546 }
547 else
548 {
549 bufp->regs_allocated = REGS_UNALLOCATED;
550 regs->num_regs = 0;
eb04c213 551 regs->start = regs->end = NULL;
3b0bdc72
UD
552 }
553}
554#ifdef _LIBC
555weak_alias (__re_set_registers, re_set_registers)
556#endif
557\f
558/* Entry points compatible with 4.2 BSD regex library. We don't define
559 them unless specifically requested. */
560
561#if defined _REGEX_RE_COMP || defined _LIBC
562int
563# ifdef _LIBC
564weak_function
565# endif
80d9be81 566re_exec (const char *s)
3b0bdc72
UD
567{
568 return 0 == regexec (&re_comp_buf, s, 0, NULL, 0);
569}
570#endif /* _REGEX_RE_COMP */
571\f
3b0bdc72
UD
572/* Internal entry point. */
573
574/* Searches for a compiled pattern PREG in the string STRING, whose
575 length is LENGTH. NMATCH, PMATCH, and EFLAGS have the same
eb04c213
AZ
576 meaning as with regexec. LAST_START is START + RANGE, where
577 START and RANGE have the same meaning as with re_search.
a9388965
UD
578 Return REG_NOERROR if we find a match, and REG_NOMATCH if not,
579 otherwise return the error code.
3b0bdc72 580 Note: We assume front end functions already check ranges.
eb04c213 581 (0 <= LAST_START && LAST_START <= LENGTH) */
3b0bdc72 582
a9388965 583static reg_errcode_t
b41bd5bc 584__attribute_warn_unused_result__
eb04c213
AZ
585re_search_internal (const regex_t *preg, const char *string, Idx length,
586 Idx start, Idx last_start, Idx stop, size_t nmatch,
85231522 587 regmatch_t pmatch[], int eflags)
3b0bdc72 588{
a9388965 589 reg_errcode_t err;
eb04c213
AZ
590 const re_dfa_t *dfa = preg->buffer;
591 Idx left_lim, right_lim;
592 int incr;
593 bool fl_longest_match;
594 int match_kind;
595 Idx match_first;
596 Idx match_last = -1;
597 Idx extra_nmatch;
598 bool sb;
599 int ch;
e3a87852
UD
600#if defined _LIBC || (defined __STDC_VERSION__ && __STDC_VERSION__ >= 199901L)
601 re_match_context_t mctx = { .dfa = dfa };
602#else
3b0bdc72 603 re_match_context_t mctx;
e3a87852 604#endif
eb04c213
AZ
605 char *fastmap = ((preg->fastmap != NULL && preg->fastmap_accurate
606 && start != last_start && !preg->can_be_null)
607 ? preg->fastmap : NULL);
997470b3 608 RE_TRANSLATE_TYPE t = preg->translate;
3b0bdc72 609
e3a87852
UD
610#if !(defined _LIBC || (defined __STDC_VERSION__ && __STDC_VERSION__ >= 199901L))
611 memset (&mctx, '\0', sizeof (re_match_context_t));
612 mctx.dfa = dfa;
613#endif
614
963d8d78
UD
615 extra_nmatch = (nmatch > preg->re_nsub) ? nmatch - (preg->re_nsub + 1) : 0;
616 nmatch -= extra_nmatch;
617
3b0bdc72 618 /* Check if the DFA haven't been compiled. */
f4efbdfb
PE
619 if (__glibc_unlikely (preg->used == 0 || dfa->init_state == NULL
620 || dfa->init_state_word == NULL
621 || dfa->init_state_nl == NULL
622 || dfa->init_state_begbuf == NULL))
a9388965 623 return REG_NOMATCH;
3b0bdc72 624
c70f81dd
UD
625#ifdef DEBUG
626 /* We assume front-end functions already check them. */
eb04c213 627 assert (0 <= last_start && last_start <= length);
c70f81dd
UD
628#endif
629
630 /* If initial states with non-begbuf contexts have no elements,
631 the regex must be anchored. If preg->newline_anchor is set,
632 we'll never use init_state_nl, so do not check it. */
633 if (dfa->init_state->nodes.nelem == 0
634 && dfa->init_state_word->nodes.nelem == 0
635 && (dfa->init_state_nl->nodes.nelem == 0
636 || !preg->newline_anchor))
637 {
eb04c213
AZ
638 if (start != 0 && last_start != 0)
639 return REG_NOMATCH;
640 start = last_start = 0;
c70f81dd
UD
641 }
642
3b0bdc72 643 /* We must check the longest matching, if nmatch > 0. */
6291ee3c 644 fl_longest_match = (nmatch != 0 || dfa->nbackref);
3b0bdc72 645
56b168be 646 err = re_string_allocate (&mctx.input, string, length, dfa->nodes_len + 1,
eb04c213
AZ
647 preg->translate, (preg->syntax & RE_ICASE) != 0,
648 dfa);
f4efbdfb 649 if (__glibc_unlikely (err != REG_NOERROR))
1b2c2628 650 goto free_return;
56b168be
UD
651 mctx.input.stop = stop;
652 mctx.input.raw_stop = stop;
653 mctx.input.newline_anchor = preg->newline_anchor;
612546c6 654
56b168be 655 err = match_ctx_init (&mctx, eflags, dfa->nbackref * 2);
f4efbdfb 656 if (__glibc_unlikely (err != REG_NOERROR))
1b2c2628 657 goto free_return;
612546c6 658
3b0bdc72
UD
659 /* We will log all the DFA states through which the dfa pass,
660 if nmatch > 1, or this dfa has "multibyte node", which is a
661 back-reference or a node which can accept multibyte character or
662 multi character collating element. */
663 if (nmatch > 1 || dfa->has_mb_node)
a9388965 664 {
eadc09f2 665 /* Avoid overflow. */
f4efbdfb
PE
666 if (__glibc_unlikely ((MIN (IDX_MAX, SIZE_MAX / sizeof (re_dfastate_t *))
667 <= mctx.input.bufs_len)))
eadc09f2
PE
668 {
669 err = REG_ESPACE;
670 goto free_return;
671 }
672
56b168be 673 mctx.state_log = re_malloc (re_dfastate_t *, mctx.input.bufs_len + 1);
f4efbdfb 674 if (__glibc_unlikely (mctx.state_log == NULL))
15a7d175
UD
675 {
676 err = REG_ESPACE;
677 goto free_return;
678 }
a9388965 679 }
3b0bdc72 680 else
612546c6 681 mctx.state_log = NULL;
3b0bdc72 682
612546c6 683 match_first = start;
56b168be
UD
684 mctx.input.tip_context = (eflags & REG_NOTBOL) ? CONTEXT_BEGBUF
685 : CONTEXT_NEWLINE | CONTEXT_BEGBUF;
612546c6 686
eb04c213
AZ
687 /* Check incrementally whether the input string matches. */
688 incr = (last_start < start) ? -1 : 1;
689 left_lim = (last_start < start) ? last_start : start;
690 right_lim = (last_start < start) ? start : last_start;
3c0fb574 691 sb = dfa->mb_cur_max == 1;
bb677c95
UD
692 match_kind =
693 (fastmap
694 ? ((sb || !(preg->syntax & RE_ICASE || t) ? 4 : 0)
eb04c213 695 | (start <= last_start ? 2 : 0)
bb677c95
UD
696 | (t != NULL ? 1 : 0))
697 : 8);
698
699 for (;; match_first += incr)
3b0bdc72 700 {
bb677c95
UD
701 err = REG_NOMATCH;
702 if (match_first < left_lim || right_lim < match_first)
703 goto free_return;
704
705 /* Advance as rapidly as possible through the string, until we
706 find a plausible place to start matching. This may be done
707 with varying efficiency, so there are various possibilities:
708 only the most common of them are specialized, in order to
709 save on code size. We use a switch statement for speed. */
710 switch (match_kind)
1b2c2628 711 {
bb677c95
UD
712 case 8:
713 /* No fastmap. */
714 break;
715
716 case 7:
717 /* Fastmap with single-byte translation, match forward. */
f4efbdfb 718 while (__glibc_likely (match_first < right_lim)
bb677c95
UD
719 && !fastmap[t[(unsigned char) string[match_first]]])
720 ++match_first;
721 goto forward_match_found_start_or_reached_end;
722
723 case 6:
724 /* Fastmap without translation, match forward. */
f4efbdfb 725 while (__glibc_likely (match_first < right_lim)
bb677c95
UD
726 && !fastmap[(unsigned char) string[match_first]])
727 ++match_first;
728
729 forward_match_found_start_or_reached_end:
f4efbdfb 730 if (__glibc_unlikely (match_first == right_lim))
1b2c2628 731 {
bb677c95
UD
732 ch = match_first >= length
733 ? 0 : (unsigned char) string[match_first];
734 if (!fastmap[t ? t[ch] : ch])
735 goto free_return;
1b2c2628 736 }
bb677c95
UD
737 break;
738
739 case 4:
740 case 5:
741 /* Fastmap without multi-byte translation, match backwards. */
742 while (match_first >= left_lim)
1b2c2628 743 {
bb677c95
UD
744 ch = match_first >= length
745 ? 0 : (unsigned char) string[match_first];
746 if (fastmap[t ? t[ch] : ch])
747 break;
748 --match_first;
749 }
750 if (match_first < left_lim)
751 goto free_return;
752 break;
1b2c2628 753
bb677c95
UD
754 default:
755 /* In this case, we can't determine easily the current byte,
756 since it might be a component byte of a multibyte
757 character. Then we use the constructed buffer instead. */
758 for (;;)
759 {
760 /* If MATCH_FIRST is out of the valid range, reconstruct the
761 buffers. */
eb04c213 762 __re_size_t offset = match_first - mctx.input.raw_mbs_idx;
f4efbdfb
PE
763 if (__glibc_unlikely (offset
764 >= (__re_size_t) mctx.input.valid_raw_len))
1b2c2628 765 {
bb677c95
UD
766 err = re_string_reconstruct (&mctx.input, match_first,
767 eflags);
f4efbdfb 768 if (__glibc_unlikely (err != REG_NOERROR))
bb677c95
UD
769 goto free_return;
770
771 offset = match_first - mctx.input.raw_mbs_idx;
1b2c2628 772 }
bb677c95
UD
773 /* If MATCH_FIRST is out of the buffer, leave it as '\0'.
774 Note that MATCH_FIRST must not be smaller than 0. */
775 ch = (match_first >= length
776 ? 0 : re_string_byte_at (&mctx.input, offset));
777 if (fastmap[ch])
1b2c2628 778 break;
bb677c95
UD
779 match_first += incr;
780 if (match_first < left_lim || match_first > right_lim)
2da42bc0
UD
781 {
782 err = REG_NOMATCH;
783 goto free_return;
784 }
1b2c2628 785 }
bb677c95 786 break;
1b2c2628 787 }
612546c6 788
1b2c2628 789 /* Reconstruct the buffers so that the matcher can assume that
fe9434bb 790 the matching starts from the beginning of the buffer. */
56b168be 791 err = re_string_reconstruct (&mctx.input, match_first, eflags);
f4efbdfb 792 if (__glibc_unlikely (err != REG_NOERROR))
1b2c2628 793 goto free_return;
bb677c95 794
3b0bdc72 795#ifdef RE_ENABLE_I18N
bb677c95
UD
796 /* Don't consider this char as a possible match start if it part,
797 yet isn't the head, of a multibyte character. */
798 if (!sb && !re_string_first_byte (&mctx.input, 0))
799 continue;
3b0bdc72 800#endif
bb677c95
UD
801
802 /* It seems to be appropriate one, then use the matcher. */
803 /* We assume that the matching starts from 0. */
804 mctx.state_log_top = mctx.nbkref_ents = mctx.max_mb_elem_len = 0;
805 match_last = check_matching (&mctx, fl_longest_match,
eb04c213 806 start <= last_start ? &match_first : NULL);
bb677c95 807 if (match_last != -1)
1b2c2628 808 {
f4efbdfb 809 if (__glibc_unlikely (match_last == -2))
1b2c2628 810 {
bb677c95
UD
811 err = REG_ESPACE;
812 goto free_return;
813 }
814 else
815 {
816 mctx.match_last = match_last;
817 if ((!preg->no_sub && nmatch > 1) || dfa->nbackref)
1b2c2628 818 {
bb677c95
UD
819 re_dfastate_t *pstate = mctx.state_log[match_last];
820 mctx.last_node = check_halt_state_context (&mctx, pstate,
821 match_last);
1b2c2628 822 }
bb677c95
UD
823 if ((!preg->no_sub && nmatch > 1 && dfa->has_plural_match)
824 || dfa->nbackref)
6291ee3c 825 {
bb677c95
UD
826 err = prune_impossible_nodes (&mctx);
827 if (err == REG_NOERROR)
828 break;
f4efbdfb 829 if (__glibc_unlikely (err != REG_NOMATCH))
bb677c95
UD
830 goto free_return;
831 match_last = -1;
6291ee3c 832 }
bb677c95
UD
833 else
834 break; /* We found a match. */
1b2c2628
UD
835 }
836 }
bb677c95
UD
837
838 match_ctx_clean (&mctx);
3b0bdc72
UD
839 }
840
bb677c95
UD
841#ifdef DEBUG
842 assert (match_last != -1);
843 assert (err == REG_NOERROR);
844#endif
845
3b0bdc72 846 /* Set pmatch[] if we need. */
bb677c95 847 if (nmatch > 0)
3b0bdc72 848 {
eb04c213 849 Idx reg_idx;
3b0bdc72
UD
850
851 /* Initialize registers. */
97fd3a30 852 for (reg_idx = 1; reg_idx < nmatch; ++reg_idx)
15a7d175 853 pmatch[reg_idx].rm_so = pmatch[reg_idx].rm_eo = -1;
3b0bdc72
UD
854
855 /* Set the points where matching start/end. */
612546c6 856 pmatch[0].rm_so = 0;
6291ee3c 857 pmatch[0].rm_eo = mctx.match_last;
eb04c213
AZ
858 /* FIXME: This function should fail if mctx.match_last exceeds
859 the maximum possible regoff_t value. We need a new error
860 code REG_OVERFLOW. */
3b0bdc72
UD
861
862 if (!preg->no_sub && nmatch > 1)
15a7d175 863 {
15a7d175
UD
864 err = set_regs (preg, &mctx, nmatch, pmatch,
865 dfa->has_plural_match && dfa->nbackref > 0);
f4efbdfb 866 if (__glibc_unlikely (err != REG_NOERROR))
15a7d175
UD
867 goto free_return;
868 }
612546c6 869
5069ff32 870 /* At last, add the offset to each register, since we slid
97fd3a30
UD
871 the buffers so that we could assume that the matching starts
872 from 0. */
612546c6 873 for (reg_idx = 0; reg_idx < nmatch; ++reg_idx)
15a7d175
UD
874 if (pmatch[reg_idx].rm_so != -1)
875 {
c0d5034e 876#ifdef RE_ENABLE_I18N
f4efbdfb 877 if (__glibc_unlikely (mctx.input.offsets_needed != 0))
bb3f4825 878 {
01ed6ceb
UD
879 pmatch[reg_idx].rm_so =
880 (pmatch[reg_idx].rm_so == mctx.input.valid_len
881 ? mctx.input.valid_raw_len
882 : mctx.input.offsets[pmatch[reg_idx].rm_so]);
883 pmatch[reg_idx].rm_eo =
884 (pmatch[reg_idx].rm_eo == mctx.input.valid_len
885 ? mctx.input.valid_raw_len
886 : mctx.input.offsets[pmatch[reg_idx].rm_eo]);
bb3f4825 887 }
c0d5034e 888#else
56b168be 889 assert (mctx.input.offsets_needed == 0);
c0d5034e 890#endif
15a7d175
UD
891 pmatch[reg_idx].rm_so += match_first;
892 pmatch[reg_idx].rm_eo += match_first;
893 }
963d8d78
UD
894 for (reg_idx = 0; reg_idx < extra_nmatch; ++reg_idx)
895 {
896 pmatch[nmatch + reg_idx].rm_so = -1;
897 pmatch[nmatch + reg_idx].rm_eo = -1;
898 }
c06a6956
UD
899
900 if (dfa->subexp_map)
2da42bc0
UD
901 for (reg_idx = 0; reg_idx + 1 < nmatch; reg_idx++)
902 if (dfa->subexp_map[reg_idx] != reg_idx)
903 {
904 pmatch[reg_idx + 1].rm_so
905 = pmatch[dfa->subexp_map[reg_idx] + 1].rm_so;
906 pmatch[reg_idx + 1].rm_eo
907 = pmatch[dfa->subexp_map[reg_idx] + 1].rm_eo;
908 }
3b0bdc72 909 }
bb677c95 910
1b2c2628 911 free_return:
612546c6 912 re_free (mctx.state_log);
3b0bdc72
UD
913 if (dfa->nbackref)
914 match_ctx_free (&mctx);
56b168be 915 re_string_destruct (&mctx.input);
1b2c2628 916 return err;
3b0bdc72
UD
917}
918
6291ee3c 919static reg_errcode_t
b41bd5bc 920__attribute_warn_unused_result__
9dd346ff 921prune_impossible_nodes (re_match_context_t *mctx)
6291ee3c 922{
76b864c8 923 const re_dfa_t *const dfa = mctx->dfa;
eb04c213 924 Idx halt_node, match_last;
6291ee3c 925 reg_errcode_t ret;
6291ee3c
UD
926 re_dfastate_t **sifted_states;
927 re_dfastate_t **lim_states = NULL;
928 re_sift_context_t sctx;
929#ifdef DEBUG
930 assert (mctx->state_log != NULL);
931#endif
932 match_last = mctx->match_last;
933 halt_node = mctx->last_node;
4cd02867
PE
934
935 /* Avoid overflow. */
f4efbdfb
PE
936 if (__glibc_unlikely (MIN (IDX_MAX, SIZE_MAX / sizeof (re_dfastate_t *))
937 <= match_last))
4cd02867
PE
938 return REG_ESPACE;
939
6291ee3c 940 sifted_states = re_malloc (re_dfastate_t *, match_last + 1);
f4efbdfb 941 if (__glibc_unlikely (sifted_states == NULL))
6291ee3c
UD
942 {
943 ret = REG_ESPACE;
944 goto free_return;
945 }
946 if (dfa->nbackref)
947 {
948 lim_states = re_malloc (re_dfastate_t *, match_last + 1);
f4efbdfb 949 if (__glibc_unlikely (lim_states == NULL))
6291ee3c
UD
950 {
951 ret = REG_ESPACE;
952 goto free_return;
953 }
954 while (1)
955 {
956 memset (lim_states, '\0',
957 sizeof (re_dfastate_t *) * (match_last + 1));
6291ee3c 958 sift_ctx_init (&sctx, sifted_states, lim_states, halt_node,
d2c38eb3 959 match_last);
e3a87852 960 ret = sift_states_backward (mctx, &sctx);
6291ee3c 961 re_node_set_free (&sctx.limits);
f4efbdfb 962 if (__glibc_unlikely (ret != REG_NOERROR))
6291ee3c
UD
963 goto free_return;
964 if (sifted_states[0] != NULL || lim_states[0] != NULL)
965 break;
966 do
967 {
968 --match_last;
969 if (match_last < 0)
970 {
971 ret = REG_NOMATCH;
972 goto free_return;
973 }
97fd3a30
UD
974 } while (mctx->state_log[match_last] == NULL
975 || !mctx->state_log[match_last]->halt);
e3a87852 976 halt_node = check_halt_state_context (mctx,
6291ee3c 977 mctx->state_log[match_last],
e3a87852 978 match_last);
6291ee3c
UD
979 }
980 ret = merge_state_array (dfa, sifted_states, lim_states,
981 match_last + 1);
982 re_free (lim_states);
983 lim_states = NULL;
f4efbdfb 984 if (__glibc_unlikely (ret != REG_NOERROR))
6291ee3c
UD
985 goto free_return;
986 }
987 else
988 {
d2c38eb3 989 sift_ctx_init (&sctx, sifted_states, lim_states, halt_node, match_last);
e3a87852 990 ret = sift_states_backward (mctx, &sctx);
6291ee3c 991 re_node_set_free (&sctx.limits);
f4efbdfb 992 if (__glibc_unlikely (ret != REG_NOERROR))
6291ee3c 993 goto free_return;
76c7f2cd
UD
994 if (sifted_states[0] == NULL)
995 {
996 ret = REG_NOMATCH;
997 goto free_return;
998 }
6291ee3c
UD
999 }
1000 re_free (mctx->state_log);
1001 mctx->state_log = sifted_states;
1002 sifted_states = NULL;
1003 mctx->last_node = halt_node;
1004 mctx->match_last = match_last;
1005 ret = REG_NOERROR;
1006 free_return:
1007 re_free (sifted_states);
1008 re_free (lim_states);
1009 return ret;
1010}
1011
a9388965 1012/* Acquire an initial state and return it.
3b0bdc72
UD
1013 We must select appropriate initial state depending on the context,
1014 since initial states may have constraints like "\<", "^", etc.. */
1015
bb3f4825 1016static inline re_dfastate_t *
eb04c213 1017__attribute__ ((always_inline))
e2f55264 1018acquire_init_state_context (reg_errcode_t *err, const re_match_context_t *mctx,
eb04c213 1019 Idx idx)
3b0bdc72 1020{
76b864c8 1021 const re_dfa_t *const dfa = mctx->dfa;
3b0bdc72
UD
1022 if (dfa->init_state->has_constraint)
1023 {
1024 unsigned int context;
56b168be 1025 context = re_string_context_at (&mctx->input, idx - 1, mctx->eflags);
3b0bdc72 1026 if (IS_WORD_CONTEXT (context))
15a7d175 1027 return dfa->init_state_word;
3b0bdc72 1028 else if (IS_ORDINARY_CONTEXT (context))
15a7d175 1029 return dfa->init_state;
3b0bdc72 1030 else if (IS_BEGBUF_CONTEXT (context) && IS_NEWLINE_CONTEXT (context))
15a7d175 1031 return dfa->init_state_begbuf;
3b0bdc72 1032 else if (IS_NEWLINE_CONTEXT (context))
15a7d175 1033 return dfa->init_state_nl;
3b0bdc72 1034 else if (IS_BEGBUF_CONTEXT (context))
15a7d175
UD
1035 {
1036 /* It is relatively rare case, then calculate on demand. */
4c595adb
UD
1037 return re_acquire_state_context (err, dfa,
1038 dfa->init_state->entrance_nodes,
1039 context);
15a7d175 1040 }
3b0bdc72 1041 else
15a7d175
UD
1042 /* Must not happen? */
1043 return dfa->init_state;
3b0bdc72
UD
1044 }
1045 else
1046 return dfa->init_state;
1047}
1048
1049/* Check whether the regular expression match input string INPUT or not,
eb04c213
AZ
1050 and return the index where the matching end. Return -1 if
1051 there is no match, and return -2 in case of an error.
612546c6 1052 FL_LONGEST_MATCH means we want the POSIX longest matching.
15bad1a5
UD
1053 If P_MATCH_FIRST is not NULL, and the match fails, it is set to the
1054 next place where we may want to try matching.
eb04c213 1055 Note that the matcher assumes that the matching starts from the current
612546c6 1056 index of the buffer. */
3b0bdc72 1057
eb04c213 1058static Idx
b41bd5bc 1059__attribute_warn_unused_result__
eb04c213
AZ
1060check_matching (re_match_context_t *mctx, bool fl_longest_match,
1061 Idx *p_match_first)
3b0bdc72 1062{
76b864c8 1063 const re_dfa_t *const dfa = mctx->dfa;
a9388965 1064 reg_errcode_t err;
eb04c213
AZ
1065 Idx match = 0;
1066 Idx match_last = -1;
1067 Idx cur_str_idx = re_string_cur_idx (&mctx->input);
3b0bdc72 1068 re_dfastate_t *cur_state;
eb04c213
AZ
1069 bool at_init_state = p_match_first != NULL;
1070 Idx next_start_idx = cur_str_idx;
3b0bdc72 1071
4c595adb 1072 err = REG_NOERROR;
e3a87852 1073 cur_state = acquire_init_state_context (&err, mctx, cur_str_idx);
4c595adb 1074 /* An initial state must not be NULL (invalid). */
f4efbdfb 1075 if (__glibc_unlikely (cur_state == NULL))
4c595adb
UD
1076 {
1077 assert (err == REG_ESPACE);
1078 return -2;
1079 }
0742e48e 1080
4c595adb 1081 if (mctx->state_log != NULL)
6291ee3c 1082 {
4c595adb 1083 mctx->state_log[cur_str_idx] = cur_state;
6291ee3c 1084
4c595adb
UD
1085 /* Check OP_OPEN_SUBEXP in the initial state in case that we use them
1086 later. E.g. Processing back references. */
f4efbdfb 1087 if (__glibc_unlikely (dfa->nbackref))
bb3f4825 1088 {
eb04c213 1089 at_init_state = false;
4c595adb 1090 err = check_subexp_matching_top (mctx, &cur_state->nodes, 0);
f4efbdfb 1091 if (__glibc_unlikely (err != REG_NOERROR))
bb3f4825 1092 return err;
4c595adb
UD
1093
1094 if (cur_state->has_backref)
1095 {
1096 err = transit_state_bkref (mctx, &cur_state->nodes);
f4efbdfb 1097 if (__glibc_unlikely (err != REG_NOERROR))
2da42bc0 1098 return err;
4c595adb 1099 }
bb3f4825 1100 }
0742e48e
UD
1101 }
1102
3b0bdc72 1103 /* If the RE accepts NULL string. */
f4efbdfb 1104 if (__glibc_unlikely (cur_state->halt))
3b0bdc72
UD
1105 {
1106 if (!cur_state->has_constraint
e3a87852 1107 || check_halt_state_context (mctx, cur_state, cur_str_idx))
15a7d175
UD
1108 {
1109 if (!fl_longest_match)
1110 return cur_str_idx;
1111 else
1112 {
1113 match_last = cur_str_idx;
1114 match = 1;
1115 }
1116 }
3b0bdc72
UD
1117 }
1118
56b168be 1119 while (!re_string_eoi (&mctx->input))
3b0bdc72 1120 {
15bad1a5 1121 re_dfastate_t *old_state = cur_state;
eb04c213 1122 Idx next_char_idx = re_string_cur_idx (&mctx->input) + 1;
bb677c95 1123
f4efbdfb 1124 if ((__glibc_unlikely (next_char_idx >= mctx->input.bufs_len)
8887a920 1125 && mctx->input.bufs_len < mctx->input.len)
f4efbdfb 1126 || (__glibc_unlikely (next_char_idx >= mctx->input.valid_len)
2da42bc0
UD
1127 && mctx->input.valid_len < mctx->input.len))
1128 {
a445af0b 1129 err = extend_buffers (mctx, next_char_idx + 1);
f4efbdfb 1130 if (__glibc_unlikely (err != REG_NOERROR))
bb677c95
UD
1131 {
1132 assert (err == REG_ESPACE);
1133 return -2;
1134 }
2da42bc0 1135 }
bb677c95 1136
e3a87852 1137 cur_state = transit_state (&err, mctx, cur_state);
4c595adb
UD
1138 if (mctx->state_log != NULL)
1139 cur_state = merge_state_with_log (&err, mctx, cur_state);
15bad1a5 1140
4c595adb 1141 if (cur_state == NULL)
15a7d175 1142 {
4c595adb
UD
1143 /* Reached the invalid state or an error. Try to recover a valid
1144 state using the state log, if available and if we have not
1145 already found a valid (even if not the longest) match. */
f4efbdfb 1146 if (__glibc_unlikely (err != REG_NOERROR))
15a7d175 1147 return -2;
4c595adb
UD
1148
1149 if (mctx->state_log == NULL
1150 || (match && !fl_longest_match)
1151 || (cur_state = find_recover_state (&err, mctx)) == NULL)
15a7d175 1152 break;
4c595adb
UD
1153 }
1154
f4efbdfb 1155 if (__glibc_unlikely (at_init_state))
4c595adb
UD
1156 {
1157 if (old_state == cur_state)
bb677c95 1158 next_start_idx = next_char_idx;
c13c99fa 1159 else
eb04c213 1160 at_init_state = false;
15a7d175 1161 }
3b0bdc72 1162
4c595adb 1163 if (cur_state->halt)
15a7d175 1164 {
4c595adb 1165 /* Reached a halt state.
15a7d175
UD
1166 Check the halt state can satisfy the current context. */
1167 if (!cur_state->has_constraint
e3a87852 1168 || check_halt_state_context (mctx, cur_state,
56b168be 1169 re_string_cur_idx (&mctx->input)))
15a7d175
UD
1170 {
1171 /* We found an appropriate halt state. */
56b168be 1172 match_last = re_string_cur_idx (&mctx->input);
15a7d175 1173 match = 1;
bb677c95
UD
1174
1175 /* We found a match, do not modify match_first below. */
1176 p_match_first = NULL;
15a7d175
UD
1177 if (!fl_longest_match)
1178 break;
1179 }
1180 }
bb677c95 1181 }
15bad1a5 1182
bb677c95 1183 if (p_match_first)
4c595adb 1184 *p_match_first += next_start_idx;
15bad1a5 1185
3b0bdc72
UD
1186 return match_last;
1187}
1188
1189/* Check NODE match the current context. */
1190
eb04c213
AZ
1191static bool
1192check_halt_node_context (const re_dfa_t *dfa, Idx node, unsigned int context)
3b0bdc72 1193{
3b0bdc72 1194 re_token_type_t type = dfa->nodes[node].type;
485d775d
UD
1195 unsigned int constraint = dfa->nodes[node].constraint;
1196 if (type != END_OF_RE)
eb04c213 1197 return false;
485d775d 1198 if (!constraint)
eb04c213 1199 return true;
485d775d 1200 if (NOT_SATISFY_NEXT_CONSTRAINT (constraint, context))
eb04c213
AZ
1201 return false;
1202 return true;
3b0bdc72
UD
1203}
1204
1205/* Check the halt state STATE match the current context.
1206 Return 0 if not match, if the node, STATE has, is a halt node and
1207 match the context, return the node. */
1208
eb04c213 1209static Idx
e2f55264 1210check_halt_state_context (const re_match_context_t *mctx,
eb04c213 1211 const re_dfastate_t *state, Idx idx)
3b0bdc72 1212{
eb04c213 1213 Idx i;
3b0bdc72
UD
1214 unsigned int context;
1215#ifdef DEBUG
1216 assert (state->halt);
1217#endif
56b168be 1218 context = re_string_context_at (&mctx->input, idx, mctx->eflags);
3b0bdc72 1219 for (i = 0; i < state->nodes.nelem; ++i)
e3a87852 1220 if (check_halt_node_context (mctx->dfa, state->nodes.elems[i], context))
3b0bdc72
UD
1221 return state->nodes.elems[i];
1222 return 0;
1223}
1224
a9388965
UD
1225/* Compute the next node to which "NFA" transit from NODE("NFA" is a NFA
1226 corresponding to the DFA).
eb04c213
AZ
1227 Return the destination node, and update EPS_VIA_NODES;
1228 return -1 in case of errors. */
3b0bdc72 1229
eb04c213
AZ
1230static Idx
1231proceed_next_node (const re_match_context_t *mctx, Idx nregs, regmatch_t *regs,
1232 Idx *pidx, Idx node, re_node_set *eps_via_nodes,
e2f55264 1233 struct re_fail_stack_t *fs)
3b0bdc72 1234{
76b864c8 1235 const re_dfa_t *const dfa = mctx->dfa;
eb04c213
AZ
1236 Idx i;
1237 bool ok;
3b0bdc72
UD
1238 if (IS_EPSILON_NODE (dfa->nodes[node].type))
1239 {
485d775d 1240 re_node_set *cur_nodes = &mctx->state_log[*pidx]->nodes;
5cf1ec52 1241 re_node_set *edests = &dfa->edests[node];
eb04c213
AZ
1242 Idx dest_node;
1243 ok = re_node_set_insert (eps_via_nodes, node);
f4efbdfb 1244 if (__glibc_unlikely (! ok))
44777771 1245 return -2;
eb04c213
AZ
1246 /* Pick up a valid destination, or return -1 if none
1247 is found. */
5cf1ec52 1248 for (dest_node = -1, i = 0; i < edests->nelem; ++i)
15a7d175 1249 {
eb04c213 1250 Idx candidate = edests->elems[i];
15a7d175
UD
1251 if (!re_node_set_contains (cur_nodes, candidate))
1252 continue;
eb04c213 1253 if (dest_node == -1)
5cf1ec52
UD
1254 dest_node = candidate;
1255
2da42bc0 1256 else
5cf1ec52
UD
1257 {
1258 /* In order to avoid infinite loop like "(a*)*", return the second
2da42bc0 1259 epsilon-transition if the first was already considered. */
5cf1ec52 1260 if (re_node_set_contains (eps_via_nodes, dest_node))
2da42bc0 1261 return candidate;
5cf1ec52
UD
1262
1263 /* Otherwise, push the second epsilon-transition on the fail stack. */
1264 else if (fs != NULL
1265 && push_fail_stack (fs, *pidx, candidate, nregs, regs,
2da42bc0 1266 eps_via_nodes))
5cf1ec52
UD
1267 return -2;
1268
1269 /* We know we are going to exit. */
1270 break;
1271 }
1272 }
1273 return dest_node;
3b0bdc72
UD
1274 }
1275 else
1276 {
eb04c213 1277 Idx naccepted = 0;
3b0bdc72 1278 re_token_type_t type = dfa->nodes[node].type;
3b0bdc72 1279
434d3784 1280#ifdef RE_ENABLE_I18N
02f3550c 1281 if (dfa->nodes[node].accept_mb)
56b168be 1282 naccepted = check_node_accept_bytes (dfa, node, &mctx->input, *pidx);
434d3784
UD
1283 else
1284#endif /* RE_ENABLE_I18N */
1285 if (type == OP_BACK_REF)
15a7d175 1286 {
eb04c213 1287 Idx subexp_idx = dfa->nodes[node].opr.idx + 1;
15a7d175
UD
1288 naccepted = regs[subexp_idx].rm_eo - regs[subexp_idx].rm_so;
1289 if (fs != NULL)
1290 {
1291 if (regs[subexp_idx].rm_so == -1 || regs[subexp_idx].rm_eo == -1)
1292 return -1;
1293 else if (naccepted)
1294 {
56b168be 1295 char *buf = (char *) re_string_get_buffer (&mctx->input);
583dd860
PE
1296 if (mctx->input.valid_len - *pidx < naccepted
1297 || (memcmp (buf + regs[subexp_idx].rm_so, buf + *pidx,
1298 naccepted)
1299 != 0))
15a7d175
UD
1300 return -1;
1301 }
1302 }
1303
1304 if (naccepted == 0)
1305 {
eb04c213
AZ
1306 Idx dest_node;
1307 ok = re_node_set_insert (eps_via_nodes, node);
f4efbdfb 1308 if (__glibc_unlikely (! ok))
15a7d175
UD
1309 return -2;
1310 dest_node = dfa->edests[node].elems[0];
1311 if (re_node_set_contains (&mctx->state_log[*pidx]->nodes,
1312 dest_node))
1313 return dest_node;
1314 }
1315 }
3b0bdc72
UD
1316
1317 if (naccepted != 0
e3a87852 1318 || check_node_accept (mctx, dfa->nodes + node, *pidx))
15a7d175 1319 {
eb04c213 1320 Idx dest_node = dfa->nexts[node];
15a7d175
UD
1321 *pidx = (naccepted == 0) ? *pidx + 1 : *pidx + naccepted;
1322 if (fs && (*pidx > mctx->match_last || mctx->state_log[*pidx] == NULL
1323 || !re_node_set_contains (&mctx->state_log[*pidx]->nodes,
1324 dest_node)))
1325 return -1;
1326 re_node_set_empty (eps_via_nodes);
1327 return dest_node;
1328 }
3b0bdc72 1329 }
a3022b82
UD
1330 return -1;
1331}
1332
1333static reg_errcode_t
b41bd5bc 1334__attribute_warn_unused_result__
eb04c213
AZ
1335push_fail_stack (struct re_fail_stack_t *fs, Idx str_idx, Idx dest_node,
1336 Idx nregs, regmatch_t *regs, re_node_set *eps_via_nodes)
a3022b82
UD
1337{
1338 reg_errcode_t err;
eb04c213 1339 Idx num = fs->num++;
a3022b82
UD
1340 if (fs->num == fs->alloc)
1341 {
1b2c2628 1342 struct re_fail_stack_ent_t *new_array;
eb04c213
AZ
1343 new_array = re_realloc (fs->stack, struct re_fail_stack_ent_t,
1344 fs->alloc * 2);
1b2c2628 1345 if (new_array == NULL)
15a7d175 1346 return REG_ESPACE;
44777771 1347 fs->alloc *= 2;
1b2c2628 1348 fs->stack = new_array;
a3022b82
UD
1349 }
1350 fs->stack[num].idx = str_idx;
5cf1ec52 1351 fs->stack[num].node = dest_node;
a3022b82 1352 fs->stack[num].regs = re_malloc (regmatch_t, nregs);
5a299c96
UD
1353 if (fs->stack[num].regs == NULL)
1354 return REG_ESPACE;
a3022b82
UD
1355 memcpy (fs->stack[num].regs, regs, sizeof (regmatch_t) * nregs);
1356 err = re_node_set_init_copy (&fs->stack[num].eps_via_nodes, eps_via_nodes);
1357 return err;
1358}
1b2c2628 1359
eb04c213
AZ
1360static Idx
1361pop_fail_stack (struct re_fail_stack_t *fs, Idx *pidx, Idx nregs,
e2f55264 1362 regmatch_t *regs, re_node_set *eps_via_nodes)
a3022b82 1363{
eb04c213 1364 Idx num = --fs->num;
a3022b82 1365 assert (num >= 0);
44777771 1366 *pidx = fs->stack[num].idx;
a3022b82
UD
1367 memcpy (regs, fs->stack[num].regs, sizeof (regmatch_t) * nregs);
1368 re_node_set_free (eps_via_nodes);
485d775d 1369 re_free (fs->stack[num].regs);
a3022b82
UD
1370 *eps_via_nodes = fs->stack[num].eps_via_nodes;
1371 return fs->stack[num].node;
3b0bdc72
UD
1372}
1373
1374/* Set the positions where the subexpressions are starts/ends to registers
1375 PMATCH.
1376 Note: We assume that pmatch[0] is already set, and
97fd3a30 1377 pmatch[i].rm_so == pmatch[i].rm_eo == -1 for 0 < i < nmatch. */
3b0bdc72 1378
a9388965 1379static reg_errcode_t
b41bd5bc 1380__attribute_warn_unused_result__
e2f55264 1381set_regs (const regex_t *preg, const re_match_context_t *mctx, size_t nmatch,
eb04c213 1382 regmatch_t *pmatch, bool fl_backtrack)
3b0bdc72 1383{
eb04c213
AZ
1384 const re_dfa_t *dfa = preg->buffer;
1385 Idx idx, cur_node;
3b0bdc72 1386 re_node_set eps_via_nodes;
a3022b82 1387 struct re_fail_stack_t *fs;
6b6557e8
UD
1388 struct re_fail_stack_t fs_body = { 0, 2, NULL };
1389 regmatch_t *prev_idx_match;
eb04c213 1390 bool prev_idx_match_malloced = false;
6b6557e8 1391
3b0bdc72
UD
1392#ifdef DEBUG
1393 assert (nmatch > 1);
612546c6 1394 assert (mctx->state_log != NULL);
3b0bdc72 1395#endif
a3022b82
UD
1396 if (fl_backtrack)
1397 {
1398 fs = &fs_body;
1399 fs->stack = re_malloc (struct re_fail_stack_ent_t, fs->alloc);
6b6557e8
UD
1400 if (fs->stack == NULL)
1401 return REG_ESPACE;
a3022b82
UD
1402 }
1403 else
1404 fs = NULL;
6b6557e8 1405
3b0bdc72 1406 cur_node = dfa->init_node;
3b0bdc72 1407 re_node_set_init_empty (&eps_via_nodes);
6b6557e8 1408
2d87db5b
UD
1409 if (__libc_use_alloca (nmatch * sizeof (regmatch_t)))
1410 prev_idx_match = (regmatch_t *) alloca (nmatch * sizeof (regmatch_t));
1411 else
1412 {
1413 prev_idx_match = re_malloc (regmatch_t, nmatch);
1414 if (prev_idx_match == NULL)
1415 {
1416 free_fail_stack_return (fs);
1417 return REG_ESPACE;
1418 }
eb04c213 1419 prev_idx_match_malloced = true;
2d87db5b 1420 }
963d8d78 1421 memcpy (prev_idx_match, pmatch, sizeof (regmatch_t) * nmatch);
6b6557e8 1422
3b0bdc72
UD
1423 for (idx = pmatch[0].rm_so; idx <= pmatch[0].rm_eo ;)
1424 {
963d8d78 1425 update_regs (dfa, pmatch, prev_idx_match, cur_node, idx, nmatch);
6b6557e8 1426
a3022b82 1427 if (idx == pmatch[0].rm_eo && cur_node == mctx->last_node)
15a7d175 1428 {
eb04c213 1429 Idx reg_idx;
15a7d175
UD
1430 if (fs)
1431 {
1432 for (reg_idx = 0; reg_idx < nmatch; ++reg_idx)
1433 if (pmatch[reg_idx].rm_so > -1 && pmatch[reg_idx].rm_eo == -1)
1434 break;
1435 if (reg_idx == nmatch)
1436 {
1437 re_node_set_free (&eps_via_nodes);
2d87db5b
UD
1438 if (prev_idx_match_malloced)
1439 re_free (prev_idx_match);
15a7d175
UD
1440 return free_fail_stack_return (fs);
1441 }
1442 cur_node = pop_fail_stack (fs, &idx, nmatch, pmatch,
1443 &eps_via_nodes);
1444 }
1445 else
1446 {
1447 re_node_set_free (&eps_via_nodes);
2d87db5b
UD
1448 if (prev_idx_match_malloced)
1449 re_free (prev_idx_match);
15a7d175
UD
1450 return REG_NOERROR;
1451 }
1452 }
3b0bdc72
UD
1453
1454 /* Proceed to next node. */
e3a87852 1455 cur_node = proceed_next_node (mctx, nmatch, pmatch, &idx, cur_node,
15a7d175 1456 &eps_via_nodes, fs);
a3022b82 1457
f4efbdfb 1458 if (__glibc_unlikely (cur_node < 0))
15a7d175 1459 {
f4efbdfb 1460 if (__glibc_unlikely (cur_node == -2))
44777771
UD
1461 {
1462 re_node_set_free (&eps_via_nodes);
2d87db5b
UD
1463 if (prev_idx_match_malloced)
1464 re_free (prev_idx_match);
44777771
UD
1465 free_fail_stack_return (fs);
1466 return REG_ESPACE;
1467 }
15a7d175
UD
1468 if (fs)
1469 cur_node = pop_fail_stack (fs, &idx, nmatch, pmatch,
1470 &eps_via_nodes);
1471 else
1472 {
1473 re_node_set_free (&eps_via_nodes);
2d87db5b
UD
1474 if (prev_idx_match_malloced)
1475 re_free (prev_idx_match);
15a7d175
UD
1476 return REG_NOMATCH;
1477 }
1478 }
3b0bdc72
UD
1479 }
1480 re_node_set_free (&eps_via_nodes);
2d87db5b
UD
1481 if (prev_idx_match_malloced)
1482 re_free (prev_idx_match);
485d775d
UD
1483 return free_fail_stack_return (fs);
1484}
1485
1486static reg_errcode_t
e2f55264 1487free_fail_stack_return (struct re_fail_stack_t *fs)
485d775d
UD
1488{
1489 if (fs)
1490 {
eb04c213 1491 Idx fs_idx;
485d775d 1492 for (fs_idx = 0; fs_idx < fs->num; ++fs_idx)
15a7d175
UD
1493 {
1494 re_node_set_free (&fs->stack[fs_idx].eps_via_nodes);
1495 re_free (fs->stack[fs_idx].regs);
1496 }
485d775d
UD
1497 re_free (fs->stack);
1498 }
a9388965 1499 return REG_NOERROR;
3b0bdc72
UD
1500}
1501
81c64d40 1502static void
e2f55264 1503update_regs (const re_dfa_t *dfa, regmatch_t *pmatch,
eb04c213 1504 regmatch_t *prev_idx_match, Idx cur_node, Idx cur_idx, Idx nmatch)
81c64d40
UD
1505{
1506 int type = dfa->nodes[cur_node].type;
81c64d40
UD
1507 if (type == OP_OPEN_SUBEXP)
1508 {
eb04c213 1509 Idx reg_num = dfa->nodes[cur_node].opr.idx + 1;
6b6557e8 1510
81c64d40 1511 /* We are at the first node of this sub expression. */
6b6557e8
UD
1512 if (reg_num < nmatch)
1513 {
1514 pmatch[reg_num].rm_so = cur_idx;
1515 pmatch[reg_num].rm_eo = -1;
1516 }
81c64d40
UD
1517 }
1518 else if (type == OP_CLOSE_SUBEXP)
6b6557e8 1519 {
eb04c213 1520 Idx reg_num = dfa->nodes[cur_node].opr.idx + 1;
6b6557e8
UD
1521 if (reg_num < nmatch)
1522 {
1523 /* We are at the last node of this sub expression. */
1524 if (pmatch[reg_num].rm_so < cur_idx)
1525 {
1526 pmatch[reg_num].rm_eo = cur_idx;
1527 /* This is a non-empty match or we are not inside an optional
1528 subexpression. Accept this right away. */
1529 memcpy (prev_idx_match, pmatch, sizeof (regmatch_t) * nmatch);
1530 }
1531 else
1532 {
1533 if (dfa->nodes[cur_node].opt_subexp
1534 && prev_idx_match[reg_num].rm_so != -1)
1535 /* We transited through an empty match for an optional
1536 subexpression, like (a?)*, and this is not the subexp's
1537 first match. Copy back the old content of the registers
1538 so that matches of an inner subexpression are undone as
1539 well, like in ((a?))*. */
1540 memcpy (pmatch, prev_idx_match, sizeof (regmatch_t) * nmatch);
1541 else
1542 /* We completed a subexpression, but it may be part of
1543 an optional one, so do not update PREV_IDX_MATCH. */
1544 pmatch[reg_num].rm_eo = cur_idx;
1545 }
1546 }
1547 }
0742e48e 1548}
81c64d40 1549
0742e48e 1550/* This function checks the STATE_LOG from the SCTX->last_str_idx to 0
612546c6
UD
1551 and sift the nodes in each states according to the following rules.
1552 Updated state_log will be wrote to STATE_LOG.
3b0bdc72 1553
eb04c213 1554 Rules: We throw away the Node 'a' in the STATE_LOG[STR_IDX] if...
3b0bdc72 1555 1. When STR_IDX == MATCH_LAST(the last index in the state_log):
eb04c213
AZ
1556 If 'a' isn't the LAST_NODE and 'a' can't epsilon transit to
1557 the LAST_NODE, we throw away the node 'a'.
1558 2. When 0 <= STR_IDX < MATCH_LAST and 'a' accepts
1559 string 's' and transit to 'b':
15a7d175 1560 i. If 'b' isn't in the STATE_LOG[STR_IDX+strlen('s')], we throw
eb04c213 1561 away the node 'a'.
15a7d175 1562 ii. If 'b' is in the STATE_LOG[STR_IDX+strlen('s')] but 'b' is
eb04c213 1563 thrown away, we throw away the node 'a'.
d0b96fc4 1564 3. When 0 <= STR_IDX < MATCH_LAST and 'a' epsilon transit to 'b':
15a7d175 1565 i. If 'b' isn't in the STATE_LOG[STR_IDX], we throw away the
eb04c213 1566 node 'a'.
bb3f4825 1567 ii. If 'b' is in the STATE_LOG[STR_IDX] but 'b' is thrown away,
eb04c213 1568 we throw away the node 'a'. */
3b0bdc72
UD
1569
1570#define STATE_NODE_CONTAINS(state,node) \
1571 ((state) != NULL && re_node_set_contains (&(state)->nodes, node))
1572
a9388965 1573static reg_errcode_t
e2f55264 1574sift_states_backward (const re_match_context_t *mctx, re_sift_context_t *sctx)
3b0bdc72 1575{
a9388965 1576 reg_errcode_t err;
0742e48e 1577 int null_cnt = 0;
eb04c213 1578 Idx str_idx = sctx->last_str_idx;
0742e48e 1579 re_node_set cur_dest;
3b0bdc72
UD
1580
1581#ifdef DEBUG
612546c6 1582 assert (mctx->state_log != NULL && mctx->state_log[str_idx] != NULL);
3b0bdc72 1583#endif
3b0bdc72
UD
1584
1585 /* Build sifted state_log[str_idx]. It has the nodes which can epsilon
1586 transit to the last_node and the last_node itself. */
0742e48e 1587 err = re_node_set_init_1 (&cur_dest, sctx->last_node);
f4efbdfb 1588 if (__glibc_unlikely (err != REG_NOERROR))
a9388965 1589 return err;
e3a87852 1590 err = update_cur_sifted_state (mctx, sctx, str_idx, &cur_dest);
f4efbdfb 1591 if (__glibc_unlikely (err != REG_NOERROR))
1b2c2628 1592 goto free_return;
3b0bdc72
UD
1593
1594 /* Then check each states in the state_log. */
612546c6 1595 while (str_idx > 0)
3b0bdc72 1596 {
3b0bdc72 1597 /* Update counters. */
0742e48e
UD
1598 null_cnt = (sctx->sifted_states[str_idx] == NULL) ? null_cnt + 1 : 0;
1599 if (null_cnt > mctx->max_mb_elem_len)
15a7d175
UD
1600 {
1601 memset (sctx->sifted_states, '\0',
1602 sizeof (re_dfastate_t *) * str_idx);
1603 re_node_set_free (&cur_dest);
1604 return REG_NOERROR;
1605 }
0742e48e 1606 re_node_set_empty (&cur_dest);
3b0bdc72 1607 --str_idx;
15a7d175 1608
e40a38b3
UD
1609 if (mctx->state_log[str_idx])
1610 {
1611 err = build_sifted_states (mctx, sctx, str_idx, &cur_dest);
f4efbdfb 1612 if (__glibc_unlikely (err != REG_NOERROR))
e40a38b3 1613 goto free_return;
15a7d175 1614 }
3b0bdc72 1615
0742e48e 1616 /* Add all the nodes which satisfy the following conditions:
15a7d175
UD
1617 - It can epsilon transit to a node in CUR_DEST.
1618 - It is in CUR_SRC.
1619 And update state_log. */
e3a87852 1620 err = update_cur_sifted_state (mctx, sctx, str_idx, &cur_dest);
f4efbdfb 1621 if (__glibc_unlikely (err != REG_NOERROR))
15a7d175 1622 goto free_return;
3b0bdc72 1623 }
1b2c2628
UD
1624 err = REG_NOERROR;
1625 free_return:
0742e48e 1626 re_node_set_free (&cur_dest);
1b2c2628 1627 return err;
3b0bdc72
UD
1628}
1629
e40a38b3 1630static reg_errcode_t
b41bd5bc 1631__attribute_warn_unused_result__
e2f55264 1632build_sifted_states (const re_match_context_t *mctx, re_sift_context_t *sctx,
eb04c213 1633 Idx str_idx, re_node_set *cur_dest)
e40a38b3 1634{
76b864c8
UD
1635 const re_dfa_t *const dfa = mctx->dfa;
1636 const re_node_set *cur_src = &mctx->state_log[str_idx]->non_eps_nodes;
eb04c213 1637 Idx i;
e40a38b3
UD
1638
1639 /* Then build the next sifted state.
eb04c213
AZ
1640 We build the next sifted state on 'cur_dest', and update
1641 'sifted_states[str_idx]' with 'cur_dest'.
e40a38b3 1642 Note:
eb04c213
AZ
1643 'cur_dest' is the sifted state from 'state_log[str_idx + 1]'.
1644 'cur_src' points the node_set of the old 'state_log[str_idx]'
5cf1ec52 1645 (with the epsilon nodes pre-filtered out). */
e40a38b3
UD
1646 for (i = 0; i < cur_src->nelem; i++)
1647 {
eb04c213 1648 Idx prev_node = cur_src->elems[i];
e40a38b3 1649 int naccepted = 0;
eb04c213 1650 bool ok;
e40a38b3 1651
a334319f 1652#ifdef DEBUG
02f3550c 1653 re_token_type_t type = dfa->nodes[prev_node].type;
5cf1ec52
UD
1654 assert (!IS_EPSILON_NODE (type));
1655#endif
e40a38b3 1656#ifdef RE_ENABLE_I18N
eb04c213 1657 /* If the node may accept "multi byte". */
02f3550c 1658 if (dfa->nodes[prev_node].accept_mb)
e40a38b3
UD
1659 naccepted = sift_states_iter_mb (mctx, sctx, prev_node,
1660 str_idx, sctx->last_str_idx);
1661#endif /* RE_ENABLE_I18N */
1662
1663 /* We don't check backreferences here.
1664 See update_cur_sifted_state(). */
1665 if (!naccepted
1666 && check_node_accept (mctx, dfa->nodes + prev_node, str_idx)
1667 && STATE_NODE_CONTAINS (sctx->sifted_states[str_idx + 1],
1668 dfa->nexts[prev_node]))
1669 naccepted = 1;
1670
1671 if (naccepted == 0)
1672 continue;
1673
1674 if (sctx->limits.nelem)
1675 {
eb04c213 1676 Idx to_idx = str_idx + naccepted;
e40a38b3
UD
1677 if (check_dst_limits (mctx, &sctx->limits,
1678 dfa->nexts[prev_node], to_idx,
1679 prev_node, str_idx))
1680 continue;
1681 }
eb04c213 1682 ok = re_node_set_insert (cur_dest, prev_node);
f4efbdfb 1683 if (__glibc_unlikely (! ok))
e40a38b3
UD
1684 return REG_ESPACE;
1685 }
1686
1687 return REG_NOERROR;
1688}
1689
3b0bdc72
UD
1690/* Helper functions. */
1691
25337753 1692static reg_errcode_t
eb04c213 1693clean_state_log_if_needed (re_match_context_t *mctx, Idx next_state_log_idx)
3b0bdc72 1694{
eb04c213 1695 Idx top = mctx->state_log_top;
612546c6 1696
8887a920
UD
1697 if ((next_state_log_idx >= mctx->input.bufs_len
1698 && mctx->input.bufs_len < mctx->input.len)
56b168be
UD
1699 || (next_state_log_idx >= mctx->input.valid_len
1700 && mctx->input.valid_len < mctx->input.len))
612546c6
UD
1701 {
1702 reg_errcode_t err;
a445af0b 1703 err = extend_buffers (mctx, next_state_log_idx + 1);
f4efbdfb 1704 if (__glibc_unlikely (err != REG_NOERROR))
15a7d175 1705 return err;
612546c6
UD
1706 }
1707
3b0bdc72
UD
1708 if (top < next_state_log_idx)
1709 {
612546c6 1710 memset (mctx->state_log + top + 1, '\0',
15a7d175 1711 sizeof (re_dfastate_t *) * (next_state_log_idx - top));
3b0bdc72
UD
1712 mctx->state_log_top = next_state_log_idx;
1713 }
612546c6 1714 return REG_NOERROR;
3b0bdc72
UD
1715}
1716
1b2c2628 1717static reg_errcode_t
e2f55264 1718merge_state_array (const re_dfa_t *dfa, re_dfastate_t **dst,
eb04c213 1719 re_dfastate_t **src, Idx num)
3b0bdc72 1720{
eb04c213 1721 Idx st_idx;
0742e48e
UD
1722 reg_errcode_t err;
1723 for (st_idx = 0; st_idx < num; ++st_idx)
1724 {
1725 if (dst[st_idx] == NULL)
15a7d175 1726 dst[st_idx] = src[st_idx];
0742e48e 1727 else if (src[st_idx] != NULL)
15a7d175
UD
1728 {
1729 re_node_set merged_set;
1730 err = re_node_set_init_union (&merged_set, &dst[st_idx]->nodes,
1731 &src[st_idx]->nodes);
f4efbdfb 1732 if (__glibc_unlikely (err != REG_NOERROR))
15a7d175
UD
1733 return err;
1734 dst[st_idx] = re_acquire_state (&err, dfa, &merged_set);
1735 re_node_set_free (&merged_set);
f4efbdfb 1736 if (__glibc_unlikely (err != REG_NOERROR))
15a7d175
UD
1737 return err;
1738 }
0742e48e
UD
1739 }
1740 return REG_NOERROR;
3b0bdc72
UD
1741}
1742
0742e48e 1743static reg_errcode_t
e2f55264 1744update_cur_sifted_state (const re_match_context_t *mctx,
eb04c213 1745 re_sift_context_t *sctx, Idx str_idx,
e2f55264 1746 re_node_set *dest_nodes)
3b0bdc72 1747{
76b864c8 1748 const re_dfa_t *const dfa = mctx->dfa;
c0c9615c 1749 reg_errcode_t err = REG_NOERROR;
0742e48e 1750 const re_node_set *candidates;
e40a38b3 1751 candidates = ((mctx->state_log[str_idx] == NULL) ? NULL
15a7d175 1752 : &mctx->state_log[str_idx]->nodes);
0742e48e 1753
e40a38b3
UD
1754 if (dest_nodes->nelem == 0)
1755 sctx->sifted_states[str_idx] = NULL;
1756 else
485d775d 1757 {
e40a38b3
UD
1758 if (candidates)
1759 {
1760 /* At first, add the nodes which can epsilon transit to a node in
1761 DEST_NODE. */
1762 err = add_epsilon_src_nodes (dfa, dest_nodes, candidates);
f4efbdfb 1763 if (__glibc_unlikely (err != REG_NOERROR))
e40a38b3 1764 return err;
0742e48e 1765
e40a38b3
UD
1766 /* Then, check the limitations in the current sift_context. */
1767 if (sctx->limits.nelem)
1768 {
1769 err = check_subexp_limits (dfa, dest_nodes, candidates, &sctx->limits,
1770 mctx->bkref_ents, str_idx);
f4efbdfb 1771 if (__glibc_unlikely (err != REG_NOERROR))
e40a38b3
UD
1772 return err;
1773 }
1774 }
1775
1776 sctx->sifted_states[str_idx] = re_acquire_state (&err, dfa, dest_nodes);
f4efbdfb 1777 if (__glibc_unlikely (err != REG_NOERROR))
15a7d175 1778 return err;
0742e48e
UD
1779 }
1780
e40a38b3 1781 if (candidates && mctx->state_log[str_idx]->has_backref)
0742e48e 1782 {
d2c38eb3 1783 err = sift_states_bkref (mctx, sctx, str_idx, candidates);
f4efbdfb 1784 if (__glibc_unlikely (err != REG_NOERROR))
15a7d175 1785 return err;
0742e48e
UD
1786 }
1787 return REG_NOERROR;
3b0bdc72
UD
1788}
1789
a9388965 1790static reg_errcode_t
b41bd5bc 1791__attribute_warn_unused_result__
e2f55264
UD
1792add_epsilon_src_nodes (const re_dfa_t *dfa, re_node_set *dest_nodes,
1793 const re_node_set *candidates)
3b0bdc72 1794{
5cf1ec52 1795 reg_errcode_t err = REG_NOERROR;
eb04c213 1796 Idx i;
0742e48e 1797
5cf1ec52 1798 re_dfastate_t *state = re_acquire_state (&err, dfa, dest_nodes);
f4efbdfb 1799 if (__glibc_unlikely (err != REG_NOERROR))
0742e48e 1800 return err;
5cf1ec52
UD
1801
1802 if (!state->inveclosure.alloc)
3b0bdc72 1803 {
5cf1ec52 1804 err = re_node_set_alloc (&state->inveclosure, dest_nodes->nelem);
f4efbdfb 1805 if (__glibc_unlikely (err != REG_NOERROR))
2da42bc0 1806 return REG_ESPACE;
5cf1ec52 1807 for (i = 0; i < dest_nodes->nelem; i++)
2da42bc0
UD
1808 {
1809 err = re_node_set_merge (&state->inveclosure,
1810 dfa->inveclosures + dest_nodes->elems[i]);
f4efbdfb 1811 if (__glibc_unlikely (err != REG_NOERROR))
2da42bc0
UD
1812 return REG_ESPACE;
1813 }
0742e48e 1814 }
5cf1ec52
UD
1815 return re_node_set_add_intersect (dest_nodes, candidates,
1816 &state->inveclosure);
0742e48e 1817}
3b0bdc72 1818
0742e48e 1819static reg_errcode_t
eb04c213 1820sub_epsilon_src_nodes (const re_dfa_t *dfa, Idx node, re_node_set *dest_nodes,
e2f55264 1821 const re_node_set *candidates)
0742e48e 1822{
eb04c213 1823 Idx ecl_idx;
0742e48e
UD
1824 reg_errcode_t err;
1825 re_node_set *inv_eclosure = dfa->inveclosures + node;
1826 re_node_set except_nodes;
1827 re_node_set_init_empty (&except_nodes);
1828 for (ecl_idx = 0; ecl_idx < inv_eclosure->nelem; ++ecl_idx)
1829 {
eb04c213 1830 Idx cur_node = inv_eclosure->elems[ecl_idx];
15a7d175
UD
1831 if (cur_node == node)
1832 continue;
1833 if (IS_EPSILON_NODE (dfa->nodes[cur_node].type))
1834 {
eb04c213
AZ
1835 Idx edst1 = dfa->edests[cur_node].elems[0];
1836 Idx edst2 = ((dfa->edests[cur_node].nelem > 1)
15a7d175
UD
1837 ? dfa->edests[cur_node].elems[1] : -1);
1838 if ((!re_node_set_contains (inv_eclosure, edst1)
1839 && re_node_set_contains (dest_nodes, edst1))
1840 || (edst2 > 0
1841 && !re_node_set_contains (inv_eclosure, edst2)
1842 && re_node_set_contains (dest_nodes, edst2)))
1843 {
1844 err = re_node_set_add_intersect (&except_nodes, candidates,
1845 dfa->inveclosures + cur_node);
f4efbdfb 1846 if (__glibc_unlikely (err != REG_NOERROR))
15a7d175
UD
1847 {
1848 re_node_set_free (&except_nodes);
1849 return err;
1850 }
1851 }
1852 }
0742e48e
UD
1853 }
1854 for (ecl_idx = 0; ecl_idx < inv_eclosure->nelem; ++ecl_idx)
1855 {
eb04c213 1856 Idx cur_node = inv_eclosure->elems[ecl_idx];
15a7d175
UD
1857 if (!re_node_set_contains (&except_nodes, cur_node))
1858 {
eb04c213 1859 Idx idx = re_node_set_contains (dest_nodes, cur_node) - 1;
15a7d175
UD
1860 re_node_set_remove_at (dest_nodes, idx);
1861 }
0742e48e
UD
1862 }
1863 re_node_set_free (&except_nodes);
1864 return REG_NOERROR;
1865}
1866
eb04c213
AZ
1867static bool
1868check_dst_limits (const re_match_context_t *mctx, const re_node_set *limits,
1869 Idx dst_node, Idx dst_idx, Idx src_node, Idx src_idx)
a3022b82 1870{
76b864c8 1871 const re_dfa_t *const dfa = mctx->dfa;
eb04c213 1872 Idx lim_idx, src_pos, dst_pos;
a3022b82 1873
eb04c213
AZ
1874 Idx dst_bkref_idx = search_cur_bkref_entry (mctx, dst_idx);
1875 Idx src_bkref_idx = search_cur_bkref_entry (mctx, src_idx);
a3022b82
UD
1876 for (lim_idx = 0; lim_idx < limits->nelem; ++lim_idx)
1877 {
eb04c213 1878 Idx subexp_idx;
a3022b82
UD
1879 struct re_backref_cache_entry *ent;
1880 ent = mctx->bkref_ents + limits->elems[lim_idx];
d8f73de8 1881 subexp_idx = dfa->nodes[ent->node].opr.idx;
a3022b82 1882
e3a87852 1883 dst_pos = check_dst_limits_calc_pos (mctx, limits->elems[lim_idx],
e40a38b3
UD
1884 subexp_idx, dst_node, dst_idx,
1885 dst_bkref_idx);
e3a87852 1886 src_pos = check_dst_limits_calc_pos (mctx, limits->elems[lim_idx],
e40a38b3
UD
1887 subexp_idx, src_node, src_idx,
1888 src_bkref_idx);
a3022b82
UD
1889
1890 /* In case of:
15a7d175
UD
1891 <src> <dst> ( <subexp> )
1892 ( <subexp> ) <src> <dst>
1893 ( <subexp1> <src> <subexp2> <dst> <subexp3> ) */
a3022b82 1894 if (src_pos == dst_pos)
15a7d175 1895 continue; /* This is unrelated limitation. */
a3022b82 1896 else
eb04c213 1897 return true;
a3022b82 1898 }
eb04c213 1899 return false;
a3022b82
UD
1900}
1901
1902static int
e2f55264 1903check_dst_limits_calc_pos_1 (const re_match_context_t *mctx, int boundaries,
eb04c213 1904 Idx subexp_idx, Idx from_node, Idx bkref_idx)
a3022b82 1905{
76b864c8
UD
1906 const re_dfa_t *const dfa = mctx->dfa;
1907 const re_node_set *eclosures = dfa->eclosures + from_node;
eb04c213 1908 Idx node_idx;
d0b96fc4 1909
d0b96fc4
UD
1910 /* Else, we are on the boundary: examine the nodes on the epsilon
1911 closure. */
1cef7b3c
UD
1912 for (node_idx = 0; node_idx < eclosures->nelem; ++node_idx)
1913 {
eb04c213 1914 Idx node = eclosures->elems[node_idx];
d0b96fc4
UD
1915 switch (dfa->nodes[node].type)
1916 {
1917 case OP_BACK_REF:
d8f73de8
UD
1918 if (bkref_idx != -1)
1919 {
1920 struct re_backref_cache_entry *ent = mctx->bkref_ents + bkref_idx;
1921 do
2da42bc0 1922 {
eb04c213
AZ
1923 Idx dst;
1924 int cpos;
d0b96fc4 1925
d8f73de8
UD
1926 if (ent->node != node)
1927 continue;
d0b96fc4 1928
2c05d33f
UD
1929 if (subexp_idx < BITSET_WORD_BITS
1930 && !(ent->eps_reachable_subexps_map
1931 & ((bitset_word_t) 1 << subexp_idx)))
d8f73de8 1932 continue;
d0b96fc4 1933
d8f73de8
UD
1934 /* Recurse trying to reach the OP_OPEN_SUBEXP and
1935 OP_CLOSE_SUBEXP cases below. But, if the
1936 destination node is the same node as the source
1937 node, don't recurse because it would cause an
1938 infinite loop: a regex that exhibits this behavior
1939 is ()\1*\1* */
1940 dst = dfa->edests[node].elems[0];
1941 if (dst == from_node)
1942 {
1943 if (boundaries & 1)
2da42bc0 1944 return -1;
d8f73de8 1945 else /* if (boundaries & 2) */
2da42bc0 1946 return 0;
d8f73de8 1947 }
7db61208 1948
d8f73de8
UD
1949 cpos =
1950 check_dst_limits_calc_pos_1 (mctx, boundaries, subexp_idx,
1951 dst, bkref_idx);
1952 if (cpos == -1 /* && (boundaries & 1) */)
1953 return -1;
1954 if (cpos == 0 && (boundaries & 2))
1955 return 0;
1956
2c05d33f
UD
1957 if (subexp_idx < BITSET_WORD_BITS)
1958 ent->eps_reachable_subexps_map
1959 &= ~((bitset_word_t) 1 << subexp_idx);
2da42bc0 1960 }
d8f73de8
UD
1961 while (ent++->more);
1962 }
1963 break;
f0c7c524 1964
d0b96fc4 1965 case OP_OPEN_SUBEXP:
e40a38b3 1966 if ((boundaries & 1) && subexp_idx == dfa->nodes[node].opr.idx)
d0b96fc4
UD
1967 return -1;
1968 break;
f0c7c524 1969
d0b96fc4 1970 case OP_CLOSE_SUBEXP:
e40a38b3 1971 if ((boundaries & 2) && subexp_idx == dfa->nodes[node].opr.idx)
d0b96fc4
UD
1972 return 0;
1973 break;
1974
1975 default:
15a7d175
UD
1976 break;
1977 }
a3022b82 1978 }
d0b96fc4 1979
e40a38b3
UD
1980 return (boundaries & 2) ? 1 : 0;
1981}
1982
1983static int
eb04c213
AZ
1984check_dst_limits_calc_pos (const re_match_context_t *mctx, Idx limit,
1985 Idx subexp_idx, Idx from_node, Idx str_idx,
1986 Idx bkref_idx)
e40a38b3
UD
1987{
1988 struct re_backref_cache_entry *lim = mctx->bkref_ents + limit;
1989 int boundaries;
1990
1991 /* If we are outside the range of the subexpression, return -1 or 1. */
1992 if (str_idx < lim->subexp_from)
1993 return -1;
1994
1995 if (lim->subexp_to < str_idx)
d0b96fc4 1996 return 1;
e40a38b3
UD
1997
1998 /* If we are within the subexpression, return 0. */
1999 boundaries = (str_idx == lim->subexp_from);
2000 boundaries |= (str_idx == lim->subexp_to) << 1;
2001 if (boundaries == 0)
d0b96fc4 2002 return 0;
e40a38b3
UD
2003
2004 /* Else, examine epsilon closure. */
2005 return check_dst_limits_calc_pos_1 (mctx, boundaries, subexp_idx,
2006 from_node, bkref_idx);
a3022b82
UD
2007}
2008
0742e48e
UD
2009/* Check the limitations of sub expressions LIMITS, and remove the nodes
2010 which are against limitations from DEST_NODES. */
2011
2012static reg_errcode_t
e2f55264
UD
2013check_subexp_limits (const re_dfa_t *dfa, re_node_set *dest_nodes,
2014 const re_node_set *candidates, re_node_set *limits,
eb04c213 2015 struct re_backref_cache_entry *bkref_ents, Idx str_idx)
0742e48e
UD
2016{
2017 reg_errcode_t err;
eb04c213 2018 Idx node_idx, lim_idx;
0742e48e
UD
2019
2020 for (lim_idx = 0; lim_idx < limits->nelem; ++lim_idx)
2021 {
eb04c213 2022 Idx subexp_idx;
0742e48e
UD
2023 struct re_backref_cache_entry *ent;
2024 ent = bkref_ents + limits->elems[lim_idx];
2025
2026 if (str_idx <= ent->subexp_from || ent->str_idx < str_idx)
15a7d175 2027 continue; /* This is unrelated limitation. */
0742e48e 2028
d8f73de8 2029 subexp_idx = dfa->nodes[ent->node].opr.idx;
0742e48e 2030 if (ent->subexp_to == str_idx)
15a7d175 2031 {
eb04c213
AZ
2032 Idx ops_node = -1;
2033 Idx cls_node = -1;
15a7d175
UD
2034 for (node_idx = 0; node_idx < dest_nodes->nelem; ++node_idx)
2035 {
eb04c213 2036 Idx node = dest_nodes->elems[node_idx];
46bf9de7 2037 re_token_type_t type = dfa->nodes[node].type;
15a7d175
UD
2038 if (type == OP_OPEN_SUBEXP
2039 && subexp_idx == dfa->nodes[node].opr.idx)
2040 ops_node = node;
2041 else if (type == OP_CLOSE_SUBEXP
2042 && subexp_idx == dfa->nodes[node].opr.idx)
2043 cls_node = node;
2044 }
2045
2046 /* Check the limitation of the open subexpression. */
2047 /* Note that (ent->subexp_to = str_idx != ent->subexp_from). */
2048 if (ops_node >= 0)
2049 {
46bf9de7
UD
2050 err = sub_epsilon_src_nodes (dfa, ops_node, dest_nodes,
2051 candidates);
f4efbdfb 2052 if (__glibc_unlikely (err != REG_NOERROR))
15a7d175
UD
2053 return err;
2054 }
46bf9de7 2055
15a7d175 2056 /* Check the limitation of the close subexpression. */
46bf9de7
UD
2057 if (cls_node >= 0)
2058 for (node_idx = 0; node_idx < dest_nodes->nelem; ++node_idx)
2059 {
eb04c213 2060 Idx node = dest_nodes->elems[node_idx];
46bf9de7
UD
2061 if (!re_node_set_contains (dfa->inveclosures + node,
2062 cls_node)
2063 && !re_node_set_contains (dfa->eclosures + node,
2064 cls_node))
2065 {
2066 /* It is against this limitation.
2067 Remove it form the current sifted state. */
2068 err = sub_epsilon_src_nodes (dfa, node, dest_nodes,
2069 candidates);
f4efbdfb 2070 if (__glibc_unlikely (err != REG_NOERROR))
46bf9de7
UD
2071 return err;
2072 --node_idx;
2073 }
2074 }
15a7d175 2075 }
0742e48e 2076 else /* (ent->subexp_to != str_idx) */
15a7d175
UD
2077 {
2078 for (node_idx = 0; node_idx < dest_nodes->nelem; ++node_idx)
2079 {
eb04c213 2080 Idx node = dest_nodes->elems[node_idx];
46bf9de7 2081 re_token_type_t type = dfa->nodes[node].type;
15a7d175
UD
2082 if (type == OP_CLOSE_SUBEXP || type == OP_OPEN_SUBEXP)
2083 {
2084 if (subexp_idx != dfa->nodes[node].opr.idx)
2085 continue;
d8f73de8
UD
2086 /* It is against this limitation.
2087 Remove it form the current sifted state. */
2088 err = sub_epsilon_src_nodes (dfa, node, dest_nodes,
2089 candidates);
f4efbdfb 2090 if (__glibc_unlikely (err != REG_NOERROR))
d8f73de8 2091 return err;
15a7d175
UD
2092 }
2093 }
2094 }
0742e48e
UD
2095 }
2096 return REG_NOERROR;
2097}
2098
0742e48e 2099static reg_errcode_t
b41bd5bc 2100__attribute_warn_unused_result__
e2f55264 2101sift_states_bkref (const re_match_context_t *mctx, re_sift_context_t *sctx,
eb04c213 2102 Idx str_idx, const re_node_set *candidates)
0742e48e 2103{
76b864c8 2104 const re_dfa_t *const dfa = mctx->dfa;
0742e48e 2105 reg_errcode_t err;
eb04c213 2106 Idx node_idx, node;
0742e48e 2107 re_sift_context_t local_sctx;
eb04c213 2108 Idx first_idx = search_cur_bkref_entry (mctx, str_idx);
e40a38b3
UD
2109
2110 if (first_idx == -1)
2111 return REG_NOERROR;
2112
0742e48e
UD
2113 local_sctx.sifted_states = NULL; /* Mark that it hasn't been initialized. */
2114
2115 for (node_idx = 0; node_idx < candidates->nelem; ++node_idx)
2116 {
eb04c213 2117 Idx enabled_idx;
0742e48e 2118 re_token_type_t type;
e40a38b3 2119 struct re_backref_cache_entry *entry;
0742e48e
UD
2120 node = candidates->elems[node_idx];
2121 type = dfa->nodes[node].type;
0742e48e
UD
2122 /* Avoid infinite loop for the REs like "()\1+". */
2123 if (node == sctx->last_node && str_idx == sctx->last_str_idx)
15a7d175 2124 continue;
e40a38b3
UD
2125 if (type != OP_BACK_REF)
2126 continue;
2127
2128 entry = mctx->bkref_ents + first_idx;
2129 enabled_idx = first_idx;
2130 do
15a7d175 2131 {
eb04c213
AZ
2132 Idx subexp_len;
2133 Idx to_idx;
2134 Idx dst_node;
2135 bool ok;
e40a38b3
UD
2136 re_dfastate_t *cur_state;
2137
2138 if (entry->node != node)
2139 continue;
2140 subexp_len = entry->subexp_to - entry->subexp_from;
2141 to_idx = str_idx + subexp_len;
2142 dst_node = (subexp_len ? dfa->nexts[node]
2143 : dfa->edests[node].elems[0]);
2144
2145 if (to_idx > sctx->last_str_idx
2146 || sctx->sifted_states[to_idx] == NULL
2147 || !STATE_NODE_CONTAINS (sctx->sifted_states[to_idx], dst_node)
2148 || check_dst_limits (mctx, &sctx->limits, node,
2149 str_idx, dst_node, to_idx))
2150 continue;
2151
2152 if (local_sctx.sifted_states == NULL)
15a7d175 2153 {
e40a38b3
UD
2154 local_sctx = *sctx;
2155 err = re_node_set_init_copy (&local_sctx.limits, &sctx->limits);
f4efbdfb 2156 if (__glibc_unlikely (err != REG_NOERROR))
e40a38b3
UD
2157 goto free_return;
2158 }
2159 local_sctx.last_node = node;
2160 local_sctx.last_str_idx = str_idx;
eb04c213 2161 ok = re_node_set_insert (&local_sctx.limits, enabled_idx);
f4efbdfb 2162 if (__glibc_unlikely (! ok))
e40a38b3
UD
2163 {
2164 err = REG_ESPACE;
2165 goto free_return;
15a7d175 2166 }
e40a38b3
UD
2167 cur_state = local_sctx.sifted_states[str_idx];
2168 err = sift_states_backward (mctx, &local_sctx);
f4efbdfb 2169 if (__glibc_unlikely (err != REG_NOERROR))
e40a38b3
UD
2170 goto free_return;
2171 if (sctx->limited_states != NULL)
2172 {
2173 err = merge_state_array (dfa, sctx->limited_states,
2174 local_sctx.sifted_states,
2175 str_idx + 1);
f4efbdfb 2176 if (__glibc_unlikely (err != REG_NOERROR))
e40a38b3
UD
2177 goto free_return;
2178 }
2179 local_sctx.sifted_states[str_idx] = cur_state;
2180 re_node_set_remove (&local_sctx.limits, enabled_idx);
2181
2182 /* mctx->bkref_ents may have changed, reload the pointer. */
2da42bc0 2183 entry = mctx->bkref_ents + enabled_idx;
15a7d175 2184 }
e40a38b3 2185 while (enabled_idx++, entry++->more);
0742e48e 2186 }
1b2c2628
UD
2187 err = REG_NOERROR;
2188 free_return:
0742e48e
UD
2189 if (local_sctx.sifted_states != NULL)
2190 {
0742e48e
UD
2191 re_node_set_free (&local_sctx.limits);
2192 }
2193
1b2c2628 2194 return err;
0742e48e
UD
2195}
2196
2197
2198#ifdef RE_ENABLE_I18N
2199static int
e2f55264 2200sift_states_iter_mb (const re_match_context_t *mctx, re_sift_context_t *sctx,
eb04c213 2201 Idx node_idx, Idx str_idx, Idx max_str_idx)
0742e48e 2202{
c42b4152 2203 const re_dfa_t *const dfa = mctx->dfa;
0742e48e 2204 int naccepted;
eb04c213 2205 /* Check the node can accept "multi byte". */
56b168be 2206 naccepted = check_node_accept_bytes (dfa, node_idx, &mctx->input, str_idx);
34a5a146
JM
2207 if (naccepted > 0 && str_idx + naccepted <= max_str_idx
2208 && !STATE_NODE_CONTAINS (sctx->sifted_states[str_idx + naccepted],
2209 dfa->nexts[node_idx]))
eb04c213 2210 /* The node can't accept the "multi byte", or the
bb3f4825 2211 destination was already thrown away, then the node
a1b02ae7 2212 couldn't accept the current input "multi byte". */
0742e48e
UD
2213 naccepted = 0;
2214 /* Otherwise, it is sure that the node could accept
eb04c213 2215 'naccepted' bytes input. */
0742e48e
UD
2216 return naccepted;
2217}
2218#endif /* RE_ENABLE_I18N */
2219
3b0bdc72
UD
2220\f
2221/* Functions for state transition. */
2222
2223/* Return the next state to which the current state STATE will transit by
2224 accepting the current input byte, and update STATE_LOG if necessary.
2225 If STATE can accept a multibyte char/collating element/back reference
2226 update the destination of STATE_LOG. */
2227
2228static re_dfastate_t *
b41bd5bc 2229__attribute_warn_unused_result__
e2f55264
UD
2230transit_state (reg_errcode_t *err, re_match_context_t *mctx,
2231 re_dfastate_t *state)
3b0bdc72 2232{
58845a70 2233 re_dfastate_t **trtable;
3b0bdc72
UD
2234 unsigned char ch;
2235
bb677c95
UD
2236#ifdef RE_ENABLE_I18N
2237 /* If the current state can accept multibyte. */
f4efbdfb 2238 if (__glibc_unlikely (state->accept_mb))
612546c6 2239 {
bb677c95 2240 *err = transit_state_mb (mctx, state);
f4efbdfb 2241 if (__glibc_unlikely (*err != REG_NOERROR))
15a7d175 2242 return NULL;
612546c6 2243 }
434d3784 2244#endif /* RE_ENABLE_I18N */
3b0bdc72 2245
4c595adb 2246 /* Then decide the next state with the single byte. */
ab4b89fe
UD
2247#if 0
2248 if (0)
2249 /* don't use transition table */
2250 return transit_state_sb (err, mctx, state);
2251#endif
2252
2253 /* Use transition table */
2254 ch = re_string_fetch_byte (&mctx->input);
2255 for (;;)
4c595adb 2256 {
4c595adb 2257 trtable = state->trtable;
f4efbdfb 2258 if (__glibc_likely (trtable != NULL))
ab4b89fe
UD
2259 return trtable[ch];
2260
2261 trtable = state->word_trtable;
f4efbdfb 2262 if (__glibc_likely (trtable != NULL))
2da42bc0 2263 {
4c595adb
UD
2264 unsigned int context;
2265 context
2266 = re_string_context_at (&mctx->input,
2267 re_string_cur_idx (&mctx->input) - 1,
2268 mctx->eflags);
2269 if (IS_WORD_CONTEXT (context))
2270 return trtable[ch + SBC_MAX];
c13c99fa 2271 else
4c595adb 2272 return trtable[ch];
15a7d175 2273 }
ab4b89fe
UD
2274
2275 if (!build_trtable (mctx->dfa, state))
2276 {
2277 *err = REG_ESPACE;
2278 return NULL;
2279 }
2280
2281 /* Retry, we now have a transition table. */
3b0bdc72 2282 }
4c595adb 2283}
3b0bdc72 2284
4c595adb 2285/* Update the state_log if we need */
eb04c213 2286static re_dfastate_t *
e2f55264
UD
2287merge_state_with_log (reg_errcode_t *err, re_match_context_t *mctx,
2288 re_dfastate_t *next_state)
58845a70 2289{
c42b4152 2290 const re_dfa_t *const dfa = mctx->dfa;
eb04c213 2291 Idx cur_idx = re_string_cur_idx (&mctx->input);
4c595adb
UD
2292
2293 if (cur_idx > mctx->state_log_top)
3b0bdc72 2294 {
4c595adb
UD
2295 mctx->state_log[cur_idx] = next_state;
2296 mctx->state_log_top = cur_idx;
2297 }
2298 else if (mctx->state_log[cur_idx] == 0)
2299 {
2300 mctx->state_log[cur_idx] = next_state;
2301 }
2302 else
2303 {
2304 re_dfastate_t *pstate;
2305 unsigned int context;
2306 re_node_set next_nodes, *log_nodes, *table_nodes = NULL;
2307 /* If (state_log[cur_idx] != 0), it implies that cur_idx is
2da42bc0
UD
2308 the destination of a multibyte char/collating element/
2309 back reference. Then the next state is the union set of
2310 these destinations and the results of the transition table. */
4c595adb
UD
2311 pstate = mctx->state_log[cur_idx];
2312 log_nodes = pstate->entrance_nodes;
2313 if (next_state != NULL)
2da42bc0
UD
2314 {
2315 table_nodes = next_state->entrance_nodes;
2316 *err = re_node_set_init_union (&next_nodes, table_nodes,
15a7d175 2317 log_nodes);
f4efbdfb 2318 if (__glibc_unlikely (*err != REG_NOERROR))
4c595adb 2319 return NULL;
2da42bc0 2320 }
4c595adb 2321 else
2da42bc0 2322 next_nodes = *log_nodes;
4c595adb
UD
2323 /* Note: We already add the nodes of the initial state,
2324 then we don't need to add them here. */
2325
2326 context = re_string_context_at (&mctx->input,
2327 re_string_cur_idx (&mctx->input) - 1,
2328 mctx->eflags);
2329 next_state = mctx->state_log[cur_idx]
2da42bc0 2330 = re_acquire_state_context (err, dfa, &next_nodes, context);
4c595adb 2331 /* We don't need to check errors here, since the return value of
2da42bc0 2332 this function is next_state and ERR is already set. */
4c595adb
UD
2333
2334 if (table_nodes != NULL)
2da42bc0 2335 re_node_set_free (&next_nodes);
6291ee3c
UD
2336 }
2337
f4efbdfb 2338 if (__glibc_unlikely (dfa->nbackref) && next_state != NULL)
6291ee3c 2339 {
bb3f4825
UD
2340 /* Check OP_OPEN_SUBEXP in the current state in case that we use them
2341 later. We must check them here, since the back references in the
2342 next state might use them. */
e3a87852 2343 *err = check_subexp_matching_top (mctx, &next_state->nodes,
6291ee3c 2344 cur_idx);
f4efbdfb 2345 if (__glibc_unlikely (*err != REG_NOERROR))
6291ee3c 2346 return NULL;
6291ee3c 2347
bb3f4825
UD
2348 /* If the next state has back references. */
2349 if (next_state->has_backref)
2350 {
e3a87852 2351 *err = transit_state_bkref (mctx, &next_state->nodes);
f4efbdfb 2352 if (__glibc_unlikely (*err != REG_NOERROR))
bb3f4825
UD
2353 return NULL;
2354 next_state = mctx->state_log[cur_idx];
2355 }
3b0bdc72 2356 }
4c595adb 2357
3b0bdc72
UD
2358 return next_state;
2359}
2360
4c595adb
UD
2361/* Skip bytes in the input that correspond to part of a
2362 multi-byte match, then look in the log for a state
2363 from which to restart matching. */
eb04c213 2364static re_dfastate_t *
e2f55264 2365find_recover_state (reg_errcode_t *err, re_match_context_t *mctx)
4c595adb 2366{
1878e9af 2367 re_dfastate_t *cur_state;
4c595adb
UD
2368 do
2369 {
eb04c213
AZ
2370 Idx max = mctx->state_log_top;
2371 Idx cur_str_idx = re_string_cur_idx (&mctx->input);
4c595adb
UD
2372
2373 do
2374 {
2da42bc0
UD
2375 if (++cur_str_idx > max)
2376 return NULL;
2377 re_string_skip_bytes (&mctx->input, 1);
4c595adb
UD
2378 }
2379 while (mctx->state_log[cur_str_idx] == NULL);
2380
2381 cur_state = merge_state_with_log (err, mctx, NULL);
2382 }
2d87db5b 2383 while (*err == REG_NOERROR && cur_state == NULL);
4c595adb
UD
2384 return cur_state;
2385}
2386
3b0bdc72
UD
2387/* Helper functions for transit_state. */
2388
6291ee3c
UD
2389/* From the node set CUR_NODES, pick up the nodes whose types are
2390 OP_OPEN_SUBEXP and which have corresponding back references in the regular
2391 expression. And register them to use them later for evaluating the
5069ff32 2392 corresponding back references. */
6291ee3c
UD
2393
2394static reg_errcode_t
e2f55264 2395check_subexp_matching_top (re_match_context_t *mctx, re_node_set *cur_nodes,
eb04c213 2396 Idx str_idx)
6291ee3c 2397{
76b864c8 2398 const re_dfa_t *const dfa = mctx->dfa;
eb04c213 2399 Idx node_idx;
6291ee3c
UD
2400 reg_errcode_t err;
2401
2402 /* TODO: This isn't efficient.
2403 Because there might be more than one nodes whose types are
2404 OP_OPEN_SUBEXP and whose index is SUBEXP_IDX, we must check all
2405 nodes.
2406 E.g. RE: (a){2} */
2407 for (node_idx = 0; node_idx < cur_nodes->nelem; ++node_idx)
2408 {
eb04c213 2409 Idx node = cur_nodes->elems[node_idx];
6291ee3c 2410 if (dfa->nodes[node].type == OP_OPEN_SUBEXP
2c05d33f
UD
2411 && dfa->nodes[node].opr.idx < BITSET_WORD_BITS
2412 && (dfa->used_bkref_map
2413 & ((bitset_word_t) 1 << dfa->nodes[node].opr.idx)))
6291ee3c
UD
2414 {
2415 err = match_ctx_add_subtop (mctx, node, str_idx);
f4efbdfb 2416 if (__glibc_unlikely (err != REG_NOERROR))
6291ee3c
UD
2417 return err;
2418 }
2419 }
2420 return REG_NOERROR;
2421}
2422
c13c99fa 2423#if 0
3b0bdc72
UD
2424/* Return the next state to which the current state STATE will transit by
2425 accepting the current input byte. */
2426
2427static re_dfastate_t *
e2f55264
UD
2428transit_state_sb (reg_errcode_t *err, re_match_context_t *mctx,
2429 re_dfastate_t *state)
3b0bdc72 2430{
c42b4152 2431 const re_dfa_t *const dfa = mctx->dfa;
3b0bdc72
UD
2432 re_node_set next_nodes;
2433 re_dfastate_t *next_state;
eb04c213 2434 Idx node_cnt, cur_str_idx = re_string_cur_idx (&mctx->input);
3b0bdc72
UD
2435 unsigned int context;
2436
a9388965 2437 *err = re_node_set_alloc (&next_nodes, state->nodes.nelem + 1);
f4efbdfb 2438 if (__glibc_unlikely (*err != REG_NOERROR))
a9388965 2439 return NULL;
3b0bdc72
UD
2440 for (node_cnt = 0; node_cnt < state->nodes.nelem; ++node_cnt)
2441 {
eb04c213 2442 Idx cur_node = state->nodes.elems[node_cnt];
e3a87852 2443 if (check_node_accept (mctx, dfa->nodes + cur_node, cur_str_idx))
15a7d175
UD
2444 {
2445 *err = re_node_set_merge (&next_nodes,
2446 dfa->eclosures + dfa->nexts[cur_node]);
f4efbdfb 2447 if (__glibc_unlikely (*err != REG_NOERROR))
15a7d175
UD
2448 {
2449 re_node_set_free (&next_nodes);
2450 return NULL;
2451 }
2452 }
3b0bdc72 2453 }
56b168be 2454 context = re_string_context_at (&mctx->input, cur_str_idx, mctx->eflags);
a9388965
UD
2455 next_state = re_acquire_state_context (err, dfa, &next_nodes, context);
2456 /* We don't need to check errors here, since the return value of
2457 this function is next_state and ERR is already set. */
2458
3b0bdc72 2459 re_node_set_free (&next_nodes);
56b168be 2460 re_string_skip_bytes (&mctx->input, 1);
3b0bdc72
UD
2461 return next_state;
2462}
c13c99fa 2463#endif
3b0bdc72 2464
434d3784 2465#ifdef RE_ENABLE_I18N
a9388965 2466static reg_errcode_t
e2f55264 2467transit_state_mb (re_match_context_t *mctx, re_dfastate_t *pstate)
3b0bdc72 2468{
c42b4152 2469 const re_dfa_t *const dfa = mctx->dfa;
a9388965 2470 reg_errcode_t err;
eb04c213 2471 Idx i;
3b0bdc72
UD
2472
2473 for (i = 0; i < pstate->nodes.nelem; ++i)
2474 {
2475 re_node_set dest_nodes, *new_nodes;
eb04c213
AZ
2476 Idx cur_node_idx = pstate->nodes.elems[i];
2477 int naccepted;
2478 Idx dest_idx;
3b0bdc72
UD
2479 unsigned int context;
2480 re_dfastate_t *dest_state;
2481
963d8d78 2482 if (!dfa->nodes[cur_node_idx].accept_mb)
2da42bc0 2483 continue;
963d8d78 2484
485d775d 2485 if (dfa->nodes[cur_node_idx].constraint)
15a7d175 2486 {
56b168be
UD
2487 context = re_string_context_at (&mctx->input,
2488 re_string_cur_idx (&mctx->input),
2489 mctx->eflags);
15a7d175
UD
2490 if (NOT_SATISFY_NEXT_CONSTRAINT (dfa->nodes[cur_node_idx].constraint,
2491 context))
2492 continue;
2493 }
3b0bdc72 2494
ad7f28c2 2495 /* How many bytes the node can accept? */
963d8d78
UD
2496 naccepted = check_node_accept_bytes (dfa, cur_node_idx, &mctx->input,
2497 re_string_cur_idx (&mctx->input));
3b0bdc72 2498 if (naccepted == 0)
15a7d175 2499 continue;
3b0bdc72 2500
eb04c213 2501 /* The node can accepts 'naccepted' bytes. */
56b168be 2502 dest_idx = re_string_cur_idx (&mctx->input) + naccepted;
0742e48e 2503 mctx->max_mb_elem_len = ((mctx->max_mb_elem_len < naccepted) ? naccepted
15a7d175 2504 : mctx->max_mb_elem_len);
7c1be3ec 2505 err = clean_state_log_if_needed (mctx, dest_idx);
f4efbdfb 2506 if (__glibc_unlikely (err != REG_NOERROR))
15a7d175 2507 return err;
3b0bdc72
UD
2508#ifdef DEBUG
2509 assert (dfa->nexts[cur_node_idx] != -1);
2510#endif
963d8d78 2511 new_nodes = dfa->eclosures + dfa->nexts[cur_node_idx];
3b0bdc72 2512
612546c6 2513 dest_state = mctx->state_log[dest_idx];
3b0bdc72 2514 if (dest_state == NULL)
15a7d175 2515 dest_nodes = *new_nodes;
3b0bdc72 2516 else
15a7d175
UD
2517 {
2518 err = re_node_set_init_union (&dest_nodes,
2519 dest_state->entrance_nodes, new_nodes);
f4efbdfb 2520 if (__glibc_unlikely (err != REG_NOERROR))
15a7d175
UD
2521 return err;
2522 }
01ed6ceb
UD
2523 context = re_string_context_at (&mctx->input, dest_idx - 1,
2524 mctx->eflags);
612546c6 2525 mctx->state_log[dest_idx]
15a7d175 2526 = re_acquire_state_context (&err, dfa, &dest_nodes, context);
3b0bdc72 2527 if (dest_state != NULL)
15a7d175 2528 re_node_set_free (&dest_nodes);
f4efbdfb
PE
2529 if (__glibc_unlikely (mctx->state_log[dest_idx] == NULL
2530 && err != REG_NOERROR))
15a7d175 2531 return err;
3b0bdc72 2532 }
a9388965 2533 return REG_NOERROR;
3b0bdc72 2534}
434d3784 2535#endif /* RE_ENABLE_I18N */
3b0bdc72 2536
a9388965 2537static reg_errcode_t
e2f55264 2538transit_state_bkref (re_match_context_t *mctx, const re_node_set *nodes)
3b0bdc72 2539{
6efbd82c 2540 const re_dfa_t *const dfa = mctx->dfa;
a9388965 2541 reg_errcode_t err;
eb04c213
AZ
2542 Idx i;
2543 Idx cur_str_idx = re_string_cur_idx (&mctx->input);
3b0bdc72
UD
2544
2545 for (i = 0; i < nodes->nelem; ++i)
2546 {
eb04c213
AZ
2547 Idx dest_str_idx, prev_nelem, bkc_idx;
2548 Idx node_idx = nodes->elems[i];
3b0bdc72 2549 unsigned int context;
fe9434bb 2550 const re_token_t *node = dfa->nodes + node_idx;
3b0bdc72
UD
2551 re_node_set *new_dest_nodes;
2552
eb04c213 2553 /* Check whether 'node' is a backreference or not. */
6291ee3c 2554 if (node->type != OP_BACK_REF)
15a7d175 2555 continue;
485d775d
UD
2556
2557 if (node->constraint)
15a7d175 2558 {
56b168be
UD
2559 context = re_string_context_at (&mctx->input, cur_str_idx,
2560 mctx->eflags);
15a7d175
UD
2561 if (NOT_SATISFY_NEXT_CONSTRAINT (node->constraint, context))
2562 continue;
2563 }
3b0bdc72 2564
eb04c213 2565 /* 'node' is a backreference.
15a7d175 2566 Check the substring which the substring matched. */
6291ee3c 2567 bkc_idx = mctx->nbkref_ents;
e3a87852 2568 err = get_subexp (mctx, node_idx, cur_str_idx);
f4efbdfb 2569 if (__glibc_unlikely (err != REG_NOERROR))
15a7d175 2570 goto free_return;
3b0bdc72 2571
eb04c213 2572 /* And add the epsilon closures (which is 'new_dest_nodes') of
15a7d175 2573 the backreference to appropriate state_log. */
3b0bdc72
UD
2574#ifdef DEBUG
2575 assert (dfa->nexts[node_idx] != -1);
2576#endif
6291ee3c 2577 for (; bkc_idx < mctx->nbkref_ents; ++bkc_idx)
15a7d175 2578 {
eb04c213 2579 Idx subexp_len;
15a7d175
UD
2580 re_dfastate_t *dest_state;
2581 struct re_backref_cache_entry *bkref_ent;
2582 bkref_ent = mctx->bkref_ents + bkc_idx;
2583 if (bkref_ent->node != node_idx || bkref_ent->str_idx != cur_str_idx)
2584 continue;
2585 subexp_len = bkref_ent->subexp_to - bkref_ent->subexp_from;
2586 new_dest_nodes = (subexp_len == 0
2587 ? dfa->eclosures + dfa->edests[node_idx].elems[0]
2588 : dfa->eclosures + dfa->nexts[node_idx]);
2589 dest_str_idx = (cur_str_idx + bkref_ent->subexp_to
2590 - bkref_ent->subexp_from);
56b168be
UD
2591 context = re_string_context_at (&mctx->input, dest_str_idx - 1,
2592 mctx->eflags);
15a7d175
UD
2593 dest_state = mctx->state_log[dest_str_idx];
2594 prev_nelem = ((mctx->state_log[cur_str_idx] == NULL) ? 0
2595 : mctx->state_log[cur_str_idx]->nodes.nelem);
eb04c213 2596 /* Add 'new_dest_node' to state_log. */
15a7d175
UD
2597 if (dest_state == NULL)
2598 {
2599 mctx->state_log[dest_str_idx]
2600 = re_acquire_state_context (&err, dfa, new_dest_nodes,
2601 context);
f4efbdfb
PE
2602 if (__glibc_unlikely (mctx->state_log[dest_str_idx] == NULL
2603 && err != REG_NOERROR))
15a7d175
UD
2604 goto free_return;
2605 }
2606 else
2607 {
2608 re_node_set dest_nodes;
2609 err = re_node_set_init_union (&dest_nodes,
2610 dest_state->entrance_nodes,
2611 new_dest_nodes);
f4efbdfb 2612 if (__glibc_unlikely (err != REG_NOERROR))
15a7d175
UD
2613 {
2614 re_node_set_free (&dest_nodes);
2615 goto free_return;
2616 }
2617 mctx->state_log[dest_str_idx]
2618 = re_acquire_state_context (&err, dfa, &dest_nodes, context);
2619 re_node_set_free (&dest_nodes);
f4efbdfb
PE
2620 if (__glibc_unlikely (mctx->state_log[dest_str_idx] == NULL
2621 && err != REG_NOERROR))
15a7d175
UD
2622 goto free_return;
2623 }
2624 /* We need to check recursively if the backreference can epsilon
2625 transit. */
2626 if (subexp_len == 0
2627 && mctx->state_log[cur_str_idx]->nodes.nelem > prev_nelem)
2628 {
e3a87852 2629 err = check_subexp_matching_top (mctx, new_dest_nodes,
6291ee3c 2630 cur_str_idx);
f4efbdfb 2631 if (__glibc_unlikely (err != REG_NOERROR))
6291ee3c 2632 goto free_return;
e3a87852 2633 err = transit_state_bkref (mctx, new_dest_nodes);
f4efbdfb 2634 if (__glibc_unlikely (err != REG_NOERROR))
15a7d175
UD
2635 goto free_return;
2636 }
2637 }
3b0bdc72 2638 }
1b2c2628
UD
2639 err = REG_NOERROR;
2640 free_return:
1b2c2628 2641 return err;
3b0bdc72
UD
2642}
2643
6291ee3c
UD
2644/* Enumerate all the candidates which the backreference BKREF_NODE can match
2645 at BKREF_STR_IDX, and register them by match_ctx_add_entry().
2646 Note that we might collect inappropriate candidates here.
2647 However, the cost of checking them strictly here is too high, then we
2648 delay these checking for prune_impossible_nodes(). */
3b0bdc72 2649
6291ee3c 2650static reg_errcode_t
b41bd5bc 2651__attribute_warn_unused_result__
eb04c213 2652get_subexp (re_match_context_t *mctx, Idx bkref_node, Idx bkref_str_idx)
3b0bdc72 2653{
6efbd82c 2654 const re_dfa_t *const dfa = mctx->dfa;
eb04c213 2655 Idx subexp_num, sub_top_idx;
56b168be 2656 const char *buf = (const char *) re_string_get_buffer (&mctx->input);
6291ee3c 2657 /* Return if we have already checked BKREF_NODE at BKREF_STR_IDX. */
eb04c213 2658 Idx cache_idx = search_cur_bkref_entry (mctx, bkref_str_idx);
e40a38b3 2659 if (cache_idx != -1)
6291ee3c 2660 {
01ed6ceb
UD
2661 const struct re_backref_cache_entry *entry
2662 = mctx->bkref_ents + cache_idx;
e40a38b3 2663 do
2da42bc0 2664 if (entry->node == bkref_node)
e40a38b3
UD
2665 return REG_NOERROR; /* We already checked it. */
2666 while (entry++->more);
6291ee3c 2667 }
e40a38b3 2668
d8f73de8 2669 subexp_num = dfa->nodes[bkref_node].opr.idx;
6291ee3c
UD
2670
2671 /* For each sub expression */
2672 for (sub_top_idx = 0; sub_top_idx < mctx->nsub_tops; ++sub_top_idx)
2673 {
2674 reg_errcode_t err;
2675 re_sub_match_top_t *sub_top = mctx->sub_tops[sub_top_idx];
2676 re_sub_match_last_t *sub_last;
eb04c213 2677 Idx sub_last_idx, sl_str, bkref_str_off;
6291ee3c
UD
2678
2679 if (dfa->nodes[sub_top->node].opr.idx != subexp_num)
2680 continue; /* It isn't related. */
2681
2682 sl_str = sub_top->str_idx;
7c1be3ec 2683 bkref_str_off = bkref_str_idx;
6291ee3c
UD
2684 /* At first, check the last node of sub expressions we already
2685 evaluated. */
2686 for (sub_last_idx = 0; sub_last_idx < sub_top->nlasts; ++sub_last_idx)
2687 {
eb04c213 2688 regoff_t sl_str_diff;
6291ee3c
UD
2689 sub_last = sub_top->lasts[sub_last_idx];
2690 sl_str_diff = sub_last->str_idx - sl_str;
2691 /* The matched string by the sub expression match with the substring
2692 at the back reference? */
c1baba0f
UD
2693 if (sl_str_diff > 0)
2694 {
f4efbdfb
PE
2695 if (__glibc_unlikely (bkref_str_off + sl_str_diff
2696 > mctx->input.valid_len))
c1baba0f
UD
2697 {
2698 /* Not enough chars for a successful match. */
2699 if (bkref_str_off + sl_str_diff > mctx->input.len)
2700 break;
2701
2702 err = clean_state_log_if_needed (mctx,
2703 bkref_str_off
2704 + sl_str_diff);
f4efbdfb 2705 if (__glibc_unlikely (err != REG_NOERROR))
c1baba0f
UD
2706 return err;
2707 buf = (const char *) re_string_get_buffer (&mctx->input);
2708 }
2709 if (memcmp (buf + bkref_str_off, buf + sl_str, sl_str_diff) != 0)
01ed6ceb
UD
2710 /* We don't need to search this sub expression any more. */
2711 break;
c1baba0f 2712 }
7c1be3ec 2713 bkref_str_off += sl_str_diff;
6291ee3c 2714 sl_str += sl_str_diff;
e3a87852 2715 err = get_subexp_sub (mctx, sub_top, sub_last, bkref_node,
6291ee3c 2716 bkref_str_idx);
3ee363e2 2717
7c1be3ec
UD
2718 /* Reload buf, since the preceding call might have reallocated
2719 the buffer. */
56b168be 2720 buf = (const char *) re_string_get_buffer (&mctx->input);
3ee363e2 2721
6291ee3c
UD
2722 if (err == REG_NOMATCH)
2723 continue;
f4efbdfb 2724 if (__glibc_unlikely (err != REG_NOERROR))
6291ee3c
UD
2725 return err;
2726 }
7c1be3ec 2727
6291ee3c
UD
2728 if (sub_last_idx < sub_top->nlasts)
2729 continue;
2730 if (sub_last_idx > 0)
2731 ++sl_str;
2732 /* Then, search for the other last nodes of the sub expression. */
2733 for (; sl_str <= bkref_str_idx; ++sl_str)
2734 {
eb04c213
AZ
2735 Idx cls_node;
2736 regoff_t sl_str_off;
fe9434bb 2737 const re_node_set *nodes;
6291ee3c
UD
2738 sl_str_off = sl_str - sub_top->str_idx;
2739 /* The matched string by the sub expression match with the substring
2740 at the back reference? */
c1baba0f
UD
2741 if (sl_str_off > 0)
2742 {
f4efbdfb 2743 if (__glibc_unlikely (bkref_str_off >= mctx->input.valid_len))
c1baba0f
UD
2744 {
2745 /* If we are at the end of the input, we cannot match. */
2746 if (bkref_str_off >= mctx->input.len)
2747 break;
2748
a445af0b 2749 err = extend_buffers (mctx, bkref_str_off + 1);
f4efbdfb 2750 if (__glibc_unlikely (err != REG_NOERROR))
c1baba0f
UD
2751 return err;
2752
2753 buf = (const char *) re_string_get_buffer (&mctx->input);
2754 }
2755 if (buf [bkref_str_off++] != buf[sl_str - 1])
2756 break; /* We don't need to search this sub expression
2757 any more. */
2758 }
6291ee3c
UD
2759 if (mctx->state_log[sl_str] == NULL)
2760 continue;
2761 /* Does this state have a ')' of the sub expression? */
2762 nodes = &mctx->state_log[sl_str]->nodes;
01ed6ceb
UD
2763 cls_node = find_subexp_node (dfa, nodes, subexp_num,
2764 OP_CLOSE_SUBEXP);
6291ee3c
UD
2765 if (cls_node == -1)
2766 continue; /* No. */
2767 if (sub_top->path == NULL)
2768 {
2769 sub_top->path = calloc (sizeof (state_array_t),
2770 sl_str - sub_top->str_idx + 1);
2771 if (sub_top->path == NULL)
2772 return REG_ESPACE;
2773 }
2774 /* Can the OP_OPEN_SUBEXP node arrive the OP_CLOSE_SUBEXP node
2775 in the current context? */
e3a87852 2776 err = check_arrival (mctx, sub_top->path, sub_top->node,
01ed6ceb
UD
2777 sub_top->str_idx, cls_node, sl_str,
2778 OP_CLOSE_SUBEXP);
6291ee3c
UD
2779 if (err == REG_NOMATCH)
2780 continue;
f4efbdfb 2781 if (__glibc_unlikely (err != REG_NOERROR))
6291ee3c
UD
2782 return err;
2783 sub_last = match_ctx_add_sublast (sub_top, cls_node, sl_str);
f4efbdfb 2784 if (__glibc_unlikely (sub_last == NULL))
6291ee3c 2785 return REG_ESPACE;
e3a87852 2786 err = get_subexp_sub (mctx, sub_top, sub_last, bkref_node,
6291ee3c 2787 bkref_str_idx);
077caf61 2788 buf = (const char *) re_string_get_buffer (&mctx->input);
6291ee3c
UD
2789 if (err == REG_NOMATCH)
2790 continue;
ef202e53
PE
2791 if (__glibc_unlikely (err != REG_NOERROR))
2792 return err;
6291ee3c
UD
2793 }
2794 }
2795 return REG_NOERROR;
2796}
2797
2798/* Helper functions for get_subexp(). */
2799
2800/* Check SUB_LAST can arrive to the back reference BKREF_NODE at BKREF_STR.
2801 If it can arrive, register the sub expression expressed with SUB_TOP
2802 and SUB_LAST. */
2803
2804static reg_errcode_t
e2f55264 2805get_subexp_sub (re_match_context_t *mctx, const re_sub_match_top_t *sub_top,
eb04c213 2806 re_sub_match_last_t *sub_last, Idx bkref_node, Idx bkref_str)
6291ee3c
UD
2807{
2808 reg_errcode_t err;
eb04c213 2809 Idx to_idx;
6291ee3c 2810 /* Can the subexpression arrive the back reference? */
e3a87852 2811 err = check_arrival (mctx, &sub_last->path, sub_last->node,
01ed6ceb
UD
2812 sub_last->str_idx, bkref_node, bkref_str,
2813 OP_OPEN_SUBEXP);
6291ee3c
UD
2814 if (err != REG_NOERROR)
2815 return err;
2816 err = match_ctx_add_entry (mctx, bkref_node, bkref_str, sub_top->str_idx,
2817 sub_last->str_idx);
f4efbdfb 2818 if (__glibc_unlikely (err != REG_NOERROR))
6291ee3c
UD
2819 return err;
2820 to_idx = bkref_str + sub_last->str_idx - sub_top->str_idx;
c1baba0f 2821 return clean_state_log_if_needed (mctx, to_idx);
6291ee3c
UD
2822}
2823
2824/* Find the first node which is '(' or ')' and whose index is SUBEXP_IDX.
2825 Search '(' if FL_OPEN, or search ')' otherwise.
2826 TODO: This function isn't efficient...
2827 Because there might be more than one nodes whose types are
2828 OP_OPEN_SUBEXP and whose index is SUBEXP_IDX, we must check all
2829 nodes.
2830 E.g. RE: (a){2} */
2831
eb04c213 2832static Idx
e2f55264 2833find_subexp_node (const re_dfa_t *dfa, const re_node_set *nodes,
eb04c213 2834 Idx subexp_idx, int type)
6291ee3c 2835{
eb04c213 2836 Idx cls_idx;
6291ee3c
UD
2837 for (cls_idx = 0; cls_idx < nodes->nelem; ++cls_idx)
2838 {
eb04c213 2839 Idx cls_node = nodes->elems[cls_idx];
fe9434bb 2840 const re_token_t *node = dfa->nodes + cls_node;
0ce7f49c 2841 if (node->type == type
6291ee3c
UD
2842 && node->opr.idx == subexp_idx)
2843 return cls_node;
2844 }
2845 return -1;
2846}
2847
2848/* Check whether the node TOP_NODE at TOP_STR can arrive to the node
2849 LAST_NODE at LAST_STR. We record the path onto PATH since it will be
2850 heavily reused.
2851 Return REG_NOERROR if it can arrive, or REG_NOMATCH otherwise. */
2852
2853static reg_errcode_t
b41bd5bc 2854__attribute_warn_unused_result__
eb04c213
AZ
2855check_arrival (re_match_context_t *mctx, state_array_t *path, Idx top_node,
2856 Idx top_str, Idx last_node, Idx last_str, int type)
6291ee3c 2857{
6efbd82c 2858 const re_dfa_t *const dfa = mctx->dfa;
c0c9615c 2859 reg_errcode_t err = REG_NOERROR;
eb04c213 2860 Idx subexp_num, backup_cur_idx, str_idx, null_cnt;
6291ee3c
UD
2861 re_dfastate_t *cur_state = NULL;
2862 re_node_set *cur_nodes, next_nodes;
2863 re_dfastate_t **backup_state_log;
2864 unsigned int context;
2865
2866 subexp_num = dfa->nodes[top_node].opr.idx;
2867 /* Extend the buffer if we need. */
f4efbdfb 2868 if (__glibc_unlikely (path->alloc < last_str + mctx->max_mb_elem_len + 1))
6291ee3c
UD
2869 {
2870 re_dfastate_t **new_array;
eb04c213
AZ
2871 Idx old_alloc = path->alloc;
2872 Idx incr_alloc = last_str + mctx->max_mb_elem_len + 1;
2873 Idx new_alloc;
f4efbdfb 2874 if (__glibc_unlikely (IDX_MAX - old_alloc < incr_alloc))
eb04c213
AZ
2875 return REG_ESPACE;
2876 new_alloc = old_alloc + incr_alloc;
f4efbdfb 2877 if (__glibc_unlikely (SIZE_MAX / sizeof (re_dfastate_t *) < new_alloc))
eb04c213
AZ
2878 return REG_ESPACE;
2879 new_array = re_realloc (path->array, re_dfastate_t *, new_alloc);
f4efbdfb 2880 if (__glibc_unlikely (new_array == NULL))
eb04c213 2881 return REG_ESPACE;
6291ee3c 2882 path->array = new_array;
eb04c213 2883 path->alloc = new_alloc;
6291ee3c
UD
2884 memset (new_array + old_alloc, '\0',
2885 sizeof (re_dfastate_t *) * (path->alloc - old_alloc));
2886 }
2887
eb04c213 2888 str_idx = path->next_idx ? path->next_idx : top_str;
6291ee3c
UD
2889
2890 /* Temporary modify MCTX. */
2891 backup_state_log = mctx->state_log;
56b168be 2892 backup_cur_idx = mctx->input.cur_idx;
6291ee3c 2893 mctx->state_log = path->array;
56b168be 2894 mctx->input.cur_idx = str_idx;
6291ee3c
UD
2895
2896 /* Setup initial node set. */
56b168be 2897 context = re_string_context_at (&mctx->input, str_idx - 1, mctx->eflags);
6291ee3c
UD
2898 if (str_idx == top_str)
2899 {
2900 err = re_node_set_init_1 (&next_nodes, top_node);
f4efbdfb 2901 if (__glibc_unlikely (err != REG_NOERROR))
6291ee3c 2902 return err;
0ce7f49c 2903 err = check_arrival_expand_ecl (dfa, &next_nodes, subexp_num, type);
f4efbdfb 2904 if (__glibc_unlikely (err != REG_NOERROR))
6291ee3c
UD
2905 {
2906 re_node_set_free (&next_nodes);
2907 return err;
2908 }
2909 }
2910 else
2911 {
2912 cur_state = mctx->state_log[str_idx];
2913 if (cur_state && cur_state->has_backref)
2914 {
2915 err = re_node_set_init_copy (&next_nodes, &cur_state->nodes);
f4efbdfb 2916 if (__glibc_unlikely (err != REG_NOERROR))
6291ee3c
UD
2917 return err;
2918 }
2919 else
2920 re_node_set_init_empty (&next_nodes);
2921 }
2922 if (str_idx == top_str || (cur_state && cur_state->has_backref))
2923 {
2924 if (next_nodes.nelem)
2925 {
d2c38eb3 2926 err = expand_bkref_cache (mctx, &next_nodes, str_idx,
0ce7f49c 2927 subexp_num, type);
f4efbdfb 2928 if (__glibc_unlikely (err != REG_NOERROR))
6291ee3c
UD
2929 {
2930 re_node_set_free (&next_nodes);
2931 return err;
2932 }
2933 }
2934 cur_state = re_acquire_state_context (&err, dfa, &next_nodes, context);
f4efbdfb 2935 if (__glibc_unlikely (cur_state == NULL && err != REG_NOERROR))
6291ee3c
UD
2936 {
2937 re_node_set_free (&next_nodes);
2938 return err;
2939 }
2940 mctx->state_log[str_idx] = cur_state;
2941 }
2942
2943 for (null_cnt = 0; str_idx < last_str && null_cnt <= mctx->max_mb_elem_len;)
2944 {
2945 re_node_set_empty (&next_nodes);
2946 if (mctx->state_log[str_idx + 1])
2947 {
2948 err = re_node_set_merge (&next_nodes,
2949 &mctx->state_log[str_idx + 1]->nodes);
f4efbdfb 2950 if (__glibc_unlikely (err != REG_NOERROR))
6291ee3c
UD
2951 {
2952 re_node_set_free (&next_nodes);
2953 return err;
2954 }
2955 }
2956 if (cur_state)
2957 {
e3a87852 2958 err = check_arrival_add_next_nodes (mctx, str_idx,
6efbd82c
UD
2959 &cur_state->non_eps_nodes,
2960 &next_nodes);
f4efbdfb 2961 if (__glibc_unlikely (err != REG_NOERROR))
6291ee3c
UD
2962 {
2963 re_node_set_free (&next_nodes);
2964 return err;
2965 }
2966 }
2967 ++str_idx;
2968 if (next_nodes.nelem)
2969 {
0ce7f49c 2970 err = check_arrival_expand_ecl (dfa, &next_nodes, subexp_num, type);
f4efbdfb 2971 if (__glibc_unlikely (err != REG_NOERROR))
6291ee3c
UD
2972 {
2973 re_node_set_free (&next_nodes);
2974 return err;
2975 }
d2c38eb3 2976 err = expand_bkref_cache (mctx, &next_nodes, str_idx,
0ce7f49c 2977 subexp_num, type);
f4efbdfb 2978 if (__glibc_unlikely (err != REG_NOERROR))
6291ee3c
UD
2979 {
2980 re_node_set_free (&next_nodes);
2981 return err;
2982 }
2983 }
56b168be 2984 context = re_string_context_at (&mctx->input, str_idx - 1, mctx->eflags);
6291ee3c 2985 cur_state = re_acquire_state_context (&err, dfa, &next_nodes, context);
f4efbdfb 2986 if (__glibc_unlikely (cur_state == NULL && err != REG_NOERROR))
6291ee3c
UD
2987 {
2988 re_node_set_free (&next_nodes);
2989 return err;
2990 }
2991 mctx->state_log[str_idx] = cur_state;
2992 null_cnt = cur_state == NULL ? null_cnt + 1 : 0;
2993 }
2994 re_node_set_free (&next_nodes);
2995 cur_nodes = (mctx->state_log[last_str] == NULL ? NULL
2996 : &mctx->state_log[last_str]->nodes);
2997 path->next_idx = str_idx;
2998
2999 /* Fix MCTX. */
3000 mctx->state_log = backup_state_log;
56b168be 3001 mctx->input.cur_idx = backup_cur_idx;
6291ee3c 3002
6291ee3c 3003 /* Then check the current node set has the node LAST_NODE. */
c0d5034e
UD
3004 if (cur_nodes != NULL && re_node_set_contains (cur_nodes, last_node))
3005 return REG_NOERROR;
3006
3007 return REG_NOMATCH;
6291ee3c
UD
3008}
3009
3010/* Helper functions for check_arrival. */
3011
3012/* Calculate the destination nodes of CUR_NODES at STR_IDX, and append them
3013 to NEXT_NODES.
3014 TODO: This function is similar to the functions transit_state*(),
3015 however this function has many additional works.
3016 Can't we unify them? */
3017
3018static reg_errcode_t
b41bd5bc 3019__attribute_warn_unused_result__
eb04c213 3020check_arrival_add_next_nodes (re_match_context_t *mctx, Idx str_idx,
e2f55264 3021 re_node_set *cur_nodes, re_node_set *next_nodes)
6291ee3c 3022{
c42b4152 3023 const re_dfa_t *const dfa = mctx->dfa;
eb04c213
AZ
3024 bool ok;
3025 Idx cur_idx;
ed8ae46b 3026#ifdef RE_ENABLE_I18N
c0c9615c 3027 reg_errcode_t err = REG_NOERROR;
ed8ae46b 3028#endif
6291ee3c
UD
3029 re_node_set union_set;
3030 re_node_set_init_empty (&union_set);
3031 for (cur_idx = 0; cur_idx < cur_nodes->nelem; ++cur_idx)
3032 {
3033 int naccepted = 0;
eb04c213 3034 Idx cur_node = cur_nodes->elems[cur_idx];
a334319f 3035#ifdef DEBUG
02f3550c 3036 re_token_type_t type = dfa->nodes[cur_node].type;
5cf1ec52
UD
3037 assert (!IS_EPSILON_NODE (type));
3038#endif
6291ee3c 3039#ifdef RE_ENABLE_I18N
eb04c213 3040 /* If the node may accept "multi byte". */
02f3550c 3041 if (dfa->nodes[cur_node].accept_mb)
6291ee3c 3042 {
56b168be 3043 naccepted = check_node_accept_bytes (dfa, cur_node, &mctx->input,
6291ee3c
UD
3044 str_idx);
3045 if (naccepted > 1)
3046 {
3047 re_dfastate_t *dest_state;
eb04c213
AZ
3048 Idx next_node = dfa->nexts[cur_node];
3049 Idx next_idx = str_idx + naccepted;
6291ee3c
UD
3050 dest_state = mctx->state_log[next_idx];
3051 re_node_set_empty (&union_set);
3052 if (dest_state)
3053 {
3054 err = re_node_set_merge (&union_set, &dest_state->nodes);
f4efbdfb 3055 if (__glibc_unlikely (err != REG_NOERROR))
6291ee3c
UD
3056 {
3057 re_node_set_free (&union_set);
3058 return err;
3059 }
6291ee3c 3060 }
eb04c213 3061 ok = re_node_set_insert (&union_set, next_node);
f4efbdfb 3062 if (__glibc_unlikely (! ok))
6291ee3c 3063 {
44777771
UD
3064 re_node_set_free (&union_set);
3065 return REG_ESPACE;
6291ee3c
UD
3066 }
3067 mctx->state_log[next_idx] = re_acquire_state (&err, dfa,
3068 &union_set);
f4efbdfb
PE
3069 if (__glibc_unlikely (mctx->state_log[next_idx] == NULL
3070 && err != REG_NOERROR))
6291ee3c
UD
3071 {
3072 re_node_set_free (&union_set);
3073 return err;
3074 }
3075 }
3076 }
3077#endif /* RE_ENABLE_I18N */
3078 if (naccepted
e3a87852 3079 || check_node_accept (mctx, dfa->nodes + cur_node, str_idx))
6291ee3c 3080 {
eb04c213 3081 ok = re_node_set_insert (next_nodes, dfa->nexts[cur_node]);
f4efbdfb 3082 if (__glibc_unlikely (! ok))
6291ee3c
UD
3083 {
3084 re_node_set_free (&union_set);
3085 return REG_ESPACE;
3086 }
3087 }
3088 }
3089 re_node_set_free (&union_set);
3090 return REG_NOERROR;
3091}
3092
3093/* For all the nodes in CUR_NODES, add the epsilon closures of them to
3094 CUR_NODES, however exclude the nodes which are:
3095 - inside the sub expression whose number is EX_SUBEXP, if FL_OPEN.
3096 - out of the sub expression whose number is EX_SUBEXP, if !FL_OPEN.
3097*/
3098
3099static reg_errcode_t
e2f55264 3100check_arrival_expand_ecl (const re_dfa_t *dfa, re_node_set *cur_nodes,
eb04c213 3101 Idx ex_subexp, int type)
6291ee3c
UD
3102{
3103 reg_errcode_t err;
eb04c213 3104 Idx idx, outside_node;
6291ee3c
UD
3105 re_node_set new_nodes;
3106#ifdef DEBUG
3107 assert (cur_nodes->nelem);
3108#endif
3109 err = re_node_set_alloc (&new_nodes, cur_nodes->nelem);
f4efbdfb 3110 if (__glibc_unlikely (err != REG_NOERROR))
6291ee3c
UD
3111 return err;
3112 /* Create a new node set NEW_NODES with the nodes which are epsilon
3113 closures of the node in CUR_NODES. */
3114
3115 for (idx = 0; idx < cur_nodes->nelem; ++idx)
3116 {
eb04c213 3117 Idx cur_node = cur_nodes->elems[idx];
6efbd82c 3118 const re_node_set *eclosure = dfa->eclosures + cur_node;
0ce7f49c 3119 outside_node = find_subexp_node (dfa, eclosure, ex_subexp, type);
6291ee3c
UD
3120 if (outside_node == -1)
3121 {
3122 /* There are no problematic nodes, just merge them. */
3123 err = re_node_set_merge (&new_nodes, eclosure);
f4efbdfb 3124 if (__glibc_unlikely (err != REG_NOERROR))
6291ee3c
UD
3125 {
3126 re_node_set_free (&new_nodes);
3127 return err;
3128 }
3129 }
3130 else
3131 {
3132 /* There are problematic nodes, re-calculate incrementally. */
3133 err = check_arrival_expand_ecl_sub (dfa, &new_nodes, cur_node,
0ce7f49c 3134 ex_subexp, type);
f4efbdfb 3135 if (__glibc_unlikely (err != REG_NOERROR))
6291ee3c
UD
3136 {
3137 re_node_set_free (&new_nodes);
3138 return err;
3139 }
3140 }
3141 }
3142 re_node_set_free (cur_nodes);
3143 *cur_nodes = new_nodes;
3144 return REG_NOERROR;
3145}
3146
3147/* Helper function for check_arrival_expand_ecl.
3148 Check incrementally the epsilon closure of TARGET, and if it isn't
3149 problematic append it to DST_NODES. */
3150
3151static reg_errcode_t
b41bd5bc 3152__attribute_warn_unused_result__
e2f55264 3153check_arrival_expand_ecl_sub (const re_dfa_t *dfa, re_node_set *dst_nodes,
eb04c213 3154 Idx target, Idx ex_subexp, int type)
6291ee3c 3155{
eb04c213 3156 Idx cur_node;
6291ee3c
UD
3157 for (cur_node = target; !re_node_set_contains (dst_nodes, cur_node);)
3158 {
eb04c213 3159 bool ok;
6291ee3c 3160
0ce7f49c 3161 if (dfa->nodes[cur_node].type == type
6291ee3c
UD
3162 && dfa->nodes[cur_node].opr.idx == ex_subexp)
3163 {
0ce7f49c 3164 if (type == OP_CLOSE_SUBEXP)
6291ee3c 3165 {
eb04c213 3166 ok = re_node_set_insert (dst_nodes, cur_node);
f4efbdfb 3167 if (__glibc_unlikely (! ok))
6291ee3c
UD
3168 return REG_ESPACE;
3169 }
3170 break;
3171 }
eb04c213 3172 ok = re_node_set_insert (dst_nodes, cur_node);
f4efbdfb 3173 if (__glibc_unlikely (! ok))
6291ee3c
UD
3174 return REG_ESPACE;
3175 if (dfa->edests[cur_node].nelem == 0)
3176 break;
3177 if (dfa->edests[cur_node].nelem == 2)
3178 {
eb04c213 3179 reg_errcode_t err;
6291ee3c
UD
3180 err = check_arrival_expand_ecl_sub (dfa, dst_nodes,
3181 dfa->edests[cur_node].elems[1],
0ce7f49c 3182 ex_subexp, type);
f4efbdfb 3183 if (__glibc_unlikely (err != REG_NOERROR))
6291ee3c
UD
3184 return err;
3185 }
3186 cur_node = dfa->edests[cur_node].elems[0];
3187 }
3188 return REG_NOERROR;
3189}
3190
3191
3192/* For all the back references in the current state, calculate the
3193 destination of the back references by the appropriate entry
3194 in MCTX->BKREF_ENTS. */
3195
3196static reg_errcode_t
b41bd5bc 3197__attribute_warn_unused_result__
e2f55264 3198expand_bkref_cache (re_match_context_t *mctx, re_node_set *cur_nodes,
eb04c213 3199 Idx cur_str, Idx subexp_num, int type)
6291ee3c 3200{
76b864c8 3201 const re_dfa_t *const dfa = mctx->dfa;
6291ee3c 3202 reg_errcode_t err;
eb04c213 3203 Idx cache_idx_start = search_cur_bkref_entry (mctx, cur_str);
e40a38b3 3204 struct re_backref_cache_entry *ent;
6291ee3c 3205
e40a38b3
UD
3206 if (cache_idx_start == -1)
3207 return REG_NOERROR;
3208
3209 restart:
3210 ent = mctx->bkref_ents + cache_idx_start;
3211 do
6291ee3c 3212 {
eb04c213 3213 Idx to_idx, next_node;
e40a38b3 3214
6291ee3c
UD
3215 /* Is this entry ENT is appropriate? */
3216 if (!re_node_set_contains (cur_nodes, ent->node))
3217 continue; /* No. */
3218
3219 to_idx = cur_str + ent->subexp_to - ent->subexp_from;
3220 /* Calculate the destination of the back reference, and append it
3221 to MCTX->STATE_LOG. */
3222 if (to_idx == cur_str)
3223 {
3224 /* The backreference did epsilon transit, we must re-check all the
3225 node in the current state. */
3226 re_node_set new_dests;
3227 reg_errcode_t err2, err3;
3228 next_node = dfa->edests[ent->node].elems[0];
3229 if (re_node_set_contains (cur_nodes, next_node))
3230 continue;
3231 err = re_node_set_init_1 (&new_dests, next_node);
0ce7f49c 3232 err2 = check_arrival_expand_ecl (dfa, &new_dests, subexp_num, type);
6291ee3c
UD
3233 err3 = re_node_set_merge (cur_nodes, &new_dests);
3234 re_node_set_free (&new_dests);
f4efbdfb
PE
3235 if (__glibc_unlikely (err != REG_NOERROR || err2 != REG_NOERROR
3236 || err3 != REG_NOERROR))
6291ee3c
UD
3237 {
3238 err = (err != REG_NOERROR ? err
3239 : (err2 != REG_NOERROR ? err2 : err3));
3240 return err;
3241 }
3242 /* TODO: It is still inefficient... */
e40a38b3 3243 goto restart;
6291ee3c
UD
3244 }
3245 else
3246 {
3247 re_node_set union_set;
3248 next_node = dfa->nexts[ent->node];
3249 if (mctx->state_log[to_idx])
3250 {
eb04c213 3251 bool ok;
6291ee3c
UD
3252 if (re_node_set_contains (&mctx->state_log[to_idx]->nodes,
3253 next_node))
3254 continue;
3255 err = re_node_set_init_copy (&union_set,
3256 &mctx->state_log[to_idx]->nodes);
eb04c213 3257 ok = re_node_set_insert (&union_set, next_node);
f4efbdfb 3258 if (__glibc_unlikely (err != REG_NOERROR || ! ok))
6291ee3c
UD
3259 {
3260 re_node_set_free (&union_set);
3261 err = err != REG_NOERROR ? err : REG_ESPACE;
3262 return err;
3263 }
3264 }
3265 else
3266 {
3267 err = re_node_set_init_1 (&union_set, next_node);
f4efbdfb 3268 if (__glibc_unlikely (err != REG_NOERROR))
6291ee3c
UD
3269 return err;
3270 }
3271 mctx->state_log[to_idx] = re_acquire_state (&err, dfa, &union_set);
3272 re_node_set_free (&union_set);
f4efbdfb
PE
3273 if (__glibc_unlikely (mctx->state_log[to_idx] == NULL
3274 && err != REG_NOERROR))
6291ee3c
UD
3275 return err;
3276 }
3277 }
e40a38b3 3278 while (ent++->more);
6291ee3c
UD
3279 return REG_NOERROR;
3280}
3281
3282/* Build transition table for the state.
eb04c213 3283 Return true if successful. */
6291ee3c 3284
eb04c213 3285static bool
e2f55264 3286build_trtable (const re_dfa_t *dfa, re_dfastate_t *state)
6291ee3c
UD
3287{
3288 reg_errcode_t err;
eb04c213
AZ
3289 Idx i, j;
3290 int ch;
3291 bool need_word_trtable = false;
2c05d33f
UD
3292 bitset_word_t elem, mask;
3293 bool dests_node_malloced = false;
3294 bool dest_states_malloced = false;
eb04c213 3295 Idx ndests; /* Number of the destination states from 'state'. */
6291ee3c
UD
3296 re_dfastate_t **trtable;
3297 re_dfastate_t **dest_states = NULL, **dest_states_word, **dest_states_nl;
3298 re_node_set follows, *dests_node;
2c05d33f
UD
3299 bitset_t *dests_ch;
3300 bitset_t acceptable;
3301
3302 struct dests_alloc
3303 {
3304 re_node_set dests_node[SBC_MAX];
3305 bitset_t dests_ch[SBC_MAX];
3306 } *dests_alloc;
3b0bdc72
UD
3307
3308 /* We build DFA states which corresponds to the destination nodes
eb04c213
AZ
3309 from 'state'. 'dests_node[i]' represents the nodes which i-th
3310 destination state contains, and 'dests_ch[i]' represents the
3b0bdc72 3311 characters which i-th destination state accepts. */
2c05d33f
UD
3312 if (__libc_use_alloca (sizeof (struct dests_alloc)))
3313 dests_alloc = (struct dests_alloc *) alloca (sizeof (struct dests_alloc));
05dab910 3314 else
05dab910 3315 {
2c05d33f 3316 dests_alloc = re_malloc (struct dests_alloc, 1);
f4efbdfb 3317 if (__glibc_unlikely (dests_alloc == NULL))
eb04c213 3318 return false;
2c05d33f 3319 dests_node_malloced = true;
05dab910 3320 }
2c05d33f
UD
3321 dests_node = dests_alloc->dests_node;
3322 dests_ch = dests_alloc->dests_ch;
3b0bdc72 3323
eb04c213 3324 /* Initialize transition table. */
ab4b89fe 3325 state->word_trtable = state->trtable = NULL;
3b0bdc72 3326
eb04c213 3327 /* At first, group all nodes belonging to 'state' into several
3b0bdc72 3328 destinations. */
56b168be 3329 ndests = group_nodes_into_DFAstates (dfa, state, dests_node, dests_ch);
f4efbdfb 3330 if (__glibc_unlikely (ndests <= 0))
3b0bdc72 3331 {
05dab910 3332 if (dests_node_malloced)
eb04c213
AZ
3333 re_free (dests_alloc);
3334 /* Return false in case of an error, true otherwise. */
05dab910 3335 if (ndests == 0)
c13c99fa 3336 {
3ce12656 3337 state->trtable = (re_dfastate_t **)
ab4b89fe 3338 calloc (sizeof (re_dfastate_t *), SBC_MAX);
f4efbdfb 3339 if (__glibc_unlikely (state->trtable == NULL))
eb04c213
AZ
3340 return false;
3341 return true;
c13c99fa 3342 }
eb04c213 3343 return false;
3b0bdc72
UD
3344 }
3345
a9388965 3346 err = re_node_set_alloc (&follows, ndests + 1);
f4efbdfb 3347 if (__glibc_unlikely (err != REG_NOERROR))
05dab910 3348 goto out_free;
daa84549
PE
3349
3350 /* Avoid arithmetic overflow in size calculation. */
f4efbdfb
PE
3351 size_t ndests_max
3352 = ((SIZE_MAX - (sizeof (re_node_set) + sizeof (bitset_t)) * SBC_MAX)
3353 / (3 * sizeof (re_dfastate_t *)));
3354 if (__glibc_unlikely (ndests_max < ndests))
daa84549 3355 goto out_free;
05dab910 3356
2c05d33f 3357 if (__libc_use_alloca ((sizeof (re_node_set) + sizeof (bitset_t)) * SBC_MAX
05dab910
RM
3358 + ndests * 3 * sizeof (re_dfastate_t *)))
3359 dest_states = (re_dfastate_t **)
ab4b89fe 3360 alloca (ndests * 3 * sizeof (re_dfastate_t *));
05dab910 3361 else
05dab910 3362 {
eb04c213 3363 dest_states = re_malloc (re_dfastate_t *, ndests * 3);
f4efbdfb 3364 if (__glibc_unlikely (dest_states == NULL))
05dab910
RM
3365 {
3366out_free:
3367 if (dest_states_malloced)
eb04c213 3368 re_free (dest_states);
05dab910
RM
3369 re_node_set_free (&follows);
3370 for (i = 0; i < ndests; ++i)
3371 re_node_set_free (dests_node + i);
05dab910 3372 if (dests_node_malloced)
eb04c213
AZ
3373 re_free (dests_alloc);
3374 return false;
05dab910 3375 }
2c05d33f 3376 dest_states_malloced = true;
05dab910
RM
3377 }
3378 dest_states_word = dest_states + ndests;
3379 dest_states_nl = dest_states_word + ndests;
3380 bitset_empty (acceptable);
a9388965 3381
3b0bdc72
UD
3382 /* Then build the states for all destinations. */
3383 for (i = 0; i < ndests; ++i)
3384 {
eb04c213 3385 Idx next_node;
3b0bdc72
UD
3386 re_node_set_empty (&follows);
3387 /* Merge the follows of this destination states. */
3388 for (j = 0; j < dests_node[i].nelem; ++j)
15a7d175
UD
3389 {
3390 next_node = dfa->nexts[dests_node[i].elems[j]];
3391 if (next_node != -1)
3392 {
3393 err = re_node_set_merge (&follows, dfa->eclosures + next_node);
f4efbdfb 3394 if (__glibc_unlikely (err != REG_NOERROR))
05dab910 3395 goto out_free;
15a7d175
UD
3396 }
3397 }
a9388965 3398 dest_states[i] = re_acquire_state_context (&err, dfa, &follows, 0);
f4efbdfb 3399 if (__glibc_unlikely (dest_states[i] == NULL && err != REG_NOERROR))
15a7d175 3400 goto out_free;
3b0bdc72 3401 /* If the new state has context constraint,
15a7d175 3402 build appropriate states for these contexts. */
3b0bdc72 3403 if (dest_states[i]->has_constraint)
15a7d175
UD
3404 {
3405 dest_states_word[i] = re_acquire_state_context (&err, dfa, &follows,
3406 CONTEXT_WORD);
f4efbdfb
PE
3407 if (__glibc_unlikely (dest_states_word[i] == NULL
3408 && err != REG_NOERROR))
15a7d175 3409 goto out_free;
3ce12656 3410
ab4b89fe 3411 if (dest_states[i] != dest_states_word[i] && dfa->mb_cur_max > 1)
eb04c213 3412 need_word_trtable = true;
3ce12656 3413
15a7d175
UD
3414 dest_states_nl[i] = re_acquire_state_context (&err, dfa, &follows,
3415 CONTEXT_NEWLINE);
f4efbdfb 3416 if (__glibc_unlikely (dest_states_nl[i] == NULL && err != REG_NOERROR))
15a7d175 3417 goto out_free;
eb04c213 3418 }
3b0bdc72 3419 else
15a7d175
UD
3420 {
3421 dest_states_word[i] = dest_states[i];
3422 dest_states_nl[i] = dest_states[i];
3423 }
3b0bdc72
UD
3424 bitset_merge (acceptable, dests_ch[i]);
3425 }
3426
f4efbdfb 3427 if (!__glibc_unlikely (need_word_trtable))
3ce12656
UD
3428 {
3429 /* We don't care about whether the following character is a word
3430 character, or we are in a single-byte character set so we can
3431 discern by looking at the character code: allocate a
3432 256-entry transition table. */
ab4b89fe
UD
3433 trtable = state->trtable =
3434 (re_dfastate_t **) calloc (sizeof (re_dfastate_t *), SBC_MAX);
f4efbdfb 3435 if (__glibc_unlikely (trtable == NULL))
3ce12656
UD
3436 goto out_free;
3437
3438 /* For all characters ch...: */
2c05d33f
UD
3439 for (i = 0; i < BITSET_WORDS; ++i)
3440 for (ch = i * BITSET_WORD_BITS, elem = acceptable[i], mask = 1;
3ce12656
UD
3441 elem;
3442 mask <<= 1, elem >>= 1, ++ch)
f4efbdfb 3443 if (__glibc_unlikely (elem & 1))
3ce12656
UD
3444 {
3445 /* There must be exactly one destination which accepts
3446 character ch. See group_nodes_into_DFAstates. */
3447 for (j = 0; (dests_ch[j][i] & mask) == 0; ++j)
3448 ;
6b6557e8 3449
3ce12656 3450 /* j-th destination accepts the word character ch. */
56b168be 3451 if (dfa->word_char[i] & mask)
3ce12656
UD
3452 trtable[ch] = dest_states_word[j];
3453 else
3454 trtable[ch] = dest_states[j];
3455 }
3456 }
3457 else
3458 {
3459 /* We care about whether the following character is a word
3460 character, and we are in a multi-byte character set: discern
3461 by looking at the character code: build two 256-entry
3462 transition tables, one starting at trtable[0] and one
3463 starting at trtable[SBC_MAX]. */
ab4b89fe
UD
3464 trtable = state->word_trtable =
3465 (re_dfastate_t **) calloc (sizeof (re_dfastate_t *), 2 * SBC_MAX);
f4efbdfb 3466 if (__glibc_unlikely (trtable == NULL))
3ce12656 3467 goto out_free;
6b6557e8 3468
3ce12656 3469 /* For all characters ch...: */
2c05d33f
UD
3470 for (i = 0; i < BITSET_WORDS; ++i)
3471 for (ch = i * BITSET_WORD_BITS, elem = acceptable[i], mask = 1;
3ce12656
UD
3472 elem;
3473 mask <<= 1, elem >>= 1, ++ch)
f4efbdfb 3474 if (__glibc_unlikely (elem & 1))
3ce12656
UD
3475 {
3476 /* There must be exactly one destination which accepts
3477 character ch. See group_nodes_into_DFAstates. */
3478 for (j = 0; (dests_ch[j][i] & mask) == 0; ++j)
3479 ;
6b6557e8 3480
3ce12656
UD
3481 /* j-th destination accepts the word character ch. */
3482 trtable[ch] = dest_states[j];
3483 trtable[ch + SBC_MAX] = dest_states_word[j];
3484 }
3485 }
6b6557e8 3486
3b0bdc72 3487 /* new line */
c202c2c5
UD
3488 if (bitset_contain (acceptable, NEWLINE_CHAR))
3489 {
3490 /* The current state accepts newline character. */
3ce12656
UD
3491 for (j = 0; j < ndests; ++j)
3492 if (bitset_contain (dests_ch[j], NEWLINE_CHAR))
15a7d175
UD
3493 {
3494 /* k-th destination accepts newline character. */
3ce12656 3495 trtable[NEWLINE_CHAR] = dest_states_nl[j];
ab4b89fe 3496 if (need_word_trtable)
3ce12656 3497 trtable[NEWLINE_CHAR + SBC_MAX] = dest_states_nl[j];
15a7d175
UD
3498 /* There must be only one destination which accepts
3499 newline. See group_nodes_into_DFAstates. */
3500 break;
3501 }
c202c2c5 3502 }
3b0bdc72 3503
05dab910 3504 if (dest_states_malloced)
eb04c213 3505 re_free (dest_states);
3b0bdc72
UD
3506
3507 re_node_set_free (&follows);
3508 for (i = 0; i < ndests; ++i)
3509 re_node_set_free (dests_node + i);
3510
05dab910 3511 if (dests_node_malloced)
eb04c213 3512 re_free (dests_alloc);
3b0bdc72 3513
eb04c213 3514 return true;
3b0bdc72
UD
3515}
3516
3517/* Group all nodes belonging to STATE into several destinations.
3518 Then for all destinations, set the nodes belonging to the destination
3519 to DESTS_NODE[i] and set the characters accepted by the destination
3520 to DEST_CH[i]. This function return the number of destinations. */
3521
eb04c213 3522static Idx
e2f55264
UD
3523group_nodes_into_DFAstates (const re_dfa_t *dfa, const re_dfastate_t *state,
3524 re_node_set *dests_node, bitset_t *dests_ch)
3b0bdc72 3525{
a9388965 3526 reg_errcode_t err;
eb04c213
AZ
3527 bool ok;
3528 Idx i, j, k;
3529 Idx ndests; /* Number of the destinations from 'state'. */
2c05d33f 3530 bitset_t accepts; /* Characters a node can accept. */
3b0bdc72
UD
3531 const re_node_set *cur_nodes = &state->nodes;
3532 bitset_empty (accepts);
3533 ndests = 0;
3534
eb04c213 3535 /* For all the nodes belonging to 'state', */
3b0bdc72
UD
3536 for (i = 0; i < cur_nodes->nelem; ++i)
3537 {
3b0bdc72
UD
3538 re_token_t *node = &dfa->nodes[cur_nodes->elems[i]];
3539 re_token_type_t type = node->type;
485d775d 3540 unsigned int constraint = node->constraint;
3b0bdc72
UD
3541
3542 /* Enumerate all single byte character this node can accept. */
3543 if (type == CHARACTER)
15a7d175 3544 bitset_set (accepts, node->opr.c);
3b0bdc72 3545 else if (type == SIMPLE_BRACKET)
15a7d175
UD
3546 {
3547 bitset_merge (accepts, node->opr.sbcset);
3548 }
3b0bdc72 3549 else if (type == OP_PERIOD)
15a7d175 3550 {
65e6becf
UD
3551#ifdef RE_ENABLE_I18N
3552 if (dfa->mb_cur_max > 1)
3553 bitset_merge (accepts, dfa->sb_char);
3554 else
0ce7f49c 3555#endif
65e6becf 3556 bitset_set_all (accepts);
56b168be 3557 if (!(dfa->syntax & RE_DOT_NEWLINE))
15a7d175 3558 bitset_clear (accepts, '\n');
56b168be 3559 if (dfa->syntax & RE_DOT_NOT_NULL)
15a7d175
UD
3560 bitset_clear (accepts, '\0');
3561 }
c0d5034e 3562#ifdef RE_ENABLE_I18N
ad7f28c2 3563 else if (type == OP_UTF8_PERIOD)
2da42bc0 3564 {
eb04c213
AZ
3565 if (ASCII_CHARS % BITSET_WORD_BITS == 0)
3566 memset (accepts, -1, ASCII_CHARS / CHAR_BIT);
3567 else
3568 bitset_merge (accepts, utf8_sb_map);
56b168be 3569 if (!(dfa->syntax & RE_DOT_NEWLINE))
ad7f28c2 3570 bitset_clear (accepts, '\n');
56b168be 3571 if (dfa->syntax & RE_DOT_NOT_NULL)
ad7f28c2 3572 bitset_clear (accepts, '\0');
2da42bc0 3573 }
c0d5034e 3574#endif
3b0bdc72 3575 else
15a7d175 3576 continue;
3b0bdc72 3577
eb04c213 3578 /* Check the 'accepts' and sift the characters which are not
15a7d175 3579 match it the context. */
3b0bdc72 3580 if (constraint)
15a7d175 3581 {
15a7d175
UD
3582 if (constraint & NEXT_NEWLINE_CONSTRAINT)
3583 {
2c05d33f 3584 bool accepts_newline = bitset_contain (accepts, NEWLINE_CHAR);
15a7d175
UD
3585 bitset_empty (accepts);
3586 if (accepts_newline)
3587 bitset_set (accepts, NEWLINE_CHAR);
3588 else
3589 continue;
3590 }
66b110e8
UD
3591 if (constraint & NEXT_ENDBUF_CONSTRAINT)
3592 {
3593 bitset_empty (accepts);
3594 continue;
3595 }
c13c99fa 3596
66b110e8 3597 if (constraint & NEXT_WORD_CONSTRAINT)
65e6becf 3598 {
2c05d33f 3599 bitset_word_t any_set = 0;
1cef7b3c
UD
3600 if (type == CHARACTER && !node->word_char)
3601 {
3602 bitset_empty (accepts);
3603 continue;
3604 }
65e6becf
UD
3605#ifdef RE_ENABLE_I18N
3606 if (dfa->mb_cur_max > 1)
2c05d33f 3607 for (j = 0; j < BITSET_WORDS; ++j)
457beec8 3608 any_set |= (accepts[j] &= (dfa->word_char[j] | ~dfa->sb_char[j]));
65e6becf
UD
3609 else
3610#endif
2c05d33f 3611 for (j = 0; j < BITSET_WORDS; ++j)
457beec8
UD
3612 any_set |= (accepts[j] &= dfa->word_char[j]);
3613 if (!any_set)
3614 continue;
65e6becf 3615 }
66b110e8 3616 if (constraint & NEXT_NOTWORD_CONSTRAINT)
65e6becf 3617 {
2c05d33f 3618 bitset_word_t any_set = 0;
1cef7b3c
UD
3619 if (type == CHARACTER && node->word_char)
3620 {
3621 bitset_empty (accepts);
3622 continue;
3623 }
65e6becf
UD
3624#ifdef RE_ENABLE_I18N
3625 if (dfa->mb_cur_max > 1)
2c05d33f 3626 for (j = 0; j < BITSET_WORDS; ++j)
457beec8 3627 any_set |= (accepts[j] &= ~(dfa->word_char[j] & dfa->sb_char[j]));
65e6becf
UD
3628 else
3629#endif
2c05d33f 3630 for (j = 0; j < BITSET_WORDS; ++j)
457beec8
UD
3631 any_set |= (accepts[j] &= ~dfa->word_char[j]);
3632 if (!any_set)
3633 continue;
65e6becf 3634 }
15a7d175 3635 }
3b0bdc72 3636
eb04c213 3637 /* Then divide 'accepts' into DFA states, or create a new
457beec8 3638 state. Above, we make sure that accepts is not empty. */
3b0bdc72 3639 for (j = 0; j < ndests; ++j)
15a7d175 3640 {
2c05d33f
UD
3641 bitset_t intersec; /* Intersection sets, see below. */
3642 bitset_t remains;
15a7d175 3643 /* Flags, see below. */
2c05d33f 3644 bitset_word_t has_intersec, not_subset, not_consumed;
15a7d175
UD
3645
3646 /* Optimization, skip if this state doesn't accept the character. */
3647 if (type == CHARACTER && !bitset_contain (dests_ch[j], node->opr.c))
3648 continue;
3649
eb04c213 3650 /* Enumerate the intersection set of this state and 'accepts'. */
15a7d175 3651 has_intersec = 0;
2c05d33f 3652 for (k = 0; k < BITSET_WORDS; ++k)
15a7d175
UD
3653 has_intersec |= intersec[k] = accepts[k] & dests_ch[j][k];
3654 /* And skip if the intersection set is empty. */
3655 if (!has_intersec)
3656 continue;
3657
eb04c213 3658 /* Then check if this state is a subset of 'accepts'. */
15a7d175 3659 not_subset = not_consumed = 0;
2c05d33f 3660 for (k = 0; k < BITSET_WORDS; ++k)
15a7d175
UD
3661 {
3662 not_subset |= remains[k] = ~accepts[k] & dests_ch[j][k];
3663 not_consumed |= accepts[k] = accepts[k] & ~dests_ch[j][k];
3664 }
3665
eb04c213
AZ
3666 /* If this state isn't a subset of 'accepts', create a
3667 new group state, which has the 'remains'. */
15a7d175
UD
3668 if (not_subset)
3669 {
3670 bitset_copy (dests_ch[ndests], remains);
3671 bitset_copy (dests_ch[j], intersec);
3672 err = re_node_set_init_copy (dests_node + ndests, &dests_node[j]);
f4efbdfb 3673 if (__glibc_unlikely (err != REG_NOERROR))
15a7d175
UD
3674 goto error_return;
3675 ++ndests;
3676 }
3677
3678 /* Put the position in the current group. */
eb04c213 3679 ok = re_node_set_insert (&dests_node[j], cur_nodes->elems[i]);
f4efbdfb 3680 if (__glibc_unlikely (! ok))
15a7d175
UD
3681 goto error_return;
3682
3683 /* If all characters are consumed, go to next node. */
3684 if (!not_consumed)
3685 break;
3686 }
3b0bdc72
UD
3687 /* Some characters remain, create a new group. */
3688 if (j == ndests)
15a7d175
UD
3689 {
3690 bitset_copy (dests_ch[ndests], accepts);
3691 err = re_node_set_init_1 (dests_node + ndests, cur_nodes->elems[i]);
f4efbdfb 3692 if (__glibc_unlikely (err != REG_NOERROR))
15a7d175
UD
3693 goto error_return;
3694 ++ndests;
3695 bitset_empty (accepts);
3696 }
3b0bdc72
UD
3697 }
3698 return ndests;
1b2c2628
UD
3699 error_return:
3700 for (j = 0; j < ndests; ++j)
3701 re_node_set_free (dests_node + j);
3702 return -1;
3b0bdc72
UD
3703}
3704
434d3784 3705#ifdef RE_ENABLE_I18N
eb04c213 3706/* Check how many bytes the node 'dfa->nodes[node_idx]' accepts.
434d3784
UD
3707 Return the number of the bytes the node accepts.
3708 STR_IDX is the current index of the input string.
3709
3710 This function handles the nodes which can accept one character, or
3711 one collating element like '.', '[a-z]', opposite to the other nodes
3712 can only accept one byte. */
3b0bdc72 3713
8c0ab919
RM
3714# ifdef _LIBC
3715# include <locale/weight.h>
3716# endif
3717
3b0bdc72 3718static int
eb04c213
AZ
3719check_node_accept_bytes (const re_dfa_t *dfa, Idx node_idx,
3720 const re_string_t *input, Idx str_idx)
3b0bdc72 3721{
3b0bdc72 3722 const re_token_t *node = dfa->nodes + node_idx;
ad7f28c2 3723 int char_len, elem_len;
eb04c213 3724 Idx i;
ad7f28c2 3725
f4efbdfb 3726 if (__glibc_unlikely (node->type == OP_UTF8_PERIOD))
ad7f28c2
UD
3727 {
3728 unsigned char c = re_string_byte_at (input, str_idx), d;
f4efbdfb 3729 if (__glibc_likely (c < 0xc2))
ad7f28c2
UD
3730 return 0;
3731
3732 if (str_idx + 2 > input->len)
3733 return 0;
3734
3735 d = re_string_byte_at (input, str_idx + 1);
3736 if (c < 0xe0)
3737 return (d < 0x80 || d > 0xbf) ? 0 : 2;
3738 else if (c < 0xf0)
3739 {
3740 char_len = 3;
3741 if (c == 0xe0 && d < 0xa0)
3742 return 0;
3743 }
3744 else if (c < 0xf8)
3745 {
3746 char_len = 4;
3747 if (c == 0xf0 && d < 0x90)
3748 return 0;
3749 }
3750 else if (c < 0xfc)
3751 {
3752 char_len = 5;
3753 if (c == 0xf8 && d < 0x88)
3754 return 0;
3755 }
3756 else if (c < 0xfe)
3757 {
3758 char_len = 6;
3759 if (c == 0xfc && d < 0x84)
3760 return 0;
3761 }
3762 else
3763 return 0;
3764
3765 if (str_idx + char_len > input->len)
3766 return 0;
3767
3768 for (i = 1; i < char_len; ++i)
3769 {
3770 d = re_string_byte_at (input, str_idx + i);
3771 if (d < 0x80 || d > 0xbf)
3772 return 0;
3773 }
3774 return char_len;
3775 }
3776
3777 char_len = re_string_char_size_at (input, str_idx);
3b0bdc72
UD
3778 if (node->type == OP_PERIOD)
3779 {
ad7f28c2 3780 if (char_len <= 1)
2da42bc0 3781 return 0;
ad7f28c2
UD
3782 /* FIXME: I don't think this if is needed, as both '\n'
3783 and '\0' are char_len == 1. */
434d3784 3784 /* '.' accepts any one character except the following two cases. */
34a5a146
JM
3785 if ((!(dfa->syntax & RE_DOT_NEWLINE)
3786 && re_string_byte_at (input, str_idx) == '\n')
3787 || ((dfa->syntax & RE_DOT_NOT_NULL)
3788 && re_string_byte_at (input, str_idx) == '\0'))
15a7d175 3789 return 0;
3b0bdc72
UD
3790 return char_len;
3791 }
ad7f28c2
UD
3792
3793 elem_len = re_string_elem_size_at (input, str_idx);
6c2a04a7 3794 if ((elem_len <= 1 && char_len <= 1) || char_len == 0)
ad7f28c2
UD
3795 return 0;
3796
3797 if (node->type == COMPLEX_BRACKET)
3b0bdc72
UD
3798 {
3799 const re_charset_t *cset = node->opr.mbcset;
434d3784 3800# ifdef _LIBC
1c99f950
UD
3801 const unsigned char *pin
3802 = ((const unsigned char *) re_string_get_buffer (input) + str_idx);
eb04c213 3803 Idx j;
5f93cd52 3804 uint32_t nrules;
434d3784
UD
3805# endif /* _LIBC */
3806 int match_len = 0;
3807 wchar_t wc = ((cset->nranges || cset->nchar_classes || cset->nmbchars)
15a7d175 3808 ? re_string_wchar_at (input, str_idx) : 0);
434d3784
UD
3809
3810 /* match with multibyte character? */
3811 for (i = 0; i < cset->nmbchars; ++i)
15a7d175
UD
3812 if (wc == cset->mbchars[i])
3813 {
3814 match_len = char_len;
3815 goto check_node_accept_bytes_match;
3816 }
434d3784
UD
3817 /* match with character_class? */
3818 for (i = 0; i < cset->nchar_classes; ++i)
15a7d175
UD
3819 {
3820 wctype_t wt = cset->char_classes[i];
3821 if (__iswctype (wc, wt))
3822 {
3823 match_len = char_len;
3824 goto check_node_accept_bytes_match;
3825 }
3826 }
434d3784
UD
3827
3828# ifdef _LIBC
5f93cd52 3829 nrules = _NL_CURRENT_WORD (LC_COLLATE, _NL_COLLATE_NRULES);
3b0bdc72 3830 if (nrules != 0)
15a7d175
UD
3831 {
3832 unsigned int in_collseq = 0;
3833 const int32_t *table, *indirect;
3834 const unsigned char *weights, *extra;
3835 const char *collseqwc;
3b0bdc72 3836
15a7d175
UD
3837 /* match with collating_symbol? */
3838 if (cset->ncoll_syms)
3839 extra = (const unsigned char *)
3840 _NL_CURRENT (LC_COLLATE, _NL_COLLATE_SYMB_EXTRAMB);
3841 for (i = 0; i < cset->ncoll_syms; ++i)
3842 {
3843 const unsigned char *coll_sym = extra + cset->coll_syms[i];
3844 /* Compare the length of input collating element and
3845 the length of current collating element. */
3846 if (*coll_sym != elem_len)
3847 continue;
3848 /* Compare each bytes. */
3849 for (j = 0; j < *coll_sym; j++)
3850 if (pin[j] != coll_sym[1 + j])
3851 break;
3852 if (j == *coll_sym)
3853 {
3854 /* Match if every bytes is equal. */
3855 match_len = j;
3856 goto check_node_accept_bytes_match;
3857 }
3858 }
3859
3860 if (cset->nranges)
3861 {
3862 if (elem_len <= char_len)
3863 {
3864 collseqwc = _NL_CURRENT (LC_COLLATE, _NL_COLLATE_COLLSEQWC);
25337753 3865 in_collseq = __collseq_table_lookup (collseqwc, wc);
15a7d175
UD
3866 }
3867 else
3868 in_collseq = find_collation_sequence_value (pin, elem_len);
3869 }
3870 /* match with range expression? */
eb04c213 3871 /* FIXME: Implement rational ranges here, too. */
15a7d175
UD
3872 for (i = 0; i < cset->nranges; ++i)
3873 if (cset->range_starts[i] <= in_collseq
3874 && in_collseq <= cset->range_ends[i])
3875 {
3876 match_len = elem_len;
3877 goto check_node_accept_bytes_match;
3878 }
3879
3880 /* match with equivalence_class? */
3881 if (cset->nequiv_classes)
3882 {
3883 const unsigned char *cp = pin;
3884 table = (const int32_t *)
3885 _NL_CURRENT (LC_COLLATE, _NL_COLLATE_TABLEMB);
3886 weights = (const unsigned char *)
3887 _NL_CURRENT (LC_COLLATE, _NL_COLLATE_WEIGHTMB);
3888 extra = (const unsigned char *)
3889 _NL_CURRENT (LC_COLLATE, _NL_COLLATE_EXTRAMB);
3890 indirect = (const int32_t *)
3891 _NL_CURRENT (LC_COLLATE, _NL_COLLATE_INDIRECTMB);
8c0ab919 3892 int32_t idx = findidx (table, indirect, extra, &cp, elem_len);
7b2f4ced
FW
3893 int32_t rule = idx >> 24;
3894 idx &= 0xffffff;
15a7d175 3895 if (idx > 0)
7b2f4ced
FW
3896 {
3897 size_t weight_len = weights[idx];
3898 for (i = 0; i < cset->nequiv_classes; ++i)
3899 {
3900 int32_t equiv_class_idx = cset->equiv_classes[i];
3901 int32_t equiv_class_rule = equiv_class_idx >> 24;
3902 equiv_class_idx &= 0xffffff;
3903 if (weights[equiv_class_idx] == weight_len
3904 && equiv_class_rule == rule
3905 && memcmp (weights + idx + 1,
3906 weights + equiv_class_idx + 1,
3907 weight_len) == 0)
3908 {
3909 match_len = elem_len;
3910 goto check_node_accept_bytes_match;
3911 }
3912 }
3913 }
15a7d175
UD
3914 }
3915 }
434d3784
UD
3916 else
3917# endif /* _LIBC */
15a7d175
UD
3918 {
3919 /* match with range expression? */
15a7d175
UD
3920 for (i = 0; i < cset->nranges; ++i)
3921 {
eb04c213 3922 if (cset->range_starts[i] <= wc && wc <= cset->range_ends[i])
15a7d175
UD
3923 {
3924 match_len = char_len;
3925 goto check_node_accept_bytes_match;
3926 }
3927 }
3928 }
434d3784
UD
3929 check_node_accept_bytes_match:
3930 if (!cset->non_match)
15a7d175 3931 return match_len;
434d3784 3932 else
15a7d175
UD
3933 {
3934 if (match_len > 0)
3935 return 0;
3936 else
3937 return (elem_len > char_len) ? elem_len : char_len;
3938 }
3b0bdc72
UD
3939 }
3940 return 0;
3941}
3942
434d3784 3943# ifdef _LIBC
3b0bdc72 3944static unsigned int
e2f55264 3945find_collation_sequence_value (const unsigned char *mbs, size_t mbs_len)
3b0bdc72
UD
3946{
3947 uint32_t nrules = _NL_CURRENT_WORD (LC_COLLATE, _NL_COLLATE_NRULES);
3948 if (nrules == 0)
3949 {
3950 if (mbs_len == 1)
15a7d175
UD
3951 {
3952 /* No valid character. Match it as a single byte character. */
3953 const unsigned char *collseq = (const unsigned char *)
3954 _NL_CURRENT (LC_COLLATE, _NL_COLLATE_COLLSEQMB);
3955 return collseq[mbs[0]];
3956 }
3b0bdc72
UD
3957 return UINT_MAX;
3958 }
3959 else
3960 {
3961 int32_t idx;
3962 const unsigned char *extra = (const unsigned char *)
15a7d175 3963 _NL_CURRENT (LC_COLLATE, _NL_COLLATE_SYMB_EXTRAMB);
6c2a04a7
UD
3964 int32_t extrasize = (const unsigned char *)
3965 _NL_CURRENT (LC_COLLATE, _NL_COLLATE_SYMB_EXTRAMB + 1) - extra;
3b0bdc72 3966
6c2a04a7 3967 for (idx = 0; idx < extrasize;)
15a7d175 3968 {
eb04c213
AZ
3969 int mbs_cnt;
3970 bool found = false;
15a7d175
UD
3971 int32_t elem_mbs_len;
3972 /* Skip the name of collating element name. */
3973 idx = idx + extra[idx] + 1;
3974 elem_mbs_len = extra[idx++];
3975 if (mbs_len == elem_mbs_len)
3976 {
3977 for (mbs_cnt = 0; mbs_cnt < elem_mbs_len; ++mbs_cnt)
3978 if (extra[idx + mbs_cnt] != mbs[mbs_cnt])
3979 break;
3980 if (mbs_cnt == elem_mbs_len)
3981 /* Found the entry. */
eb04c213 3982 found = true;
15a7d175
UD
3983 }
3984 /* Skip the byte sequence of the collating element. */
3985 idx += elem_mbs_len;
3986 /* Adjust for the alignment. */
3987 idx = (idx + 3) & ~3;
3988 /* Skip the collation sequence value. */
3989 idx += sizeof (uint32_t);
3990 /* Skip the wide char sequence of the collating element. */
d84acf38 3991 idx = idx + sizeof (uint32_t) * (*(int32_t *) (extra + idx) + 1);
15a7d175
UD
3992 /* If we found the entry, return the sequence value. */
3993 if (found)
3994 return *(uint32_t *) (extra + idx);
3995 /* Skip the collation sequence value. */
3996 idx += sizeof (uint32_t);
3997 }
6c2a04a7 3998 return UINT_MAX;
3b0bdc72
UD
3999 }
4000}
434d3784
UD
4001# endif /* _LIBC */
4002#endif /* RE_ENABLE_I18N */
3b0bdc72
UD
4003
4004/* Check whether the node accepts the byte which is IDX-th
4005 byte of the INPUT. */
4006
eb04c213 4007static bool
e2f55264 4008check_node_accept (const re_match_context_t *mctx, const re_token_t *node,
eb04c213 4009 Idx idx)
3b0bdc72 4010{
3b0bdc72 4011 unsigned char ch;
56b168be 4012 ch = re_string_byte_at (&mctx->input, idx);
ad7f28c2
UD
4013 switch (node->type)
4014 {
4015 case CHARACTER:
5cf1ec52 4016 if (node->opr.c != ch)
eb04c213 4017 return false;
5cf1ec52
UD
4018 break;
4019
ad7f28c2 4020 case SIMPLE_BRACKET:
5cf1ec52 4021 if (!bitset_contain (node->opr.sbcset, ch))
eb04c213 4022 return false;
5cf1ec52
UD
4023 break;
4024
c0d5034e 4025#ifdef RE_ENABLE_I18N
ad7f28c2 4026 case OP_UTF8_PERIOD:
eb04c213
AZ
4027 if (ch >= ASCII_CHARS)
4028 return false;
4029 FALLTHROUGH;
c0d5034e 4030#endif
ad7f28c2 4031 case OP_PERIOD:
5cf1ec52
UD
4032 if ((ch == '\n' && !(mctx->dfa->syntax & RE_DOT_NEWLINE))
4033 || (ch == '\0' && (mctx->dfa->syntax & RE_DOT_NOT_NULL)))
eb04c213 4034 return false;
5cf1ec52
UD
4035 break;
4036
ad7f28c2 4037 default:
eb04c213 4038 return false;
ad7f28c2 4039 }
5cf1ec52
UD
4040
4041 if (node->constraint)
4042 {
4043 /* The node has constraints. Check whether the current context
4044 satisfies the constraints. */
4045 unsigned int context = re_string_context_at (&mctx->input, idx,
4046 mctx->eflags);
4047 if (NOT_SATISFY_NEXT_CONSTRAINT (node->constraint, context))
eb04c213 4048 return false;
5cf1ec52
UD
4049 }
4050
eb04c213 4051 return true;
3b0bdc72 4052}
612546c6
UD
4053
4054/* Extend the buffers, if the buffers have run out. */
4055
4056static reg_errcode_t
b41bd5bc 4057__attribute_warn_unused_result__
a445af0b 4058extend_buffers (re_match_context_t *mctx, int min_len)
612546c6
UD
4059{
4060 reg_errcode_t ret;
56b168be 4061 re_string_t *pstr = &mctx->input;
612546c6 4062
aef699dc 4063 /* Avoid overflow. */
f4efbdfb
PE
4064 if (__glibc_unlikely (MIN (IDX_MAX, SIZE_MAX / sizeof (re_dfastate_t *)) / 2
4065 <= pstr->bufs_len))
aef699dc
PE
4066 return REG_ESPACE;
4067
eb04c213 4068 /* Double the lengths of the buffers, but allocate at least MIN_LEN. */
a445af0b
AS
4069 ret = re_string_realloc_buffers (pstr,
4070 MAX (min_len,
4071 MIN (pstr->len, pstr->bufs_len * 2)));
f4efbdfb 4072 if (__glibc_unlikely (ret != REG_NOERROR))
612546c6
UD
4073 return ret;
4074
4075 if (mctx->state_log != NULL)
4076 {
4077 /* And double the length of state_log. */
951d6408
UD
4078 /* XXX We have no indication of the size of this buffer. If this
4079 allocation fail we have no indication that the state_log array
4080 does not have the right size. */
4081 re_dfastate_t **new_array = re_realloc (mctx->state_log, re_dfastate_t *,
4082 pstr->bufs_len + 1);
f4efbdfb 4083 if (__glibc_unlikely (new_array == NULL))
15a7d175 4084 return REG_ESPACE;
1b2c2628 4085 mctx->state_log = new_array;
612546c6
UD
4086 }
4087
4088 /* Then reconstruct the buffers. */
4089 if (pstr->icase)
4090 {
4091#ifdef RE_ENABLE_I18N
3c0fb574 4092 if (pstr->mb_cur_max > 1)
bb3f4825
UD
4093 {
4094 ret = build_wcs_upper_buffer (pstr);
f4efbdfb 4095 if (__glibc_unlikely (ret != REG_NOERROR))
bb3f4825
UD
4096 return ret;
4097 }
612546c6
UD
4098 else
4099#endif /* RE_ENABLE_I18N */
15a7d175 4100 build_upper_buffer (pstr);
612546c6
UD
4101 }
4102 else
4103 {
4104#ifdef RE_ENABLE_I18N
3c0fb574 4105 if (pstr->mb_cur_max > 1)
15a7d175 4106 build_wcs_buffer (pstr);
612546c6
UD
4107 else
4108#endif /* RE_ENABLE_I18N */
15a7d175
UD
4109 {
4110 if (pstr->trans != NULL)
4111 re_string_translate_buffer (pstr);
15a7d175 4112 }
612546c6
UD
4113 }
4114 return REG_NOERROR;
4115}
4116
3b0bdc72
UD
4117\f
4118/* Functions for matching context. */
4119
6291ee3c
UD
4120/* Initialize MCTX. */
4121
a9388965 4122static reg_errcode_t
b41bd5bc 4123__attribute_warn_unused_result__
eb04c213 4124match_ctx_init (re_match_context_t *mctx, int eflags, Idx n)
3b0bdc72
UD
4125{
4126 mctx->eflags = eflags;
612546c6 4127 mctx->match_last = -1;
3b0bdc72 4128 if (n > 0)
a9388965 4129 {
eb04c213
AZ
4130 /* Avoid overflow. */
4131 size_t max_object_size =
4132 MAX (sizeof (struct re_backref_cache_entry),
4133 sizeof (re_sub_match_top_t *));
f4efbdfb 4134 if (__glibc_unlikely (MIN (IDX_MAX, SIZE_MAX / max_object_size) < n))
eb04c213
AZ
4135 return REG_ESPACE;
4136
a9388965 4137 mctx->bkref_ents = re_malloc (struct re_backref_cache_entry, n);
6291ee3c 4138 mctx->sub_tops = re_malloc (re_sub_match_top_t *, n);
f4efbdfb 4139 if (__glibc_unlikely (mctx->bkref_ents == NULL || mctx->sub_tops == NULL))
15a7d175 4140 return REG_ESPACE;
a9388965 4141 }
56b168be
UD
4142 /* Already zero-ed by the caller.
4143 else
4144 mctx->bkref_ents = NULL;
4145 mctx->nbkref_ents = 0;
4146 mctx->nsub_tops = 0; */
3b0bdc72 4147 mctx->abkref_ents = n;
6291ee3c 4148 mctx->max_mb_elem_len = 1;
6291ee3c 4149 mctx->asub_tops = n;
a9388965 4150 return REG_NOERROR;
3b0bdc72
UD
4151}
4152
6291ee3c
UD
4153/* Clean the entries which depend on the current input in MCTX.
4154 This function must be invoked when the matcher changes the start index
4155 of the input, or changes the input string. */
4156
4157static void
e2f55264 4158match_ctx_clean (re_match_context_t *mctx)
6291ee3c 4159{
eb04c213 4160 Idx st_idx;
6291ee3c
UD
4161 for (st_idx = 0; st_idx < mctx->nsub_tops; ++st_idx)
4162 {
eb04c213 4163 Idx sl_idx;
6291ee3c
UD
4164 re_sub_match_top_t *top = mctx->sub_tops[st_idx];
4165 for (sl_idx = 0; sl_idx < top->nlasts; ++sl_idx)
4166 {
4167 re_sub_match_last_t *last = top->lasts[sl_idx];
4168 re_free (last->path.array);
4169 re_free (last);
4170 }
4171 re_free (top->lasts);
4172 if (top->path)
4173 {
4174 re_free (top->path->array);
4175 re_free (top->path);
4176 }
eb04c213 4177 re_free (top);
6291ee3c 4178 }
cb265fec
UD
4179
4180 mctx->nsub_tops = 0;
4181 mctx->nbkref_ents = 0;
4182}
4183
4184/* Free all the memory associated with MCTX. */
4185
4186static void
e2f55264 4187match_ctx_free (re_match_context_t *mctx)
cb265fec
UD
4188{
4189 /* First, free all the memory associated with MCTX->SUB_TOPS. */
4190 match_ctx_clean (mctx);
4191 re_free (mctx->sub_tops);
4192 re_free (mctx->bkref_ents);
6291ee3c
UD
4193}
4194
4195/* Add a new backreference entry to MCTX.
4196 Note that we assume that caller never call this function with duplicate
4197 entry, and call with STR_IDX which isn't smaller than any existing entry.
4198*/
3b0bdc72 4199
a9388965 4200static reg_errcode_t
b41bd5bc 4201__attribute_warn_unused_result__
eb04c213
AZ
4202match_ctx_add_entry (re_match_context_t *mctx, Idx node, Idx str_idx, Idx from,
4203 Idx to)
3b0bdc72
UD
4204{
4205 if (mctx->nbkref_ents >= mctx->abkref_ents)
4206 {
1b2c2628
UD
4207 struct re_backref_cache_entry* new_entry;
4208 new_entry = re_realloc (mctx->bkref_ents, struct re_backref_cache_entry,
15a7d175 4209 mctx->abkref_ents * 2);
f4efbdfb 4210 if (__glibc_unlikely (new_entry == NULL))
15a7d175
UD
4211 {
4212 re_free (mctx->bkref_ents);
4213 return REG_ESPACE;
4214 }
1b2c2628 4215 mctx->bkref_ents = new_entry;
3b0bdc72 4216 memset (mctx->bkref_ents + mctx->nbkref_ents, '\0',
15a7d175 4217 sizeof (struct re_backref_cache_entry) * mctx->abkref_ents);
3b0bdc72
UD
4218 mctx->abkref_ents *= 2;
4219 }
e40a38b3
UD
4220 if (mctx->nbkref_ents > 0
4221 && mctx->bkref_ents[mctx->nbkref_ents - 1].str_idx == str_idx)
4222 mctx->bkref_ents[mctx->nbkref_ents - 1].more = 1;
4223
3b0bdc72 4224 mctx->bkref_ents[mctx->nbkref_ents].node = node;
0742e48e
UD
4225 mctx->bkref_ents[mctx->nbkref_ents].str_idx = str_idx;
4226 mctx->bkref_ents[mctx->nbkref_ents].subexp_from = from;
e40a38b3 4227 mctx->bkref_ents[mctx->nbkref_ents].subexp_to = to;
7db61208
UD
4228
4229 /* This is a cache that saves negative results of check_dst_limits_calc_pos.
4230 If bit N is clear, means that this entry won't epsilon-transition to
4231 an OP_OPEN_SUBEXP or OP_CLOSE_SUBEXP for the N+1-th subexpression. If
4232 it is set, check_dst_limits_calc_pos_1 will recurse and try to find one
4233 such node.
4234
4235 A backreference does not epsilon-transition unless it is empty, so set
4236 to all zeros if FROM != TO. */
4237 mctx->bkref_ents[mctx->nbkref_ents].eps_reachable_subexps_map
eb04c213 4238 = (from == to ? -1 : 0);
7db61208 4239
e40a38b3 4240 mctx->bkref_ents[mctx->nbkref_ents++].more = 0;
0742e48e
UD
4241 if (mctx->max_mb_elem_len < to - from)
4242 mctx->max_mb_elem_len = to - from;
a9388965 4243 return REG_NOERROR;
3b0bdc72 4244}
0742e48e 4245
eb04c213 4246/* Return the first entry with the same str_idx, or -1 if none is
e40a38b3 4247 found. Note that MCTX->BKREF_ENTS is already sorted by MCTX->STR_IDX. */
6291ee3c 4248
eb04c213
AZ
4249static Idx
4250search_cur_bkref_entry (const re_match_context_t *mctx, Idx str_idx)
6291ee3c 4251{
eb04c213 4252 Idx left, right, mid, last;
e40a38b3 4253 last = right = mctx->nbkref_ents;
6291ee3c
UD
4254 for (left = 0; left < right;)
4255 {
4256 mid = (left + right) / 2;
4257 if (mctx->bkref_ents[mid].str_idx < str_idx)
4258 left = mid + 1;
4259 else
4260 right = mid;
4261 }
e40a38b3
UD
4262 if (left < last && mctx->bkref_ents[left].str_idx == str_idx)
4263 return left;
4264 else
4265 return -1;
6291ee3c
UD
4266}
4267
6291ee3c
UD
4268/* Register the node NODE, whose type is OP_OPEN_SUBEXP, and which matches
4269 at STR_IDX. */
4270
4271static reg_errcode_t
b41bd5bc 4272__attribute_warn_unused_result__
eb04c213 4273match_ctx_add_subtop (re_match_context_t *mctx, Idx node, Idx str_idx)
6291ee3c
UD
4274{
4275#ifdef DEBUG
4276 assert (mctx->sub_tops != NULL);
4277 assert (mctx->asub_tops > 0);
4278#endif
f4efbdfb 4279 if (__glibc_unlikely (mctx->nsub_tops == mctx->asub_tops))
6291ee3c 4280 {
eb04c213 4281 Idx new_asub_tops = mctx->asub_tops * 2;
951d6408
UD
4282 re_sub_match_top_t **new_array = re_realloc (mctx->sub_tops,
4283 re_sub_match_top_t *,
4284 new_asub_tops);
f4efbdfb 4285 if (__glibc_unlikely (new_array == NULL))
6291ee3c
UD
4286 return REG_ESPACE;
4287 mctx->sub_tops = new_array;
951d6408 4288 mctx->asub_tops = new_asub_tops;
6291ee3c
UD
4289 }
4290 mctx->sub_tops[mctx->nsub_tops] = calloc (1, sizeof (re_sub_match_top_t));
f4efbdfb 4291 if (__glibc_unlikely (mctx->sub_tops[mctx->nsub_tops] == NULL))
6291ee3c
UD
4292 return REG_ESPACE;
4293 mctx->sub_tops[mctx->nsub_tops]->node = node;
4294 mctx->sub_tops[mctx->nsub_tops++]->str_idx = str_idx;
4295 return REG_NOERROR;
4296}
4297
4298/* Register the node NODE, whose type is OP_CLOSE_SUBEXP, and which matches
4299 at STR_IDX, whose corresponding OP_OPEN_SUBEXP is SUB_TOP. */
4300
4301static re_sub_match_last_t *
eb04c213 4302match_ctx_add_sublast (re_sub_match_top_t *subtop, Idx node, Idx str_idx)
6291ee3c
UD
4303{
4304 re_sub_match_last_t *new_entry;
f4efbdfb 4305 if (__glibc_unlikely (subtop->nlasts == subtop->alasts))
6291ee3c 4306 {
eb04c213 4307 Idx new_alasts = 2 * subtop->alasts + 1;
951d6408
UD
4308 re_sub_match_last_t **new_array = re_realloc (subtop->lasts,
4309 re_sub_match_last_t *,
4310 new_alasts);
f4efbdfb 4311 if (__glibc_unlikely (new_array == NULL))
6291ee3c
UD
4312 return NULL;
4313 subtop->lasts = new_array;
951d6408 4314 subtop->alasts = new_alasts;
6291ee3c
UD
4315 }
4316 new_entry = calloc (1, sizeof (re_sub_match_last_t));
f4efbdfb 4317 if (__glibc_likely (new_entry != NULL))
951d6408
UD
4318 {
4319 subtop->lasts[subtop->nlasts] = new_entry;
4320 new_entry->node = node;
4321 new_entry->str_idx = str_idx;
4322 ++subtop->nlasts;
4323 }
6291ee3c
UD
4324 return new_entry;
4325}
4326
0742e48e 4327static void
e2f55264 4328sift_ctx_init (re_sift_context_t *sctx, re_dfastate_t **sifted_sts,
eb04c213 4329 re_dfastate_t **limited_sts, Idx last_node, Idx last_str_idx)
0742e48e
UD
4330{
4331 sctx->sifted_states = sifted_sts;
4332 sctx->limited_states = limited_sts;
4333 sctx->last_node = last_node;
4334 sctx->last_str_idx = last_str_idx;
0742e48e
UD
4335 re_node_set_init_empty (&sctx->limits);
4336}