aboutsummaryrefslogtreecommitdiffstats
path: root/lib/regexec.c
diff options
context:
space:
mode:
authorPaul Eggert2019-10-11 18:23:47 -0700
committerPaul Eggert2019-10-11 18:24:19 -0700
commit4b60e0722d0a79751f345bd470d07db0d635aa28 (patch)
tree9ebcaa21ec5f08c0caafc7190daf34ed047f8e1f /lib/regexec.c
parentf9d8babe6a28b19c781778c361e45b93f7a01f17 (diff)
downloademacs-4b60e0722d0a79751f345bd470d07db0d635aa28.tar.gz
emacs-4b60e0722d0a79751f345bd470d07db0d635aa28.zip
Update from Gnulib
This incorporates: 2019-10-11 Simplify and regularize regex use of ‘assert’ 2019-10-09 regex: omit debug assignment when not debugging 2019-10-09 regex: tell compiler there’s at most 256 arcs out 2019-10-09 regex: simplify by assuming C99 2019-10-09 regex: avoid copying of uninitialized storage 2019-09-29 fbufmode: Fix compilation error on glibc >= 2.28 systems 2019-09-28 Update comments that refer to POSIX 2019-09-23 Update URLs and associated text * doc/misc/texinfo.tex, lib/open.c, lib/regcomp.c: * lib/regex_internal.c, lib/regex_internal.h, lib/regexec.c: * lib/stdio-impl.h: Copy from Gnulib.
Diffstat (limited to 'lib/regexec.c')
-rw-r--r--lib/regexec.c83
1 files changed, 24 insertions, 59 deletions
diff --git a/lib/regexec.c b/lib/regexec.c
index f464869fb03..c5dc6220b2d 100644
--- a/lib/regexec.c
+++ b/lib/regexec.c
@@ -443,7 +443,7 @@ re_search_stub (struct re_pattern_buffer *bufp, const char *string, Idx length,
443 { 443 {
444 if (ret_len) 444 if (ret_len)
445 { 445 {
446 assert (pmatch[0].rm_so == start); 446 DEBUG_ASSERT (pmatch[0].rm_so == start);
447 rval = pmatch[0].rm_eo - start; 447 rval = pmatch[0].rm_eo - start;
448 } 448 }
449 else 449 else
@@ -502,9 +502,9 @@ re_copy_regs (struct re_registers *regs, regmatch_t *pmatch, Idx nregs,
502 } 502 }
503 else 503 else
504 { 504 {
505 assert (regs_allocated == REGS_FIXED); 505 DEBUG_ASSERT (regs_allocated == REGS_FIXED);
506 /* This function may not be called with REGS_FIXED and nregs too big. */ 506 /* This function may not be called with REGS_FIXED and nregs too big. */
507 assert (regs->num_regs >= nregs); 507 DEBUG_ASSERT (nregs <= regs->num_regs);
508 rval = REGS_FIXED; 508 rval = REGS_FIXED;
509 } 509 }
510 510
@@ -597,21 +597,12 @@ re_search_internal (const regex_t *preg, const char *string, Idx length,
597 Idx extra_nmatch; 597 Idx extra_nmatch;
598 bool sb; 598 bool sb;
599 int ch; 599 int ch;
600#if defined _LIBC || (defined __STDC_VERSION__ && __STDC_VERSION__ >= 199901L)
601 re_match_context_t mctx = { .dfa = dfa }; 600 re_match_context_t mctx = { .dfa = dfa };
602#else
603 re_match_context_t mctx;
604#endif
605 char *fastmap = ((preg->fastmap != NULL && preg->fastmap_accurate 601 char *fastmap = ((preg->fastmap != NULL && preg->fastmap_accurate
606 && start != last_start && !preg->can_be_null) 602 && start != last_start && !preg->can_be_null)
607 ? preg->fastmap : NULL); 603 ? preg->fastmap : NULL);
608 RE_TRANSLATE_TYPE t = preg->translate; 604 RE_TRANSLATE_TYPE t = preg->translate;
609 605
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
615 extra_nmatch = (nmatch > preg->re_nsub) ? nmatch - (preg->re_nsub + 1) : 0; 606 extra_nmatch = (nmatch > preg->re_nsub) ? nmatch - (preg->re_nsub + 1) : 0;
616 nmatch -= extra_nmatch; 607 nmatch -= extra_nmatch;
617 608
@@ -622,10 +613,8 @@ re_search_internal (const regex_t *preg, const char *string, Idx length,
622 || dfa->init_state_begbuf == NULL)) 613 || dfa->init_state_begbuf == NULL))
623 return REG_NOMATCH; 614 return REG_NOMATCH;
624 615
625#ifdef DEBUG
626 /* We assume front-end functions already check them. */ 616 /* We assume front-end functions already check them. */
627 assert (0 <= last_start && last_start <= length); 617 DEBUG_ASSERT (0 <= last_start && last_start <= length);
628#endif
629 618
630 /* If initial states with non-begbuf contexts have no elements, 619 /* If initial states with non-begbuf contexts have no elements,
631 the regex must be anchored. If preg->newline_anchor is set, 620 the regex must be anchored. If preg->newline_anchor is set,
@@ -677,8 +666,6 @@ re_search_internal (const regex_t *preg, const char *string, Idx length,
677 goto free_return; 666 goto free_return;
678 } 667 }
679 } 668 }
680 else
681 mctx.state_log = NULL;
682 669
683 match_first = start; 670 match_first = start;
684 mctx.input.tip_context = (eflags & REG_NOTBOL) ? CONTEXT_BEGBUF 671 mctx.input.tip_context = (eflags & REG_NOTBOL) ? CONTEXT_BEGBUF
@@ -838,10 +825,8 @@ re_search_internal (const regex_t *preg, const char *string, Idx length,
838 match_ctx_clean (&mctx); 825 match_ctx_clean (&mctx);
839 } 826 }
840 827
841#ifdef DEBUG 828 DEBUG_ASSERT (match_last != -1);
842 assert (match_last != -1); 829 DEBUG_ASSERT (err == REG_NOERROR);
843 assert (err == REG_NOERROR);
844#endif
845 830
846 /* Set pmatch[] if we need. */ 831 /* Set pmatch[] if we need. */
847 if (nmatch > 0) 832 if (nmatch > 0)
@@ -886,7 +871,7 @@ re_search_internal (const regex_t *preg, const char *string, Idx length,
886 : mctx.input.offsets[pmatch[reg_idx].rm_eo]); 871 : mctx.input.offsets[pmatch[reg_idx].rm_eo]);
887 } 872 }
888#else 873#else
889 assert (mctx.input.offsets_needed == 0); 874 DEBUG_ASSERT (mctx.input.offsets_needed == 0);
890#endif 875#endif
891 pmatch[reg_idx].rm_so += match_first; 876 pmatch[reg_idx].rm_so += match_first;
892 pmatch[reg_idx].rm_eo += match_first; 877 pmatch[reg_idx].rm_eo += match_first;
@@ -926,9 +911,7 @@ prune_impossible_nodes (re_match_context_t *mctx)
926 re_dfastate_t **sifted_states; 911 re_dfastate_t **sifted_states;
927 re_dfastate_t **lim_states = NULL; 912 re_dfastate_t **lim_states = NULL;
928 re_sift_context_t sctx; 913 re_sift_context_t sctx;
929#ifdef DEBUG 914 DEBUG_ASSERT (mctx->state_log != NULL);
930 assert (mctx->state_log != NULL);
931#endif
932 match_last = mctx->match_last; 915 match_last = mctx->match_last;
933 halt_node = mctx->last_node; 916 halt_node = mctx->last_node;
934 917
@@ -1074,7 +1057,7 @@ check_matching (re_match_context_t *mctx, bool fl_longest_match,
1074 /* An initial state must not be NULL (invalid). */ 1057 /* An initial state must not be NULL (invalid). */
1075 if (__glibc_unlikely (cur_state == NULL)) 1058 if (__glibc_unlikely (cur_state == NULL))
1076 { 1059 {
1077 assert (err == REG_ESPACE); 1060 DEBUG_ASSERT (err == REG_ESPACE);
1078 return -2; 1061 return -2;
1079 } 1062 }
1080 1063
@@ -1129,7 +1112,7 @@ check_matching (re_match_context_t *mctx, bool fl_longest_match,
1129 err = extend_buffers (mctx, next_char_idx + 1); 1112 err = extend_buffers (mctx, next_char_idx + 1);
1130 if (__glibc_unlikely (err != REG_NOERROR)) 1113 if (__glibc_unlikely (err != REG_NOERROR))
1131 { 1114 {
1132 assert (err == REG_ESPACE); 1115 DEBUG_ASSERT (err == REG_ESPACE);
1133 return -2; 1116 return -2;
1134 } 1117 }
1135 } 1118 }
@@ -1212,9 +1195,7 @@ check_halt_state_context (const re_match_context_t *mctx,
1212{ 1195{
1213 Idx i; 1196 Idx i;
1214 unsigned int context; 1197 unsigned int context;
1215#ifdef DEBUG 1198 DEBUG_ASSERT (state->halt);
1216 assert (state->halt);
1217#endif
1218 context = re_string_context_at (&mctx->input, idx, mctx->eflags); 1199 context = re_string_context_at (&mctx->input, idx, mctx->eflags);
1219 for (i = 0; i < state->nodes.nelem; ++i) 1200 for (i = 0; i < state->nodes.nelem; ++i)
1220 if (check_halt_node_context (mctx->dfa, state->nodes.elems[i], context)) 1201 if (check_halt_node_context (mctx->dfa, state->nodes.elems[i], context))
@@ -1362,7 +1343,7 @@ pop_fail_stack (struct re_fail_stack_t *fs, Idx *pidx, Idx nregs,
1362 regmatch_t *regs, re_node_set *eps_via_nodes) 1343 regmatch_t *regs, re_node_set *eps_via_nodes)
1363{ 1344{
1364 Idx num = --fs->num; 1345 Idx num = --fs->num;
1365 assert (num >= 0); 1346 DEBUG_ASSERT (num >= 0);
1366 *pidx = fs->stack[num].idx; 1347 *pidx = fs->stack[num].idx;
1367 memcpy (regs, fs->stack[num].regs, sizeof (regmatch_t) * nregs); 1348 memcpy (regs, fs->stack[num].regs, sizeof (regmatch_t) * nregs);
1368 re_node_set_free (eps_via_nodes); 1349 re_node_set_free (eps_via_nodes);
@@ -1389,10 +1370,8 @@ set_regs (const regex_t *preg, const re_match_context_t *mctx, size_t nmatch,
1389 regmatch_t *prev_idx_match; 1370 regmatch_t *prev_idx_match;
1390 bool prev_idx_match_malloced = false; 1371 bool prev_idx_match_malloced = false;
1391 1372
1392#ifdef DEBUG 1373 DEBUG_ASSERT (nmatch > 1);
1393 assert (nmatch > 1); 1374 DEBUG_ASSERT (mctx->state_log != NULL);
1394 assert (mctx->state_log != NULL);
1395#endif
1396 if (fl_backtrack) 1375 if (fl_backtrack)
1397 { 1376 {
1398 fs = &fs_body; 1377 fs = &fs_body;
@@ -1578,9 +1557,7 @@ sift_states_backward (const re_match_context_t *mctx, re_sift_context_t *sctx)
1578 Idx str_idx = sctx->last_str_idx; 1557 Idx str_idx = sctx->last_str_idx;
1579 re_node_set cur_dest; 1558 re_node_set cur_dest;
1580 1559
1581#ifdef DEBUG 1560 DEBUG_ASSERT (mctx->state_log != NULL && mctx->state_log[str_idx] != NULL);
1582 assert (mctx->state_log != NULL && mctx->state_log[str_idx] != NULL);
1583#endif
1584 1561
1585 /* Build sifted state_log[str_idx]. It has the nodes which can epsilon 1562 /* Build sifted state_log[str_idx]. It has the nodes which can epsilon
1586 transit to the last_node and the last_node itself. */ 1563 transit to the last_node and the last_node itself. */
@@ -1648,11 +1625,8 @@ build_sifted_states (const re_match_context_t *mctx, re_sift_context_t *sctx,
1648 Idx prev_node = cur_src->elems[i]; 1625 Idx prev_node = cur_src->elems[i];
1649 int naccepted = 0; 1626 int naccepted = 0;
1650 bool ok; 1627 bool ok;
1628 DEBUG_ASSERT (!IS_EPSILON_NODE (dfa->nodes[prev_node].type));
1651 1629
1652#ifdef DEBUG
1653 re_token_type_t type = dfa->nodes[prev_node].type;
1654 assert (!IS_EPSILON_NODE (type));
1655#endif
1656#ifdef RE_ENABLE_I18N 1630#ifdef RE_ENABLE_I18N
1657 /* If the node may accept "multi byte". */ 1631 /* If the node may accept "multi byte". */
1658 if (dfa->nodes[prev_node].accept_mb) 1632 if (dfa->nodes[prev_node].accept_mb)
@@ -2505,9 +2479,7 @@ transit_state_mb (re_match_context_t *mctx, re_dfastate_t *pstate)
2505 err = clean_state_log_if_needed (mctx, dest_idx); 2479 err = clean_state_log_if_needed (mctx, dest_idx);
2506 if (__glibc_unlikely (err != REG_NOERROR)) 2480 if (__glibc_unlikely (err != REG_NOERROR))
2507 return err; 2481 return err;
2508#ifdef DEBUG 2482 DEBUG_ASSERT (dfa->nexts[cur_node_idx] != -1);
2509 assert (dfa->nexts[cur_node_idx] != -1);
2510#endif
2511 new_nodes = dfa->eclosures + dfa->nexts[cur_node_idx]; 2483 new_nodes = dfa->eclosures + dfa->nexts[cur_node_idx];
2512 2484
2513 dest_state = mctx->state_log[dest_idx]; 2485 dest_state = mctx->state_log[dest_idx];
@@ -2571,9 +2543,7 @@ transit_state_bkref (re_match_context_t *mctx, const re_node_set *nodes)
2571 2543
2572 /* And add the epsilon closures (which is 'new_dest_nodes') of 2544 /* And add the epsilon closures (which is 'new_dest_nodes') of
2573 the backreference to appropriate state_log. */ 2545 the backreference to appropriate state_log. */
2574#ifdef DEBUG 2546 DEBUG_ASSERT (dfa->nexts[node_idx] != -1);
2575 assert (dfa->nexts[node_idx] != -1);
2576#endif
2577 for (; bkc_idx < mctx->nbkref_ents; ++bkc_idx) 2547 for (; bkc_idx < mctx->nbkref_ents; ++bkc_idx)
2578 { 2548 {
2579 Idx subexp_len; 2549 Idx subexp_len;
@@ -3032,10 +3002,8 @@ check_arrival_add_next_nodes (re_match_context_t *mctx, Idx str_idx,
3032 { 3002 {
3033 int naccepted = 0; 3003 int naccepted = 0;
3034 Idx cur_node = cur_nodes->elems[cur_idx]; 3004 Idx cur_node = cur_nodes->elems[cur_idx];
3035#ifdef DEBUG 3005 DEBUG_ASSERT (!IS_EPSILON_NODE (dfa->nodes[cur_node].type));
3036 re_token_type_t type = dfa->nodes[cur_node].type; 3006
3037 assert (!IS_EPSILON_NODE (type));
3038#endif
3039#ifdef RE_ENABLE_I18N 3007#ifdef RE_ENABLE_I18N
3040 /* If the node may accept "multi byte". */ 3008 /* If the node may accept "multi byte". */
3041 if (dfa->nodes[cur_node].accept_mb) 3009 if (dfa->nodes[cur_node].accept_mb)
@@ -3103,9 +3071,7 @@ check_arrival_expand_ecl (const re_dfa_t *dfa, re_node_set *cur_nodes,
3103 reg_errcode_t err; 3071 reg_errcode_t err;
3104 Idx idx, outside_node; 3072 Idx idx, outside_node;
3105 re_node_set new_nodes; 3073 re_node_set new_nodes;
3106#ifdef DEBUG 3074 DEBUG_ASSERT (cur_nodes->nelem);
3107 assert (cur_nodes->nelem);
3108#endif
3109 err = re_node_set_alloc (&new_nodes, cur_nodes->nelem); 3075 err = re_node_set_alloc (&new_nodes, cur_nodes->nelem);
3110 if (__glibc_unlikely (err != REG_NOERROR)) 3076 if (__glibc_unlikely (err != REG_NOERROR))
3111 return err; 3077 return err;
@@ -3695,6 +3661,7 @@ group_nodes_into_DFAstates (const re_dfa_t *dfa, const re_dfastate_t *state,
3695 bitset_empty (accepts); 3661 bitset_empty (accepts);
3696 } 3662 }
3697 } 3663 }
3664 assume (ndests <= SBC_MAX);
3698 return ndests; 3665 return ndests;
3699 error_return: 3666 error_return:
3700 for (j = 0; j < ndests; ++j) 3667 for (j = 0; j < ndests; ++j)
@@ -4272,10 +4239,8 @@ static reg_errcode_t
4272__attribute_warn_unused_result__ 4239__attribute_warn_unused_result__
4273match_ctx_add_subtop (re_match_context_t *mctx, Idx node, Idx str_idx) 4240match_ctx_add_subtop (re_match_context_t *mctx, Idx node, Idx str_idx)
4274{ 4241{
4275#ifdef DEBUG 4242 DEBUG_ASSERT (mctx->sub_tops != NULL);
4276 assert (mctx->sub_tops != NULL); 4243 DEBUG_ASSERT (mctx->asub_tops > 0);
4277 assert (mctx->asub_tops > 0);
4278#endif
4279 if (__glibc_unlikely (mctx->nsub_tops == mctx->asub_tops)) 4244 if (__glibc_unlikely (mctx->nsub_tops == mctx->asub_tops))
4280 { 4245 {
4281 Idx new_asub_tops = mctx->asub_tops * 2; 4246 Idx new_asub_tops = mctx->asub_tops * 2;