Ignore:
Timestamp:
Sep 19, 2024, 2:34:43 AM (10 months ago)
Author:
bird
Message:

src/sed: Merged in changes between 4.1.5 and 4.9 from the vendor branch. (svn merge /vendor/sed/4.1.5 /vendor/sed/current .)

Location:
trunk/src/sed
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/sed

  • trunk/src/sed/lib/regexec.c

    r606 r3613  
    11/* Extended regular expression matching and search library.
    2    Copyright (C) 2002, 2003, 2004, 2005 Free Software Foundation, Inc.
     2   Copyright (C) 2002-2022 Free Software Foundation, Inc.
    33   This file is part of the GNU C Library.
    44   Contributed by Isamu Hasegawa <isamu@yamato.ibm.com>.
     
    1515
    1616   You should have received a copy of the GNU Lesser General Public
    17    License along with the GNU C Library; if not, write to the Free
    18    Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
    19    02111-1307 USA.  */
     17   License along with the GNU C Library; if not, see
     18   <https://www.gnu.org/licenses/>.  */
    2019
    2120static reg_errcode_t match_ctx_init (re_match_context_t *cache, int eflags,
    22                                      int n) internal_function;
    23 static void match_ctx_clean (re_match_context_t *mctx) internal_function;
    24 static void match_ctx_free (re_match_context_t *cache) internal_function;
    25 static reg_errcode_t match_ctx_add_entry (re_match_context_t *cache, int node,
    26                                           int str_idx, int from, int to)
    27      internal_function;
    28 static int search_cur_bkref_entry (const re_match_context_t *mctx, int str_idx)
    29      internal_function;
    30 static reg_errcode_t match_ctx_add_subtop (re_match_context_t *mctx, int node,
    31                                            int str_idx) internal_function;
     21                                     Idx n);
     22static void match_ctx_clean (re_match_context_t *mctx);
     23static void match_ctx_free (re_match_context_t *cache);
     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);
    3229static re_sub_match_last_t * match_ctx_add_sublast (re_sub_match_top_t *subtop,
    33                                                    int node, int str_idx)
    34      internal_function;
     30                                                    Idx node, Idx str_idx);
    3531static void sift_ctx_init (re_sift_context_t *sctx, re_dfastate_t **sifted_sts,
    36                            re_dfastate_t **limited_sts, int last_node,
    37                            int last_str_idx)
    38      internal_function;
     32                           re_dfastate_t **limited_sts, Idx last_node,
     33                           Idx last_str_idx);
    3934static reg_errcode_t re_search_internal (const regex_t *preg,
    40                                          const char *string, int length,
    41                                          int start, int range, int stop,
     35                                         const char *string, Idx length,
     36                                         Idx start, Idx last_start, Idx stop,
    4237                                         size_t nmatch, regmatch_t pmatch[],
    43                                          int eflags) internal_function;
    44 static int re_search_2_stub (struct re_pattern_buffer *bufp,
    45                              const char *string1, int length1,
    46                              const char *string2, int length2,
    47                              int start, int range, struct re_registers *regs,
    48                              int stop, int ret_len) internal_function;
    49 static int re_search_stub (struct re_pattern_buffer *bufp,
    50                            const char *string, int length, int start,
    51                            int range, int stop, struct re_registers *regs,
    52                            int ret_len) internal_function;
     38                                         int eflags);
     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);
    5350static unsigned re_copy_regs (struct re_registers *regs, regmatch_t *pmatch,
    54                               int nregs, int regs_allocated) internal_function;
    55 static reg_errcode_t prune_impossible_nodes (re_match_context_t *mctx)
    56      internal_function;
    57 static int check_matching (re_match_context_t *mctx, int fl_longest_match,
    58                            int *p_match_first) internal_function;
    59 static int check_halt_state_context (const re_match_context_t *mctx,
    60                                      const re_dfastate_t *state, int idx)
    61      internal_function;
     51                              Idx nregs, int regs_allocated);
     52static reg_errcode_t prune_impossible_nodes (re_match_context_t *mctx);
     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);
    6257static void update_regs (const re_dfa_t *dfa, regmatch_t *pmatch,
    63                          regmatch_t *prev_idx_match, int cur_node,
    64                          int cur_idx, int nmatch) internal_function;
     58                         regmatch_t *prev_idx_match, Idx cur_node,
     59                         Idx cur_idx, Idx nmatch);
    6560static reg_errcode_t push_fail_stack (struct re_fail_stack_t *fs,
    66                                       int str_idx, int dest_node, int nregs,
    67                                       regmatch_t *regs,
    68                                       re_node_set *eps_via_nodes)
    69      internal_function;
     61                                      Idx str_idx, Idx dest_node, Idx nregs,
     62                                      regmatch_t *regs, regmatch_t *prevregs,
     63                                      re_node_set *eps_via_nodes);
    7064static reg_errcode_t set_regs (const regex_t *preg,
    7165                               const re_match_context_t *mctx,
    7266                               size_t nmatch, regmatch_t *pmatch,
    73                                int fl_backtrack) internal_function;
    74 static reg_errcode_t free_fail_stack_return (struct re_fail_stack_t *fs)
    75      internal_function;
    76 
    77 #ifdef RE_ENABLE_I18N
     67                               bool fl_backtrack);
     68static reg_errcode_t free_fail_stack_return (struct re_fail_stack_t *fs);
     69
    7870static int sift_states_iter_mb (const re_match_context_t *mctx,
    7971                                re_sift_context_t *sctx,
    80                                 int node_idx, int str_idx, int max_str_idx)
    81      internal_function;
    82 #endif /* RE_ENABLE_I18N */
     72                                Idx node_idx, Idx str_idx, Idx max_str_idx);
    8373static reg_errcode_t sift_states_backward (const re_match_context_t *mctx,
    84                                            re_sift_context_t *sctx)
    85      internal_function;
     74                                           re_sift_context_t *sctx);
    8675static reg_errcode_t build_sifted_states (const re_match_context_t *mctx,
    87                                           re_sift_context_t *sctx, int str_idx,
    88                                           re_node_set *cur_dest)
    89      internal_function;
     76                                          re_sift_context_t *sctx, Idx str_idx,
     77                                          re_node_set *cur_dest);
    9078static reg_errcode_t update_cur_sifted_state (const re_match_context_t *mctx,
    9179                                              re_sift_context_t *sctx,
    92                                               int str_idx,
    93                                               re_node_set *dest_nodes)
    94      internal_function;
     80                                              Idx str_idx,
     81                                              re_node_set *dest_nodes);
    9582static reg_errcode_t add_epsilon_src_nodes (const re_dfa_t *dfa,
    9683                                            re_node_set *dest_nodes,
    97                                             const re_node_set *candidates)
    98      internal_function;
    99 static int check_dst_limits (const re_match_context_t *mctx,
    100                              re_node_set *limits,
    101                              int dst_node, int dst_idx, int src_node,
    102                              int src_idx) internal_function;
     84                                            const re_node_set *candidates);
     85static bool check_dst_limits (const re_match_context_t *mctx,
     86                              const re_node_set *limits,
     87                              Idx dst_node, Idx dst_idx, Idx src_node,
     88                              Idx src_idx);
    10389static int check_dst_limits_calc_pos_1 (const re_match_context_t *mctx,
    104                                         int boundaries, int subexp_idx,
    105                                         int from_node, int bkref_idx)
    106      internal_function;
     90                                        int boundaries, Idx subexp_idx,
     91                                        Idx from_node, Idx bkref_idx);
    10792static int check_dst_limits_calc_pos (const re_match_context_t *mctx,
    108                                       int limit, int subexp_idx,
    109                                       int node, int str_idx,
    110                                       int bkref_idx) internal_function;
     93                                      Idx limit, Idx subexp_idx,
     94                                      Idx node, Idx str_idx,
     95                                      Idx bkref_idx);
    11196static reg_errcode_t check_subexp_limits (const re_dfa_t *dfa,
    11297                                          re_node_set *dest_nodes,
     
    11499                                          re_node_set *limits,
    115100                                          struct re_backref_cache_entry *bkref_ents,
    116                                           int str_idx) internal_function;
     101                                          Idx str_idx);
    117102static reg_errcode_t sift_states_bkref (const re_match_context_t *mctx,
    118103                                        re_sift_context_t *sctx,
    119                                         int str_idx, const re_node_set *candidates)
    120      internal_function;
     104                                        Idx str_idx, const re_node_set *candidates);
    121105static reg_errcode_t merge_state_array (const re_dfa_t *dfa,
    122106                                        re_dfastate_t **dst,
    123                                         re_dfastate_t **src, int num)
    124      internal_function;
     107                                        re_dfastate_t **src, Idx num);
    125108static re_dfastate_t *find_recover_state (reg_errcode_t *err,
    126                                          re_match_context_t *mctx) internal_function;
     109                                         re_match_context_t *mctx);
    127110static re_dfastate_t *transit_state (reg_errcode_t *err,
    128111                                     re_match_context_t *mctx,
    129                                      re_dfastate_t *state) internal_function;
     112                                     re_dfastate_t *state);
    130113static re_dfastate_t *merge_state_with_log (reg_errcode_t *err,
    131114                                            re_match_context_t *mctx,
    132                                             re_dfastate_t *next_state)
    133      internal_function;
     115                                            re_dfastate_t *next_state);
    134116static reg_errcode_t check_subexp_matching_top (re_match_context_t *mctx,
    135117                                                re_node_set *cur_nodes,
    136                                                 int str_idx) internal_function;
     118                                                Idx str_idx);
    137119#if 0
    138120static re_dfastate_t *transit_state_sb (reg_errcode_t *err,
    139121                                        re_match_context_t *mctx,
    140                                         re_dfastate_t *pstate)
    141      internal_function;
     122                                        re_dfastate_t *pstate);
    142123#endif
    143 #ifdef RE_ENABLE_I18N
    144124static reg_errcode_t transit_state_mb (re_match_context_t *mctx,
    145                                        re_dfastate_t *pstate)
    146      internal_function;
    147 #endif /* RE_ENABLE_I18N */
     125                                       re_dfastate_t *pstate);
    148126static reg_errcode_t transit_state_bkref (re_match_context_t *mctx,
    149                                           const re_node_set *nodes)
    150      internal_function;
     127                                          const re_node_set *nodes);
    151128static reg_errcode_t get_subexp (re_match_context_t *mctx,
    152                                  int bkref_node, int bkref_str_idx)
    153      internal_function;
     129                                 Idx bkref_node, Idx bkref_str_idx);
    154130static reg_errcode_t get_subexp_sub (re_match_context_t *mctx,
    155131                                     const re_sub_match_top_t *sub_top,
    156132                                     re_sub_match_last_t *sub_last,
    157                                      int bkref_node, int bkref_str)
    158      internal_function;
    159 static int find_subexp_node (const re_dfa_t *dfa, const re_node_set *nodes,
    160                              int subexp_idx, int type) internal_function;
     133                                     Idx bkref_node, Idx bkref_str);
     134static Idx find_subexp_node (const re_dfa_t *dfa, const re_node_set *nodes,
     135                             Idx subexp_idx, int type);
    161136static reg_errcode_t check_arrival (re_match_context_t *mctx,
    162                                     state_array_t *path, int top_node,
    163                                     int top_str, int last_node, int last_str,
    164                                     int type) internal_function;
     137                                    state_array_t *path, Idx top_node,
     138                                    Idx top_str, Idx last_node, Idx last_str,
     139                                    int type);
    165140static reg_errcode_t check_arrival_add_next_nodes (re_match_context_t *mctx,
    166                                                    int str_idx,
     141                                                   Idx str_idx,
    167142                                                   re_node_set *cur_nodes,
    168                                                    re_node_set *next_nodes)
    169      internal_function;
     143                                                   re_node_set *next_nodes);
    170144static reg_errcode_t check_arrival_expand_ecl (const re_dfa_t *dfa,
    171145                                               re_node_set *cur_nodes,
    172                                                int ex_subexp, int type)
    173      internal_function;
     146                                               Idx ex_subexp, int type);
    174147static reg_errcode_t check_arrival_expand_ecl_sub (const re_dfa_t *dfa,
    175148                                                   re_node_set *dst_nodes,
    176                                                    int target, int ex_subexp,
    177                                                    int type) internal_function;
     149                                                   Idx target, Idx ex_subexp,
     150                                                   int type);
    178151static reg_errcode_t expand_bkref_cache (re_match_context_t *mctx,
    179                                          re_node_set *cur_nodes, int cur_str,
    180                                          int subexp_num, int type)
    181      internal_function;
    182 static int build_trtable (const re_dfa_t *dfa,
    183                           re_dfastate_t *state) internal_function;
    184 #ifdef RE_ENABLE_I18N
    185 static int check_node_accept_bytes (const re_dfa_t *dfa, int node_idx,
    186                                     const re_string_t *input, int idx)
    187      internal_function;
    188 # ifdef _LIBC
     152                                         re_node_set *cur_nodes, Idx cur_str,
     153                                         Idx subexp_num, int type);
     154static bool build_trtable (const re_dfa_t *dfa, re_dfastate_t *state);
     155static int check_node_accept_bytes (const re_dfa_t *dfa, Idx node_idx,
     156                                    const re_string_t *input, Idx idx);
     157#ifdef _LIBC
    189158static unsigned int find_collation_sequence_value (const unsigned char *mbs,
    190                                                    size_t name_len)
    191      internal_function;
    192 # endif /* _LIBC */
    193 #endif /* RE_ENABLE_I18N */
    194 static int group_nodes_into_DFAstates (const re_dfa_t *dfa,
     159                                                   size_t name_len);
     160#endif
     161static Idx group_nodes_into_DFAstates (const re_dfa_t *dfa,
    195162                                       const re_dfastate_t *state,
    196163                                       re_node_set *states_node,
    197                                        bitset_t *states_ch) internal_function;
    198 static int check_node_accept (const re_match_context_t *mctx,
    199                               const re_token_t *node, int idx)
    200      internal_function;
    201 static reg_errcode_t extend_buffers (re_match_context_t *mctx)
    202      internal_function;
     164                                       bitset_t *states_ch);
     165static bool check_node_accept (const re_match_context_t *mctx,
     166                               const re_token_t *node, Idx idx);
     167static reg_errcode_t extend_buffers (re_match_context_t *mctx, int min_len);
    203168
    204169
     
    209174
    210175   If NMATCH is zero or REG_NOSUB was set in the cflags argument to
    211    `regcomp', we ignore PMATCH.  Otherwise, we assume PMATCH has at
     176   'regcomp', we ignore PMATCH.  Otherwise, we assume PMATCH has at
    212177   least NMATCH elements, and we set them to the offsets of the
    213178   corresponding matched substrings.
    214179
    215    EFLAGS specifies `execution flags' which affect matching: if
     180   EFLAGS specifies "execution flags" which affect matching: if
    216181   REG_NOTBOL is set, then ^ does not match at the beginning of the
    217182   string; if REG_NOTEOL is set, then $ does not match at the end.
    218183
    219    We return 0 if we find a match and REG_NOMATCH if not.  */
     184   Return 0 if a match is found, REG_NOMATCH if not, REG_BADPAT if
     185   EFLAGS is invalid.  */
    220186
    221187int
    222 regexec (preg, string, nmatch, pmatch, eflags)
    223     const regex_t *__restrict preg;
    224     const char *__restrict string;
    225     size_t nmatch;
    226     regmatch_t pmatch[];
    227     int eflags;
     188regexec (const regex_t *__restrict preg, const char *__restrict string,
     189         size_t nmatch, regmatch_t pmatch[_REGEX_NELTS (nmatch)], int eflags)
    228190{
    229191  reg_errcode_t err;
    230   int start, length;
    231   re_dfa_t *dfa = (re_dfa_t *) preg->buffer;
     192  Idx start, length;
     193  re_dfa_t *dfa = preg->buffer;
    232194
    233195  if (eflags & ~(REG_NOTBOL | REG_NOTEOL | REG_STARTEND))
     
    245207    }
    246208
    247   __libc_lock_lock (dfa->lock);
     209  lock_lock (dfa->lock);
    248210  if (preg->no_sub)
    249     err = re_search_internal (preg, string, length, start, length - start,
     211    err = re_search_internal (preg, string, length, start, length,
    250212                              length, 0, NULL, eflags);
    251213  else
    252     err = re_search_internal (preg, string, length, start, length - start,
     214    err = re_search_internal (preg, string, length, start, length,
    253215                              length, nmatch, pmatch, eflags);
    254   __libc_lock_unlock (dfa->lock);
     216  lock_unlock (dfa->lock);
    255217  return err != REG_NOERROR;
    256218}
    257219
    258220#ifdef _LIBC
     221libc_hidden_def (__regexec)
     222
    259223# include <shlib-compat.h>
    260224versioned_symbol (libc, __regexec, regexec, GLIBC_2_3_4);
     
    267231__compat_regexec (const regex_t *__restrict preg,
    268232                  const char *__restrict string, size_t nmatch,
    269                   regmatch_t pmatch[], int eflags)
     233                  regmatch_t pmatch[_REGEX_NELTS (nmatch)], int eflags)
    270234{
    271235  return regexec (preg, string, nmatch, pmatch,
     
    297261
    298262   If REGS is not NULL, and BUFP->no_sub is not set, the offsets of the match
    299    and all groups is stroed in REGS.  (For the "_2" variants, the offsets are
     263   and all groups is stored in REGS.  (For the "_2" variants, the offsets are
    300264   computed relative to the concatenation, not relative to the individual
    301265   strings.)
    302266
    303267   On success, re_match* functions return the length of the match, re_search*
    304    return the position of the start of the match.  Return value -1 means no
    305    match was found and -2 indicates an internal error.  */
    306 
    307 int
    308 re_match (bufp, string, length, start, regs)
    309     struct re_pattern_buffer *bufp;
    310     const char *string;
    311     int length, start;
    312     struct re_registers *regs;
    313 {
    314   return re_search_stub (bufp, string, length, start, 0, length, regs, 1);
     268   return the position of the start of the match.  They return -1 on
     269   match failure, -2 on error.  */
     270
     271regoff_t
     272re_match (struct re_pattern_buffer *bufp, const char *string, Idx length,
     273          Idx start, struct re_registers *regs)
     274{
     275  return re_search_stub (bufp, string, length, start, 0, length, regs, true);
    315276}
    316277#ifdef _LIBC
     
    318279#endif
    319280
    320 int
    321 re_search (bufp, string, length, start, range, regs)
    322     struct re_pattern_buffer *bufp;
    323     const char *string;
    324     int length, start, range;
    325     struct re_registers *regs;
    326 {
    327   return re_search_stub (bufp, string, length, start, range, length, regs, 0);
     281regoff_t
     282re_search (struct re_pattern_buffer *bufp, const char *string, Idx length,
     283           Idx start, regoff_t range, struct re_registers *regs)
     284{
     285  return re_search_stub (bufp, string, length, start, range, length, regs,
     286                         false);
    328287}
    329288#ifdef _LIBC
     
    331290#endif
    332291
    333 int
    334 re_match_2 (bufp, string1, length1, string2, length2, start, regs, stop)
    335     struct re_pattern_buffer *bufp;
    336     const char *string1, *string2;
    337     int length1, length2, start, stop;
    338     struct re_registers *regs;
     292regoff_t
     293re_match_2 (struct re_pattern_buffer *bufp, const char *string1, Idx length1,
     294            const char *string2, Idx length2, Idx start,
     295            struct re_registers *regs, Idx stop)
    339296{
    340297  return re_search_2_stub (bufp, string1, length1, string2, length2,
    341                            start, 0, regs, stop, 1);
     298                           start, 0, regs, stop, true);
    342299}
    343300#ifdef _LIBC
     
    345302#endif
    346303
    347 int
    348 re_search_2 (bufp, string1, length1, string2, length2, start, range, regs, stop)
    349     struct re_pattern_buffer *bufp;
    350     const char *string1, *string2;
    351     int length1, length2, start, range, stop;
    352     struct re_registers *regs;
     304regoff_t
     305re_search_2 (struct re_pattern_buffer *bufp, const char *string1, Idx length1,
     306             const char *string2, Idx length2, Idx start, regoff_t range,
     307             struct re_registers *regs, Idx stop)
    353308{
    354309  return re_search_2_stub (bufp, string1, length1, string2, length2,
    355                            start, range, regs, stop, 0);
     310                           start, range, regs, stop, false);
    356311}
    357312#ifdef _LIBC
     
    359314#endif
    360315
    361 static int
    362 re_search_2_stub (bufp, string1, length1, string2, length2, start, range, regs,
    363                   stop, ret_len)
    364     struct re_pattern_buffer *bufp;
    365     const char *string1, *string2;
    366     int length1, length2, start, range, stop, ret_len;
    367     struct re_registers *regs;
     316static regoff_t
     317re_search_2_stub (struct re_pattern_buffer *bufp, const char *string1,
     318                  Idx length1, const char *string2, Idx length2, Idx start,
     319                  regoff_t range, struct re_registers *regs,
     320                  Idx stop, bool ret_len)
    368321{
    369322  const char *str;
    370   int rval;
    371   int len = length1 + length2;
    372   int free_str = 0;
    373 
    374   if (BE (length1 < 0 || length2 < 0 || stop < 0, 0))
     323  regoff_t rval;
     324  Idx len;
     325  char *s = NULL;
     326
     327  if (__glibc_unlikely ((length1 < 0 || length2 < 0 || stop < 0
     328                         || INT_ADD_WRAPV (length1, length2, &len))))
    375329    return -2;
    376330
     
    379333    if (length1 > 0)
    380334      {
    381         char *s = re_malloc (char, len);
    382 
    383         if (BE (s == NULL, 0))
     335        s = re_malloc (char, len);
     336
     337        if (__glibc_unlikely (s == NULL))
    384338          return -2;
    385339#ifdef _LIBC
     
    390344#endif
    391345        str = s;
    392         free_str = 1;
    393346      }
    394347    else
     
    399352  rval = re_search_stub (bufp, str, len, start, range, stop, regs,
    400353                         ret_len);
    401   if (free_str)
    402     re_free ((char *) str);
     354  re_free (s);
    403355  return rval;
    404356}
     
    406358/* The parameters have the same meaning as those of re_search.
    407359   Additional parameters:
    408    If RET_LEN is nonzero the length of the match is returned (re_match style);
     360   If RET_LEN is true the length of the match is returned (re_match style);
    409361   otherwise the position of the match is returned.  */
    410362
    411 static int
    412 re_search_stub (bufp, string, length, start, range, stop, regs, ret_len)
    413     struct re_pattern_buffer *bufp;
    414     const char *string;
    415     int length, start, range, stop, ret_len;
    416     struct re_registers *regs;
     363static regoff_t
     364re_search_stub (struct re_pattern_buffer *bufp, const char *string, Idx length,
     365                Idx start, regoff_t range, Idx stop, struct re_registers *regs,
     366                bool ret_len)
    417367{
    418368  reg_errcode_t result;
    419369  regmatch_t *pmatch;
    420   int nregs, rval;
     370  Idx nregs;
     371  regoff_t rval;
    421372  int eflags = 0;
    422   re_dfa_t *dfa = (re_dfa_t *) bufp->buffer;
     373  re_dfa_t *dfa = bufp->buffer;
     374  Idx last_start = start + range;
    423375
    424376  /* Check for out-of-range.  */
    425   if (BE (start < 0 || start > length, 0))
     377  if (__glibc_unlikely (start < 0 || start > length))
    426378    return -1;
    427   if (BE (start + range > length, 0))
    428     range = length - start;
    429   else if (BE (start + range < 0, 0))
    430     range = -start;
    431 
    432   __libc_lock_lock (dfa->lock);
     379  if (__glibc_unlikely (length < last_start
     380                        || (0 <= range && last_start < start)))
     381    last_start = length;
     382  else if (__glibc_unlikely (last_start < 0
     383                             || (range < 0 && start <= last_start)))
     384    last_start = 0;
     385
     386  lock_lock (dfa->lock);
    433387
    434388  eflags |= (bufp->not_bol) ? REG_NOTBOL : 0;
     
    436390
    437391  /* Compile fastmap if we haven't yet.  */
    438   if (range > 0 && bufp->fastmap != NULL && !bufp->fastmap_accurate)
     392  if (start < last_start && bufp->fastmap != NULL && !bufp->fastmap_accurate)
    439393    re_compile_fastmap (bufp);
    440394
    441   if (BE (bufp->no_sub, 0))
     395  if (__glibc_unlikely (bufp->no_sub))
    442396    regs = NULL;
    443397
     
    445399  if (regs == NULL)
    446400    nregs = 1;
    447   else if (BE (bufp->regs_allocated == REGS_FIXED &&
    448                regs->num_regs < bufp->re_nsub + 1, 0))
     401  else if (__glibc_unlikely (bufp->regs_allocated == REGS_FIXED
     402                             && regs->num_regs <= bufp->re_nsub))
    449403    {
    450404      nregs = regs->num_regs;
    451       if (BE (nregs < 1, 0))
     405      if (__glibc_unlikely (nregs < 1))
    452406        {
    453407          /* Nothing can be copied to regs.  */
     
    459413    nregs = bufp->re_nsub + 1;
    460414  pmatch = re_malloc (regmatch_t, nregs);
    461   if (BE (pmatch == NULL, 0))
     415  if (__glibc_unlikely (pmatch == NULL))
    462416    {
    463417      rval = -2;
     
    465419    }
    466420
    467   result = re_search_internal (bufp, string, length, start, range, stop,
     421  result = re_search_internal (bufp, string, length, start, last_start, stop,
    468422                               nregs, pmatch, eflags);
    469423
    470424  rval = 0;
    471425
    472   /* I hope we needn't fill ther regs with -1's when no match was found.  */
     426  /* I hope we needn't fill their regs with -1's when no match was found.  */
    473427  if (result != REG_NOERROR)
    474     rval = -1;
     428    rval = result == REG_NOMATCH ? -1 : -2;
    475429  else if (regs != NULL)
    476430    {
     
    478432      bufp->regs_allocated = re_copy_regs (regs, pmatch, nregs,
    479433                                           bufp->regs_allocated);
    480       if (BE (bufp->regs_allocated == REGS_UNALLOCATED, 0))
     434      if (__glibc_unlikely (bufp->regs_allocated == REGS_UNALLOCATED))
    481435        rval = -2;
    482436    }
    483437
    484   if (BE (rval == 0, 1))
     438  if (__glibc_likely (rval == 0))
    485439    {
    486440      if (ret_len)
    487441        {
    488           assert (pmatch[0].rm_so == start);
     442          DEBUG_ASSERT (pmatch[0].rm_so == start);
    489443          rval = pmatch[0].rm_eo - start;
    490444        }
     
    494448  re_free (pmatch);
    495449 out:
    496   __libc_lock_unlock (dfa->lock);
     450  lock_unlock (dfa->lock);
    497451  return rval;
    498452}
    499453
    500454static unsigned
    501 re_copy_regs (regs, pmatch, nregs, regs_allocated)
    502     struct re_registers *regs;
    503     regmatch_t *pmatch;
    504     int nregs, regs_allocated;
     455re_copy_regs (struct re_registers *regs, regmatch_t *pmatch, Idx nregs,
     456              int regs_allocated)
    505457{
    506458  int rval = REGS_REALLOCATE;
    507   int i;
    508   int need_regs = nregs + 1;
    509   /* We need one extra element beyond `num_regs' for the `-1' marker GNU code
     459  Idx i;
     460  Idx need_regs = nregs + 1;
     461  /* We need one extra element beyond 'num_regs' for the '-1' marker GNU code
    510462     uses.  */
    511463
     
    514466    { /* No.  So allocate them with malloc.  */
    515467      regs->start = re_malloc (regoff_t, need_regs);
     468      if (__glibc_unlikely (regs->start == NULL))
     469        return REGS_UNALLOCATED;
    516470      regs->end = re_malloc (regoff_t, need_regs);
    517       if (BE (regs->start == NULL, 0) || BE (regs->end == NULL, 0))
    518         return REGS_UNALLOCATED;
     471      if (__glibc_unlikely (regs->end == NULL))
     472        {
     473          re_free (regs->start);
     474          return REGS_UNALLOCATED;
     475        }
    519476      regs->num_regs = need_regs;
    520477    }
     
    523480         allocated, reallocate them.  If we need fewer, just
    524481         leave it alone.  */
    525       if (BE (need_regs > regs->num_regs, 0))
     482      if (__glibc_unlikely (need_regs > regs->num_regs))
    526483        {
    527484          regoff_t *new_start = re_realloc (regs->start, regoff_t, need_regs);
    528           regoff_t *new_end = re_realloc (regs->end, regoff_t, need_regs);
    529           if (BE (new_start == NULL, 0) || BE (new_end == NULL, 0))
     485          regoff_t *new_end;
     486          if (__glibc_unlikely (new_start == NULL))
    530487            return REGS_UNALLOCATED;
     488          new_end = re_realloc (regs->end, regoff_t, need_regs);
     489          if (__glibc_unlikely (new_end == NULL))
     490            {
     491              re_free (new_start);
     492              return REGS_UNALLOCATED;
     493            }
    531494          regs->start = new_start;
    532495          regs->end = new_end;
     
    536499  else
    537500    {
    538       assert (regs_allocated == REGS_FIXED);
     501      DEBUG_ASSERT (regs_allocated == REGS_FIXED);
    539502      /* This function may not be called with REGS_FIXED and nregs too big.  */
    540       assert (regs->num_regs >= nregs);
     503      DEBUG_ASSERT (nregs <= regs->num_regs);
    541504      rval = REGS_FIXED;
    542505    }
     
    568531
    569532void
    570 re_set_registers (bufp, regs, num_regs, starts, ends)
    571     struct re_pattern_buffer *bufp;
    572     struct re_registers *regs;
    573     unsigned num_regs;
    574     regoff_t *starts, *ends;
     533re_set_registers (struct re_pattern_buffer *bufp, struct re_registers *regs,
     534                  __re_size_t num_regs, regoff_t *starts, regoff_t *ends)
    575535{
    576536  if (num_regs)
     
    585545      bufp->regs_allocated = REGS_UNALLOCATED;
    586546      regs->num_regs = 0;
    587       regs->start = regs->end = (regoff_t *) 0;
     547      regs->start = regs->end = NULL;
    588548    }
    589549}
     
    601561weak_function
    602562# endif
    603 re_exec (s)
    604      const char *s;
     563re_exec (const char *s)
    605564{
    606565  return 0 == regexec (&re_comp_buf, s, 0, NULL, 0);
     
    613572/* Searches for a compiled pattern PREG in the string STRING, whose
    614573   length is LENGTH.  NMATCH, PMATCH, and EFLAGS have the same
    615    mingings with regexec.  START, and RANGE have the same meanings
    616    with re_search.
     574   meaning as with regexec.  LAST_START is START + RANGE, where
     575   START and RANGE have the same meaning as with re_search.
    617576   Return REG_NOERROR if we find a match, and REG_NOMATCH if not,
    618577   otherwise return the error code.
    619578   Note: We assume front end functions already check ranges.
    620    (START + RANGE >= 0 && START + RANGE <= LENGTH)  */
     579   (0 <= LAST_START && LAST_START <= LENGTH)  */
    621580
    622581static reg_errcode_t
    623 re_search_internal (preg, string, length, start, range, stop, nmatch, pmatch,
    624                     eflags)
    625     const regex_t *preg;
    626     const char *string;
    627     int length, start, range, stop, eflags;
    628     size_t nmatch;
    629     regmatch_t pmatch[];
     582__attribute_warn_unused_result__
     583re_search_internal (const regex_t *preg, const char *string, Idx length,
     584                    Idx start, Idx last_start, Idx stop, size_t nmatch,
     585                    regmatch_t pmatch[], int eflags)
    630586{
    631587  reg_errcode_t err;
    632   const re_dfa_t *dfa = (const re_dfa_t *) preg->buffer;
    633   int left_lim, right_lim, incr;
    634   int fl_longest_match, match_first, match_kind, match_last = -1;
    635   int extra_nmatch;
    636   int sb, ch;
    637 #if defined _LIBC || (defined __STDC_VERSION__ && __STDC_VERSION__ >= 199901L)
     588  const re_dfa_t *dfa = preg->buffer;
     589  Idx left_lim, right_lim;
     590  int incr;
     591  bool fl_longest_match;
     592  int match_kind;
     593  Idx match_first;
     594  Idx match_last = -1;
     595  Idx extra_nmatch;
     596  bool sb;
     597  int ch;
    638598  re_match_context_t mctx = { .dfa = dfa };
    639 #else
    640   re_match_context_t mctx;
    641 #endif
    642   char *fastmap = (preg->fastmap != NULL && preg->fastmap_accurate
    643                    && range && !preg->can_be_null) ? preg->fastmap : NULL;
     599  char *fastmap = ((preg->fastmap != NULL && preg->fastmap_accurate
     600                    && start != last_start && !preg->can_be_null)
     601                   ? preg->fastmap : NULL);
    644602  RE_TRANSLATE_TYPE t = preg->translate;
    645 
    646 #if !(defined _LIBC || (defined __STDC_VERSION__ && __STDC_VERSION__ >= 199901L))
    647   memset (&mctx, '\0', sizeof (re_match_context_t));
    648   mctx.dfa = dfa;
    649 #endif
    650603
    651604  extra_nmatch = (nmatch > preg->re_nsub) ? nmatch - (preg->re_nsub + 1) : 0;
     
    653606
    654607  /* Check if the DFA haven't been compiled.  */
    655   if (BE (preg->used == 0 || dfa->init_state == NULL
    656           || dfa->init_state_word == NULL || dfa->init_state_nl == NULL
    657           || dfa->init_state_begbuf == NULL, 0))
     608  if (__glibc_unlikely (preg->used == 0 || dfa->init_state == NULL
     609                        || dfa->init_state_word == NULL
     610                        || dfa->init_state_nl == NULL
     611                        || dfa->init_state_begbuf == NULL))
    658612    return REG_NOMATCH;
    659613
    660 #ifdef DEBUG
    661614  /* We assume front-end functions already check them.  */
    662   assert (start + range >= 0 && start + range <= length);
    663 #endif
     615  DEBUG_ASSERT (0 <= last_start && last_start <= length);
    664616
    665617  /* If initial states with non-begbuf contexts have no elements,
     
    671623          || !preg->newline_anchor))
    672624    {
    673       if (start != 0 && start + range != 0)
     625      if (start != 0 && last_start != 0)
    674626        return REG_NOMATCH;
    675       start = range = 0;
     627      start = last_start = 0;
    676628    }
    677629
     
    680632
    681633  err = re_string_allocate (&mctx.input, string, length, dfa->nodes_len + 1,
    682                             preg->translate, preg->syntax & RE_ICASE, dfa);
    683   if (BE (err != REG_NOERROR, 0))
     634                            preg->translate, (preg->syntax & RE_ICASE) != 0,
     635                            dfa);
     636  if (__glibc_unlikely (err != REG_NOERROR))
    684637    goto free_return;
    685638  mctx.input.stop = stop;
     
    688641
    689642  err = match_ctx_init (&mctx, eflags, dfa->nbackref * 2);
    690   if (BE (err != REG_NOERROR, 0))
     643  if (__glibc_unlikely (err != REG_NOERROR))
    691644    goto free_return;
    692645
     
    697650  if (nmatch > 1 || dfa->has_mb_node)
    698651    {
    699       mctx.state_log = re_malloc (re_dfastate_t *, mctx.input.bufs_len + 1);
    700       if (BE (mctx.state_log == NULL, 0))
     652      /* Avoid overflow.  */
     653      if (__glibc_unlikely ((MIN (IDX_MAX, SIZE_MAX / sizeof (re_dfastate_t *))
     654                             <= mctx.input.bufs_len)))
    701655        {
    702656          err = REG_ESPACE;
    703657          goto free_return;
    704658        }
    705     }
    706   else
    707     mctx.state_log = NULL;
     659
     660      mctx.state_log = re_malloc (re_dfastate_t *, mctx.input.bufs_len + 1);
     661      if (__glibc_unlikely (mctx.state_log == NULL))
     662        {
     663          err = REG_ESPACE;
     664          goto free_return;
     665        }
     666    }
    708667
    709668  match_first = start;
     
    711670                           : CONTEXT_NEWLINE | CONTEXT_BEGBUF;
    712671
    713   /* Check incrementally whether of not the input string match.  */
    714   incr = (range < 0) ? -1 : 1;
    715   left_lim = (range < 0) ? start + range : start;
    716   right_lim = (range < 0) ? start : start + range;
     672  /* Check incrementally whether the input string matches.  */
     673  incr = (last_start < start) ? -1 : 1;
     674  left_lim = (last_start < start) ? last_start : start;
     675  right_lim = (last_start < start) ? start : last_start;
    717676  sb = dfa->mb_cur_max == 1;
    718677  match_kind =
    719678    (fastmap
    720679     ? ((sb || !(preg->syntax & RE_ICASE || t) ? 4 : 0)
    721         | (range >= 0 ? 2 : 0)
     680        | (start <= last_start ? 2 : 0)
    722681        | (t != NULL ? 1 : 0))
    723682     : 8);
     
    742701        case 7:
    743702          /* Fastmap with single-byte translation, match forward.  */
    744           while (BE (match_first < right_lim, 1)
     703          while (__glibc_likely (match_first < right_lim)
    745704                 && !fastmap[t[(unsigned char) string[match_first]]])
    746705            ++match_first;
     
    749708        case 6:
    750709          /* Fastmap without translation, match forward.  */
    751           while (BE (match_first < right_lim, 1)
     710          while (__glibc_likely (match_first < right_lim)
    752711                 && !fastmap[(unsigned char) string[match_first]])
    753712            ++match_first;
    754713
    755714        forward_match_found_start_or_reached_end:
    756           if (BE (match_first == right_lim, 0))
     715          if (__glibc_unlikely (match_first == right_lim))
    757716            {
    758717              ch = match_first >= length
     
    786745              /* If MATCH_FIRST is out of the valid range, reconstruct the
    787746                 buffers.  */
    788               unsigned int offset = match_first - mctx.input.raw_mbs_idx;
    789               if (BE (offset >= (unsigned int) mctx.input.valid_raw_len, 0))
     747              __re_size_t offset = match_first - mctx.input.raw_mbs_idx;
     748              if (__glibc_unlikely (offset
     749                                    >= (__re_size_t) mctx.input.valid_raw_len))
    790750                {
    791751                  err = re_string_reconstruct (&mctx.input, match_first,
    792752                                               eflags);
    793                   if (BE (err != REG_NOERROR, 0))
     753                  if (__glibc_unlikely (err != REG_NOERROR))
    794754                    goto free_return;
    795755
    796756                  offset = match_first - mctx.input.raw_mbs_idx;
    797757                }
    798               /* If MATCH_FIRST is out of the buffer, leave it as '\0'.
    799                  Note that MATCH_FIRST must not be smaller than 0.  */
    800               ch = (match_first >= length
    801                     ? 0 : re_string_byte_at (&mctx.input, offset));
     758              /* Use buffer byte if OFFSET is in buffer, otherwise '\0'.  */
     759              ch = (offset < mctx.input.valid_len
     760                    ? re_string_byte_at (&mctx.input, offset) : 0);
    802761              if (fastmap[ch])
    803762                break;
    804763              match_first += incr;
    805764              if (match_first < left_lim || match_first > right_lim)
    806                 {
    807                   err = REG_NOMATCH;
    808                   goto free_return;
    809                 }
     765                {
     766                  err = REG_NOMATCH;
     767                  goto free_return;
     768                }
    810769            }
    811770          break;
     
    815774         the matching starts from the beginning of the buffer.  */
    816775      err = re_string_reconstruct (&mctx.input, match_first, eflags);
    817       if (BE (err != REG_NOERROR, 0))
     776      if (__glibc_unlikely (err != REG_NOERROR))
    818777        goto free_return;
    819778
    820 #ifdef RE_ENABLE_I18N
    821      /* Don't consider this char as a possible match start if it part,
    822         yet isn't the head, of a multibyte character.  */
     779      /* Don't consider this char as a possible match start if it part,
     780         yet isn't the head, of a multibyte character.  */
    823781      if (!sb && !re_string_first_byte (&mctx.input, 0))
    824782        continue;
    825 #endif
    826783
    827784      /* It seems to be appropriate one, then use the matcher.  */
     
    829786      mctx.state_log_top = mctx.nbkref_ents = mctx.max_mb_elem_len = 0;
    830787      match_last = check_matching (&mctx, fl_longest_match,
    831                                    range >= 0 ? &match_first : NULL);
     788                                   start <= last_start ? &match_first : NULL);
    832789      if (match_last != -1)
    833790        {
    834           if (BE (match_last == -2, 0))
     791          if (__glibc_unlikely (match_last == -2))
    835792            {
    836793              err = REG_ESPACE;
     
    852809                  if (err == REG_NOERROR)
    853810                    break;
    854                   if (BE (err != REG_NOMATCH, 0))
     811                  if (__glibc_unlikely (err != REG_NOMATCH))
    855812                    goto free_return;
    856813                  match_last = -1;
     
    864821    }
    865822
    866 #ifdef DEBUG
    867   assert (match_last != -1);
    868   assert (err == REG_NOERROR);
    869 #endif
     823  DEBUG_ASSERT (match_last != -1);
     824  DEBUG_ASSERT (err == REG_NOERROR);
    870825
    871826  /* Set pmatch[] if we need.  */
    872827  if (nmatch > 0)
    873828    {
    874       int reg_idx;
     829      Idx reg_idx;
    875830
    876831      /* Initialize registers.  */
     
    881836      pmatch[0].rm_so = 0;
    882837      pmatch[0].rm_eo = mctx.match_last;
     838      /* FIXME: This function should fail if mctx.match_last exceeds
     839         the maximum possible regoff_t value.  We need a new error
     840         code REG_OVERFLOW.  */
    883841
    884842      if (!preg->no_sub && nmatch > 1)
     
    886844          err = set_regs (preg, &mctx, nmatch, pmatch,
    887845                          dfa->has_plural_match && dfa->nbackref > 0);
    888           if (BE (err != REG_NOERROR, 0))
     846          if (__glibc_unlikely (err != REG_NOERROR))
    889847            goto free_return;
    890848        }
    891849
    892       /* At last, add the offset to the each registers, since we slided
     850      /* At last, add the offset to each register, since we slid
    893851         the buffers so that we could assume that the matching starts
    894852         from 0.  */
     
    896854        if (pmatch[reg_idx].rm_so != -1)
    897855          {
    898 #ifdef RE_ENABLE_I18N
    899             if (BE (mctx.input.offsets_needed != 0, 0))
     856            if (__glibc_unlikely (mctx.input.offsets_needed != 0))
    900857              {
    901858                pmatch[reg_idx].rm_so =
     
    908865                   : mctx.input.offsets[pmatch[reg_idx].rm_eo]);
    909866              }
    910 #else
    911             assert (mctx.input.offsets_needed == 0);
    912 #endif
    913867            pmatch[reg_idx].rm_so += match_first;
    914868            pmatch[reg_idx].rm_eo += match_first;
     
    921875
    922876      if (dfa->subexp_map)
    923         for (reg_idx = 0; reg_idx + 1 < nmatch; reg_idx++)
    924           if (dfa->subexp_map[reg_idx] != reg_idx)
    925             {
    926               pmatch[reg_idx + 1].rm_so
    927                 = pmatch[dfa->subexp_map[reg_idx] + 1].rm_so;
    928               pmatch[reg_idx + 1].rm_eo
    929                 = pmatch[dfa->subexp_map[reg_idx] + 1].rm_eo;
    930             }
     877        for (reg_idx = 0; reg_idx + 1 < nmatch; reg_idx++)
     878          if (dfa->subexp_map[reg_idx] != reg_idx)
     879            {
     880              pmatch[reg_idx + 1].rm_so
     881                = pmatch[dfa->subexp_map[reg_idx] + 1].rm_so;
     882              pmatch[reg_idx + 1].rm_eo
     883                = pmatch[dfa->subexp_map[reg_idx] + 1].rm_eo;
     884            }
    931885    }
    932886
     
    940894
    941895static reg_errcode_t
    942 prune_impossible_nodes (mctx)
    943      re_match_context_t *mctx;
     896__attribute_warn_unused_result__
     897prune_impossible_nodes (re_match_context_t *mctx)
    944898{
    945899  const re_dfa_t *const dfa = mctx->dfa;
    946   int halt_node, match_last;
     900  Idx halt_node, match_last;
    947901  reg_errcode_t ret;
    948902  re_dfastate_t **sifted_states;
    949903  re_dfastate_t **lim_states = NULL;
    950904  re_sift_context_t sctx;
    951 #ifdef DEBUG
    952   assert (mctx->state_log != NULL);
    953 #endif
     905  DEBUG_ASSERT (mctx->state_log != NULL);
    954906  match_last = mctx->match_last;
    955907  halt_node = mctx->last_node;
     908
     909  /* Avoid overflow.  */
     910  if (__glibc_unlikely (MIN (IDX_MAX, SIZE_MAX / sizeof (re_dfastate_t *))
     911                        <= match_last))
     912    return REG_ESPACE;
     913
    956914  sifted_states = re_malloc (re_dfastate_t *, match_last + 1);
    957   if (BE (sifted_states == NULL, 0))
     915  if (__glibc_unlikely (sifted_states == NULL))
    958916    {
    959917      ret = REG_ESPACE;
     
    963921    {
    964922      lim_states = re_malloc (re_dfastate_t *, match_last + 1);
    965       if (BE (lim_states == NULL, 0))
     923      if (__glibc_unlikely (lim_states == NULL))
    966924        {
    967925          ret = REG_ESPACE;
     
    976934          ret = sift_states_backward (mctx, &sctx);
    977935          re_node_set_free (&sctx.limits);
    978           if (BE (ret != REG_NOERROR, 0))
     936          if (__glibc_unlikely (ret != REG_NOERROR))
    979937              goto free_return;
    980938          if (sifted_states[0] != NULL || lim_states[0] != NULL)
     
    998956      re_free (lim_states);
    999957      lim_states = NULL;
    1000       if (BE (ret != REG_NOERROR, 0))
     958      if (__glibc_unlikely (ret != REG_NOERROR))
    1001959        goto free_return;
    1002960    }
     
    1006964      ret = sift_states_backward (mctx, &sctx);
    1007965      re_node_set_free (&sctx.limits);
    1008       if (BE (ret != REG_NOERROR, 0))
     966      if (__glibc_unlikely (ret != REG_NOERROR))
    1009967        goto free_return;
     968      if (sifted_states[0] == NULL)
     969        {
     970          ret = REG_NOMATCH;
     971          goto free_return;
     972        }
    1010973    }
    1011974  re_free (mctx->state_log);
     
    1025988   since initial states may have constraints like "\<", "^", etc..  */
    1026989
    1027 static inline re_dfastate_t *
    1028 __attribute ((always_inline)) internal_function
     990static __always_inline re_dfastate_t *
    1029991acquire_init_state_context (reg_errcode_t *err, const re_match_context_t *mctx,
    1030                             int idx)
     992                            Idx idx)
    1031993{
    1032994  const re_dfa_t *const dfa = mctx->dfa;
     
    10591021
    10601022/* Check whether the regular expression match input string INPUT or not,
    1061    and return the index where the matching end, return -1 if not match,
    1062    or return -2 in case of an error.
     1023   and return the index where the matching end.  Return -1 if
     1024   there is no match, and return -2 in case of an error.
    10631025   FL_LONGEST_MATCH means we want the POSIX longest matching.
    10641026   If P_MATCH_FIRST is not NULL, and the match fails, it is set to the
    10651027   next place where we may want to try matching.
    1066    Note that the matcher assume that the maching starts from the current
     1028   Note that the matcher assumes that the matching starts from the current
    10671029   index of the buffer.  */
    10681030
    1069 static int
    1070 internal_function
    1071 check_matching (re_match_context_t *mctx, int fl_longest_match,
    1072                 int *p_match_first)
     1031static Idx
     1032__attribute_warn_unused_result__
     1033check_matching (re_match_context_t *mctx, bool fl_longest_match,
     1034                Idx *p_match_first)
    10731035{
    10741036  const re_dfa_t *const dfa = mctx->dfa;
    10751037  reg_errcode_t err;
    1076   int match = 0;
    1077   int match_last = -1;
    1078   int cur_str_idx = re_string_cur_idx (&mctx->input);
     1038  Idx match = 0;
     1039  Idx match_last = -1;
     1040  Idx cur_str_idx = re_string_cur_idx (&mctx->input);
    10791041  re_dfastate_t *cur_state;
    1080   int at_init_state = p_match_first != NULL;
    1081   int next_start_idx = cur_str_idx;
     1042  bool at_init_state = p_match_first != NULL;
     1043  Idx next_start_idx = cur_str_idx;
    10821044
    10831045  err = REG_NOERROR;
    10841046  cur_state = acquire_init_state_context (&err, mctx, cur_str_idx);
    10851047  /* An initial state must not be NULL (invalid).  */
    1086   if (BE (cur_state == NULL, 0))
    1087     {
    1088       assert (err == REG_ESPACE);
     1048  if (__glibc_unlikely (cur_state == NULL))
     1049    {
     1050      DEBUG_ASSERT (err == REG_ESPACE);
    10891051      return -2;
    10901052    }
     
    10961058      /* Check OP_OPEN_SUBEXP in the initial state in case that we use them
    10971059         later.  E.g. Processing back references.  */
    1098       if (BE (dfa->nbackref, 0))
    1099         {
    1100           at_init_state = 0;
     1060      if (__glibc_unlikely (dfa->nbackref))
     1061        {
     1062          at_init_state = false;
    11011063          err = check_subexp_matching_top (mctx, &cur_state->nodes, 0);
    1102           if (BE (err != REG_NOERROR, 0))
     1064          if (__glibc_unlikely (err != REG_NOERROR))
    11031065            return err;
    11041066
     
    11061068            {
    11071069              err = transit_state_bkref (mctx, &cur_state->nodes);
    1108               if (BE (err != REG_NOERROR, 0))
    1109                 return err;
     1070              if (__glibc_unlikely (err != REG_NOERROR))
     1071                return err;
    11101072            }
    11111073        }
     
    11131075
    11141076  /* If the RE accepts NULL string.  */
    1115   if (BE (cur_state->halt, 0))
     1077  if (__glibc_unlikely (cur_state->halt))
    11161078    {
    11171079      if (!cur_state->has_constraint
     
    11311093    {
    11321094      re_dfastate_t *old_state = cur_state;
    1133       int next_char_idx = re_string_cur_idx (&mctx->input) + 1;
    1134 
    1135       if (BE (next_char_idx >= mctx->input.bufs_len, 0)
    1136           || (BE (next_char_idx >= mctx->input.valid_len, 0)
    1137               && mctx->input.valid_len < mctx->input.len))
    1138         {
    1139           err = extend_buffers (mctx);
    1140           if (BE (err != REG_NOERROR, 0))
    1141             {
    1142               assert (err == REG_ESPACE);
     1095      Idx next_char_idx = re_string_cur_idx (&mctx->input) + 1;
     1096
     1097      if ((__glibc_unlikely (next_char_idx >= mctx->input.bufs_len)
     1098           && mctx->input.bufs_len < mctx->input.len)
     1099          || (__glibc_unlikely (next_char_idx >= mctx->input.valid_len)
     1100              && mctx->input.valid_len < mctx->input.len))
     1101        {
     1102          err = extend_buffers (mctx, next_char_idx + 1);
     1103          if (__glibc_unlikely (err != REG_NOERROR))
     1104            {
     1105              DEBUG_ASSERT (err == REG_ESPACE);
    11431106              return -2;
    11441107            }
    1145         }
     1108        }
    11461109
    11471110      cur_state = transit_state (&err, mctx, cur_state);
     
    11541117             state using the state log, if available and if we have not
    11551118             already found a valid (even if not the longest) match.  */
    1156           if (BE (err != REG_NOERROR, 0))
     1119          if (__glibc_unlikely (err != REG_NOERROR))
    11571120            return -2;
    11581121
     
    11631126        }
    11641127
    1165       if (BE (at_init_state, 0))
     1128      if (__glibc_unlikely (at_init_state))
    11661129        {
    11671130          if (old_state == cur_state)
    11681131            next_start_idx = next_char_idx;
    11691132          else
    1170             at_init_state = 0;
     1133            at_init_state = false;
    11711134        }
    11721135
     
    11991162/* Check NODE match the current context.  */
    12001163
    1201 static int
    1202 internal_function
    1203 check_halt_node_context (const re_dfa_t *dfa, int node, unsigned int context)
     1164static bool
     1165check_halt_node_context (const re_dfa_t *dfa, Idx node, unsigned int context)
    12041166{
    12051167  re_token_type_t type = dfa->nodes[node].type;
    12061168  unsigned int constraint = dfa->nodes[node].constraint;
    12071169  if (type != END_OF_RE)
    1208     return 0;
     1170    return false;
    12091171  if (!constraint)
    1210     return 1;
     1172    return true;
    12111173  if (NOT_SATISFY_NEXT_CONSTRAINT (constraint, context))
    1212     return 0;
    1213   return 1;
     1174    return false;
     1175  return true;
    12141176}
    12151177
     
    12181180   match the context, return the node.  */
    12191181
    1220 static int
    1221 internal_function
     1182static Idx
    12221183check_halt_state_context (const re_match_context_t *mctx,
    1223                           const re_dfastate_t *state, int idx)
    1224 {
    1225   int i;
     1184                          const re_dfastate_t *state, Idx idx)
     1185{
     1186  Idx i;
    12261187  unsigned int context;
    1227 #ifdef DEBUG
    1228   assert (state->halt);
    1229 #endif
     1188  DEBUG_ASSERT (state->halt);
    12301189  context = re_string_context_at (&mctx->input, idx, mctx->eflags);
    12311190  for (i = 0; i < state->nodes.nelem; ++i)
     
    12371196/* Compute the next node to which "NFA" transit from NODE("NFA" is a NFA
    12381197   corresponding to the DFA).
    1239    Return the destination node, and update EPS_VIA_NODES, return -1 in case
    1240    of errors.  */
    1241 
    1242 static int
    1243 internal_function
    1244 proceed_next_node (const re_match_context_t *mctx, int nregs, regmatch_t *regs,
    1245                    int *pidx, int node, re_node_set *eps_via_nodes,
     1198   Return the destination node, and update EPS_VIA_NODES;
     1199   return -1 on match failure, -2 on error.  */
     1200
     1201static Idx
     1202proceed_next_node (const re_match_context_t *mctx, Idx nregs, regmatch_t *regs,
     1203                   regmatch_t *prevregs,
     1204                   Idx *pidx, Idx node, re_node_set *eps_via_nodes,
    12461205                   struct re_fail_stack_t *fs)
    12471206{
    12481207  const re_dfa_t *const dfa = mctx->dfa;
    1249   int i, err;
    12501208  if (IS_EPSILON_NODE (dfa->nodes[node].type))
    12511209    {
    12521210      re_node_set *cur_nodes = &mctx->state_log[*pidx]->nodes;
    12531211      re_node_set *edests = &dfa->edests[node];
    1254       int dest_node;
    1255       err = re_node_set_insert (eps_via_nodes, node);
    1256       if (BE (err < 0, 0))
    1257         return -2;
    1258       /* Pick up a valid destination, or return -1 if none is found.  */
    1259       for (dest_node = -1, i = 0; i < edests->nelem; ++i)
    1260         {
    1261           int candidate = edests->elems[i];
     1212
     1213      if (! re_node_set_contains (eps_via_nodes, node))
     1214        {
     1215          bool ok = re_node_set_insert (eps_via_nodes, node);
     1216          if (__glibc_unlikely (! ok))
     1217            return -2;
     1218        }
     1219
     1220      /* Pick a valid destination, or return -1 if none is found.  */
     1221      Idx dest_node = -1;
     1222      for (Idx i = 0; i < edests->nelem; i++)
     1223        {
     1224          Idx candidate = edests->elems[i];
    12621225          if (!re_node_set_contains (cur_nodes, candidate))
    12631226            continue;
     
    12651228            dest_node = candidate;
    12661229
    1267           else
     1230          else
    12681231            {
    12691232              /* In order to avoid infinite loop like "(a*)*", return the second
    1270                 epsilon-transition if the first was already considered.  */
     1233                epsilon-transition if the first was already considered.  */
    12711234              if (re_node_set_contains (eps_via_nodes, dest_node))
    1272                 return candidate;
     1235                return candidate;
    12731236
    12741237              /* Otherwise, push the second epsilon-transition on the fail stack.  */
    12751238              else if (fs != NULL
    12761239                       && push_fail_stack (fs, *pidx, candidate, nregs, regs,
    1277                                           eps_via_nodes))
     1240                                           prevregs, eps_via_nodes))
    12781241                return -2;
    12791242
     
    12861249  else
    12871250    {
    1288       int naccepted = 0;
     1251      Idx naccepted = 0;
    12891252      re_token_type_t type = dfa->nodes[node].type;
    12901253
    1291 #ifdef RE_ENABLE_I18N
    12921254      if (dfa->nodes[node].accept_mb)
    12931255        naccepted = check_node_accept_bytes (dfa, node, &mctx->input, *pidx);
    1294       else
    1295 #endif /* RE_ENABLE_I18N */
    1296       if (type == OP_BACK_REF)
    1297         {
    1298           int subexp_idx = dfa->nodes[node].opr.idx + 1;
    1299           naccepted = regs[subexp_idx].rm_eo - regs[subexp_idx].rm_so;
     1256      else if (type == OP_BACK_REF)
     1257        {
     1258          Idx subexp_idx = dfa->nodes[node].opr.idx + 1;
     1259          if (subexp_idx < nregs)
     1260            naccepted = regs[subexp_idx].rm_eo - regs[subexp_idx].rm_so;
    13001261          if (fs != NULL)
    13011262            {
    1302               if (regs[subexp_idx].rm_so == -1 || regs[subexp_idx].rm_eo == -1)
     1263              if (subexp_idx >= nregs
     1264                  || regs[subexp_idx].rm_so == -1
     1265                  || regs[subexp_idx].rm_eo == -1)
    13031266                return -1;
    13041267              else if (naccepted)
    13051268                {
    13061269                  char *buf = (char *) re_string_get_buffer (&mctx->input);
    1307                   if (memcmp (buf + regs[subexp_idx].rm_so, buf + *pidx,
    1308                               naccepted) != 0)
     1270                  if (mctx->input.valid_len - *pidx < naccepted
     1271                      || (memcmp (buf + regs[subexp_idx].rm_so, buf + *pidx,
     1272                                  naccepted)
     1273                          != 0))
    13091274                    return -1;
    13101275                }
     
    13131278          if (naccepted == 0)
    13141279            {
    1315               int dest_node;
    1316               err = re_node_set_insert (eps_via_nodes, node);
    1317               if (BE (err < 0, 0))
     1280              Idx dest_node;
     1281              bool ok = re_node_set_insert (eps_via_nodes, node);
     1282              if (__glibc_unlikely (! ok))
    13181283                return -2;
    13191284              dest_node = dfa->edests[node].elems[0];
     
    13271292          || check_node_accept (mctx, dfa->nodes + node, *pidx))
    13281293        {
    1329           int dest_node = dfa->nexts[node];
     1294          Idx dest_node = dfa->nexts[node];
    13301295          *pidx = (naccepted == 0) ? *pidx + 1 : *pidx + naccepted;
    13311296          if (fs && (*pidx > mctx->match_last || mctx->state_log[*pidx] == NULL
     
    13411306
    13421307static reg_errcode_t
    1343 internal_function
    1344 push_fail_stack (struct re_fail_stack_t *fs, int str_idx, int dest_node,
    1345                  int nregs, regmatch_t *regs, re_node_set *eps_via_nodes)
     1308__attribute_warn_unused_result__
     1309push_fail_stack (struct re_fail_stack_t *fs, Idx str_idx, Idx dest_node,
     1310                 Idx nregs, regmatch_t *regs, regmatch_t *prevregs,
     1311                 re_node_set *eps_via_nodes)
    13461312{
    13471313  reg_errcode_t err;
    1348   int num = fs->num++;
    1349   if (fs->num == fs->alloc)
     1314  Idx num = fs->num;
     1315  if (num == fs->alloc)
    13501316    {
    13511317      struct re_fail_stack_ent_t *new_array;
    1352       new_array = realloc (fs->stack, (sizeof (struct re_fail_stack_ent_t)
    1353                                        * fs->alloc * 2));
     1318      new_array = re_realloc (fs->stack, struct re_fail_stack_ent_t,
     1319                              fs->alloc * 2);
    13541320      if (new_array == NULL)
    13551321        return REG_ESPACE;
     
    13591325  fs->stack[num].idx = str_idx;
    13601326  fs->stack[num].node = dest_node;
    1361   fs->stack[num].regs = re_malloc (regmatch_t, nregs);
     1327  fs->stack[num].regs = re_malloc (regmatch_t, 2 * nregs);
    13621328  if (fs->stack[num].regs == NULL)
    13631329    return REG_ESPACE;
     1330  fs->num = num + 1;
    13641331  memcpy (fs->stack[num].regs, regs, sizeof (regmatch_t) * nregs);
     1332  memcpy (fs->stack[num].regs + nregs, prevregs, sizeof (regmatch_t) * nregs);
    13651333  err = re_node_set_init_copy (&fs->stack[num].eps_via_nodes, eps_via_nodes);
    13661334  return err;
    13671335}
    13681336
    1369 static int
    1370 internal_function
    1371 pop_fail_stack (struct re_fail_stack_t *fs, int *pidx, int nregs,
    1372                 regmatch_t *regs, re_node_set *eps_via_nodes)
    1373 {
    1374   int num = --fs->num;
    1375   assert (num >= 0);
     1337static Idx
     1338pop_fail_stack (struct re_fail_stack_t *fs, Idx *pidx, Idx nregs,
     1339                regmatch_t *regs, regmatch_t *prevregs,
     1340                re_node_set *eps_via_nodes)
     1341{
     1342  if (fs == NULL || fs->num == 0)
     1343    return -1;
     1344  Idx num = --fs->num;
    13761345  *pidx = fs->stack[num].idx;
    13771346  memcpy (regs, fs->stack[num].regs, sizeof (regmatch_t) * nregs);
     1347  memcpy (prevregs, fs->stack[num].regs + nregs, sizeof (regmatch_t) * nregs);
    13781348  re_node_set_free (eps_via_nodes);
    13791349  re_free (fs->stack[num].regs);
    13801350  *eps_via_nodes = fs->stack[num].eps_via_nodes;
     1351  DEBUG_ASSERT (0 <= fs->stack[num].node);
    13811352  return fs->stack[num].node;
    13821353}
     1354
     1355
     1356#define DYNARRAY_STRUCT  regmatch_list
     1357#define DYNARRAY_ELEMENT regmatch_t
     1358#define DYNARRAY_PREFIX  regmatch_list_
     1359#include <malloc/dynarray-skeleton.c>
    13831360
    13841361/* Set the positions where the subexpressions are starts/ends to registers
     
    13881365
    13891366static reg_errcode_t
    1390 internal_function
     1367__attribute_warn_unused_result__
    13911368set_regs (const regex_t *preg, const re_match_context_t *mctx, size_t nmatch,
    1392           regmatch_t *pmatch, int fl_backtrack)
    1393 {
    1394   const re_dfa_t *dfa = (const re_dfa_t *) preg->buffer;
    1395   int idx, cur_node;
     1369          regmatch_t *pmatch, bool fl_backtrack)
     1370{
     1371  const re_dfa_t *dfa = preg->buffer;
     1372  Idx idx, cur_node;
    13961373  re_node_set eps_via_nodes;
    13971374  struct re_fail_stack_t *fs;
    13981375  struct re_fail_stack_t fs_body = { 0, 2, NULL };
    1399   regmatch_t *prev_idx_match;
    1400   int prev_idx_match_malloced = 0;
    1401 
    1402 #ifdef DEBUG
    1403   assert (nmatch > 1);
    1404   assert (mctx->state_log != NULL);
    1405 #endif
     1376  struct regmatch_list prev_match;
     1377  regmatch_list_init (&prev_match);
     1378
     1379  DEBUG_ASSERT (nmatch > 1);
     1380  DEBUG_ASSERT (mctx->state_log != NULL);
    14061381  if (fl_backtrack)
    14071382    {
     
    14171392  re_node_set_init_empty (&eps_via_nodes);
    14181393
    1419   if (__libc_use_alloca (nmatch * sizeof (regmatch_t)))
    1420     prev_idx_match = (regmatch_t *) alloca (nmatch * sizeof (regmatch_t));
    1421   else
    1422     {
    1423       prev_idx_match = re_malloc (regmatch_t, nmatch);
    1424       if (prev_idx_match == NULL)
    1425         {
    1426           free_fail_stack_return (fs);
    1427           return REG_ESPACE;
    1428         }
    1429       prev_idx_match_malloced = 1;
    1430     }
     1394  if (!regmatch_list_resize (&prev_match, nmatch))
     1395    {
     1396      regmatch_list_free (&prev_match);
     1397      free_fail_stack_return (fs);
     1398      return REG_ESPACE;
     1399    }
     1400  regmatch_t *prev_idx_match = regmatch_list_begin (&prev_match);
    14311401  memcpy (prev_idx_match, pmatch, sizeof (regmatch_t) * nmatch);
    14321402
     
    14351405      update_regs (dfa, pmatch, prev_idx_match, cur_node, idx, nmatch);
    14361406
    1437       if (idx == pmatch[0].rm_eo && cur_node == mctx->last_node)
    1438         {
    1439           int reg_idx;
     1407      if ((idx == pmatch[0].rm_eo && cur_node == mctx->last_node)
     1408          || (fs && re_node_set_contains (&eps_via_nodes, cur_node)))
     1409        {
     1410          Idx reg_idx;
     1411          cur_node = -1;
    14401412          if (fs)
    14411413            {
    14421414              for (reg_idx = 0; reg_idx < nmatch; ++reg_idx)
    14431415                if (pmatch[reg_idx].rm_so > -1 && pmatch[reg_idx].rm_eo == -1)
    1444                   break;
    1445               if (reg_idx == nmatch)
    1446                 {
    1447                   re_node_set_free (&eps_via_nodes);
    1448                   if (prev_idx_match_malloced)
    1449                     re_free (prev_idx_match);
    1450                   return free_fail_stack_return (fs);
    1451                 }
    1452               cur_node = pop_fail_stack (fs, &idx, nmatch, pmatch,
    1453                                          &eps_via_nodes);
    1454             }
    1455           else
     1416                  {
     1417                    cur_node = pop_fail_stack (fs, &idx, nmatch, pmatch,
     1418                                               prev_idx_match, &eps_via_nodes);
     1419                    break;
     1420                  }
     1421            }
     1422          if (cur_node < 0)
    14561423            {
    14571424              re_node_set_free (&eps_via_nodes);
    1458               if (prev_idx_match_malloced)
    1459                 re_free (prev_idx_match);
    1460               return REG_NOERROR;
     1425              regmatch_list_free (&prev_match);
     1426              return free_fail_stack_return (fs);
    14611427            }
    14621428        }
    14631429
    14641430      /* Proceed to next node.  */
    1465       cur_node = proceed_next_node (mctx, nmatch, pmatch, &idx, cur_node,
     1431      cur_node = proceed_next_node (mctx, nmatch, pmatch, prev_idx_match,
     1432                                    &idx, cur_node,
    14661433                                    &eps_via_nodes, fs);
    14671434
    1468       if (BE (cur_node < 0, 0))
    1469         {
    1470           if (BE (cur_node == -2, 0))
     1435      if (__glibc_unlikely (cur_node < 0))
     1436        {
     1437          if (__glibc_unlikely (cur_node == -2))
    14711438            {
    14721439              re_node_set_free (&eps_via_nodes);
    1473               if (prev_idx_match_malloced)
    1474                 re_free (prev_idx_match);
     1440              regmatch_list_free (&prev_match);
    14751441              free_fail_stack_return (fs);
    14761442              return REG_ESPACE;
    14771443            }
    1478           if (fs)
    1479             cur_node = pop_fail_stack (fs, &idx, nmatch, pmatch,
    1480                                        &eps_via_nodes);
    1481           else
     1444          cur_node = pop_fail_stack (fs, &idx, nmatch, pmatch,
     1445                                     prev_idx_match, &eps_via_nodes);
     1446          if (cur_node < 0)
    14821447            {
    14831448              re_node_set_free (&eps_via_nodes);
    1484               if (prev_idx_match_malloced)
    1485                 re_free (prev_idx_match);
     1449              regmatch_list_free (&prev_match);
     1450              free_fail_stack_return (fs);
    14861451              return REG_NOMATCH;
    14871452            }
     
    14891454    }
    14901455  re_node_set_free (&eps_via_nodes);
    1491   if (prev_idx_match_malloced)
    1492     re_free (prev_idx_match);
     1456  regmatch_list_free (&prev_match);
    14931457  return free_fail_stack_return (fs);
    14941458}
    14951459
    14961460static reg_errcode_t
    1497 internal_function
    14981461free_fail_stack_return (struct re_fail_stack_t *fs)
    14991462{
    15001463  if (fs)
    15011464    {
    1502       int fs_idx;
     1465      Idx fs_idx;
    15031466      for (fs_idx = 0; fs_idx < fs->num; ++fs_idx)
    15041467        {
     
    15121475
    15131476static void
    1514 internal_function
    15151477update_regs (const re_dfa_t *dfa, regmatch_t *pmatch,
    1516              regmatch_t *prev_idx_match, int cur_node, int cur_idx, int nmatch)
     1478             regmatch_t *prev_idx_match, Idx cur_node, Idx cur_idx, Idx nmatch)
    15171479{
    15181480  int type = dfa->nodes[cur_node].type;
    15191481  if (type == OP_OPEN_SUBEXP)
    15201482    {
    1521       int reg_num = dfa->nodes[cur_node].opr.idx + 1;
     1483      Idx reg_num = dfa->nodes[cur_node].opr.idx + 1;
    15221484
    15231485      /* We are at the first node of this sub expression.  */
     
    15301492  else if (type == OP_CLOSE_SUBEXP)
    15311493    {
    1532       int reg_num = dfa->nodes[cur_node].opr.idx + 1;
     1494      /* We are at the last node of this sub expression.  */
     1495      Idx reg_num = dfa->nodes[cur_node].opr.idx + 1;
    15331496      if (reg_num < nmatch)
    15341497        {
    1535           /* We are at the last node of this sub expression.  */
    15361498          if (pmatch[reg_num].rm_so < cur_idx)
    15371499            {
     
    15641526   Updated state_log will be wrote to STATE_LOG.
    15651527
    1566    Rules: We throw away the Node `a' in the STATE_LOG[STR_IDX] if...
     1528   Rules: We throw away the Node 'a' in the STATE_LOG[STR_IDX] if...
    15671529     1. When STR_IDX == MATCH_LAST(the last index in the state_log):
    1568         If `a' isn't the LAST_NODE and `a' can't epsilon transit to
    1569         the LAST_NODE, we throw away the node `a'.
    1570      2. When 0 <= STR_IDX < MATCH_LAST and `a' accepts
    1571         string `s' and transit to `b':
     1530        If 'a' isn't the LAST_NODE and 'a' can't epsilon transit to
     1531        the LAST_NODE, we throw away the node 'a'.
     1532     2. When 0 <= STR_IDX < MATCH_LAST and 'a' accepts
     1533        string 's' and transit to 'b':
    15721534        i. If 'b' isn't in the STATE_LOG[STR_IDX+strlen('s')], we throw
    1573            away the node `a'.
     1535           away the node 'a'.
    15741536        ii. If 'b' is in the STATE_LOG[STR_IDX+strlen('s')] but 'b' is
    1575             thrown away, we throw away the node `a'.
     1537            thrown away, we throw away the node 'a'.
    15761538     3. When 0 <= STR_IDX < MATCH_LAST and 'a' epsilon transit to 'b':
    15771539        i. If 'b' isn't in the STATE_LOG[STR_IDX], we throw away the
    1578            node `a'.
     1540           node 'a'.
    15791541        ii. If 'b' is in the STATE_LOG[STR_IDX] but 'b' is thrown away,
    1580             we throw away the node `a'.  */
     1542            we throw away the node 'a'.  */
    15811543
    15821544#define STATE_NODE_CONTAINS(state,node) \
     
    15841546
    15851547static reg_errcode_t
    1586 internal_function
    15871548sift_states_backward (const re_match_context_t *mctx, re_sift_context_t *sctx)
    15881549{
    15891550  reg_errcode_t err;
    15901551  int null_cnt = 0;
    1591   int str_idx = sctx->last_str_idx;
     1552  Idx str_idx = sctx->last_str_idx;
    15921553  re_node_set cur_dest;
    15931554
    1594 #ifdef DEBUG
    1595   assert (mctx->state_log != NULL && mctx->state_log[str_idx] != NULL);
    1596 #endif
     1555  DEBUG_ASSERT (mctx->state_log != NULL && mctx->state_log[str_idx] != NULL);
    15971556
    15981557  /* Build sifted state_log[str_idx].  It has the nodes which can epsilon
    15991558     transit to the last_node and the last_node itself.  */
    16001559  err = re_node_set_init_1 (&cur_dest, sctx->last_node);
    1601   if (BE (err != REG_NOERROR, 0))
     1560  if (__glibc_unlikely (err != REG_NOERROR))
    16021561    return err;
    16031562  err = update_cur_sifted_state (mctx, sctx, str_idx, &cur_dest);
    1604   if (BE (err != REG_NOERROR, 0))
     1563  if (__glibc_unlikely (err != REG_NOERROR))
    16051564    goto free_return;
    16061565
     
    16231582        {
    16241583          err = build_sifted_states (mctx, sctx, str_idx, &cur_dest);
    1625           if (BE (err != REG_NOERROR, 0))
     1584          if (__glibc_unlikely (err != REG_NOERROR))
    16261585            goto free_return;
    16271586        }
     
    16321591         And update state_log.  */
    16331592      err = update_cur_sifted_state (mctx, sctx, str_idx, &cur_dest);
    1634       if (BE (err != REG_NOERROR, 0))
     1593      if (__glibc_unlikely (err != REG_NOERROR))
    16351594        goto free_return;
    16361595    }
     
    16421601
    16431602static reg_errcode_t
    1644 internal_function
     1603__attribute_warn_unused_result__
    16451604build_sifted_states (const re_match_context_t *mctx, re_sift_context_t *sctx,
    1646                      int str_idx, re_node_set *cur_dest)
     1605                     Idx str_idx, re_node_set *cur_dest)
    16471606{
    16481607  const re_dfa_t *const dfa = mctx->dfa;
    16491608  const re_node_set *cur_src = &mctx->state_log[str_idx]->non_eps_nodes;
    1650   int i;
     1609  Idx i;
    16511610
    16521611  /* Then build the next sifted state.
    1653      We build the next sifted state on `cur_dest', and update
    1654      `sifted_states[str_idx]' with `cur_dest'.
     1612     We build the next sifted state on 'cur_dest', and update
     1613     'sifted_states[str_idx]' with 'cur_dest'.
    16551614     Note:
    1656      `cur_dest' is the sifted state from `state_log[str_idx + 1]'.
    1657      `cur_src' points the node_set of the old `state_log[str_idx]'
     1615     'cur_dest' is the sifted state from 'state_log[str_idx + 1]'.
     1616     'cur_src' points the node_set of the old 'state_log[str_idx]'
    16581617     (with the epsilon nodes pre-filtered out).  */
    16591618  for (i = 0; i < cur_src->nelem; i++)
    16601619    {
    1661       int prev_node = cur_src->elems[i];
     1620      Idx prev_node = cur_src->elems[i];
    16621621      int naccepted = 0;
    1663       int ret;
    1664 
    1665 #ifdef DEBUG
    1666       re_token_type_t type = dfa->nodes[prev_node].type;
    1667       assert (!IS_EPSILON_NODE (type));
    1668 #endif
    1669 #ifdef RE_ENABLE_I18N
    1670       /* If the node may accept `multi byte'.  */
     1622      bool ok;
     1623      DEBUG_ASSERT (!IS_EPSILON_NODE (dfa->nodes[prev_node].type));
     1624
     1625      /* If the node may accept "multi byte".  */
    16711626      if (dfa->nodes[prev_node].accept_mb)
    16721627        naccepted = sift_states_iter_mb (mctx, sctx, prev_node,
    16731628                                         str_idx, sctx->last_str_idx);
    1674 #endif /* RE_ENABLE_I18N */
    16751629
    16761630      /* We don't check backreferences here.
     
    16871641      if (sctx->limits.nelem)
    16881642        {
    1689           int to_idx = str_idx + naccepted;
     1643          Idx to_idx = str_idx + naccepted;
    16901644          if (check_dst_limits (mctx, &sctx->limits,
    16911645                                dfa->nexts[prev_node], to_idx,
     
    16931647            continue;
    16941648        }
    1695       ret = re_node_set_insert (cur_dest, prev_node);
    1696       if (BE (ret == -1, 0))
     1649      ok = re_node_set_insert (cur_dest, prev_node);
     1650      if (__glibc_unlikely (! ok))
    16971651        return REG_ESPACE;
    16981652    }
     
    17041658
    17051659static reg_errcode_t
    1706 internal_function
    1707 clean_state_log_if_needed (re_match_context_t *mctx, int next_state_log_idx)
    1708 {
    1709   int top = mctx->state_log_top;
    1710 
    1711   if (next_state_log_idx >= mctx->input.bufs_len
     1660clean_state_log_if_needed (re_match_context_t *mctx, Idx next_state_log_idx)
     1661{
     1662  Idx top = mctx->state_log_top;
     1663
     1664  if ((next_state_log_idx >= mctx->input.bufs_len
     1665       && mctx->input.bufs_len < mctx->input.len)
    17121666      || (next_state_log_idx >= mctx->input.valid_len
    17131667          && mctx->input.valid_len < mctx->input.len))
    17141668    {
    17151669      reg_errcode_t err;
    1716       err = extend_buffers (mctx);
    1717       if (BE (err != REG_NOERROR, 0))
     1670      err = extend_buffers (mctx, next_state_log_idx + 1);
     1671      if (__glibc_unlikely (err != REG_NOERROR))
    17181672        return err;
    17191673    }
     
    17211675  if (top < next_state_log_idx)
    17221676    {
     1677      DEBUG_ASSERT (mctx->state_log != NULL);
    17231678      memset (mctx->state_log + top + 1, '\0',
    17241679              sizeof (re_dfastate_t *) * (next_state_log_idx - top));
     
    17291684
    17301685static reg_errcode_t
    1731 internal_function
    17321686merge_state_array (const re_dfa_t *dfa, re_dfastate_t **dst,
    1733                    re_dfastate_t **src, int num)
    1734 {
    1735   int st_idx;
     1687                   re_dfastate_t **src, Idx num)
     1688{
     1689  Idx st_idx;
    17361690  reg_errcode_t err;
    17371691  for (st_idx = 0; st_idx < num; ++st_idx)
     
    17441698          err = re_node_set_init_union (&merged_set, &dst[st_idx]->nodes,
    17451699                                        &src[st_idx]->nodes);
    1746           if (BE (err != REG_NOERROR, 0))
     1700          if (__glibc_unlikely (err != REG_NOERROR))
    17471701            return err;
    17481702          dst[st_idx] = re_acquire_state (&err, dfa, &merged_set);
    17491703          re_node_set_free (&merged_set);
    1750           if (BE (err != REG_NOERROR, 0))
     1704          if (__glibc_unlikely (err != REG_NOERROR))
    17511705            return err;
    17521706        }
     
    17561710
    17571711static reg_errcode_t
    1758 internal_function
    17591712update_cur_sifted_state (const re_match_context_t *mctx,
    1760                          re_sift_context_t *sctx, int str_idx,
     1713                         re_sift_context_t *sctx, Idx str_idx,
    17611714                         re_node_set *dest_nodes)
    17621715{
     
    17761729             DEST_NODE.  */
    17771730          err = add_epsilon_src_nodes (dfa, dest_nodes, candidates);
    1778           if (BE (err != REG_NOERROR, 0))
     1731          if (__glibc_unlikely (err != REG_NOERROR))
    17791732            return err;
    17801733
     
    17841737              err = check_subexp_limits (dfa, dest_nodes, candidates, &sctx->limits,
    17851738                                         mctx->bkref_ents, str_idx);
    1786               if (BE (err != REG_NOERROR, 0))
     1739              if (__glibc_unlikely (err != REG_NOERROR))
    17871740                return err;
    17881741            }
     
    17901743
    17911744      sctx->sifted_states[str_idx] = re_acquire_state (&err, dfa, dest_nodes);
    1792       if (BE (err != REG_NOERROR, 0))
     1745      if (__glibc_unlikely (err != REG_NOERROR))
    17931746        return err;
    17941747    }
     
    17971750    {
    17981751      err = sift_states_bkref (mctx, sctx, str_idx, candidates);
    1799       if (BE (err != REG_NOERROR, 0))
     1752      if (__glibc_unlikely (err != REG_NOERROR))
    18001753        return err;
    18011754    }
     
    18041757
    18051758static reg_errcode_t
    1806 internal_function
     1759__attribute_warn_unused_result__
    18071760add_epsilon_src_nodes (const re_dfa_t *dfa, re_node_set *dest_nodes,
    18081761                       const re_node_set *candidates)
    18091762{
    18101763  reg_errcode_t err = REG_NOERROR;
    1811   int i;
     1764  Idx i;
    18121765
    18131766  re_dfastate_t *state = re_acquire_state (&err, dfa, dest_nodes);
    1814   if (BE (err != REG_NOERROR, 0))
     1767  if (__glibc_unlikely (err != REG_NOERROR))
    18151768    return err;
    18161769
     
    18181771    {
    18191772      err = re_node_set_alloc (&state->inveclosure, dest_nodes->nelem);
    1820       if (BE (err != REG_NOERROR, 0))
    1821         return REG_ESPACE;
     1773      if (__glibc_unlikely (err != REG_NOERROR))
     1774        return REG_ESPACE;
    18221775      for (i = 0; i < dest_nodes->nelem; i++)
    1823         re_node_set_merge (&state->inveclosure,
    1824                            dfa->inveclosures + dest_nodes->elems[i]);
     1776        {
     1777          err = re_node_set_merge (&state->inveclosure,
     1778                                   dfa->inveclosures + dest_nodes->elems[i]);
     1779          if (__glibc_unlikely (err != REG_NOERROR))
     1780            return REG_ESPACE;
     1781        }
    18251782    }
    18261783  return re_node_set_add_intersect (dest_nodes, candidates,
     
    18291786
    18301787static reg_errcode_t
    1831 internal_function
    1832 sub_epsilon_src_nodes (const re_dfa_t *dfa, int node, re_node_set *dest_nodes,
     1788sub_epsilon_src_nodes (const re_dfa_t *dfa, Idx node, re_node_set *dest_nodes,
    18331789                       const re_node_set *candidates)
    18341790{
    1835     int ecl_idx;
     1791    Idx ecl_idx;
    18361792    reg_errcode_t err;
    18371793    re_node_set *inv_eclosure = dfa->inveclosures + node;
     
    18401796    for (ecl_idx = 0; ecl_idx < inv_eclosure->nelem; ++ecl_idx)
    18411797      {
    1842         int cur_node = inv_eclosure->elems[ecl_idx];
     1798        Idx cur_node = inv_eclosure->elems[ecl_idx];
    18431799        if (cur_node == node)
    18441800          continue;
    18451801        if (IS_EPSILON_NODE (dfa->nodes[cur_node].type))
    18461802          {
    1847             int edst1 = dfa->edests[cur_node].elems[0];
    1848             int edst2 = ((dfa->edests[cur_node].nelem > 1)
     1803            Idx edst1 = dfa->edests[cur_node].elems[0];
     1804            Idx edst2 = ((dfa->edests[cur_node].nelem > 1)
    18491805                         ? dfa->edests[cur_node].elems[1] : -1);
    18501806            if ((!re_node_set_contains (inv_eclosure, edst1)
     
    18561812                err = re_node_set_add_intersect (&except_nodes, candidates,
    18571813                                                 dfa->inveclosures + cur_node);
    1858                 if (BE (err != REG_NOERROR, 0))
     1814                if (__glibc_unlikely (err != REG_NOERROR))
    18591815                  {
    18601816                    re_node_set_free (&except_nodes);
     
    18661822    for (ecl_idx = 0; ecl_idx < inv_eclosure->nelem; ++ecl_idx)
    18671823      {
    1868         int cur_node = inv_eclosure->elems[ecl_idx];
     1824        Idx cur_node = inv_eclosure->elems[ecl_idx];
    18691825        if (!re_node_set_contains (&except_nodes, cur_node))
    18701826          {
    1871             int idx = re_node_set_contains (dest_nodes, cur_node) - 1;
     1827            Idx idx = re_node_set_contains (dest_nodes, cur_node) - 1;
    18721828            re_node_set_remove_at (dest_nodes, idx);
    18731829          }
     
    18771833}
    18781834
    1879 static int
    1880 internal_function
    1881 check_dst_limits (const re_match_context_t *mctx, re_node_set *limits,
    1882                   int dst_node, int dst_idx, int src_node, int src_idx)
     1835static bool
     1836check_dst_limits (const re_match_context_t *mctx, const re_node_set *limits,
     1837                  Idx dst_node, Idx dst_idx, Idx src_node, Idx src_idx)
    18831838{
    18841839  const re_dfa_t *const dfa = mctx->dfa;
    1885   int lim_idx, src_pos, dst_pos;
    1886 
    1887   int dst_bkref_idx = search_cur_bkref_entry (mctx, dst_idx);
    1888   int src_bkref_idx = search_cur_bkref_entry (mctx, src_idx);
     1840  Idx lim_idx, src_pos, dst_pos;
     1841
     1842  Idx dst_bkref_idx = search_cur_bkref_entry (mctx, dst_idx);
     1843  Idx src_bkref_idx = search_cur_bkref_entry (mctx, src_idx);
    18891844  for (lim_idx = 0; lim_idx < limits->nelem; ++lim_idx)
    18901845    {
    1891       int subexp_idx;
     1846      Idx subexp_idx;
    18921847      struct re_backref_cache_entry *ent;
    18931848      ent = mctx->bkref_ents + limits->elems[lim_idx];
     
    19081863        continue; /* This is unrelated limitation.  */
    19091864      else
    1910         return 1;
    1911     }
    1912   return 0;
     1865        return true;
     1866    }
     1867  return false;
    19131868}
    19141869
    19151870static int
    1916 internal_function
    19171871check_dst_limits_calc_pos_1 (const re_match_context_t *mctx, int boundaries,
    1918                              int subexp_idx, int from_node, int bkref_idx)
     1872                             Idx subexp_idx, Idx from_node, Idx bkref_idx)
    19191873{
    19201874  const re_dfa_t *const dfa = mctx->dfa;
    19211875  const re_node_set *eclosures = dfa->eclosures + from_node;
    1922   int node_idx;
     1876  Idx node_idx;
    19231877
    19241878  /* Else, we are on the boundary: examine the nodes on the epsilon
     
    19261880  for (node_idx = 0; node_idx < eclosures->nelem; ++node_idx)
    19271881    {
    1928       int node = eclosures->elems[node_idx];
     1882      Idx node = eclosures->elems[node_idx];
    19291883      switch (dfa->nodes[node].type)
    19301884        {
     
    19341888              struct re_backref_cache_entry *ent = mctx->bkref_ents + bkref_idx;
    19351889              do
    1936                 {
    1937                   int dst, cpos;
     1890                {
     1891                  Idx dst;
     1892                  int cpos;
    19381893
    19391894                  if (ent->node != node)
     
    19551910                    {
    19561911                      if (boundaries & 1)
    1957                         return -1;
     1912                        return -1;
    19581913                      else /* if (boundaries & 2) */
    1959                         return 0;
     1914                        return 0;
    19601915                    }
    19611916
     
    19711926                    ent->eps_reachable_subexps_map
    19721927                      &= ~((bitset_word_t) 1 << subexp_idx);
    1973                 }
     1928                }
    19741929              while (ent++->more);
    19751930            }
     
    19951950
    19961951static int
    1997 internal_function
    1998 check_dst_limits_calc_pos (const re_match_context_t *mctx, int limit,
    1999                            int subexp_idx, int from_node, int str_idx,
    2000                            int bkref_idx)
     1952check_dst_limits_calc_pos (const re_match_context_t *mctx, Idx limit,
     1953                           Idx subexp_idx, Idx from_node, Idx str_idx,
     1954                           Idx bkref_idx)
    20011955{
    20021956  struct re_backref_cache_entry *lim = mctx->bkref_ents + limit;
     
    20251979
    20261980static reg_errcode_t
    2027 internal_function
    20281981check_subexp_limits (const re_dfa_t *dfa, re_node_set *dest_nodes,
    20291982                     const re_node_set *candidates, re_node_set *limits,
    2030                      struct re_backref_cache_entry *bkref_ents, int str_idx)
     1983                     struct re_backref_cache_entry *bkref_ents, Idx str_idx)
    20311984{
    20321985  reg_errcode_t err;
    2033   int node_idx, lim_idx;
     1986  Idx node_idx, lim_idx;
    20341987
    20351988  for (lim_idx = 0; lim_idx < limits->nelem; ++lim_idx)
    20361989    {
    2037       int subexp_idx;
     1990      Idx subexp_idx;
    20381991      struct re_backref_cache_entry *ent;
    20391992      ent = bkref_ents + limits->elems[lim_idx];
     
    20451998      if (ent->subexp_to == str_idx)
    20461999        {
    2047           int ops_node = -1;
    2048           int cls_node = -1;
     2000          Idx ops_node = -1;
     2001          Idx cls_node = -1;
    20492002          for (node_idx = 0; node_idx < dest_nodes->nelem; ++node_idx)
    20502003            {
    2051               int node = dest_nodes->elems[node_idx];
     2004              Idx node = dest_nodes->elems[node_idx];
    20522005              re_token_type_t type = dfa->nodes[node].type;
    20532006              if (type == OP_OPEN_SUBEXP
     
    20652018              err = sub_epsilon_src_nodes (dfa, ops_node, dest_nodes,
    20662019                                           candidates);
    2067               if (BE (err != REG_NOERROR, 0))
     2020              if (__glibc_unlikely (err != REG_NOERROR))
    20682021                return err;
    20692022            }
     
    20732026            for (node_idx = 0; node_idx < dest_nodes->nelem; ++node_idx)
    20742027              {
    2075                 int node = dest_nodes->elems[node_idx];
     2028                Idx node = dest_nodes->elems[node_idx];
    20762029                if (!re_node_set_contains (dfa->inveclosures + node,
    20772030                                           cls_node)
     
    20832036                    err = sub_epsilon_src_nodes (dfa, node, dest_nodes,
    20842037                                                 candidates);
    2085                     if (BE (err != REG_NOERROR, 0))
     2038                    if (__glibc_unlikely (err != REG_NOERROR))
    20862039                      return err;
    20872040                    --node_idx;
     
    20932046          for (node_idx = 0; node_idx < dest_nodes->nelem; ++node_idx)
    20942047            {
    2095               int node = dest_nodes->elems[node_idx];
     2048              Idx node = dest_nodes->elems[node_idx];
    20962049              re_token_type_t type = dfa->nodes[node].type;
    20972050              if (type == OP_CLOSE_SUBEXP || type == OP_OPEN_SUBEXP)
     
    21032056                  err = sub_epsilon_src_nodes (dfa, node, dest_nodes,
    21042057                                               candidates);
    2105                   if (BE (err != REG_NOERROR, 0))
     2058                  if (__glibc_unlikely (err != REG_NOERROR))
    21062059                    return err;
    21072060                }
     
    21132066
    21142067static reg_errcode_t
    2115 internal_function
     2068__attribute_warn_unused_result__
    21162069sift_states_bkref (const re_match_context_t *mctx, re_sift_context_t *sctx,
    2117                    int str_idx, const re_node_set *candidates)
     2070                   Idx str_idx, const re_node_set *candidates)
    21182071{
    21192072  const re_dfa_t *const dfa = mctx->dfa;
    21202073  reg_errcode_t err;
    2121   int node_idx, node;
     2074  Idx node_idx, node;
    21222075  re_sift_context_t local_sctx;
    2123   int first_idx = search_cur_bkref_entry (mctx, str_idx);
     2076  Idx first_idx = search_cur_bkref_entry (mctx, str_idx);
    21242077
    21252078  if (first_idx == -1)
     
    21302083  for (node_idx = 0; node_idx < candidates->nelem; ++node_idx)
    21312084    {
    2132       int enabled_idx;
     2085      Idx enabled_idx;
    21332086      re_token_type_t type;
    21342087      struct re_backref_cache_entry *entry;
     
    21452098      do
    21462099        {
    2147           int subexp_len;
    2148           int to_idx;
    2149           int dst_node;
    2150           int ret;
     2100          Idx subexp_len;
     2101          Idx to_idx;
     2102          Idx dst_node;
     2103          bool ok;
    21512104          re_dfastate_t *cur_state;
    21522105
     
    21692122              local_sctx = *sctx;
    21702123              err = re_node_set_init_copy (&local_sctx.limits, &sctx->limits);
    2171               if (BE (err != REG_NOERROR, 0))
     2124              if (__glibc_unlikely (err != REG_NOERROR))
    21722125                goto free_return;
    21732126            }
    21742127          local_sctx.last_node = node;
    21752128          local_sctx.last_str_idx = str_idx;
    2176           ret = re_node_set_insert (&local_sctx.limits, enabled_idx);
    2177           if (BE (ret < 0, 0))
     2129          ok = re_node_set_insert (&local_sctx.limits, enabled_idx);
     2130          if (__glibc_unlikely (! ok))
    21782131            {
    21792132              err = REG_ESPACE;
     
    21822135          cur_state = local_sctx.sifted_states[str_idx];
    21832136          err = sift_states_backward (mctx, &local_sctx);
    2184           if (BE (err != REG_NOERROR, 0))
     2137          if (__glibc_unlikely (err != REG_NOERROR))
    21852138            goto free_return;
    21862139          if (sctx->limited_states != NULL)
     
    21892142                                       local_sctx.sifted_states,
    21902143                                       str_idx + 1);
    2191               if (BE (err != REG_NOERROR, 0))
     2144              if (__glibc_unlikely (err != REG_NOERROR))
    21922145                goto free_return;
    21932146            }
     
    21962149
    21972150          /* mctx->bkref_ents may have changed, reload the pointer.  */
    2198           entry = mctx->bkref_ents + enabled_idx;
     2151          entry = mctx->bkref_ents + enabled_idx;
    21992152        }
    22002153      while (enabled_idx++, entry++->more);
     
    22112164
    22122165
    2213 #ifdef RE_ENABLE_I18N
    22142166static int
    2215 internal_function
    22162167sift_states_iter_mb (const re_match_context_t *mctx, re_sift_context_t *sctx,
    2217                      int node_idx, int str_idx, int max_str_idx)
     2168                     Idx node_idx, Idx str_idx, Idx max_str_idx)
    22182169{
    22192170  const re_dfa_t *const dfa = mctx->dfa;
    22202171  int naccepted;
    2221   /* Check the node can accept `multi byte'.  */
     2172  /* Check the node can accept "multi byte".  */
    22222173  naccepted = check_node_accept_bytes (dfa, node_idx, &mctx->input, str_idx);
    2223   if (naccepted > 0 && str_idx + naccepted <= max_str_idx &&
    2224       !STATE_NODE_CONTAINS (sctx->sifted_states[str_idx + naccepted],
    2225                             dfa->nexts[node_idx]))
    2226     /* The node can't accept the `multi byte', or the
     2174  if (naccepted > 0 && str_idx + naccepted <= max_str_idx
     2175      && !STATE_NODE_CONTAINS (sctx->sifted_states[str_idx + naccepted],
     2176                               dfa->nexts[node_idx]))
     2177    /* The node can't accept the "multi byte", or the
    22272178       destination was already thrown away, then the node
    2228        could't accept the current input `multi byte'.   */
     2179       couldn't accept the current input "multi byte".   */
    22292180    naccepted = 0;
    22302181  /* Otherwise, it is sure that the node could accept
    2231      `naccepted' bytes input.  */
     2182     'naccepted' bytes input.  */
    22322183  return naccepted;
    22332184}
    2234 #endif /* RE_ENABLE_I18N */
    2235 
    22362185
    22372186
     
    22402189/* Return the next state to which the current state STATE will transit by
    22412190   accepting the current input byte, and update STATE_LOG if necessary.
     2191   Return NULL on failure.
    22422192   If STATE can accept a multibyte char/collating element/back reference
    22432193   update the destination of STATE_LOG.  */
    22442194
    22452195static re_dfastate_t *
    2246 internal_function
     2196__attribute_warn_unused_result__
    22472197transit_state (reg_errcode_t *err, re_match_context_t *mctx,
    22482198               re_dfastate_t *state)
     
    22512201  unsigned char ch;
    22522202
    2253 #ifdef RE_ENABLE_I18N
    22542203  /* If the current state can accept multibyte.  */
    2255   if (BE (state->accept_mb, 0))
     2204  if (__glibc_unlikely (state->accept_mb))
    22562205    {
    22572206      *err = transit_state_mb (mctx, state);
    2258       if (BE (*err != REG_NOERROR, 0))
     2207      if (__glibc_unlikely (*err != REG_NOERROR))
    22592208        return NULL;
    22602209    }
    2261 #endif /* RE_ENABLE_I18N */
    22622210
    22632211  /* Then decide the next state with the single byte.  */
     
    22732221    {
    22742222      trtable = state->trtable;
    2275       if (BE (trtable != NULL, 1))
     2223      if (__glibc_likely (trtable != NULL))
    22762224        return trtable[ch];
    22772225
    22782226      trtable = state->word_trtable;
    2279       if (BE (trtable != NULL, 1))
    2280         {
     2227      if (__glibc_likely (trtable != NULL))
     2228        {
    22812229          unsigned int context;
    22822230          context
     
    23012249
    23022250/* Update the state_log if we need */
    2303 re_dfastate_t *
    2304 internal_function
     2251static re_dfastate_t *
    23052252merge_state_with_log (reg_errcode_t *err, re_match_context_t *mctx,
    23062253                      re_dfastate_t *next_state)
    23072254{
    23082255  const re_dfa_t *const dfa = mctx->dfa;
    2309   int cur_idx = re_string_cur_idx (&mctx->input);
     2256  Idx cur_idx = re_string_cur_idx (&mctx->input);
    23102257
    23112258  if (cur_idx > mctx->state_log_top)
     
    23242271      re_node_set next_nodes, *log_nodes, *table_nodes = NULL;
    23252272      /* If (state_log[cur_idx] != 0), it implies that cur_idx is
    2326         the destination of a multibyte char/collating element/
    2327         back reference.  Then the next state is the union set of
    2328         these destinations and the results of the transition table.  */
     2273        the destination of a multibyte char/collating element/
     2274        back reference.  Then the next state is the union set of
     2275        these destinations and the results of the transition table.  */
    23292276      pstate = mctx->state_log[cur_idx];
    23302277      log_nodes = pstate->entrance_nodes;
    23312278      if (next_state != NULL)
    2332         {
    2333           table_nodes = next_state->entrance_nodes;
    2334           *err = re_node_set_init_union (&next_nodes, table_nodes,
     2279        {
     2280          table_nodes = next_state->entrance_nodes;
     2281          *err = re_node_set_init_union (&next_nodes, table_nodes,
    23352282                                             log_nodes);
    2336           if (BE (*err != REG_NOERROR, 0))
     2283          if (__glibc_unlikely (*err != REG_NOERROR))
    23372284            return NULL;
    2338         }
     2285        }
    23392286      else
    2340         next_nodes = *log_nodes;
     2287        next_nodes = *log_nodes;
    23412288      /* Note: We already add the nodes of the initial state,
    23422289         then we don't need to add them here.  */
     
    23462293                                      mctx->eflags);
    23472294      next_state = mctx->state_log[cur_idx]
    2348         = re_acquire_state_context (err, dfa, &next_nodes, context);
     2295        = re_acquire_state_context (err, dfa, &next_nodes, context);
    23492296      /* We don't need to check errors here, since the return value of
    2350         this function is next_state and ERR is already set.  */
     2297        this function is next_state and ERR is already set.  */
    23512298
    23522299      if (table_nodes != NULL)
    2353         re_node_set_free (&next_nodes);
    2354     }
    2355 
    2356   if (BE (dfa->nbackref, 0) && next_state != NULL)
     2300        re_node_set_free (&next_nodes);
     2301    }
     2302
     2303  if (__glibc_unlikely (dfa->nbackref) && next_state != NULL)
    23572304    {
    23582305      /* Check OP_OPEN_SUBEXP in the current state in case that we use them
     
    23612308      *err = check_subexp_matching_top (mctx, &next_state->nodes,
    23622309                                        cur_idx);
    2363       if (BE (*err != REG_NOERROR, 0))
     2310      if (__glibc_unlikely (*err != REG_NOERROR))
    23642311        return NULL;
    23652312
     
    23682315        {
    23692316          *err = transit_state_bkref (mctx, &next_state->nodes);
    2370           if (BE (*err != REG_NOERROR, 0))
     2317          if (__glibc_unlikely (*err != REG_NOERROR))
    23712318            return NULL;
    23722319          next_state = mctx->state_log[cur_idx];
     
    23802327   multi-byte match, then look in the log for a state
    23812328   from which to restart matching.  */
    2382 re_dfastate_t *
    2383 internal_function
     2329static re_dfastate_t *
    23842330find_recover_state (reg_errcode_t *err, re_match_context_t *mctx)
    23852331{
     
    23872333  do
    23882334    {
    2389       int max = mctx->state_log_top;
    2390       int cur_str_idx = re_string_cur_idx (&mctx->input);
     2335      Idx max = mctx->state_log_top;
     2336      Idx cur_str_idx = re_string_cur_idx (&mctx->input);
    23912337
    23922338      do
    23932339        {
    2394           if (++cur_str_idx > max)
    2395             return NULL;
    2396           re_string_skip_bytes (&mctx->input, 1);
     2340          if (++cur_str_idx > max)
     2341            return NULL;
     2342          re_string_skip_bytes (&mctx->input, 1);
    23972343        }
    23982344      while (mctx->state_log[cur_str_idx] == NULL);
     
    24092355   OP_OPEN_SUBEXP and which have corresponding back references in the regular
    24102356   expression. And register them to use them later for evaluating the
    2411    correspoding back references.  */
     2357   corresponding back references.  */
    24122358
    24132359static reg_errcode_t
    2414 internal_function
    24152360check_subexp_matching_top (re_match_context_t *mctx, re_node_set *cur_nodes,
    2416                            int str_idx)
     2361                           Idx str_idx)
    24172362{
    24182363  const re_dfa_t *const dfa = mctx->dfa;
    2419   int node_idx;
     2364  Idx node_idx;
    24202365  reg_errcode_t err;
    24212366
     
    24272372  for (node_idx = 0; node_idx < cur_nodes->nelem; ++node_idx)
    24282373    {
    2429       int node = cur_nodes->elems[node_idx];
     2374      Idx node = cur_nodes->elems[node_idx];
    24302375      if (dfa->nodes[node].type == OP_OPEN_SUBEXP
    24312376          && dfa->nodes[node].opr.idx < BITSET_WORD_BITS
     
    24342379        {
    24352380          err = match_ctx_add_subtop (mctx, node, str_idx);
    2436           if (BE (err != REG_NOERROR, 0))
     2381          if (__glibc_unlikely (err != REG_NOERROR))
    24372382            return err;
    24382383        }
     
    24432388#if 0
    24442389/* Return the next state to which the current state STATE will transit by
    2445    accepting the current input byte.  */
     2390   accepting the current input byte.  Return NULL on failure.  */
    24462391
    24472392static re_dfastate_t *
     
    24522397  re_node_set next_nodes;
    24532398  re_dfastate_t *next_state;
    2454   int node_cnt, cur_str_idx = re_string_cur_idx (&mctx->input);
     2399  Idx node_cnt, cur_str_idx = re_string_cur_idx (&mctx->input);
    24552400  unsigned int context;
    24562401
    24572402  *err = re_node_set_alloc (&next_nodes, state->nodes.nelem + 1);
    2458   if (BE (*err != REG_NOERROR, 0))
     2403  if (__glibc_unlikely (*err != REG_NOERROR))
    24592404    return NULL;
    24602405  for (node_cnt = 0; node_cnt < state->nodes.nelem; ++node_cnt)
    24612406    {
    2462       int cur_node = state->nodes.elems[node_cnt];
     2407      Idx cur_node = state->nodes.elems[node_cnt];
    24632408      if (check_node_accept (mctx, dfa->nodes + cur_node, cur_str_idx))
    24642409        {
    24652410          *err = re_node_set_merge (&next_nodes,
    24662411                                    dfa->eclosures + dfa->nexts[cur_node]);
    2467           if (BE (*err != REG_NOERROR, 0))
     2412          if (__glibc_unlikely (*err != REG_NOERROR))
    24682413            {
    24692414              re_node_set_free (&next_nodes);
     
    24832428#endif
    24842429
    2485 #ifdef RE_ENABLE_I18N
    24862430static reg_errcode_t
    2487 internal_function
    24882431transit_state_mb (re_match_context_t *mctx, re_dfastate_t *pstate)
    24892432{
    24902433  const re_dfa_t *const dfa = mctx->dfa;
    24912434  reg_errcode_t err;
    2492   int i;
     2435  Idx i;
    24932436
    24942437  for (i = 0; i < pstate->nodes.nelem; ++i)
    24952438    {
    24962439      re_node_set dest_nodes, *new_nodes;
    2497       int cur_node_idx = pstate->nodes.elems[i];
    2498       int naccepted, dest_idx;
     2440      Idx cur_node_idx = pstate->nodes.elems[i];
     2441      int naccepted;
     2442      Idx dest_idx;
    24992443      unsigned int context;
    25002444      re_dfastate_t *dest_state;
    25012445
    25022446      if (!dfa->nodes[cur_node_idx].accept_mb)
    2503         continue;
     2447        continue;
    25042448
    25052449      if (dfa->nodes[cur_node_idx].constraint)
     
    25192463        continue;
    25202464
    2521       /* The node can accepts `naccepted' bytes.  */
     2465      /* The node can accepts 'naccepted' bytes.  */
    25222466      dest_idx = re_string_cur_idx (&mctx->input) + naccepted;
    25232467      mctx->max_mb_elem_len = ((mctx->max_mb_elem_len < naccepted) ? naccepted
    25242468                               : mctx->max_mb_elem_len);
    25252469      err = clean_state_log_if_needed (mctx, dest_idx);
    2526       if (BE (err != REG_NOERROR, 0))
     2470      if (__glibc_unlikely (err != REG_NOERROR))
    25272471        return err;
    2528 #ifdef DEBUG
    2529       assert (dfa->nexts[cur_node_idx] != -1);
    2530 #endif
     2472      DEBUG_ASSERT (dfa->nexts[cur_node_idx] != -1);
    25312473      new_nodes = dfa->eclosures + dfa->nexts[cur_node_idx];
    25322474
     
    25382480          err = re_node_set_init_union (&dest_nodes,
    25392481                                        dest_state->entrance_nodes, new_nodes);
    2540           if (BE (err != REG_NOERROR, 0))
     2482          if (__glibc_unlikely (err != REG_NOERROR))
    25412483            return err;
    25422484        }
     
    25472489      if (dest_state != NULL)
    25482490        re_node_set_free (&dest_nodes);
    2549       if (BE (mctx->state_log[dest_idx] == NULL && err != REG_NOERROR, 0))
     2491      if (__glibc_unlikely (mctx->state_log[dest_idx] == NULL
     2492                            && err != REG_NOERROR))
    25502493        return err;
    25512494    }
    25522495  return REG_NOERROR;
    25532496}
    2554 #endif /* RE_ENABLE_I18N */
    25552497
    25562498static reg_errcode_t
    2557 internal_function
    25582499transit_state_bkref (re_match_context_t *mctx, const re_node_set *nodes)
    25592500{
    25602501  const re_dfa_t *const dfa = mctx->dfa;
    25612502  reg_errcode_t err;
    2562   int i;
    2563   int cur_str_idx = re_string_cur_idx (&mctx->input);
     2503  Idx i;
     2504  Idx cur_str_idx = re_string_cur_idx (&mctx->input);
    25642505
    25652506  for (i = 0; i < nodes->nelem; ++i)
    25662507    {
    2567       int dest_str_idx, prev_nelem, bkc_idx;
    2568       int node_idx = nodes->elems[i];
     2508      Idx dest_str_idx, prev_nelem, bkc_idx;
     2509      Idx node_idx = nodes->elems[i];
    25692510      unsigned int context;
    25702511      const re_token_t *node = dfa->nodes + node_idx;
    25712512      re_node_set *new_dest_nodes;
    25722513
    2573       /* Check whether `node' is a backreference or not.  */
     2514      /* Check whether 'node' is a backreference or not.  */
    25742515      if (node->type != OP_BACK_REF)
    25752516        continue;
     
    25832524        }
    25842525
    2585       /* `node' is a backreference.
     2526      /* 'node' is a backreference.
    25862527         Check the substring which the substring matched.  */
    25872528      bkc_idx = mctx->nbkref_ents;
    25882529      err = get_subexp (mctx, node_idx, cur_str_idx);
    2589       if (BE (err != REG_NOERROR, 0))
     2530      if (__glibc_unlikely (err != REG_NOERROR))
    25902531        goto free_return;
    25912532
    2592       /* And add the epsilon closures (which is `new_dest_nodes') of
     2533      /* And add the epsilon closures (which is 'new_dest_nodes') of
    25932534         the backreference to appropriate state_log.  */
    2594 #ifdef DEBUG
    2595       assert (dfa->nexts[node_idx] != -1);
    2596 #endif
     2535      DEBUG_ASSERT (dfa->nexts[node_idx] != -1);
    25972536      for (; bkc_idx < mctx->nbkref_ents; ++bkc_idx)
    25982537        {
    2599           int subexp_len;
     2538          Idx subexp_len;
    26002539          re_dfastate_t *dest_state;
    26012540          struct re_backref_cache_entry *bkref_ent;
     
    26142553          prev_nelem = ((mctx->state_log[cur_str_idx] == NULL) ? 0
    26152554                        : mctx->state_log[cur_str_idx]->nodes.nelem);
    2616           /* Add `new_dest_node' to state_log.  */
     2555          /* Add 'new_dest_node' to state_log.  */
    26172556          if (dest_state == NULL)
    26182557            {
     
    26202559                = re_acquire_state_context (&err, dfa, new_dest_nodes,
    26212560                                            context);
    2622               if (BE (mctx->state_log[dest_str_idx] == NULL
    2623                       && err != REG_NOERROR, 0))
     2561              if (__glibc_unlikely (mctx->state_log[dest_str_idx] == NULL
     2562                                    && err != REG_NOERROR))
    26242563                goto free_return;
    26252564            }
     
    26302569                                            dest_state->entrance_nodes,
    26312570                                            new_dest_nodes);
    2632               if (BE (err != REG_NOERROR, 0))
     2571              if (__glibc_unlikely (err != REG_NOERROR))
    26332572                {
    26342573                  re_node_set_free (&dest_nodes);
     
    26382577                = re_acquire_state_context (&err, dfa, &dest_nodes, context);
    26392578              re_node_set_free (&dest_nodes);
    2640               if (BE (mctx->state_log[dest_str_idx] == NULL
    2641                       && err != REG_NOERROR, 0))
     2579              if (__glibc_unlikely (mctx->state_log[dest_str_idx] == NULL
     2580                                    && err != REG_NOERROR))
    26422581                goto free_return;
    26432582            }
     
    26492588              err = check_subexp_matching_top (mctx, new_dest_nodes,
    26502589                                               cur_str_idx);
    2651               if (BE (err != REG_NOERROR, 0))
     2590              if (__glibc_unlikely (err != REG_NOERROR))
    26522591                goto free_return;
    26532592              err = transit_state_bkref (mctx, new_dest_nodes);
    2654               if (BE (err != REG_NOERROR, 0))
     2593              if (__glibc_unlikely (err != REG_NOERROR))
    26552594                goto free_return;
    26562595            }
     
    26692608
    26702609static reg_errcode_t
    2671 internal_function
    2672 get_subexp (re_match_context_t *mctx, int bkref_node, int bkref_str_idx)
     2610__attribute_warn_unused_result__
     2611get_subexp (re_match_context_t *mctx, Idx bkref_node, Idx bkref_str_idx)
    26732612{
    26742613  const re_dfa_t *const dfa = mctx->dfa;
    2675   int subexp_num, sub_top_idx;
     2614  Idx subexp_num, sub_top_idx;
    26762615  const char *buf = (const char *) re_string_get_buffer (&mctx->input);
    26772616  /* Return if we have already checked BKREF_NODE at BKREF_STR_IDX.  */
    2678   int cache_idx = search_cur_bkref_entry (mctx, bkref_str_idx);
     2617  Idx cache_idx = search_cur_bkref_entry (mctx, bkref_str_idx);
    26792618  if (cache_idx != -1)
    26802619    {
     
    26822621        = mctx->bkref_ents + cache_idx;
    26832622      do
    2684         if (entry->node == bkref_node)
     2623        if (entry->node == bkref_node)
    26852624          return REG_NOERROR; /* We already checked it.  */
    26862625      while (entry++->more);
     
    26952634      re_sub_match_top_t *sub_top = mctx->sub_tops[sub_top_idx];
    26962635      re_sub_match_last_t *sub_last;
    2697       int sub_last_idx, sl_str, bkref_str_off;
     2636      Idx sub_last_idx, sl_str, bkref_str_off;
    26982637
    26992638      if (dfa->nodes[sub_top->node].opr.idx != subexp_num)
     
    27062645      for (sub_last_idx = 0; sub_last_idx < sub_top->nlasts; ++sub_last_idx)
    27072646        {
    2708           int sl_str_diff;
     2647          regoff_t sl_str_diff;
    27092648          sub_last = sub_top->lasts[sub_last_idx];
    27102649          sl_str_diff = sub_last->str_idx - sl_str;
     
    27132652          if (sl_str_diff > 0)
    27142653            {
    2715               if (BE (bkref_str_off + sl_str_diff > mctx->input.valid_len, 0))
     2654              if (__glibc_unlikely (bkref_str_off + sl_str_diff
     2655                                    > mctx->input.valid_len))
    27162656                {
    27172657                  /* Not enough chars for a successful match.  */
     
    27222662                                                   bkref_str_off
    27232663                                                   + sl_str_diff);
    2724                   if (BE (err != REG_NOERROR, 0))
     2664                  if (__glibc_unlikely (err != REG_NOERROR))
    27252665                    return err;
    27262666                  buf = (const char *) re_string_get_buffer (&mctx->input);
     
    27412681          if (err == REG_NOMATCH)
    27422682            continue;
    2743           if (BE (err != REG_NOERROR, 0))
     2683          if (__glibc_unlikely (err != REG_NOERROR))
    27442684            return err;
    27452685        }
     
    27522692      for (; sl_str <= bkref_str_idx; ++sl_str)
    27532693        {
    2754           int cls_node, sl_str_off;
     2694          Idx cls_node;
     2695          regoff_t sl_str_off;
    27552696          const re_node_set *nodes;
    27562697          sl_str_off = sl_str - sub_top->str_idx;
     
    27592700          if (sl_str_off > 0)
    27602701            {
    2761               if (BE (bkref_str_off >= mctx->input.valid_len, 0))
     2702              if (__glibc_unlikely (bkref_str_off >= mctx->input.valid_len))
    27622703                {
    27632704                  /* If we are at the end of the input, we cannot match.  */
     
    27652706                    break;
    27662707
    2767                   err = extend_buffers (mctx);
    2768                   if (BE (err != REG_NOERROR, 0))
     2708                  err = extend_buffers (mctx, bkref_str_off + 1);
     2709                  if (__glibc_unlikely (err != REG_NOERROR))
    27692710                    return err;
    27702711
     
    27972738          if (err == REG_NOMATCH)
    27982739              continue;
    2799           if (BE (err != REG_NOERROR, 0))
     2740          if (__glibc_unlikely (err != REG_NOERROR))
    28002741              return err;
    28012742          sub_last = match_ctx_add_sublast (sub_top, cls_node, sl_str);
    2802           if (BE (sub_last == NULL, 0))
     2743          if (__glibc_unlikely (sub_last == NULL))
    28032744            return REG_ESPACE;
    28042745          err = get_subexp_sub (mctx, sub_top, sub_last, bkref_node,
    28052746                                bkref_str_idx);
     2747          buf = (const char *) re_string_get_buffer (&mctx->input);
    28062748          if (err == REG_NOMATCH)
    28072749            continue;
     2750          if (__glibc_unlikely (err != REG_NOERROR))
     2751            return err;
    28082752        }
    28092753    }
     
    28182762
    28192763static reg_errcode_t
    2820 internal_function
    28212764get_subexp_sub (re_match_context_t *mctx, const re_sub_match_top_t *sub_top,
    2822                 re_sub_match_last_t *sub_last, int bkref_node, int bkref_str)
     2765                re_sub_match_last_t *sub_last, Idx bkref_node, Idx bkref_str)
    28232766{
    28242767  reg_errcode_t err;
    2825   int to_idx;
     2768  Idx to_idx;
    28262769  /* Can the subexpression arrive the back reference?  */
    28272770  err = check_arrival (mctx, &sub_last->path, sub_last->node,
     
    28322775  err = match_ctx_add_entry (mctx, bkref_node, bkref_str, sub_top->str_idx,
    28332776                             sub_last->str_idx);
    2834   if (BE (err != REG_NOERROR, 0))
     2777  if (__glibc_unlikely (err != REG_NOERROR))
    28352778    return err;
    28362779  to_idx = bkref_str + sub_last->str_idx - sub_top->str_idx;
     
    28462789         E.g. RE: (a){2}  */
    28472790
    2848 static int
    2849 internal_function
     2791static Idx
    28502792find_subexp_node (const re_dfa_t *dfa, const re_node_set *nodes,
    2851                   int subexp_idx, int type)
    2852 {
    2853   int cls_idx;
     2793                  Idx subexp_idx, int type)
     2794{
     2795  Idx cls_idx;
    28542796  for (cls_idx = 0; cls_idx < nodes->nelem; ++cls_idx)
    28552797    {
    2856       int cls_node = nodes->elems[cls_idx];
     2798      Idx cls_node = nodes->elems[cls_idx];
    28572799      const re_token_t *node = dfa->nodes + cls_node;
    28582800      if (node->type == type
     
    28662808   LAST_NODE at LAST_STR.  We record the path onto PATH since it will be
    28672809   heavily reused.
    2868    Return REG_NOERROR if it can arrive, or REG_NOMATCH otherwise.  */
     2810   Return REG_NOERROR if it can arrive, REG_NOMATCH if it cannot,
     2811   REG_ESPACE if memory is exhausted.  */
    28692812
    28702813static reg_errcode_t
    2871 internal_function
    2872 check_arrival (re_match_context_t *mctx, state_array_t *path, int top_node,
    2873                int top_str, int last_node, int last_str, int type)
     2814__attribute_warn_unused_result__
     2815check_arrival (re_match_context_t *mctx, state_array_t *path, Idx top_node,
     2816               Idx top_str, Idx last_node, Idx last_str, int type)
    28742817{
    28752818  const re_dfa_t *const dfa = mctx->dfa;
    28762819  reg_errcode_t err = REG_NOERROR;
    2877   int subexp_num, backup_cur_idx, str_idx, null_cnt;
     2820  Idx subexp_num, backup_cur_idx, str_idx, null_cnt;
    28782821  re_dfastate_t *cur_state = NULL;
    28792822  re_node_set *cur_nodes, next_nodes;
     
    28832826  subexp_num = dfa->nodes[top_node].opr.idx;
    28842827  /* Extend the buffer if we need.  */
    2885   if (BE (path->alloc < last_str + mctx->max_mb_elem_len + 1, 0))
     2828  if (__glibc_unlikely (path->alloc < last_str + mctx->max_mb_elem_len + 1))
    28862829    {
    28872830      re_dfastate_t **new_array;
    2888       int old_alloc = path->alloc;
    2889       path->alloc += last_str + mctx->max_mb_elem_len + 1;
    2890       new_array = re_realloc (path->array, re_dfastate_t *, path->alloc);
    2891       if (BE (new_array == NULL, 0))
    2892         {
    2893           path->alloc = old_alloc;
    2894           return REG_ESPACE;
    2895         }
     2831      Idx old_alloc = path->alloc;
     2832      Idx incr_alloc = last_str + mctx->max_mb_elem_len + 1;
     2833      Idx new_alloc;
     2834      if (__glibc_unlikely (IDX_MAX - old_alloc < incr_alloc))
     2835        return REG_ESPACE;
     2836      new_alloc = old_alloc + incr_alloc;
     2837      if (__glibc_unlikely (SIZE_MAX / sizeof (re_dfastate_t *) < new_alloc))
     2838        return REG_ESPACE;
     2839      new_array = re_realloc (path->array, re_dfastate_t *, new_alloc);
     2840      if (__glibc_unlikely (new_array == NULL))
     2841        return REG_ESPACE;
    28962842      path->array = new_array;
     2843      path->alloc = new_alloc;
    28972844      memset (new_array + old_alloc, '\0',
    28982845              sizeof (re_dfastate_t *) * (path->alloc - old_alloc));
    28992846    }
    29002847
    2901 #ifdef __GNUC__ /* silly buggers. */
    2902   str_idx = path->next_idx ?: top_str;
    2903 #else
    29042848  str_idx = path->next_idx ? path->next_idx : top_str;
    2905 #endif
    29062849
    29072850  /* Temporary modify MCTX.  */
     
    29162859    {
    29172860      err = re_node_set_init_1 (&next_nodes, top_node);
    2918       if (BE (err != REG_NOERROR, 0))
     2861      if (__glibc_unlikely (err != REG_NOERROR))
    29192862        return err;
    29202863      err = check_arrival_expand_ecl (dfa, &next_nodes, subexp_num, type);
    2921       if (BE (err != REG_NOERROR, 0))
     2864      if (__glibc_unlikely (err != REG_NOERROR))
    29222865        {
    29232866          re_node_set_free (&next_nodes);
     
    29312874        {
    29322875          err = re_node_set_init_copy (&next_nodes, &cur_state->nodes);
    2933           if (BE (err != REG_NOERROR, 0))
     2876          if (__glibc_unlikely (err != REG_NOERROR))
    29342877            return err;
    29352878        }
     
    29432886          err = expand_bkref_cache (mctx, &next_nodes, str_idx,
    29442887                                    subexp_num, type);
    2945           if (BE (err != REG_NOERROR, 0))
     2888          if (__glibc_unlikely (err != REG_NOERROR))
    29462889            {
    29472890              re_node_set_free (&next_nodes);
     
    29502893        }
    29512894      cur_state = re_acquire_state_context (&err, dfa, &next_nodes, context);
    2952       if (BE (cur_state == NULL && err != REG_NOERROR, 0))
     2895      if (__glibc_unlikely (cur_state == NULL && err != REG_NOERROR))
    29532896        {
    29542897          re_node_set_free (&next_nodes);
     
    29652908          err = re_node_set_merge (&next_nodes,
    29662909                                   &mctx->state_log[str_idx + 1]->nodes);
    2967           if (BE (err != REG_NOERROR, 0))
     2910          if (__glibc_unlikely (err != REG_NOERROR))
    29682911            {
    29692912              re_node_set_free (&next_nodes);
     
    29762919                                              &cur_state->non_eps_nodes,
    29772920                                              &next_nodes);
    2978           if (BE (err != REG_NOERROR, 0))
     2921          if (__glibc_unlikely (err != REG_NOERROR))
    29792922            {
    29802923              re_node_set_free (&next_nodes);
     
    29862929        {
    29872930          err = check_arrival_expand_ecl (dfa, &next_nodes, subexp_num, type);
    2988           if (BE (err != REG_NOERROR, 0))
     2931          if (__glibc_unlikely (err != REG_NOERROR))
    29892932            {
    29902933              re_node_set_free (&next_nodes);
     
    29932936          err = expand_bkref_cache (mctx, &next_nodes, str_idx,
    29942937                                    subexp_num, type);
    2995           if (BE (err != REG_NOERROR, 0))
     2938          if (__glibc_unlikely (err != REG_NOERROR))
    29962939            {
    29972940              re_node_set_free (&next_nodes);
     
    30012944      context = re_string_context_at (&mctx->input, str_idx - 1, mctx->eflags);
    30022945      cur_state = re_acquire_state_context (&err, dfa, &next_nodes, context);
    3003       if (BE (cur_state == NULL && err != REG_NOERROR, 0))
     2946      if (__glibc_unlikely (cur_state == NULL && err != REG_NOERROR))
    30042947        {
    30052948          re_node_set_free (&next_nodes);
     
    30342977
    30352978static reg_errcode_t
    3036 internal_function
    3037 check_arrival_add_next_nodes (re_match_context_t *mctx, int str_idx,
     2979__attribute_warn_unused_result__
     2980check_arrival_add_next_nodes (re_match_context_t *mctx, Idx str_idx,
    30382981                              re_node_set *cur_nodes, re_node_set *next_nodes)
    30392982{
    30402983  const re_dfa_t *const dfa = mctx->dfa;
    3041   int result;
    3042   int cur_idx;
     2984  bool ok;
     2985  Idx cur_idx;
    30432986  reg_errcode_t err = REG_NOERROR;
    30442987  re_node_set union_set;
     
    30472990    {
    30482991      int naccepted = 0;
    3049       int cur_node = cur_nodes->elems[cur_idx];
    3050 #ifdef DEBUG
    3051       re_token_type_t type = dfa->nodes[cur_node].type;
    3052       assert (!IS_EPSILON_NODE (type));
    3053 #endif
    3054 #ifdef RE_ENABLE_I18N
    3055       /* If the node may accept `multi byte'.  */
     2992      Idx cur_node = cur_nodes->elems[cur_idx];
     2993      DEBUG_ASSERT (!IS_EPSILON_NODE (dfa->nodes[cur_node].type));
     2994
     2995      /* If the node may accept "multi byte".  */
    30562996      if (dfa->nodes[cur_node].accept_mb)
    30572997        {
     
    30613001            {
    30623002              re_dfastate_t *dest_state;
    3063               int next_node = dfa->nexts[cur_node];
    3064               int next_idx = str_idx + naccepted;
     3003              Idx next_node = dfa->nexts[cur_node];
     3004              Idx next_idx = str_idx + naccepted;
    30653005              dest_state = mctx->state_log[next_idx];
    30663006              re_node_set_empty (&union_set);
     
    30683008                {
    30693009                  err = re_node_set_merge (&union_set, &dest_state->nodes);
    3070                   if (BE (err != REG_NOERROR, 0))
     3010                  if (__glibc_unlikely (err != REG_NOERROR))
    30713011                    {
    30723012                      re_node_set_free (&union_set);
     
    30743014                    }
    30753015                }
    3076               result = re_node_set_insert (&union_set, next_node);
    3077               if (BE (result < 0, 0))
     3016              ok = re_node_set_insert (&union_set, next_node);
     3017              if (__glibc_unlikely (! ok))
    30783018                {
    30793019                  re_node_set_free (&union_set);
     
    30823022              mctx->state_log[next_idx] = re_acquire_state (&err, dfa,
    30833023                                                            &union_set);
    3084               if (BE (mctx->state_log[next_idx] == NULL
    3085                       && err != REG_NOERROR, 0))
     3024              if (__glibc_unlikely (mctx->state_log[next_idx] == NULL
     3025                                    && err != REG_NOERROR))
    30863026                {
    30873027                  re_node_set_free (&union_set);
     
    30903030            }
    30913031        }
    3092 #endif /* RE_ENABLE_I18N */
     3032
    30933033      if (naccepted
    30943034          || check_node_accept (mctx, dfa->nodes + cur_node, str_idx))
    30953035        {
    3096           result = re_node_set_insert (next_nodes, dfa->nexts[cur_node]);
    3097           if (BE (result < 0, 0))
     3036          ok = re_node_set_insert (next_nodes, dfa->nexts[cur_node]);
     3037          if (__glibc_unlikely (! ok))
    30983038            {
    30993039              re_node_set_free (&union_set);
     
    31133053
    31143054static reg_errcode_t
    3115 internal_function
    31163055check_arrival_expand_ecl (const re_dfa_t *dfa, re_node_set *cur_nodes,
    3117                           int ex_subexp, int type)
     3056                          Idx ex_subexp, int type)
    31183057{
    31193058  reg_errcode_t err;
    3120   int idx, outside_node;
     3059  Idx idx, outside_node;
    31213060  re_node_set new_nodes;
    3122 #ifdef DEBUG
    3123   assert (cur_nodes->nelem);
    3124 #endif
     3061  DEBUG_ASSERT (cur_nodes->nelem);
    31253062  err = re_node_set_alloc (&new_nodes, cur_nodes->nelem);
    3126   if (BE (err != REG_NOERROR, 0))
     3063  if (__glibc_unlikely (err != REG_NOERROR))
    31273064    return err;
    31283065  /* Create a new node set NEW_NODES with the nodes which are epsilon
     
    31313068  for (idx = 0; idx < cur_nodes->nelem; ++idx)
    31323069    {
    3133       int cur_node = cur_nodes->elems[idx];
     3070      Idx cur_node = cur_nodes->elems[idx];
    31343071      const re_node_set *eclosure = dfa->eclosures + cur_node;
    31353072      outside_node = find_subexp_node (dfa, eclosure, ex_subexp, type);
     
    31383075          /* There are no problematic nodes, just merge them.  */
    31393076          err = re_node_set_merge (&new_nodes, eclosure);
    3140           if (BE (err != REG_NOERROR, 0))
     3077          if (__glibc_unlikely (err != REG_NOERROR))
    31413078            {
    31423079              re_node_set_free (&new_nodes);
     
    31493086          err = check_arrival_expand_ecl_sub (dfa, &new_nodes, cur_node,
    31503087                                              ex_subexp, type);
    3151           if (BE (err != REG_NOERROR, 0))
     3088          if (__glibc_unlikely (err != REG_NOERROR))
    31523089            {
    31533090              re_node_set_free (&new_nodes);
     
    31663103
    31673104static reg_errcode_t
    3168 internal_function
     3105__attribute_warn_unused_result__
    31693106check_arrival_expand_ecl_sub (const re_dfa_t *dfa, re_node_set *dst_nodes,
    3170                               int target, int ex_subexp, int type)
    3171 {
    3172   int cur_node;
     3107                              Idx target, Idx ex_subexp, int type)
     3108{
     3109  Idx cur_node;
    31733110  for (cur_node = target; !re_node_set_contains (dst_nodes, cur_node);)
    31743111    {
    3175       int err;
     3112      bool ok;
    31763113
    31773114      if (dfa->nodes[cur_node].type == type
     
    31803117          if (type == OP_CLOSE_SUBEXP)
    31813118            {
    3182               err = re_node_set_insert (dst_nodes, cur_node);
    3183               if (BE (err == -1, 0))
     3119              ok = re_node_set_insert (dst_nodes, cur_node);
     3120              if (__glibc_unlikely (! ok))
    31843121                return REG_ESPACE;
    31853122            }
    31863123          break;
    31873124        }
    3188       err = re_node_set_insert (dst_nodes, cur_node);
    3189       if (BE (err == -1, 0))
     3125      ok = re_node_set_insert (dst_nodes, cur_node);
     3126      if (__glibc_unlikely (! ok))
    31903127        return REG_ESPACE;
    31913128      if (dfa->edests[cur_node].nelem == 0)
     
    31933130      if (dfa->edests[cur_node].nelem == 2)
    31943131        {
     3132          reg_errcode_t err;
    31953133          err = check_arrival_expand_ecl_sub (dfa, dst_nodes,
    31963134                                              dfa->edests[cur_node].elems[1],
    31973135                                              ex_subexp, type);
    3198           if (BE (err != REG_NOERROR, 0))
     3136          if (__glibc_unlikely (err != REG_NOERROR))
    31993137            return err;
    32003138        }
     
    32103148
    32113149static reg_errcode_t
    3212 internal_function
     3150__attribute_warn_unused_result__
    32133151expand_bkref_cache (re_match_context_t *mctx, re_node_set *cur_nodes,
    3214                     int cur_str, int subexp_num, int type)
     3152                    Idx cur_str, Idx subexp_num, int type)
    32153153{
    32163154  const re_dfa_t *const dfa = mctx->dfa;
    32173155  reg_errcode_t err;
    3218   int cache_idx_start = search_cur_bkref_entry (mctx, cur_str);
     3156  Idx cache_idx_start = search_cur_bkref_entry (mctx, cur_str);
    32193157  struct re_backref_cache_entry *ent;
    32203158
     
    32263164  do
    32273165    {
    3228       int to_idx, next_node;
     3166      Idx to_idx, next_node;
    32293167
    32303168      /* Is this entry ENT is appropriate?  */
     
    32483186          err3 = re_node_set_merge (cur_nodes, &new_dests);
    32493187          re_node_set_free (&new_dests);
    3250           if (BE (err != REG_NOERROR || err2 != REG_NOERROR
    3251                   || err3 != REG_NOERROR, 0))
     3188          if (__glibc_unlikely (err != REG_NOERROR || err2 != REG_NOERROR
     3189                                || err3 != REG_NOERROR))
    32523190            {
    32533191              err = (err != REG_NOERROR ? err
     
    32643202          if (mctx->state_log[to_idx])
    32653203            {
    3266               int ret;
     3204              bool ok;
    32673205              if (re_node_set_contains (&mctx->state_log[to_idx]->nodes,
    32683206                                        next_node))
     
    32703208              err = re_node_set_init_copy (&union_set,
    32713209                                           &mctx->state_log[to_idx]->nodes);
    3272               ret = re_node_set_insert (&union_set, next_node);
    3273               if (BE (err != REG_NOERROR || ret < 0, 0))
     3210              ok = re_node_set_insert (&union_set, next_node);
     3211              if (__glibc_unlikely (err != REG_NOERROR || ! ok))
    32743212                {
    32753213                  re_node_set_free (&union_set);
     
    32813219            {
    32823220              err = re_node_set_init_1 (&union_set, next_node);
    3283               if (BE (err != REG_NOERROR, 0))
     3221              if (__glibc_unlikely (err != REG_NOERROR))
    32843222                return err;
    32853223            }
    32863224          mctx->state_log[to_idx] = re_acquire_state (&err, dfa, &union_set);
    32873225          re_node_set_free (&union_set);
    3288           if (BE (mctx->state_log[to_idx] == NULL
    3289                   && err != REG_NOERROR, 0))
     3226          if (__glibc_unlikely (mctx->state_log[to_idx] == NULL
     3227                                && err != REG_NOERROR))
    32903228            return err;
    32913229        }
     
    32963234
    32973235/* Build transition table for the state.
    3298    Return 1 if succeeded, otherwise return NULL.  */
    3299 
    3300 static int
    3301 internal_function
     3236   Return true if successful.  */
     3237
     3238static bool __attribute_noinline__
    33023239build_trtable (const re_dfa_t *dfa, re_dfastate_t *state)
    33033240{
    33043241  reg_errcode_t err;
    3305   int i, j, ch, need_word_trtable = 0;
     3242  Idx i, j;
     3243  int ch;
     3244  bool need_word_trtable = false;
    33063245  bitset_word_t elem, mask;
    3307   bool dests_node_malloced = false;
    3308   bool dest_states_malloced = false;
    3309   int ndests; /* Number of the destination states from `state'.  */
     3246  Idx ndests; /* Number of the destination states from 'state'.  */
    33103247  re_dfastate_t **trtable;
    3311   re_dfastate_t **dest_states = NULL, **dest_states_word, **dest_states_nl;
    3312   re_node_set follows, *dests_node;
    3313   bitset_t *dests_ch;
     3248  re_dfastate_t *dest_states[SBC_MAX];
     3249  re_dfastate_t *dest_states_word[SBC_MAX];
     3250  re_dfastate_t *dest_states_nl[SBC_MAX];
     3251  re_node_set follows;
    33143252  bitset_t acceptable;
    33153253
    3316   struct dests_alloc
    3317   {
    3318     re_node_set dests_node[SBC_MAX];
    3319     bitset_t dests_ch[SBC_MAX];
    3320   } *dests_alloc;
    3321 
    33223254  /* We build DFA states which corresponds to the destination nodes
    3323      from `state'.  `dests_node[i]' represents the nodes which i-th
    3324      destination state contains, and `dests_ch[i]' represents the
     3255     from 'state'.  'dests_node[i]' represents the nodes which i-th
     3256     destination state contains, and 'dests_ch[i]' represents the
    33253257     characters which i-th destination state accepts.  */
    3326   if (__libc_use_alloca (sizeof (struct dests_alloc)))
    3327     dests_alloc = (struct dests_alloc *) alloca (sizeof (struct dests_alloc));
    3328   else
    3329     {
    3330       dests_alloc = re_malloc (struct dests_alloc, 1);
    3331       if (BE (dests_alloc == NULL, 0))
    3332         return 0;
    3333       dests_node_malloced = true;
    3334     }
    3335   dests_node = dests_alloc->dests_node;
    3336   dests_ch = dests_alloc->dests_ch;
    3337 
    3338   /* Initialize transiton table.  */
     3258  re_node_set dests_node[SBC_MAX];
     3259  bitset_t dests_ch[SBC_MAX];
     3260
     3261  /* Initialize transition table.  */
    33393262  state->word_trtable = state->trtable = NULL;
    33403263
    3341   /* At first, group all nodes belonging to `state' into several
     3264  /* At first, group all nodes belonging to 'state' into several
    33423265     destinations.  */
    33433266  ndests = group_nodes_into_DFAstates (dfa, state, dests_node, dests_ch);
    3344   if (BE (ndests <= 0, 0))
    3345     {
    3346       if (dests_node_malloced)
    3347         free (dests_alloc);
    3348       /* Return 0 in case of an error, 1 otherwise.  */
     3267  if (__glibc_unlikely (ndests <= 0))
     3268    {
     3269      /* Return false in case of an error, true otherwise.  */
    33493270      if (ndests == 0)
    33503271        {
    33513272          state->trtable = (re_dfastate_t **)
    33523273            calloc (sizeof (re_dfastate_t *), SBC_MAX);
    3353           return 1;
    3354         }
    3355       return 0;
     3274          if (__glibc_unlikely (state->trtable == NULL))
     3275            return false;
     3276          return true;
     3277        }
     3278      return false;
    33563279    }
    33573280
    33583281  err = re_node_set_alloc (&follows, ndests + 1);
    3359   if (BE (err != REG_NOERROR, 0))
    3360     goto out_free;
    3361 
    3362   if (__libc_use_alloca ((sizeof (re_node_set) + sizeof (bitset_t)) * SBC_MAX
    3363                          + ndests * 3 * sizeof (re_dfastate_t *)))
    3364     dest_states = (re_dfastate_t **)
    3365       alloca (ndests * 3 * sizeof (re_dfastate_t *));
    3366   else
    3367     {
    3368       dest_states = (re_dfastate_t **)
    3369         malloc (ndests * 3 * sizeof (re_dfastate_t *));
    3370       if (BE (dest_states == NULL, 0))
    3371         {
    3372 out_free:
    3373           if (dest_states_malloced)
    3374             free (dest_states);
    3375           re_node_set_free (&follows);
    3376           for (i = 0; i < ndests; ++i)
    3377             re_node_set_free (dests_node + i);
    3378           if (dests_node_malloced)
    3379             free (dests_alloc);
    3380           return 0;
    3381         }
    3382       dest_states_malloced = true;
    3383     }
    3384   dest_states_word = dest_states + ndests;
    3385   dest_states_nl = dest_states_word + ndests;
     3282  if (__glibc_unlikely (err != REG_NOERROR))
     3283    {
     3284    out_free:
     3285      re_node_set_free (&follows);
     3286      for (i = 0; i < ndests; ++i)
     3287        re_node_set_free (dests_node + i);
     3288      return false;
     3289    }
     3290
    33863291  bitset_empty (acceptable);
    33873292
     
    33893294  for (i = 0; i < ndests; ++i)
    33903295    {
    3391       int next_node;
     3296      Idx next_node;
    33923297      re_node_set_empty (&follows);
    33933298      /* Merge the follows of this destination states.  */
     
    33983303            {
    33993304              err = re_node_set_merge (&follows, dfa->eclosures + next_node);
    3400               if (BE (err != REG_NOERROR, 0))
     3305              if (__glibc_unlikely (err != REG_NOERROR))
    34013306                goto out_free;
    34023307            }
    34033308        }
    34043309      dest_states[i] = re_acquire_state_context (&err, dfa, &follows, 0);
    3405       if (BE (dest_states[i] == NULL && err != REG_NOERROR, 0))
     3310      if (__glibc_unlikely (dest_states[i] == NULL && err != REG_NOERROR))
    34063311        goto out_free;
    34073312      /* If the new state has context constraint,
     
    34113316          dest_states_word[i] = re_acquire_state_context (&err, dfa, &follows,
    34123317                                                          CONTEXT_WORD);
    3413           if (BE (dest_states_word[i] == NULL && err != REG_NOERROR, 0))
     3318          if (__glibc_unlikely (dest_states_word[i] == NULL
     3319                                && err != REG_NOERROR))
    34143320            goto out_free;
    34153321
    34163322          if (dest_states[i] != dest_states_word[i] && dfa->mb_cur_max > 1)
    3417             need_word_trtable = 1;
     3323            need_word_trtable = true;
    34183324
    34193325          dest_states_nl[i] = re_acquire_state_context (&err, dfa, &follows,
    34203326                                                        CONTEXT_NEWLINE);
    3421           if (BE (dest_states_nl[i] == NULL && err != REG_NOERROR, 0))
     3327          if (__glibc_unlikely (dest_states_nl[i] == NULL && err != REG_NOERROR))
    34223328            goto out_free;
    3423         }
     3329        }
    34243330      else
    34253331        {
     
    34303336    }
    34313337
    3432   if (!BE (need_word_trtable, 0))
     3338  if (!__glibc_unlikely (need_word_trtable))
    34333339    {
    34343340      /* We don't care about whether the following character is a word
     
    34383344      trtable = state->trtable =
    34393345        (re_dfastate_t **) calloc (sizeof (re_dfastate_t *), SBC_MAX);
    3440       if (BE (trtable == NULL, 0))
     3346      if (__glibc_unlikely (trtable == NULL))
    34413347        goto out_free;
    34423348
     
    34463352             elem;
    34473353             mask <<= 1, elem >>= 1, ++ch)
    3448           if (BE (elem & 1, 0))
     3354          if (__glibc_unlikely (elem & 1))
    34493355            {
    34503356              /* There must be exactly one destination which accepts
     
    34693375      trtable = state->word_trtable =
    34703376        (re_dfastate_t **) calloc (sizeof (re_dfastate_t *), 2 * SBC_MAX);
    3471       if (BE (trtable == NULL, 0))
     3377      if (__glibc_unlikely (trtable == NULL))
    34723378        goto out_free;
    34733379
     
    34773383             elem;
    34783384             mask <<= 1, elem >>= 1, ++ch)
    3479           if (BE (elem & 1, 0))
     3385          if (__glibc_unlikely (elem & 1))
    34803386            {
    34813387              /* There must be exactly one destination which accepts
     
    35073413    }
    35083414
    3509   if (dest_states_malloced)
    3510     free (dest_states);
    3511 
    35123415  re_node_set_free (&follows);
    35133416  for (i = 0; i < ndests; ++i)
    35143417    re_node_set_free (dests_node + i);
    3515 
    3516   if (dests_node_malloced)
    3517     free (dests_alloc);
    3518 
    3519   return 1;
     3418  return true;
    35203419}
    35213420
     
    35233422   Then for all destinations, set the nodes belonging to the destination
    35243423   to DESTS_NODE[i] and set the characters accepted by the destination
    3525    to DEST_CH[i].  This function return the number of destinations.  */
    3526 
    3527 static int
    3528 internal_function
     3424   to DEST_CH[i].  Return the number of destinations if successful,
     3425   -1 on internal error.  */
     3426
     3427static Idx
    35293428group_nodes_into_DFAstates (const re_dfa_t *dfa, const re_dfastate_t *state,
    35303429                            re_node_set *dests_node, bitset_t *dests_ch)
    35313430{
    35323431  reg_errcode_t err;
    3533   int result;
    3534   int i, j, k;
    3535   int ndests; /* Number of the destinations from `state'.  */
     3432  bool ok;
     3433  Idx i, j, k;
     3434  Idx ndests; /* Number of the destinations from 'state'.  */
    35363435  bitset_t accepts; /* Characters a node can accept.  */
    35373436  const re_node_set *cur_nodes = &state->nodes;
     
    35393438  ndests = 0;
    35403439
    3541   /* For all the nodes belonging to `state',  */
     3440  /* For all the nodes belonging to 'state',  */
    35423441  for (i = 0; i < cur_nodes->nelem; ++i)
    35433442    {
     
    35553454      else if (type == OP_PERIOD)
    35563455        {
    3557 #ifdef RE_ENABLE_I18N
    35583456          if (dfa->mb_cur_max > 1)
    35593457            bitset_merge (accepts, dfa->sb_char);
    35603458          else
    3561 #endif
    35623459            bitset_set_all (accepts);
    35633460          if (!(dfa->syntax & RE_DOT_NEWLINE))
     
    35663463            bitset_clear (accepts, '\0');
    35673464        }
    3568 #ifdef RE_ENABLE_I18N
    35693465      else if (type == OP_UTF8_PERIOD)
    3570         {
    3571           memset (accepts, '\xff', sizeof (bitset_t) / 2);
     3466        {
     3467          if (ASCII_CHARS % BITSET_WORD_BITS == 0)
     3468            memset (accepts, -1, ASCII_CHARS / CHAR_BIT);
     3469          else
     3470            bitset_merge (accepts, utf8_sb_map);
    35723471          if (!(dfa->syntax & RE_DOT_NEWLINE))
    35733472            bitset_clear (accepts, '\n');
    35743473          if (dfa->syntax & RE_DOT_NOT_NULL)
    35753474            bitset_clear (accepts, '\0');
    3576         }
    3577 #endif
     3475        }
    35783476      else
    35793477        continue;
    35803478
    3581       /* Check the `accepts' and sift the characters which are not
     3479      /* Check the 'accepts' and sift the characters which are not
    35823480         match it the context.  */
    35833481      if (constraint)
     
    36063504                  continue;
    36073505                }
    3608 #ifdef RE_ENABLE_I18N
    36093506              if (dfa->mb_cur_max > 1)
    36103507                for (j = 0; j < BITSET_WORDS; ++j)
    36113508                  any_set |= (accepts[j] &= (dfa->word_char[j] | ~dfa->sb_char[j]));
    36123509              else
    3613 #endif
    36143510                for (j = 0; j < BITSET_WORDS; ++j)
    36153511                  any_set |= (accepts[j] &= dfa->word_char[j]);
     
    36253521                  continue;
    36263522                }
    3627 #ifdef RE_ENABLE_I18N
    36283523              if (dfa->mb_cur_max > 1)
    36293524                for (j = 0; j < BITSET_WORDS; ++j)
    36303525                  any_set |= (accepts[j] &= ~(dfa->word_char[j] & dfa->sb_char[j]));
    36313526              else
    3632 #endif
    36333527                for (j = 0; j < BITSET_WORDS; ++j)
    36343528                  any_set |= (accepts[j] &= ~dfa->word_char[j]);
     
    36383532        }
    36393533
    3640       /* Then divide `accepts' into DFA states, or create a new
     3534      /* Then divide 'accepts' into DFA states, or create a new
    36413535         state.  Above, we make sure that accepts is not empty.  */
    36423536      for (j = 0; j < ndests; ++j)
     
    36513545            continue;
    36523546
    3653           /* Enumerate the intersection set of this state and `accepts'.  */
     3547          /* Enumerate the intersection set of this state and 'accepts'.  */
    36543548          has_intersec = 0;
    36553549          for (k = 0; k < BITSET_WORDS; ++k)
     
    36593553            continue;
    36603554
    3661           /* Then check if this state is a subset of `accepts'.  */
     3555          /* Then check if this state is a subset of 'accepts'.  */
    36623556          not_subset = not_consumed = 0;
    36633557          for (k = 0; k < BITSET_WORDS; ++k)
     
    36673561            }
    36683562
    3669           /* If this state isn't a subset of `accepts', create a
    3670              new group state, which has the `remains'. */
     3563          /* If this state isn't a subset of 'accepts', create a
     3564             new group state, which has the 'remains'. */
    36713565          if (not_subset)
    36723566            {
     
    36743568              bitset_copy (dests_ch[j], intersec);
    36753569              err = re_node_set_init_copy (dests_node + ndests, &dests_node[j]);
    3676               if (BE (err != REG_NOERROR, 0))
     3570              if (__glibc_unlikely (err != REG_NOERROR))
    36773571                goto error_return;
    36783572              ++ndests;
     
    36803574
    36813575          /* Put the position in the current group. */
    3682           result = re_node_set_insert (&dests_node[j], cur_nodes->elems[i]);
    3683           if (BE (result < 0, 0))
     3576          ok = re_node_set_insert (&dests_node[j], cur_nodes->elems[i]);
     3577          if (__glibc_unlikely (! ok))
    36843578            goto error_return;
    36853579
     
    36933587          bitset_copy (dests_ch[ndests], accepts);
    36943588          err = re_node_set_init_1 (dests_node + ndests, cur_nodes->elems[i]);
    3695           if (BE (err != REG_NOERROR, 0))
     3589          if (__glibc_unlikely (err != REG_NOERROR))
    36963590            goto error_return;
    36973591          ++ndests;
     
    36993593        }
    37003594    }
     3595  assume (ndests <= SBC_MAX);
    37013596  return ndests;
    37023597 error_return:
     
    37063601}
    37073602
    3708 #ifdef RE_ENABLE_I18N
    3709 /* Check how many bytes the node `dfa->nodes[node_idx]' accepts.
     3603/* Check how many bytes the node 'dfa->nodes[node_idx]' accepts.
    37103604   Return the number of the bytes the node accepts.
    37113605   STR_IDX is the current index of the input string.
     
    37153609   can only accept one byte.  */
    37163610
     3611#ifdef _LIBC
     3612# include <locale/weight.h>
     3613#endif
     3614
    37173615static int
    3718 internal_function
    3719 check_node_accept_bytes (const re_dfa_t *dfa, int node_idx,
    3720                          const re_string_t *input, int str_idx)
     3616check_node_accept_bytes (const re_dfa_t *dfa, Idx node_idx,
     3617                         const re_string_t *input, Idx str_idx)
    37213618{
    37223619  const re_token_t *node = dfa->nodes + node_idx;
    37233620  int char_len, elem_len;
    3724   int i;
    3725 
    3726   if (BE (node->type == OP_UTF8_PERIOD, 0))
     3621  Idx i;
     3622
     3623  if (__glibc_unlikely (node->type == OP_UTF8_PERIOD))
    37273624    {
    37283625      unsigned char c = re_string_byte_at (input, str_idx), d;
    3729       if (BE (c < 0xc2, 1))
     3626      if (__glibc_likely (c < 0xc2))
    37303627        return 0;
    37313628
     
    37793676    {
    37803677      if (char_len <= 1)
    3781         return 0;
     3678        return 0;
    37823679      /* FIXME: I don't think this if is needed, as both '\n'
    37833680         and '\0' are char_len == 1.  */
    37843681      /* '.' accepts any one character except the following two cases.  */
    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'))
     3682      if ((!(dfa->syntax & RE_DOT_NEWLINE)
     3683           && re_string_byte_at (input, str_idx) == '\n')
     3684          || ((dfa->syntax & RE_DOT_NOT_NULL)
     3685              && re_string_byte_at (input, str_idx) == '\0'))
    37893686        return 0;
    37903687      return char_len;
     
    37983695    {
    37993696      const re_charset_t *cset = node->opr.mbcset;
    3800 # ifdef _LIBC
     3697#ifdef _LIBC
    38013698      const unsigned char *pin
    38023699        = ((const unsigned char *) re_string_get_buffer (input) + str_idx);
    3803       int j;
     3700      Idx j;
    38043701      uint32_t nrules;
    3805 # endif /* _LIBC */
     3702#endif
    38063703      int match_len = 0;
    38073704      wchar_t wc = ((cset->nranges || cset->nchar_classes || cset->nmbchars)
     
    38263723        }
    38273724
    3828 # ifdef _LIBC
     3725#ifdef _LIBC
    38293726      nrules = _NL_CURRENT_WORD (LC_COLLATE, _NL_COLLATE_NRULES);
    38303727      if (nrules != 0)
     
    38343731          const unsigned char *weights, *extra;
    38353732          const char *collseqwc;
    3836           int32_t idx;
    3837           /* This #include defines a local function!  */
    3838 #  include <locale/weight.h>
    38393733
    38403734          /* match with collating_symbol?  */
     
    38723766            }
    38733767          /* match with range expression?  */
     3768          /* FIXME: Implement rational ranges here, too.  */
    38743769          for (i = 0; i < cset->nranges; ++i)
    38753770            if (cset->range_starts[i] <= in_collseq
     
    38923787              indirect = (const int32_t *)
    38933788                _NL_CURRENT (LC_COLLATE, _NL_COLLATE_INDIRECTMB);
    3894               idx = findidx (&cp);
     3789              int32_t idx = findidx (table, indirect, extra, &cp, elem_len);
     3790              int32_t rule = idx >> 24;
     3791              idx &= 0xffffff;
    38953792              if (idx > 0)
    3896                 for (i = 0; i < cset->nequiv_classes; ++i)
    3897                   {
    3898                     int32_t equiv_class_idx = cset->equiv_classes[i];
    3899                     size_t weight_len = weights[idx];
    3900                     if (weight_len == weights[equiv_class_idx])
    3901                       {
    3902                         int cnt = 0;
    3903                         while (cnt <= weight_len
    3904                                && (weights[equiv_class_idx + 1 + cnt]
    3905                                    == weights[idx + 1 + cnt]))
    3906                           ++cnt;
    3907                         if (cnt > weight_len)
    3908                           {
    3909                             match_len = elem_len;
    3910                             goto check_node_accept_bytes_match;
    3911                           }
    3912                       }
    3913                   }
     3793                {
     3794                  size_t weight_len = weights[idx];
     3795                  for (i = 0; i < cset->nequiv_classes; ++i)
     3796                    {
     3797                      int32_t equiv_class_idx = cset->equiv_classes[i];
     3798                      int32_t equiv_class_rule = equiv_class_idx >> 24;
     3799                      equiv_class_idx &= 0xffffff;
     3800                      if (weights[equiv_class_idx] == weight_len
     3801                          && equiv_class_rule == rule
     3802                          && memcmp (weights + idx + 1,
     3803                                     weights + equiv_class_idx + 1,
     3804                                     weight_len) == 0)
     3805                        {
     3806                          match_len = elem_len;
     3807                          goto check_node_accept_bytes_match;
     3808                        }
     3809                    }
     3810                }
    39143811            }
    39153812        }
    39163813      else
    3917 # endif /* _LIBC */
     3814#endif /* _LIBC */
    39183815        {
    39193816          /* match with range expression?  */
    3920 #if __GNUC__ >= 2
    3921           wchar_t cmp_buf[] = {L'\0', L'\0', wc, L'\0', L'\0', L'\0'};
    3922 #else
    3923           wchar_t cmp_buf[] = {L'\0', L'\0', L'\0', L'\0', L'\0', L'\0'};
    3924           cmp_buf[2] = wc;
    3925 #endif
    39263817          for (i = 0; i < cset->nranges; ++i)
    39273818            {
    3928               cmp_buf[0] = cset->range_starts[i];
    3929               cmp_buf[4] = cset->range_ends[i];
    3930               if (wcscoll (cmp_buf, cmp_buf + 2) <= 0
    3931                   && wcscoll (cmp_buf + 2, cmp_buf + 4) <= 0)
     3819              if (cset->range_starts[i] <= wc && wc <= cset->range_ends[i])
    39323820                {
    39333821                  match_len = char_len;
     
    39503838}
    39513839
    3952 # ifdef _LIBC
     3840#ifdef _LIBC
    39533841static unsigned int
    3954 internal_function
    39553842find_collation_sequence_value (const unsigned char *mbs, size_t mbs_len)
    39563843{
     
    39773864      for (idx = 0; idx < extrasize;)
    39783865        {
    3979           int mbs_cnt, found = 0;
     3866          int mbs_cnt;
     3867          bool found = false;
    39803868          int32_t elem_mbs_len;
    39813869          /* Skip the name of collating element name.  */
     
    39893877              if (mbs_cnt == elem_mbs_len)
    39903878                /* Found the entry.  */
    3991                 found = 1;
     3879                found = true;
    39923880            }
    39933881          /* Skip the byte sequence of the collating element.  */
     
    39983886          idx += sizeof (uint32_t);
    39993887          /* Skip the wide char sequence of the collating element.  */
    4000           idx = idx + sizeof (uint32_t) * (extra[idx] + 1);
     3888          idx = idx + sizeof (uint32_t) * (*(int32_t *) (extra + idx) + 1);
    40013889          /* If we found the entry, return the sequence value.  */
    40023890          if (found)
     
    40083896    }
    40093897}
    4010 # endif /* _LIBC */
    4011 #endif /* RE_ENABLE_I18N */
     3898#endif /* _LIBC */
    40123899
    40133900/* Check whether the node accepts the byte which is IDX-th
    40143901   byte of the INPUT.  */
    40153902
    4016 static int
    4017 internal_function
     3903static bool
    40183904check_node_accept (const re_match_context_t *mctx, const re_token_t *node,
    4019                    int idx)
     3905                   Idx idx)
    40203906{
    40213907  unsigned char ch;
     
    40253911    case CHARACTER:
    40263912      if (node->opr.c != ch)
    4027         return 0;
     3913        return false;
    40283914      break;
    40293915
    40303916    case SIMPLE_BRACKET:
    40313917      if (!bitset_contain (node->opr.sbcset, ch))
    4032         return 0;
     3918        return false;
    40333919      break;
    40343920
    4035 #ifdef RE_ENABLE_I18N
    40363921    case OP_UTF8_PERIOD:
    4037       if (ch >= 0x80)
    4038         return 0;
    4039       /* FALLTHROUGH */
    4040 #endif
     3922      if (ch >= ASCII_CHARS)
     3923        return false;
     3924      FALLTHROUGH;
    40413925    case OP_PERIOD:
    40423926      if ((ch == '\n' && !(mctx->dfa->syntax & RE_DOT_NEWLINE))
    40433927          || (ch == '\0' && (mctx->dfa->syntax & RE_DOT_NOT_NULL)))
    4044         return 0;
     3928        return false;
    40453929      break;
    40463930
    40473931    default:
    4048       return 0;
     3932      return false;
    40493933    }
    40503934
     
    40563940                                                   mctx->eflags);
    40573941      if (NOT_SATISFY_NEXT_CONSTRAINT (node->constraint, context))
    4058         return 0;
    4059     }
    4060 
    4061   return 1;
     3942        return false;
     3943    }
     3944
     3945  return true;
    40623946}
    40633947
     
    40653949
    40663950static reg_errcode_t
    4067 internal_function
    4068 extend_buffers (re_match_context_t *mctx)
     3951__attribute_warn_unused_result__
     3952extend_buffers (re_match_context_t *mctx, int min_len)
    40693953{
    40703954  reg_errcode_t ret;
    40713955  re_string_t *pstr = &mctx->input;
    40723956
    4073   /* Double the lengthes of the buffers.  */
    4074   ret = re_string_realloc_buffers (pstr, pstr->bufs_len * 2);
    4075   if (BE (ret != REG_NOERROR, 0))
     3957  /* Avoid overflow.  */
     3958  if (__glibc_unlikely (MIN (IDX_MAX, SIZE_MAX / sizeof (re_dfastate_t *)) / 2
     3959                        <= pstr->bufs_len))
     3960    return REG_ESPACE;
     3961
     3962  /* Double the lengths of the buffers, but allocate at least MIN_LEN.  */
     3963  ret = re_string_realloc_buffers (pstr,
     3964                                   MAX (min_len,
     3965                                        MIN (pstr->len, pstr->bufs_len * 2)));
     3966  if (__glibc_unlikely (ret != REG_NOERROR))
    40763967    return ret;
    40773968
     
    40843975      re_dfastate_t **new_array = re_realloc (mctx->state_log, re_dfastate_t *,
    40853976                                              pstr->bufs_len + 1);
    4086       if (BE (new_array == NULL, 0))
     3977      if (__glibc_unlikely (new_array == NULL))
    40873978        return REG_ESPACE;
    40883979      mctx->state_log = new_array;
     
    40923983  if (pstr->icase)
    40933984    {
    4094 #ifdef RE_ENABLE_I18N
    40953985      if (pstr->mb_cur_max > 1)
    40963986        {
    40973987          ret = build_wcs_upper_buffer (pstr);
    4098           if (BE (ret != REG_NOERROR, 0))
     3988          if (__glibc_unlikely (ret != REG_NOERROR))
    40993989            return ret;
    41003990        }
    41013991      else
    4102 #endif /* RE_ENABLE_I18N  */
    41033992        build_upper_buffer (pstr);
    41043993    }
    41053994  else
    41063995    {
    4107 #ifdef RE_ENABLE_I18N
    41083996      if (pstr->mb_cur_max > 1)
    41093997        build_wcs_buffer (pstr);
    41103998      else
    4111 #endif /* RE_ENABLE_I18N  */
    41123999        {
    41134000          if (pstr->trans != NULL)
     
    41254012
    41264013static reg_errcode_t
    4127 internal_function
    4128 match_ctx_init (re_match_context_t *mctx, int eflags, int n)
     4014__attribute_warn_unused_result__
     4015match_ctx_init (re_match_context_t *mctx, int eflags, Idx n)
    41294016{
    41304017  mctx->eflags = eflags;
     
    41324019  if (n > 0)
    41334020    {
     4021      /* Avoid overflow.  */
     4022      size_t max_object_size =
     4023        MAX (sizeof (struct re_backref_cache_entry),
     4024             sizeof (re_sub_match_top_t *));
     4025      if (__glibc_unlikely (MIN (IDX_MAX, SIZE_MAX / max_object_size) < n))
     4026        return REG_ESPACE;
     4027
    41344028      mctx->bkref_ents = re_malloc (struct re_backref_cache_entry, n);
    41354029      mctx->sub_tops = re_malloc (re_sub_match_top_t *, n);
    4136       if (BE (mctx->bkref_ents == NULL || mctx->sub_tops == NULL, 0))
     4030      if (__glibc_unlikely (mctx->bkref_ents == NULL || mctx->sub_tops == NULL))
    41374031        return REG_ESPACE;
    41384032    }
     
    41534047
    41544048static void
    4155 internal_function
    41564049match_ctx_clean (re_match_context_t *mctx)
    41574050{
    4158   int st_idx;
     4051  Idx st_idx;
    41594052  for (st_idx = 0; st_idx < mctx->nsub_tops; ++st_idx)
    41604053    {
    4161       int sl_idx;
     4054      Idx sl_idx;
    41624055      re_sub_match_top_t *top = mctx->sub_tops[st_idx];
    41634056      for (sl_idx = 0; sl_idx < top->nlasts; ++sl_idx)
     
    41734066          re_free (top->path);
    41744067        }
    4175       free (top);
     4068      re_free (top);
    41764069    }
    41774070
     
    41834076
    41844077static void
    4185 internal_function
    41864078match_ctx_free (re_match_context_t *mctx)
    41874079{
     
    41984090
    41994091static reg_errcode_t
    4200 internal_function
    4201 match_ctx_add_entry (re_match_context_t *mctx, int node, int str_idx, int from,
    4202                      int to)
     4092__attribute_warn_unused_result__
     4093match_ctx_add_entry (re_match_context_t *mctx, Idx node, Idx str_idx, Idx from,
     4094                     Idx to)
    42034095{
    42044096  if (mctx->nbkref_ents >= mctx->abkref_ents)
     
    42074099      new_entry = re_realloc (mctx->bkref_ents, struct re_backref_cache_entry,
    42084100                              mctx->abkref_ents * 2);
    4209       if (BE (new_entry == NULL, 0))
     4101      if (__glibc_unlikely (new_entry == NULL))
    42104102        {
    42114103          re_free (mctx->bkref_ents);
     
    42354127     to all zeros if FROM != TO.  */
    42364128  mctx->bkref_ents[mctx->nbkref_ents].eps_reachable_subexps_map
    4237     = (from == to ? ~0 : 0);
     4129    = (from == to ? -1 : 0);
    42384130
    42394131  mctx->bkref_ents[mctx->nbkref_ents++].more = 0;
     
    42434135}
    42444136
    4245 /* Search for the first entry which has the same str_idx, or -1 if none is
     4137/* Return the first entry with the same str_idx, or -1 if none is
    42464138   found.  Note that MCTX->BKREF_ENTS is already sorted by MCTX->STR_IDX.  */
    42474139
    4248 static int
    4249 internal_function
    4250 search_cur_bkref_entry (const re_match_context_t *mctx, int str_idx)
    4251 {
    4252   int left, right, mid, last;
     4140static Idx
     4141search_cur_bkref_entry (const re_match_context_t *mctx, Idx str_idx)
     4142{
     4143  Idx left, right, mid, last;
    42534144  last = right = mctx->nbkref_ents;
    42544145  for (left = 0; left < right;)
     
    42704161
    42714162static reg_errcode_t
    4272 internal_function
    4273 match_ctx_add_subtop (re_match_context_t *mctx, int node, int str_idx)
    4274 {
    4275 #ifdef DEBUG
    4276   assert (mctx->sub_tops != NULL);
    4277   assert (mctx->asub_tops > 0);
    4278 #endif
    4279   if (BE (mctx->nsub_tops == mctx->asub_tops, 0))
    4280     {
    4281       int new_asub_tops = mctx->asub_tops * 2;
     4163__attribute_warn_unused_result__
     4164match_ctx_add_subtop (re_match_context_t *mctx, Idx node, Idx str_idx)
     4165{
     4166  DEBUG_ASSERT (mctx->sub_tops != NULL);
     4167  DEBUG_ASSERT (mctx->asub_tops > 0);
     4168  if (__glibc_unlikely (mctx->nsub_tops == mctx->asub_tops))
     4169    {
     4170      Idx new_asub_tops = mctx->asub_tops * 2;
    42824171      re_sub_match_top_t **new_array = re_realloc (mctx->sub_tops,
    42834172                                                   re_sub_match_top_t *,
    42844173                                                   new_asub_tops);
    4285       if (BE (new_array == NULL, 0))
     4174      if (__glibc_unlikely (new_array == NULL))
    42864175        return REG_ESPACE;
    42874176      mctx->sub_tops = new_array;
     
    42894178    }
    42904179  mctx->sub_tops[mctx->nsub_tops] = calloc (1, sizeof (re_sub_match_top_t));
    4291   if (BE (mctx->sub_tops[mctx->nsub_tops] == NULL, 0))
     4180  if (__glibc_unlikely (mctx->sub_tops[mctx->nsub_tops] == NULL))
    42924181    return REG_ESPACE;
    42934182  mctx->sub_tops[mctx->nsub_tops]->node = node;
     
    42974186
    42984187/* 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.  */
     4188   at STR_IDX, whose corresponding OP_OPEN_SUBEXP is SUB_TOP.
     4189   Return the new entry if successful, NULL if memory is exhausted.  */
    43004190
    43014191static re_sub_match_last_t *
    4302 internal_function
    4303 match_ctx_add_sublast (re_sub_match_top_t *subtop, int node, int str_idx)
     4192match_ctx_add_sublast (re_sub_match_top_t *subtop, Idx node, Idx str_idx)
    43044193{
    43054194  re_sub_match_last_t *new_entry;
    4306   if (BE (subtop->nlasts == subtop->alasts, 0))
    4307     {
    4308       int new_alasts = 2 * subtop->alasts + 1;
     4195  if (__glibc_unlikely (subtop->nlasts == subtop->alasts))
     4196    {
     4197      Idx new_alasts = 2 * subtop->alasts + 1;
    43094198      re_sub_match_last_t **new_array = re_realloc (subtop->lasts,
    43104199                                                    re_sub_match_last_t *,
    43114200                                                    new_alasts);
    4312       if (BE (new_array == NULL, 0))
     4201      if (__glibc_unlikely (new_array == NULL))
    43134202        return NULL;
    43144203      subtop->lasts = new_array;
     
    43164205    }
    43174206  new_entry = calloc (1, sizeof (re_sub_match_last_t));
    4318   if (BE (new_entry != NULL, 1))
     4207  if (__glibc_likely (new_entry != NULL))
    43194208    {
    43204209      subtop->lasts[subtop->nlasts] = new_entry;
     
    43274216
    43284217static void
    4329 internal_function
    43304218sift_ctx_init (re_sift_context_t *sctx, re_dfastate_t **sifted_sts,
    4331                re_dfastate_t **limited_sts, int last_node, int last_str_idx)
     4219               re_dfastate_t **limited_sts, Idx last_node, Idx last_str_idx)
    43324220{
    43334221  sctx->sifted_states = sifted_sts;
Note: See TracChangeset for help on using the changeset viewer.