bug-coreutils
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

fixes for 64-bit regex bugs in coreutils


From: Paul Eggert
Subject: fixes for 64-bit regex bugs in coreutils
Date: Fri, 09 Sep 2005 14:15:43 -0700
User-agent: Gnus/5.1007 (Gnus v5.10.7) Emacs/21.4 (gnu/linux)

I installed the following to fix some bugs in the coreutils regular
expression handling on 64-bit hosts.

This is a big patch, but most of it is a straight import from gnulib.
The gnulib regex code will probably mutate as a result of feedback
from the other glibc maintainers but I don't think this'll affect
coreutils proper, so I thought it best to install the necessary
coreutils changes now.

2005-09-09  Paul Eggert  <address@hidden>

        Support regular expressions in 64-bit code correctly, by
        importing the latest gnulib regexp code, and not assuming
        that sizes fit in 32 bits.
        * src/csplit.c (process_regexp): Store match length in regoff_t,
        not int.  Assume that negative return values less than -2
        represent regoff_t overflow.
        * src/expr.c (docolon): Likewise.
        * src/nl.c (proc_text): Likewise.
        * src/ptx.c (SKIP_SOMETHING, find_occurs_in_text): Likewise.
        * src/tac.c (tac_seekable): Likewise.
        * src/expr.c (docolon) Check for size calculation overflow.
        * src/nl.c (build_type_arg): Likewise.
        * src/ptx.c (matcher_error): New function.
        (SKIP_SOMETHING): Use it to report matcher errors.
        (alloc_and_compile_regex): No longer any need to worry about
        int versus size_t mismatch.
        * lib/regcomp.c, lib/regex.c, lib/regex.h, lib/regex_internal.c:
        * lib/regex_internal.h, lib/regexec.c, m4/regex.m4:
        Import from gnulib.

Index: src/csplit.c
===================================================================
RCS file: /fetish/cu/src/csplit.c,v
retrieving revision 1.143
diff -p -u -r1.143 csplit.c
--- src/csplit.c        12 Aug 2005 07:16:25 -0000      1.143
+++ src/csplit.c        9 Sep 2005 20:58:15 -0000
@@ -777,7 +777,7 @@ process_regexp (struct control *p, uintm
   size_t line_len;             /* To make "$" in regexps work. */
   uintmax_t break_line;                /* First line number of next file. */
   bool ignore = p->ignore;     /* If true, skip this section. */
-  int ret;
+  regoff_t ret;
 
   if (!ignore)
     create_output_file ();
@@ -854,7 +854,7 @@ process_regexp (struct control *p, uintm
              error (0, 0, _("error in regular expression search"));
              cleanup_fatal ();
            }
-         if (ret >= 0)
+         if (ret != -1)
            break;
        }
     }
Index: src/expr.c
===================================================================
RCS file: /fetish/cu/src/expr.c,v
retrieving revision 1.106
diff -p -u -r1.106 expr.c
--- src/expr.c  16 Jun 2005 21:41:57 -0000      1.106
+++ src/expr.c  9 Sep 2005 20:58:15 -0000
@@ -414,7 +414,7 @@ docolon (VALUE *sv, VALUE *pv)
   struct re_pattern_buffer re_buffer;
   struct re_registers re_regs;
   size_t len;
-  int matchlen;
+  regoff_t matchlen;
 
   tostring (sv);
   tostring (pv);
@@ -430,10 +430,8 @@ of the basic regular expression is not p
   len = strlen (pv->u.s);
   memset (&re_buffer, 0, sizeof (re_buffer));
   memset (&re_regs, 0, sizeof (re_regs));
+  re_buffer.buffer = xnmalloc (len, 2);
   re_buffer.allocated = 2 * len;
-  if (re_buffer.allocated < len)
-    xalloc_die ();
-  re_buffer.buffer = xmalloc (re_buffer.allocated);
   re_buffer.translate = NULL;
   re_syntax_options = RE_SYNTAX_POSIX_BASIC;
   errmsg = re_compile_pattern (pv->u.s, len, &re_buffer);
@@ -452,7 +450,7 @@ of the basic regular expression is not p
       else
        v = int_value (matchlen);
     }
-  else
+  else if (matchlen == -1)
     {
       /* Match failed -- return the right kind of null.  */
       if (re_buffer.re_nsub > 0)
@@ -460,6 +458,11 @@ of the basic regular expression is not p
       else
        v = int_value (0);
     }
+  else
+    error (EXPR_FAILURE,
+          (matchlen == -2 ? errno : EOVERFLOW),
+          _("error in regular expression matcher"));
+
   free (re_buffer.buffer);
   return v;
 }
Index: src/nl.c
===================================================================
RCS file: /fetish/cu/src/nl.c,v
retrieving revision 1.85
diff -p -u -r1.85 nl.c
--- src/nl.c    14 May 2005 07:58:37 -0000      1.85
+++ src/nl.c    9 Sep 2005 20:58:15 -0000
@@ -247,7 +247,7 @@ build_type_arg (char **typep, struct re_
       *typep = optarg++;
       optlen = strlen (optarg);
       regexp->allocated = optlen * 2;
-      regexp->buffer = xmalloc (regexp->allocated);
+      regexp->buffer = xnmalloc (optlen, 2);
       regexp->translate = NULL;
       regexp->fastmap = xmalloc (256);
       regexp->fastmap_accurate = 0;
@@ -342,12 +342,20 @@ proc_text (void)
       fputs (print_no_line_fmt, stdout);
       break;
     case 'p':
-      if (re_search (current_regex, line_buf.buffer, line_buf.length - 1,
-                    0, line_buf.length - 1, (struct re_registers *) 0) < 0)
-       fputs (print_no_line_fmt, stdout);
-      else
-       print_lineno ();
-      break;
+      switch (re_search (current_regex, line_buf.buffer, line_buf.length - 1,
+                        0, line_buf.length - 1, (struct re_registers *) 0))
+       {
+       case -2:
+         error (EXIT_FAILURE, errno, _("error in regular expression search"));
+
+       case -1:
+         fputs (print_no_line_fmt, stdout);
+         break;
+
+       default:
+         print_lineno ();
+         break;
+       }
     }
   fwrite (line_buf.buffer, sizeof (char), line_buf.length, stdout);
 }
Index: src/ptx.c
===================================================================
RCS file: /fetish/cu/src/ptx.c,v
retrieving revision 1.51
diff -p -u -r1.51 ptx.c
--- src/ptx.c   12 Aug 2005 08:18:18 -0000      1.51
+++ src/ptx.c   9 Sep 2005 20:58:16 -0000
@@ -190,9 +190,11 @@ static BLOCK text_buffer;  /* file to stu
 #define SKIP_SOMETHING(cursor, limit) \
   if (word_regex_string)                                               \
     {                                                                  \
-      int count;                                                       \
+      regoff_t count;                                                  \
       count = re_match (word_regex, cursor, limit - cursor, 0, NULL);  \
-      cursor += count <= 0 ? 1 : count;                                        
\
+      if (count == -2)                                                 \
+        matcher_error ();                                              \
+      cursor += count == -1 ? 1 : count;                               \
     }                                                                  \
   else if (word_fastmap[to_uchar (*cursor)])                           \
     while (cursor < limit && word_fastmap[to_uchar (*cursor)])         \
@@ -278,6 +280,15 @@ static BLOCK reference;            /* reference fi
 
 /* Miscellaneous routines.  */
 
+/* Diagnose an error in the regular expression matcher.  Then exit.  */
+
+static void ATTRIBUTE_NORETURN
+matcher_error (void)
+{
+  error (0, errno, _("error in regular expression matcher"));
+  exit (EXIT_FAILURE);
+}
+
 /*------------------------------------------------------.
 | Duplicate string STRING, while evaluating \-escapes.  |
 `------------------------------------------------------*/
@@ -417,12 +428,8 @@ alloc_and_compile_regex (const char *str
 
   /* Do not waste extra allocated space.  */
 
-  if (pattern->allocated > pattern->used)
-    {
-      pattern->buffer
-       = xrealloc (pattern->buffer, (size_t) pattern->used);
-      pattern->allocated = pattern->used;
-    }
+  pattern->buffer = xrealloc (pattern->buffer, pattern->used);
+  pattern->allocated = pattern->used;
 
   return pattern;
 }
@@ -872,14 +879,21 @@ find_occurs_in_text (void)
         This test also accounts for the case of an incomplete line or
         sentence at the end of the buffer.  */
 
-      if (context_regex_string
-         && (re_search (context_regex, cursor, text_buffer.end - cursor,
-                        0, text_buffer.end - cursor, &context_regs)
-             >= 0))
-       next_context_start = cursor + context_regs.end[0];
+      next_context_start = text_buffer.end;
+      if (context_regex_string)
+       switch (re_search (context_regex, cursor, text_buffer.end - cursor,
+                          0, text_buffer.end - cursor, &context_regs))
+         {
+         case -2:
+           matcher_error ();
 
-      else
-       next_context_start = text_buffer.end;
+         case -1:
+           break;
+
+         default:
+           next_context_start = cursor + context_regs.end[0];
+           break;
+         }
 
       /* Include the separator into the right context, but not any suffix
         white space in this separator; this insures it will be seen in
@@ -900,9 +914,11 @@ find_occurs_in_text (void)
               the loop.  */
 
            {
-             if (re_search (word_regex, cursor, context_end - cursor,
-                            0, context_end - cursor, &word_regs)
-                 < 0)
+             regoff_t r = re_search (word_regex, cursor, context_end - cursor,
+                                     0, context_end - cursor, &word_regs);
+             if (r == -2)
+               matcher_error ();
+             if (r == -1)
                break;
              word_start = cursor + word_regs.start[0];
              word_end = cursor + word_regs.end[0];
Index: src/tac.c
===================================================================
RCS file: /fetish/cu/src/tac.c,v
retrieving revision 1.124
diff -p -u -r1.124 tac.c
--- src/tac.c   11 Jul 2005 18:25:56 -0000      1.124
+++ src/tac.c   9 Sep 2005 20:58:16 -0000
@@ -255,14 +255,18 @@ tac_seekable (int input_fd, const char *
         Otherwise, make `match_start' < `G_buffer'. */
       if (sentinel_length == 0)
        {
-         ptrdiff_t i = match_start - G_buffer;
-         int ret;
+         size_t i = match_start - G_buffer;
+         regoff_t ri = i;
+         regoff_t range = 1 - ri;
+         regoff_t ret;
 
-         if (! (INT_MIN < i && i <= INT_MAX))
+         if (1 < range)
            error (EXIT_FAILURE, 0, _("record too large"));
 
-         ret = re_search (&compiled_separator, G_buffer, i, i - 1, -i, &regs);
-         if (ret == -1)
+         if (range == 1
+             || ((ret = re_search (&compiled_separator, G_buffer,
+                                   i, i - 1, range, &regs))
+                 == -1))
            match_start = G_buffer - 1;
          else if (ret == -2)
            {
Index: lib/regcomp.c
===================================================================
RCS file: /fetish/cu/lib/regcomp.c,v
retrieving revision 1.4
diff -p -u -r1.4 regcomp.c
--- lib/regcomp.c       23 Aug 2005 19:41:09 -0000      1.4
+++ lib/regcomp.c       9 Sep 2005 20:58:16 -0000
@@ -18,11 +18,11 @@
    Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
 
 static reg_errcode_t re_compile_internal (regex_t *preg, const char * pattern,
-                                         int length, reg_syntax_t syntax);
+                                         Idx length, reg_syntax_t syntax);
 static void re_compile_fastmap_iter (regex_t *bufp,
                                     const re_dfastate_t *init_state,
                                     char *fastmap);
-static reg_errcode_t init_dfa (re_dfa_t *dfa, int pat_len);
+static reg_errcode_t init_dfa (re_dfa_t *dfa, Idx pat_len);
 #ifdef RE_ENABLE_I18N
 static void free_charset (re_charset_t *cset);
 #endif /* RE_ENABLE_I18N */
@@ -45,14 +45,14 @@ static bin_tree_t *lower_subexp (reg_err
 static reg_errcode_t calc_first (void *extra, bin_tree_t *node);
 static reg_errcode_t calc_next (void *extra, bin_tree_t *node);
 static reg_errcode_t link_nfa_nodes (void *extra, bin_tree_t *node);
-static int duplicate_node (re_dfa_t *dfa, int org_idx, unsigned int 
constraint);
-static int search_duplicated_node (re_dfa_t *dfa, int org_node,
+static Idx duplicate_node (re_dfa_t *dfa, Idx org_idx, unsigned int 
constraint);
+static Idx search_duplicated_node (const re_dfa_t *dfa, Idx org_node,
                                   unsigned int constraint);
 static reg_errcode_t calc_eclosure (re_dfa_t *dfa);
 static reg_errcode_t calc_eclosure_iter (re_node_set *new_set, re_dfa_t *dfa,
-                                        int node, int root);
+                                        Idx node, bool root);
 static reg_errcode_t calc_inveclosure (re_dfa_t *dfa);
-static int fetch_number (re_string_t *input, re_token_t *token,
+static Idx fetch_number (re_string_t *input, re_token_t *token,
                         reg_syntax_t syntax);
 static int peek_token (re_token_t *token, re_string_t *input,
                        reg_syntax_t syntax);
@@ -60,16 +60,16 @@ static bin_tree_t *parse (re_string_t *r
                          reg_syntax_t syntax, reg_errcode_t *err);
 static bin_tree_t *parse_reg_exp (re_string_t *regexp, regex_t *preg,
                                  re_token_t *token, reg_syntax_t syntax,
-                                 int nest, reg_errcode_t *err);
+                                 Idx nest, reg_errcode_t *err);
 static bin_tree_t *parse_branch (re_string_t *regexp, regex_t *preg,
                                 re_token_t *token, reg_syntax_t syntax,
-                                int nest, reg_errcode_t *err);
+                                Idx nest, reg_errcode_t *err);
 static bin_tree_t *parse_expression (re_string_t *regexp, regex_t *preg,
                                     re_token_t *token, reg_syntax_t syntax,
-                                    int nest, reg_errcode_t *err);
+                                    Idx nest, reg_errcode_t *err);
 static bin_tree_t *parse_sub_exp (re_string_t *regexp, regex_t *preg,
                                  re_token_t *token, reg_syntax_t syntax,
-                                 int nest, reg_errcode_t *err);
+                                 Idx nest, reg_errcode_t *err);
 static bin_tree_t *parse_dup_op (bin_tree_t *dup_elem, re_string_t *regexp,
                                 re_dfa_t *dfa, re_token_t *token,
                                 reg_syntax_t syntax, reg_errcode_t *err);
@@ -81,34 +81,34 @@ static reg_errcode_t parse_bracket_eleme
                                            re_token_t *token, int token_len,
                                            re_dfa_t *dfa,
                                            reg_syntax_t syntax,
-                                           int accept_hyphen);
+                                           bool accept_hyphen);
 static reg_errcode_t parse_bracket_symbol (bracket_elem_t *elem,
                                          re_string_t *regexp,
                                          re_token_t *token);
 #ifdef RE_ENABLE_I18N
-static reg_errcode_t build_equiv_class (re_bitset_ptr_t sbcset,
+static reg_errcode_t build_equiv_class (bitset sbcset,
                                        re_charset_t *mbcset,
-                                       int *equiv_class_alloc,
+                                       Idx *equiv_class_alloc,
                                        const unsigned char *name);
-static reg_errcode_t build_charclass (unsigned RE_TRANSLATE_TYPE trans,
-                                     re_bitset_ptr_t sbcset,
+static reg_errcode_t build_charclass (unsigned REG_TRANSLATE_TYPE trans,
+                                     bitset sbcset,
                                      re_charset_t *mbcset,
-                                     int *char_class_alloc,
+                                     Idx *char_class_alloc,
                                      const unsigned char *class_name,
                                      reg_syntax_t syntax);
 #else  /* not RE_ENABLE_I18N */
-static reg_errcode_t build_equiv_class (re_bitset_ptr_t sbcset,
+static reg_errcode_t build_equiv_class (bitset sbcset,
                                        const unsigned char *name);
-static reg_errcode_t build_charclass (unsigned RE_TRANSLATE_TYPE trans,
-                                     re_bitset_ptr_t sbcset,
+static reg_errcode_t build_charclass (unsigned REG_TRANSLATE_TYPE trans,
+                                     bitset sbcset,
                                      const unsigned char *class_name,
                                      reg_syntax_t syntax);
 #endif /* not RE_ENABLE_I18N */
 static bin_tree_t *build_charclass_op (re_dfa_t *dfa,
-                                      unsigned RE_TRANSLATE_TYPE trans,
+                                      unsigned REG_TRANSLATE_TYPE trans,
                                       const unsigned char *class_name,
                                       const unsigned char *extra,
-                                      int non_match, reg_errcode_t *err);
+                                      bool non_match, reg_errcode_t *err);
 static bin_tree_t *create_tree (re_dfa_t *dfa,
                                bin_tree_t *left, bin_tree_t *right,
                                re_token_type_t type);
@@ -206,7 +206,7 @@ const size_t __re_error_msgid_idx[] attr
    compiles PATTERN (of length LENGTH) and puts the result in BUFP.
    Returns 0 if the pattern was valid, otherwise an error string.
 
-   Assumes the `allocated' (and perhaps `buffer') and `translate' fields
+   Assumes the `re_allocated' (and perhaps `re_buffer') and `translate' fields
    are set in BUFP on entry.  */
 
 const char *
@@ -217,11 +217,11 @@ re_compile_pattern (const char *pattern,
 
   /* And GNU code determines whether or not to get register information
      by passing null for the REGS argument to re_match, etc., not by
-     setting no_sub, unless RE_NO_SUB is set.  */
-  bufp->no_sub = !!(re_syntax_options & RE_NO_SUB);
+     setting re_no_sub, unless REG_NO_SUB is set.  */
+  bufp->re_no_sub = !!(re_syntax_options & REG_NO_SUB);
 
   /* Match anchors at newline.  */
-  bufp->newline_anchor = 1;
+  bufp->re_newline_anchor = 1;
 
   ret = re_compile_internal (bufp, pattern, length, re_syntax_options);
 
@@ -263,8 +263,8 @@ weak_alias (__re_set_syntax, re_set_synt
 int
 re_compile_fastmap (struct re_pattern_buffer *bufp)
 {
-  re_dfa_t *dfa = (re_dfa_t *) bufp->buffer;
-  char *fastmap = bufp->fastmap;
+  re_dfa_t *dfa = (re_dfa_t *) bufp->re_buffer;
+  char *fastmap = bufp->re_fastmap;
 
   memset (fastmap, '\0', sizeof (char) * SBC_MAX);
   re_compile_fastmap_iter (bufp, dfa->init_state, fastmap);
@@ -274,7 +274,7 @@ re_compile_fastmap (struct re_pattern_bu
     re_compile_fastmap_iter (bufp, dfa->init_state_nl, fastmap);
   if (dfa->init_state != dfa->init_state_begbuf)
     re_compile_fastmap_iter (bufp, dfa->init_state_begbuf, fastmap);
-  bufp->fastmap_accurate = 1;
+  bufp->re_fastmap_accurate = 1;
   return 0;
 }
 #ifdef _LIBC
@@ -283,7 +283,7 @@ weak_alias (__re_compile_fastmap, re_com
 
 static inline void
 __attribute ((always_inline))
-re_set_fastmap (char *fastmap, int icase, int ch)
+re_set_fastmap (char *fastmap, bool icase, int ch)
 {
   fastmap[ch] = 1;
   if (icase)
@@ -297,21 +297,22 @@ static void
 re_compile_fastmap_iter (regex_t *bufp, const re_dfastate_t *init_state,
                         char *fastmap)
 {
-  re_dfa_t *dfa = (re_dfa_t *) bufp->buffer;
-  int node_cnt;
-  int icase = (dfa->mb_cur_max == 1 && (bufp->syntax & RE_ICASE));
+  re_dfa_t *dfa = (re_dfa_t *) bufp->re_buffer;
+  Idx node_cnt;
+  bool icase = (dfa->mb_cur_max == 1 && (bufp->re_syntax & REG_IGNORE_CASE));
   for (node_cnt = 0; node_cnt < init_state->nodes.nelem; ++node_cnt)
     {
-      int node = init_state->nodes.elems[node_cnt];
+      Idx node = init_state->nodes.elems[node_cnt];
       re_token_type_t type = dfa->nodes[node].type;
 
       if (type == CHARACTER)
        {
          re_set_fastmap (fastmap, icase, dfa->nodes[node].opr.c);
 #ifdef RE_ENABLE_I18N
-         if ((bufp->syntax & RE_ICASE) && dfa->mb_cur_max > 1)
+         if ((bufp->re_syntax & REG_IGNORE_CASE) && dfa->mb_cur_max > 1)
            {
-             unsigned char *buf = alloca (dfa->mb_cur_max), *p;
+             unsigned char buf[MB_LEN_MAX];
+             unsigned char *p;
              wchar_t wc;
              mbstate_t state;
 
@@ -326,22 +327,22 @@ re_compile_fastmap_iter (regex_t *bufp, 
                           &state) == p - buf
                  && (__wcrtomb ((char *) buf, towlower (wc), &state)
                      != (size_t) -1))
-               re_set_fastmap (fastmap, 0, buf[0]);
+               re_set_fastmap (fastmap, false, buf[0]);
            }
 #endif
        }
       else if (type == SIMPLE_BRACKET)
        {
          int i, j, ch;
-         for (i = 0, ch = 0; i < BITSET_UINTS; ++i)
-           for (j = 0; j < UINT_BITS; ++j, ++ch)
-             if (dfa->nodes[node].opr.sbcset[i] & (1 << j))
+         for (i = 0, ch = 0; i < BITSET_WORDS; ++i)
+           for (j = 0; j < BITSET_WORD_BITS; ++j, ++ch)
+             if (dfa->nodes[node].opr.sbcset[i] & ((bitset_word) 1 << j))
                re_set_fastmap (fastmap, icase, ch);
        }
 #ifdef RE_ENABLE_I18N
       else if (type == COMPLEX_BRACKET)
        {
-         int i;
+         Idx i;
          re_charset_t *cset = dfa->nodes[node].opr.mbcset;
          if (cset->non_match || cset->ncoll_syms || cset->nequiv_classes
              || cset->nranges || cset->nchar_classes)
@@ -355,13 +356,11 @@ re_compile_fastmap_iter (regex_t *bufp, 
                          is a valid collation element, and don't catch
                          'b' since 'b' is the only collation element
                          which starts from 'b'.  */
-                 int j, ch;
                  const int32_t *table = (const int32_t *)
                    _NL_CURRENT (LC_COLLATE, _NL_COLLATE_TABLEMB);
-                 for (i = 0, ch = 0; i < BITSET_UINTS; ++i)
-                   for (j = 0; j < UINT_BITS; ++j, ++ch)
-                     if (table[ch] < 0)
-                       re_set_fastmap (fastmap, icase, ch);
+                 for (i = 0; i < SBC_MAX; ++i)
+                   if (table[i] < 0)
+                     re_set_fastmap (fastmap, icase, i);
                }
 # else
              if (dfa->mb_cur_max > 1)
@@ -377,11 +376,11 @@ re_compile_fastmap_iter (regex_t *bufp, 
              memset (&state, '\0', sizeof (state));
              if (__wcrtomb (buf, cset->mbchars[i], &state) != (size_t) -1)
                re_set_fastmap (fastmap, icase, *(unsigned char *) buf);
-             if ((bufp->syntax & RE_ICASE) && dfa->mb_cur_max > 1)
+             if ((bufp->re_syntax & REG_IGNORE_CASE) && dfa->mb_cur_max > 1)
                {
                  if (__wcrtomb (buf, towlower (cset->mbchars[i]), &state)
                      != (size_t) -1)
-                   re_set_fastmap (fastmap, 0, *(unsigned char *) buf);
+                   re_set_fastmap (fastmap, false, *(unsigned char *) buf);
                }
            }
        }
@@ -394,7 +393,7 @@ re_compile_fastmap_iter (regex_t *bufp, 
        {
          memset (fastmap, '\1', sizeof (char) * SBC_MAX);
          if (type == END_OF_RE)
-           bufp->can_be_null = 1;
+           bufp->re_can_be_null = 1;
          return;
        }
     }
@@ -406,14 +405,14 @@ re_compile_fastmap_iter (regex_t *bufp, 
    PREG is a regex_t *.  We do not expect any fields to be initialized,
    since POSIX says we shouldn't.  Thus, we set
 
-     `buffer' to the compiled pattern;
-     `used' to the length of the compiled pattern;
-     `syntax' to RE_SYNTAX_POSIX_EXTENDED if the
+     `re_buffer' to the compiled pattern;
+     `re_used' to the length of the compiled pattern;
+     `re_syntax' to REG_SYNTAX_POSIX_EXTENDED if the
        REG_EXTENDED bit in CFLAGS is set; otherwise, to
-       RE_SYNTAX_POSIX_BASIC;
-     `newline_anchor' to REG_NEWLINE being set in CFLAGS;
-     `fastmap' to an allocated space for the fastmap;
-     `fastmap_accurate' to zero;
+       REG_SYNTAX_POSIX_BASIC;
+     `re_newline_anchor' to REG_NEWLINE being set in CFLAGS;
+     `re_fastmap' to an allocated space for the fastmap;
+     `re_fastmap_accurate' to zero;
      `re_nsub' to the number of subexpressions in PATTERN.
 
    PATTERN is the address of the pattern string.
@@ -440,32 +439,32 @@ int
 regcomp (regex_t *__restrict preg, const char *__restrict pattern, int cflags)
 {
   reg_errcode_t ret;
-  reg_syntax_t syntax = ((cflags & REG_EXTENDED) ? RE_SYNTAX_POSIX_EXTENDED
-                        : RE_SYNTAX_POSIX_BASIC);
+  reg_syntax_t syntax = ((cflags & REG_EXTENDED) ? REG_SYNTAX_POSIX_EXTENDED
+                       : REG_SYNTAX_POSIX_BASIC);
 
-  preg->buffer = NULL;
-  preg->allocated = 0;
-  preg->used = 0;
+  preg->re_buffer = NULL;
+  preg->re_allocated = 0;
+  preg->re_used = 0;
 
   /* Try to allocate space for the fastmap.  */
-  preg->fastmap = re_malloc (char, SBC_MAX);
-  if (BE (preg->fastmap == NULL, 0))
+  preg->re_fastmap = re_malloc (char, SBC_MAX);
+  if (BE (preg->re_fastmap == NULL, 0))
     return REG_ESPACE;
 
-  syntax |= (cflags & REG_ICASE) ? RE_ICASE : 0;
+  syntax |= (cflags & REG_ICASE) ? REG_IGNORE_CASE : 0;
 
   /* If REG_NEWLINE is set, newlines are treated differently.  */
   if (cflags & REG_NEWLINE)
     { /* REG_NEWLINE implies neither . nor [^...] match newline.  */
-      syntax &= ~RE_DOT_NEWLINE;
-      syntax |= RE_HAT_LISTS_NOT_NEWLINE;
+      syntax &= ~REG_DOT_NEWLINE;
+      syntax |= REG_HAT_LISTS_NOT_NEWLINE;
       /* It also changes the matching behavior.  */
-      preg->newline_anchor = 1;
+      preg->re_newline_anchor = 1;
     }
   else
-    preg->newline_anchor = 0;
-  preg->no_sub = !!(cflags & REG_NOSUB);
-  preg->translate = NULL;
+    preg->re_newline_anchor = 0;
+  preg->re_no_sub = !!(cflags & REG_NOSUB);
+  preg->re_translate = NULL;
 
   ret = re_compile_internal (preg, pattern, strlen (pattern), syntax);
 
@@ -474,7 +473,7 @@ regcomp (regex_t *__restrict preg, const
   if (ret == REG_ERPAREN)
     ret = REG_EPAREN;
 
-  /* We have already checked preg->fastmap != NULL.  */
+  /* We have already checked preg->re_fastmap != NULL.  */
   if (BE (ret == REG_NOERROR, 1))
     /* Compute the fastmap now, since regexec cannot modify the pattern
        buffer.  This function never fails in this implementation.  */
@@ -482,8 +481,8 @@ regcomp (regex_t *__restrict preg, const
   else
     {
       /* Some error occurred while compiling the expression.  */
-      re_free (preg->fastmap);
-      preg->fastmap = NULL;
+      re_free (preg->re_fastmap);
+      preg->re_fastmap = NULL;
     }
 
   return (int) ret;
@@ -496,7 +495,8 @@ weak_alias (__regcomp, regcomp)
    from either regcomp or regexec.   We don't use PREG here.  */
 
 size_t
-regerror (int errcode, const regex_t *preg, char *errbuf, size_t errbuf_size)
+regerror (int errcode, const regex_t *__restrict preg,
+         char *__restrict errbuf, size_t errbuf_size)
 {
   const char *msg;
   size_t msg_size;
@@ -544,11 +544,22 @@ weak_alias (__regerror, regerror)
 static const bitset utf8_sb_map =
 {
   /* Set the first 128 bits.  */
-# if UINT_MAX == 0xffffffff
-  0xffffffff, 0xffffffff, 0xffffffff, 0xffffffff
-# else
-#  error "Add case for new unsigned int size"
+# if 2 < BITSET_WORDS
+  BITSET_WORD_MAX,
+# endif
+# if 4 < BITSET_WORDS
+  BITSET_WORD_MAX,
+# endif
+# if 6 < BITSET_WORDS
+  BITSET_WORD_MAX,
+# endif
+# if 8 < BITSET_WORDS
+#  error "Invalid BITSET_WORDS"
 # endif
+  (BITSET_WORD_MAX
+   >> (SBC_MAX % BITSET_WORD_BITS == 0
+       ? 0
+       : BITSET_WORD_BITS - SBC_MAX % BITSET_WORD_BITS))
 };
 #endif
 
@@ -556,7 +567,7 @@ static const bitset utf8_sb_map =
 static void
 free_dfa_content (re_dfa_t *dfa)
 {
-  int i, j;
+  Idx i, j;
 
   if (dfa->nodes)
     for (i = 0; i < dfa->nodes_len; ++i)
@@ -606,17 +617,17 @@ free_dfa_content (re_dfa_t *dfa)
 void
 regfree (regex_t *preg)
 {
-  re_dfa_t *dfa = (re_dfa_t *) preg->buffer;
+  re_dfa_t *dfa = (re_dfa_t *) preg->re_buffer;
   if (BE (dfa != NULL, 1))
     free_dfa_content (dfa);
-  preg->buffer = NULL;
-  preg->allocated = 0;
+  preg->re_buffer = NULL;
+  preg->re_allocated = 0;
 
-  re_free (preg->fastmap);
-  preg->fastmap = NULL;
+  re_free (preg->re_fastmap);
+  preg->re_fastmap = NULL;
 
-  re_free (preg->translate);
-  preg->translate = NULL;
+  re_free (preg->re_translate);
+  preg->re_translate = NULL;
 }
 #ifdef _LIBC
 weak_alias (__regfree, regfree)
@@ -637,32 +648,31 @@ char *
    regcomp/regexec above without link errors.  */
 weak_function
 # endif
-re_comp (s)
-     const char *s;
+re_comp (const char *s)
 {
   reg_errcode_t ret;
   char *fastmap;
 
   if (!s)
     {
-      if (!re_comp_buf.buffer)
+      if (!re_comp_buf.re_buffer)
        return gettext ("No previous regular expression");
       return 0;
     }
 
-  if (re_comp_buf.buffer)
+  if (re_comp_buf.re_buffer)
     {
-      fastmap = re_comp_buf.fastmap;
-      re_comp_buf.fastmap = NULL;
+      fastmap = re_comp_buf.re_fastmap;
+      re_comp_buf.re_fastmap = NULL;
       __regfree (&re_comp_buf);
       memset (&re_comp_buf, '\0', sizeof (re_comp_buf));
-      re_comp_buf.fastmap = fastmap;
+      re_comp_buf.re_fastmap = fastmap;
     }
 
-  if (re_comp_buf.fastmap == NULL)
+  if (re_comp_buf.re_fastmap == NULL)
     {
-      re_comp_buf.fastmap = (char *) malloc (SBC_MAX);
-      if (re_comp_buf.fastmap == NULL)
+      re_comp_buf.re_fastmap = (char *) malloc (SBC_MAX);
+      if (re_comp_buf.re_fastmap == NULL)
        return (char *) gettext (__re_error_msgid
                                 + __re_error_msgid_idx[(int) REG_ESPACE]);
     }
@@ -671,7 +681,7 @@ re_comp (s)
      don't need to initialize the pattern buffer fields which affect it.  */
 
   /* Match anchors at newlines.  */
-  re_comp_buf.newline_anchor = 1;
+  re_comp_buf.re_newline_anchor = 1;
 
   ret = re_compile_internal (&re_comp_buf, s, strlen (s), re_syntax_options);
 
@@ -696,7 +706,7 @@ libc_freeres_fn (free_mem)
    SYNTAX indicate regular expression's syntax.  */
 
 static reg_errcode_t
-re_compile_internal (regex_t *preg, const char * pattern, int length,
+re_compile_internal (regex_t *preg, const char *pattern, Idx length,
                     reg_syntax_t syntax)
 {
   reg_errcode_t err = REG_NOERROR;
@@ -704,29 +714,29 @@ re_compile_internal (regex_t *preg, cons
   re_string_t regexp;
 
   /* Initialize the pattern buffer.  */
-  preg->fastmap_accurate = 0;
-  preg->syntax = syntax;
-  preg->not_bol = preg->not_eol = 0;
-  preg->used = 0;
+  preg->re_fastmap_accurate = 0;
+  preg->re_syntax = syntax;
+  preg->re_not_bol = preg->re_not_eol = 0;
+  preg->re_used = 0;
   preg->re_nsub = 0;
-  preg->can_be_null = 0;
-  preg->regs_allocated = REGS_UNALLOCATED;
+  preg->re_can_be_null = 0;
+  preg->re_regs_allocated = REG_UNALLOCATED;
 
   /* Initialize the dfa.  */
-  dfa = (re_dfa_t *) preg->buffer;
-  if (BE (preg->allocated < sizeof (re_dfa_t), 0))
+  dfa = (re_dfa_t *) preg->re_buffer;
+  if (BE (preg->re_allocated < sizeof (re_dfa_t), 0))
     {
       /* If zero allocated, but buffer is non-null, try to realloc
         enough space.  This loses if buffer's address is bogus, but
-        that is the user's responsibility.  If ->buffer is NULL this
+        that is the user's responsibility.  If buffer is null this
         is a simple allocation.  */
-      dfa = re_realloc (preg->buffer, re_dfa_t, 1);
+      dfa = re_realloc (preg->re_buffer, re_dfa_t, 1);
       if (dfa == NULL)
        return REG_ESPACE;
-      preg->allocated = sizeof (re_dfa_t);
-      preg->buffer = (unsigned char *) dfa;
+      preg->re_allocated = sizeof (re_dfa_t);
+      preg->re_buffer = (unsigned char *) dfa;
     }
-  preg->used = sizeof (re_dfa_t);
+  preg->re_used = sizeof (re_dfa_t);
 
   __libc_lock_init (dfa->lock);
 
@@ -734,8 +744,8 @@ re_compile_internal (regex_t *preg, cons
   if (BE (err != REG_NOERROR, 0))
     {
       free_dfa_content (dfa);
-      preg->buffer = NULL;
-      preg->allocated = 0;
+      preg->re_buffer = NULL;
+      preg->re_allocated = 0;
       return err;
     }
 #ifdef DEBUG
@@ -743,16 +753,16 @@ re_compile_internal (regex_t *preg, cons
   strncpy (dfa->re_str, pattern, length + 1);
 #endif
 
-  err = re_string_construct (&regexp, pattern, length, preg->translate,
-                            syntax & RE_ICASE, dfa);
+  err = re_string_construct (&regexp, pattern, length, preg->re_translate,
+                            syntax & REG_IGNORE_CASE, dfa);
   if (BE (err != REG_NOERROR, 0))
     {
     re_compile_internal_free_return:
       free_workarea_compile (preg);
       re_string_destruct (&regexp);
       free_dfa_content (dfa);
-      preg->buffer = NULL;
-      preg->allocated = 0;
+      preg->re_buffer = NULL;
+      preg->re_allocated = 0;
       return err;
     }
 
@@ -769,7 +779,7 @@ re_compile_internal (regex_t *preg, cons
 
 #ifdef RE_ENABLE_I18N
   /* If possible, do searching in single byte encoding to speed things up.  */
-  if (dfa->is_utf8 && !(syntax & RE_ICASE) && preg->translate == NULL)
+  if (dfa->is_utf8 && !(syntax & REG_IGNORE_CASE) && preg->re_translate == 
NULL)
     optimize_utf8 (dfa);
 #endif
 
@@ -783,8 +793,8 @@ re_compile_internal (regex_t *preg, cons
   if (BE (err != REG_NOERROR, 0))
     {
       free_dfa_content (dfa);
-      preg->buffer = NULL;
-      preg->allocated = 0;
+      preg->re_buffer = NULL;
+      preg->re_allocated = 0;
     }
 
   return err;
@@ -794,9 +804,9 @@ re_compile_internal (regex_t *preg, cons
    as the initial length of some arrays.  */
 
 static reg_errcode_t
-init_dfa (re_dfa_t *dfa, int pat_len)
+init_dfa (re_dfa_t *dfa, Idx pat_len)
 {
-  int table_size;
+  __re_size_t table_size;
 #ifndef _LIBC
   char *codeset_name;
 #endif
@@ -807,16 +817,14 @@ init_dfa (re_dfa_t *dfa, int pat_len)
   dfa->str_tree_storage_idx = BIN_TREE_STORAGE_SIZE;
 
   dfa->nodes_alloc = pat_len + 1;
-  dfa->nodes = re_malloc (re_token_t, dfa->nodes_alloc);
-
-  dfa->states_alloc = pat_len + 1;
+  dfa->nodes = re_xmalloc (re_token_t, dfa->nodes_alloc);
 
   /*  table_size = 2 ^ ceil(log pat_len) */
-  for (table_size = 1; table_size > 0; table_size <<= 1)
-    if (table_size > pat_len)
-      break;
+  for (table_size = 1; table_size <= pat_len; table_size <<= 1)
+    if (0 < (Idx) -1 && table_size == 0)
+      return REG_ESPACE;
 
-  dfa->state_table = calloc (sizeof (struct re_state_table_entry), table_size);
+  dfa->state_table = re_calloc (struct re_state_table_entry, table_size);
   dfa->state_hash_mask = table_size - 1;
 
   dfa->mb_cur_max = MB_CUR_MAX;
@@ -859,20 +867,17 @@ init_dfa (re_dfa_t *dfa, int pat_len)
        {
          int i, j, ch;
 
-         dfa->sb_char = (re_bitset_ptr_t) calloc (sizeof (bitset), 1);
+         dfa->sb_char = re_calloc (bitset_word, BITSET_WORDS);
          if (BE (dfa->sb_char == NULL, 0))
            return REG_ESPACE;
 
-         /* Clear all bits by, then set those corresponding to single
-            byte chars.  */
-         bitset_empty (dfa->sb_char);
-
-         for (i = 0, ch = 0; i < BITSET_UINTS; ++i)
-           for (j = 0; j < UINT_BITS; ++j, ++ch)
+         /* Set the bits corresponding to single byte chars.  */
+         for (i = 0, ch = 0; i < BITSET_WORDS; ++i)
+           for (j = 0; j < BITSET_WORD_BITS; ++j, ++ch)
              {
                wint_t wch = __btowc (ch);
                if (wch != WEOF)
-                 dfa->sb_char[i] |= 1 << j;
+                 dfa->sb_char[i] |= (bitset_word) 1 << j;
 # ifndef _LIBC
                if (isascii (ch) && wch != ch)
                  dfa->map_notascii = 1;
@@ -896,10 +901,10 @@ init_word_char (re_dfa_t *dfa)
 {
   int i, j, ch;
   dfa->word_ops_used = 1;
-  for (i = 0, ch = 0; i < BITSET_UINTS; ++i)
-    for (j = 0; j < UINT_BITS; ++j, ++ch)
+  for (i = 0, ch = 0; i < BITSET_WORDS; ++i)
+    for (j = 0; j < BITSET_WORD_BITS; ++j, ++ch)
       if (isalnum (ch) || ch == '_')
-       dfa->word_char[i] |= 1 << j;
+       dfa->word_char[i] |= (bitset_word) 1 << j;
 }
 
 /* Free the work area which are only used while compiling.  */
@@ -907,7 +912,7 @@ init_word_char (re_dfa_t *dfa)
 static void
 free_workarea_compile (regex_t *preg)
 {
-  re_dfa_t *dfa = (re_dfa_t *) preg->buffer;
+  re_dfa_t *dfa = (re_dfa_t *) preg->re_buffer;
   bin_tree_storage_t *storage, *next;
   for (storage = dfa->str_tree_storage; storage; storage = next)
     {
@@ -926,7 +931,7 @@ free_workarea_compile (regex_t *preg)
 static reg_errcode_t
 create_initial_state (re_dfa_t *dfa)
 {
-  int first, i;
+  Idx first, i;
   reg_errcode_t err;
   re_node_set init_nodes;
 
@@ -945,10 +950,10 @@ create_initial_state (re_dfa_t *dfa)
   if (dfa->nbackref > 0)
     for (i = 0; i < init_nodes.nelem; ++i)
       {
-       int node_idx = init_nodes.elems[i];
+       Idx node_idx = init_nodes.elems[i];
        re_token_type_t type = dfa->nodes[node_idx].type;
 
-       int clexp_idx;
+       Idx clexp_idx;
        if (type != OP_BACK_REF)
          continue;
        for (clexp_idx = 0; clexp_idx < init_nodes.nelem; ++clexp_idx)
@@ -964,7 +969,7 @@ create_initial_state (re_dfa_t *dfa)
 
        if (type == OP_BACK_REF)
          {
-           int dest_idx = dfa->edests[node_idx].elems[0];
+           Idx dest_idx = dfa->edests[node_idx].elems[0];
            if (!re_node_set_contains (&init_nodes, dest_idx))
              {
                re_node_set_merge (&init_nodes, dfa->eclosures + dest_idx);
@@ -1008,14 +1013,17 @@ create_initial_state (re_dfa_t *dfa)
 static void
 optimize_utf8 (re_dfa_t *dfa)
 {
-  int node, i, mb_chars = 0, has_period = 0;
+  Idx node;
+  int i;
+  bool mb_chars = false;
+  bool has_period = false;
 
   for (node = 0; node < dfa->nodes_len; ++node)
     switch (dfa->nodes[node].type)
       {
       case CHARACTER:
        if (dfa->nodes[node].opr.c >= 0x80)
-         mb_chars = 1;
+         mb_chars = true;
        break;
       case ANCHOR:
        switch (dfa->nodes[node].opr.idx)
@@ -1031,7 +1039,7 @@ optimize_utf8 (re_dfa_t *dfa)
          }
        break;
       case OP_PERIOD:
-        has_period = 1;
+        has_period = true;
         break;
       case OP_BACK_REF:
       case OP_ALT:
@@ -1044,9 +1052,18 @@ optimize_utf8 (re_dfa_t *dfa)
        return;
       case SIMPLE_BRACKET:
        /* Just double check.  */
-        for (i = 0x80 / UINT_BITS; i < BITSET_UINTS; ++i)
-         if (dfa->nodes[node].opr.sbcset[i])
-           return;
+       {
+         int rshift =
+           (SBC_MAX / 2 % BITSET_WORD_BITS == 0
+            ? 0
+            : BITSET_WORD_BITS - SBC_MAX / 2 % BITSET_WORD_BITS);
+         for (i = SBC_MAX / 2 / BITSET_WORD_BITS; i < BITSET_WORDS; ++i)
+           {
+             if (dfa->nodes[node].opr.sbcset[i] >> rshift != 0)
+               return;
+             rshift = 0;
+           }
+       }
        break;
       default:
        abort ();
@@ -1075,22 +1092,22 @@ optimize_utf8 (re_dfa_t *dfa)
 static reg_errcode_t
 analyze (regex_t *preg)
 {
-  re_dfa_t *dfa = (re_dfa_t *) preg->buffer;
+  re_dfa_t *dfa = (re_dfa_t *) preg->re_buffer;
   reg_errcode_t ret;
 
   /* Allocate arrays.  */
-  dfa->nexts = re_malloc (int, dfa->nodes_alloc);
-  dfa->org_indices = re_malloc (int, dfa->nodes_alloc);
-  dfa->edests = re_malloc (re_node_set, dfa->nodes_alloc);
+  dfa->nexts = re_malloc (Idx, dfa->nodes_alloc);
+  dfa->org_indices = re_malloc (Idx, dfa->nodes_alloc);
+  dfa->edests = re_xmalloc (re_node_set, dfa->nodes_alloc);
   dfa->eclosures = re_malloc (re_node_set, dfa->nodes_alloc);
   if (BE (dfa->nexts == NULL || dfa->org_indices == NULL || dfa->edests == NULL
          || dfa->eclosures == NULL, 0))
     return REG_ESPACE;
 
-  dfa->subexp_map = re_malloc (int, preg->re_nsub);
+  dfa->subexp_map = re_xmalloc (Idx, preg->re_nsub);
   if (dfa->subexp_map != NULL)
     {
-      int i;
+      Idx i;
       for (i = 0; i < preg->re_nsub; i++)
        dfa->subexp_map[i] = i;
       preorder (dfa->str_tree, optimize_subexps, dfa);
@@ -1120,10 +1137,10 @@ analyze (regex_t *preg)
 
   /* We only need this during the prune_impossible_nodes pass in regexec.c;
      skip it if p_i_n will not run, as calc_inveclosure can be quadratic.  */
-  if ((!preg->no_sub && preg->re_nsub > 0 && dfa->has_plural_match)
+  if ((!preg->re_no_sub && preg->re_nsub > 0 && dfa->has_plural_match)
       || dfa->nbackref)
     {
-      dfa->inveclosures = re_malloc (re_node_set, dfa->nodes_len);
+      dfa->inveclosures = re_xmalloc (re_node_set, dfa->nodes_len);
       if (BE (dfa->inveclosures == NULL, 0))
         return REG_ESPACE;
       ret = calc_inveclosure (dfa);
@@ -1215,15 +1232,15 @@ optimize_subexps (void *extra, bin_tree_
   else if (node->token.type == SUBEXP
            && node->left && node->left->token.type == SUBEXP)
     {
-      int other_idx = node->left->token.opr.idx;
+      Idx other_idx = node->left->token.opr.idx;
 
       node->left = node->left->left;
       if (node->left)
         node->left->parent = node;
 
       dfa->subexp_map[other_idx] = dfa->subexp_map[node->token.opr.idx];
-      if (other_idx < 8 * sizeof (dfa->used_bkref_map))
-       dfa->used_bkref_map &= ~(1 << other_idx);
+      if (other_idx < BITSET_WORD_BITS)
+       dfa->used_bkref_map &= ~ ((bitset_word) 1 << other_idx);
     }
 
   return REG_NOERROR;
@@ -1256,18 +1273,18 @@ lower_subexps (void *extra, bin_tree_t *
 static bin_tree_t *
 lower_subexp (reg_errcode_t *err, regex_t *preg, bin_tree_t *node)
 {
-  re_dfa_t *dfa = (re_dfa_t *) preg->buffer;
+  re_dfa_t *dfa = (re_dfa_t *) preg->re_buffer;
   bin_tree_t *body = node->left;
   bin_tree_t *op, *cls, *tree1, *tree;
 
-  if (preg->no_sub
+  if (preg->re_no_sub
       /* We do not optimize empty subexpressions, because otherwise we may
         have bad CONCAT nodes with NULL children.  This is obviously not
         very common, so we do not lose much.  An example that triggers
         this case is the sed "script" /\(\)/x.  */
       && node->left != NULL
-      && (node->token.opr.idx >= 8 * sizeof (dfa->used_bkref_map)
-         || !(dfa->used_bkref_map & (1 << node->token.opr.idx))))
+      && ! (node->token.opr.idx < BITSET_WORD_BITS
+           && dfa->used_bkref_map & ((bitset_word) 1 << node->token.opr.idx)))
     return node->left;
 
   /* Convert the SUBEXP node to the concatenation of an
@@ -1302,7 +1319,7 @@ calc_first (void *extra, bin_tree_t *nod
     {
       node->first = node;
       node->node_idx = re_dfa_add_node (dfa, node->token);
-      if (BE (node->node_idx == -1, 0))
+      if (BE (node->node_idx == REG_MISSING, 0))
         return REG_ESPACE;
     }
   return REG_NOERROR;
@@ -1336,7 +1353,7 @@ static reg_errcode_t
 link_nfa_nodes (void *extra, bin_tree_t *node)
 {
   re_dfa_t *dfa = (re_dfa_t *) extra;
-  int idx = node->node_idx;
+  Idx idx = node->node_idx;
   reg_errcode_t err = REG_NOERROR;
 
   switch (node->token.type)
@@ -1351,7 +1368,7 @@ link_nfa_nodes (void *extra, bin_tree_t 
     case OP_DUP_ASTERISK:
     case OP_ALT:
       {
-       int left, right;
+       Idx left, right;
        dfa->has_plural_match = 1;
        if (node->left != NULL)
          left = node->left->first->node_idx;
@@ -1361,8 +1378,8 @@ link_nfa_nodes (void *extra, bin_tree_t 
          right = node->right->first->node_idx;
        else
          right = node->next->node_idx;
-       assert (left > -1);
-       assert (right > -1);
+       assert (REG_VALID_INDEX (left));
+       assert (REG_VALID_INDEX (right));
        err = re_node_set_init_2 (dfa->edests + idx, left, right);
       }
       break;
@@ -1393,14 +1410,16 @@ link_nfa_nodes (void *extra, bin_tree_t 
    to their own constraint.  */
 
 static reg_errcode_t
-duplicate_node_closure (re_dfa_t *dfa, int top_org_node, int top_clone_node,
-                       int root_node, unsigned int init_constraint)
+duplicate_node_closure (re_dfa_t *dfa, Idx top_org_node,
+                       Idx top_clone_node, Idx root_node,
+                       unsigned int init_constraint)
 {
-  int org_node, clone_node, ret;
+  Idx org_node, clone_node;
+  bool ok;
   unsigned int constraint = init_constraint;
   for (org_node = top_org_node, clone_node = top_clone_node;;)
     {
-      int org_dest, clone_dest;
+      Idx org_dest, clone_dest;
       if (dfa->nodes[org_node].type == OP_BACK_REF)
        {
          /* If the back reference epsilon-transit, its destination must
@@ -1410,11 +1429,11 @@ duplicate_node_closure (re_dfa_t *dfa, i
          org_dest = dfa->nexts[org_node];
          re_node_set_empty (dfa->edests + clone_node);
          clone_dest = duplicate_node (dfa, org_dest, constraint);
-         if (BE (clone_dest == -1, 0))
+         if (BE (clone_dest == REG_MISSING, 0))
            return REG_ESPACE;
          dfa->nexts[clone_node] = dfa->nexts[org_node];
-         ret = re_node_set_insert (dfa->edests + clone_node, clone_dest);
-         if (BE (ret < 0, 0))
+         ok = re_node_set_insert (dfa->edests + clone_node, clone_dest);
+         if (BE (! ok, 0))
            return REG_ESPACE;
        }
       else if (dfa->edests[org_node].nelem == 0)
@@ -1439,19 +1458,19 @@ duplicate_node_closure (re_dfa_t *dfa, i
                  /* ...but if the node is root_node itself, it means the
                     epsilon closure have a loop, then tie it to the
                     destination of the root_node.  */
-                 ret = re_node_set_insert (dfa->edests + clone_node,
+                 ok = re_node_set_insert (dfa->edests + clone_node,
                                            org_dest);
-                 if (BE (ret < 0, 0))
+                 if (BE (! ok, 0))
                    return REG_ESPACE;
                  break;
                }
              constraint |= dfa->nodes[org_node].opr.ctx_type;
            }
          clone_dest = duplicate_node (dfa, org_dest, constraint);
-         if (BE (clone_dest == -1, 0))
+         if (BE (clone_dest == REG_MISSING, 0))
            return REG_ESPACE;
-         ret = re_node_set_insert (dfa->edests + clone_node, clone_dest);
-         if (BE (ret < 0, 0))
+         ok = re_node_set_insert (dfa->edests + clone_node, clone_dest);
+         if (BE (! ok, 0))
            return REG_ESPACE;
        }
       else /* dfa->edests[org_node].nelem == 2 */
@@ -1462,15 +1481,15 @@ duplicate_node_closure (re_dfa_t *dfa, i
          re_node_set_empty (dfa->edests + clone_node);
          /* Search for a duplicated node which satisfies the constraint.  */
          clone_dest = search_duplicated_node (dfa, org_dest, constraint);
-         if (clone_dest == -1)
+         if (clone_dest == REG_MISSING)
            {
              /* There are no such a duplicated node, create a new one.  */
              reg_errcode_t err;
              clone_dest = duplicate_node (dfa, org_dest, constraint);
-             if (BE (clone_dest == -1, 0))
+             if (BE (clone_dest == REG_MISSING, 0))
                return REG_ESPACE;
-             ret = re_node_set_insert (dfa->edests + clone_node, clone_dest);
-             if (BE (ret < 0, 0))
+             ok = re_node_set_insert (dfa->edests + clone_node, clone_dest);
+             if (BE (! ok, 0))
                return REG_ESPACE;
              err = duplicate_node_closure (dfa, org_dest, clone_dest,
                                            root_node, constraint);
@@ -1481,17 +1500,17 @@ duplicate_node_closure (re_dfa_t *dfa, i
            {
              /* There are a duplicated node which satisfy the constraint,
                 use it to avoid infinite loop.  */
-             ret = re_node_set_insert (dfa->edests + clone_node, clone_dest);
-             if (BE (ret < 0, 0))
+             ok = re_node_set_insert (dfa->edests + clone_node, clone_dest);
+             if (BE (! ok, 0))
                return REG_ESPACE;
            }
 
          org_dest = dfa->edests[org_node].elems[1];
          clone_dest = duplicate_node (dfa, org_dest, constraint);
-         if (BE (clone_dest == -1, 0))
+         if (BE (clone_dest == REG_MISSING, 0))
            return REG_ESPACE;
-         ret = re_node_set_insert (dfa->edests + clone_node, clone_dest);
-         if (BE (ret < 0, 0))
+         ok = re_node_set_insert (dfa->edests + clone_node, clone_dest);
+         if (BE (! ok, 0))
            return REG_ESPACE;
        }
       org_node = org_dest;
@@ -1503,28 +1522,29 @@ duplicate_node_closure (re_dfa_t *dfa, i
 /* Search for a node which is duplicated from the node ORG_NODE, and
    satisfies the constraint CONSTRAINT.  */
 
-static int
-search_duplicated_node (re_dfa_t *dfa, int org_node, unsigned int constraint)
+static Idx
+search_duplicated_node (const re_dfa_t *dfa, Idx org_node,
+                       unsigned int constraint)
 {
-  int idx;
+  Idx idx;
   for (idx = dfa->nodes_len - 1; dfa->nodes[idx].duplicated && idx > 0; --idx)
     {
       if (org_node == dfa->org_indices[idx]
          && constraint == dfa->nodes[idx].constraint)
        return idx; /* Found.  */
     }
-  return -1; /* Not found.  */
+  return REG_MISSING; /* Not found.  */
 }
 
 /* Duplicate the node whose index is ORG_IDX and set the constraint CONSTRAINT.
-   Return the index of the new node, or -1 if insufficient storage is
+   Return the index of the new node, or REG_MISSING if insufficient storage is
    available.  */
 
-static int
-duplicate_node (re_dfa_t *dfa, int org_idx, unsigned int constraint)
+static Idx
+duplicate_node (re_dfa_t *dfa, Idx org_idx, unsigned int constraint)
 {
-  int dup_idx = re_dfa_add_node (dfa, dfa->nodes[org_idx]);
-  if (BE (dup_idx != -1, 1))
+  Idx dup_idx = re_dfa_add_node (dfa, dfa->nodes[org_idx]);
+  if (BE (dup_idx != REG_MISSING, 1))
     {
       dfa->nodes[dup_idx].constraint = constraint;
       if (dfa->nodes[org_idx].type == ANCHOR)
@@ -1540,17 +1560,18 @@ duplicate_node (re_dfa_t *dfa, int org_i
 static reg_errcode_t
 calc_inveclosure (re_dfa_t *dfa)
 {
-  int src, idx, ret;
+  Idx src, idx;
+  bool ok;
   for (idx = 0; idx < dfa->nodes_len; ++idx)
     re_node_set_init_empty (dfa->inveclosures + idx);
 
   for (src = 0; src < dfa->nodes_len; ++src)
     {
-      int *elems = dfa->eclosures[src].elems;
+      Idx *elems = dfa->eclosures[src].elems;
       for (idx = 0; idx < dfa->eclosures[src].nelem; ++idx)
        {
-         ret = re_node_set_insert_last (dfa->inveclosures + elems[idx], src);
-         if (BE (ret == -1, 0))
+         ok = re_node_set_insert_last (dfa->inveclosures + elems[idx], src);
+         if (BE (! ok, 0))
            return REG_ESPACE;
        }
     }
@@ -1563,11 +1584,12 @@ calc_inveclosure (re_dfa_t *dfa)
 static reg_errcode_t
 calc_eclosure (re_dfa_t *dfa)
 {
-  int node_idx, incomplete;
+  Idx node_idx;
+  bool incomplete;
 #ifdef DEBUG
   assert (dfa->nodes_len > 0);
 #endif
-  incomplete = 0;
+  incomplete = false;
   /* For each nodes, calculate epsilon closure.  */
   for (node_idx = 0; ; ++node_idx)
     {
@@ -1577,25 +1599,25 @@ calc_eclosure (re_dfa_t *dfa)
        {
          if (!incomplete)
            break;
-         incomplete = 0;
+         incomplete = false;
          node_idx = 0;
        }
 
 #ifdef DEBUG
-      assert (dfa->eclosures[node_idx].nelem != -1);
+      assert (dfa->eclosures[node_idx].nelem != REG_MISSING);
 #endif
 
       /* If we have already calculated, skip it.  */
       if (dfa->eclosures[node_idx].nelem != 0)
        continue;
       /* Calculate epsilon closure of `node_idx'.  */
-      err = calc_eclosure_iter (&eclosure_elem, dfa, node_idx, 1);
+      err = calc_eclosure_iter (&eclosure_elem, dfa, node_idx, true);
       if (BE (err != REG_NOERROR, 0))
        return err;
 
       if (dfa->eclosures[node_idx].nelem == 0)
        {
-         incomplete = 1;
+         incomplete = true;
          re_node_set_free (&eclosure_elem);
        }
     }
@@ -1605,20 +1627,22 @@ calc_eclosure (re_dfa_t *dfa)
 /* Calculate epsilon closure of NODE.  */
 
 static reg_errcode_t
-calc_eclosure_iter (re_node_set *new_set, re_dfa_t *dfa, int node, int root)
+calc_eclosure_iter (re_node_set *new_set, re_dfa_t *dfa, Idx node, bool root)
 {
   reg_errcode_t err;
   unsigned int constraint;
-  int i, incomplete;
+  Idx i;
+  bool incomplete;
+  bool ok;
   re_node_set eclosure;
-  incomplete = 0;
+  incomplete = false;
   err = re_node_set_alloc (&eclosure, dfa->edests[node].nelem + 1);
   if (BE (err != REG_NOERROR, 0))
     return err;
 
   /* This indicates that we are calculating this node now.
      We reference this value to avoid infinite loop.  */
-  dfa->eclosures[node].nelem = -1;
+  dfa->eclosures[node].nelem = REG_MISSING;
 
   constraint = ((dfa->nodes[node].type == ANCHOR)
                ? dfa->nodes[node].opr.ctx_type : 0);
@@ -1628,7 +1652,7 @@ calc_eclosure_iter (re_node_set *new_set
       && dfa->edests[node].nelem
       && !dfa->nodes[dfa->edests[node].elems[0]].duplicated)
     {
-      int org_node, cur_node;
+      Idx org_node, cur_node;
       org_node = cur_node = node;
       err = duplicate_node_closure (dfa, node, node, node, constraint);
       if (BE (err != REG_NOERROR, 0))
@@ -1640,19 +1664,19 @@ calc_eclosure_iter (re_node_set *new_set
     for (i = 0; i < dfa->edests[node].nelem; ++i)
       {
        re_node_set eclosure_elem;
-       int edest = dfa->edests[node].elems[i];
+       Idx edest = dfa->edests[node].elems[i];
        /* If calculating the epsilon closure of `edest' is in progress,
           return intermediate result.  */
-       if (dfa->eclosures[edest].nelem == -1)
+       if (dfa->eclosures[edest].nelem == REG_MISSING)
          {
-           incomplete = 1;
+           incomplete = true;
            continue;
          }
        /* If we haven't calculated the epsilon closure of `edest' yet,
           calculate now. Otherwise use calculated epsilon closure.  */
        if (dfa->eclosures[edest].nelem == 0)
          {
-           err = calc_eclosure_iter (&eclosure_elem, dfa, edest, 0);
+           err = calc_eclosure_iter (&eclosure_elem, dfa, edest, false);
            if (BE (err != REG_NOERROR, 0))
              return err;
          }
@@ -1664,13 +1688,15 @@ calc_eclosure_iter (re_node_set *new_set
           the epsilon closure of this node is also incomplete.  */
        if (dfa->eclosures[edest].nelem == 0)
          {
-           incomplete = 1;
+           incomplete = true;
            re_node_set_free (&eclosure_elem);
          }
       }
 
   /* Epsilon closures include itself.  */
-  re_node_set_insert (&eclosure, node);
+  ok = re_node_set_insert (&eclosure, node);
+  if (BE (! ok, 0))
+    return REG_ESPACE;
   if (incomplete && !root)
     dfa->eclosures[node].nelem = 0;
   else
@@ -1744,97 +1770,97 @@ peek_token (re_token_t *token, re_string
       switch (c2)
        {
        case '|':
-         if (!(syntax & RE_LIMITED_OPS) && !(syntax & RE_NO_BK_VBAR))
+         if (!(syntax & REG_LIMITED_OPS) && !(syntax & REG_NO_BK_VBAR))
            token->type = OP_ALT;
          break;
        case '1': case '2': case '3': case '4': case '5':
        case '6': case '7': case '8': case '9':
-         if (!(syntax & RE_NO_BK_REFS))
+         if (!(syntax & REG_NO_BK_REFS))
            {
              token->type = OP_BACK_REF;
              token->opr.idx = c2 - '1';
            }
          break;
        case '<':
-         if (!(syntax & RE_NO_GNU_OPS))
+         if (!(syntax & REG_NO_GNU_OPS))
            {
              token->type = ANCHOR;
              token->opr.ctx_type = WORD_FIRST;
            }
          break;
        case '>':
-         if (!(syntax & RE_NO_GNU_OPS))
+         if (!(syntax & REG_NO_GNU_OPS))
            {
              token->type = ANCHOR;
              token->opr.ctx_type = WORD_LAST;
            }
          break;
        case 'b':
-         if (!(syntax & RE_NO_GNU_OPS))
+         if (!(syntax & REG_NO_GNU_OPS))
            {
              token->type = ANCHOR;
              token->opr.ctx_type = WORD_DELIM;
            }
          break;
        case 'B':
-         if (!(syntax & RE_NO_GNU_OPS))
+         if (!(syntax & REG_NO_GNU_OPS))
            {
              token->type = ANCHOR;
              token->opr.ctx_type = NOT_WORD_DELIM;
            }
          break;
        case 'w':
-         if (!(syntax & RE_NO_GNU_OPS))
+         if (!(syntax & REG_NO_GNU_OPS))
            token->type = OP_WORD;
          break;
        case 'W':
-         if (!(syntax & RE_NO_GNU_OPS))
+         if (!(syntax & REG_NO_GNU_OPS))
            token->type = OP_NOTWORD;
          break;
        case 's':
-         if (!(syntax & RE_NO_GNU_OPS))
+         if (!(syntax & REG_NO_GNU_OPS))
            token->type = OP_SPACE;
          break;
        case 'S':
-         if (!(syntax & RE_NO_GNU_OPS))
+         if (!(syntax & REG_NO_GNU_OPS))
            token->type = OP_NOTSPACE;
          break;
        case '`':
-         if (!(syntax & RE_NO_GNU_OPS))
+         if (!(syntax & REG_NO_GNU_OPS))
            {
              token->type = ANCHOR;
              token->opr.ctx_type = BUF_FIRST;
            }
          break;
        case '\'':
-         if (!(syntax & RE_NO_GNU_OPS))
+         if (!(syntax & REG_NO_GNU_OPS))
            {
              token->type = ANCHOR;
              token->opr.ctx_type = BUF_LAST;
            }
          break;
        case '(':
-         if (!(syntax & RE_NO_BK_PARENS))
+         if (!(syntax & REG_NO_BK_PARENS))
            token->type = OP_OPEN_SUBEXP;
          break;
        case ')':
-         if (!(syntax & RE_NO_BK_PARENS))
+         if (!(syntax & REG_NO_BK_PARENS))
            token->type = OP_CLOSE_SUBEXP;
          break;
        case '+':
-         if (!(syntax & RE_LIMITED_OPS) && (syntax & RE_BK_PLUS_QM))
+         if (!(syntax & REG_LIMITED_OPS) && (syntax & REG_BK_PLUS_QM))
            token->type = OP_DUP_PLUS;
          break;
        case '?':
-         if (!(syntax & RE_LIMITED_OPS) && (syntax & RE_BK_PLUS_QM))
+         if (!(syntax & REG_LIMITED_OPS) && (syntax & REG_BK_PLUS_QM))
            token->type = OP_DUP_QUESTION;
          break;
        case '{':
-         if ((syntax & RE_INTERVALS) && (!(syntax & RE_NO_BK_BRACES)))
+         if ((syntax & REG_INTERVALS) && (!(syntax & REG_NO_BK_BRACES)))
            token->type = OP_OPEN_DUP_NUM;
          break;
        case '}':
-         if ((syntax & RE_INTERVALS) && (!(syntax & RE_NO_BK_BRACES)))
+         if ((syntax & REG_INTERVALS) && (!(syntax & REG_NO_BK_BRACES)))
            token->type = OP_CLOSE_DUP_NUM;
          break;
        default:
@@ -1857,38 +1883,38 @@ peek_token (re_token_t *token, re_string
   switch (c)
     {
     case '\n':
-      if (syntax & RE_NEWLINE_ALT)
+      if (syntax & REG_NEWLINE_ALT)
        token->type = OP_ALT;
       break;
     case '|':
-      if (!(syntax & RE_LIMITED_OPS) && (syntax & RE_NO_BK_VBAR))
+      if (!(syntax & REG_LIMITED_OPS) && (syntax & REG_NO_BK_VBAR))
        token->type = OP_ALT;
       break;
     case '*':
       token->type = OP_DUP_ASTERISK;
       break;
     case '+':
-      if (!(syntax & RE_LIMITED_OPS) && !(syntax & RE_BK_PLUS_QM))
+      if (!(syntax & REG_LIMITED_OPS) && !(syntax & REG_BK_PLUS_QM))
        token->type = OP_DUP_PLUS;
       break;
     case '?':
-      if (!(syntax & RE_LIMITED_OPS) && !(syntax & RE_BK_PLUS_QM))
+      if (!(syntax & REG_LIMITED_OPS) && !(syntax & REG_BK_PLUS_QM))
        token->type = OP_DUP_QUESTION;
       break;
     case '{':
-      if ((syntax & RE_INTERVALS) && (syntax & RE_NO_BK_BRACES))
+      if ((syntax & REG_INTERVALS) && (syntax & REG_NO_BK_BRACES))
        token->type = OP_OPEN_DUP_NUM;
       break;
     case '}':
-      if ((syntax & RE_INTERVALS) && (syntax & RE_NO_BK_BRACES))
+      if ((syntax & REG_INTERVALS) && (syntax & REG_NO_BK_BRACES))
        token->type = OP_CLOSE_DUP_NUM;
       break;
     case '(':
-      if (syntax & RE_NO_BK_PARENS)
+      if (syntax & REG_NO_BK_PARENS)
        token->type = OP_OPEN_SUBEXP;
       break;
     case ')':
-      if (syntax & RE_NO_BK_PARENS)
+      if (syntax & REG_NO_BK_PARENS)
        token->type = OP_CLOSE_SUBEXP;
       break;
     case '[':
@@ -1898,18 +1924,18 @@ peek_token (re_token_t *token, re_string
       token->type = OP_PERIOD;
       break;
     case '^':
-      if (!(syntax & (RE_CONTEXT_INDEP_ANCHORS | RE_CARET_ANCHORS_HERE)) &&
+      if (!(syntax & (REG_CONTEXT_INDEP_ANCHORS | REG_CARET_ANCHORS_HERE)) &&
          re_string_cur_idx (input) != 0)
        {
          char prev = re_string_peek_byte (input, -1);
-         if (!(syntax & RE_NEWLINE_ALT) || prev != '\n')
+         if (!(syntax & REG_NEWLINE_ALT) || prev != '\n')
            break;
        }
       token->type = ANCHOR;
       token->opr.ctx_type = LINE_FIRST;
       break;
     case '$':
-      if (!(syntax & RE_CONTEXT_INDEP_ANCHORS) &&
+      if (!(syntax & REG_CONTEXT_INDEP_ANCHORS) &&
          re_string_cur_idx (input) + 1 != re_string_length (input))
        {
          re_token_t next;
@@ -1952,7 +1978,7 @@ peek_token_bracket (re_token_t *token, r
     }
 #endif /* RE_ENABLE_I18N */
 
-  if (c == '\\' && (syntax & RE_BACKSLASH_ESCAPE_IN_LISTS)
+  if (c == '\\' && (syntax & REG_BACKSLASH_ESCAPE_IN_LISTS)
       && re_string_cur_idx (input) + 1 < re_string_length (input))
     {
       /* In this case, '\' escape a character.  */
@@ -1982,7 +2008,7 @@ peek_token_bracket (re_token_t *token, r
          token->type = OP_OPEN_EQUIV_CLASS;
          break;
        case ':':
-         if (syntax & RE_CHAR_CLASSES)
+         if (syntax & REG_CHAR_CLASSES)
            {
              token->type = OP_OPEN_CHAR_CLASS;
              break;
@@ -2031,11 +2057,11 @@ static bin_tree_t *
 parse (re_string_t *regexp, regex_t *preg, reg_syntax_t syntax,
        reg_errcode_t *err)
 {
-  re_dfa_t *dfa = (re_dfa_t *) preg->buffer;
+  re_dfa_t *dfa = (re_dfa_t *) preg->re_buffer;
   bin_tree_t *tree, *eor, *root;
   re_token_t current_token;
   dfa->syntax = syntax;
-  fetch_token (&current_token, regexp, syntax | RE_CARET_ANCHORS_HERE);
+  fetch_token (&current_token, regexp, syntax | REG_CARET_ANCHORS_HERE);
   tree = parse_reg_exp (regexp, preg, &current_token, syntax, 0, err);
   if (BE (*err != REG_NOERROR && tree == NULL, 0))
     return NULL;
@@ -2063,9 +2089,9 @@ parse (re_string_t *regexp, regex_t *pre
 
 static bin_tree_t *
 parse_reg_exp (re_string_t *regexp, regex_t *preg, re_token_t *token,
-              reg_syntax_t syntax, int nest, reg_errcode_t *err)
+              reg_syntax_t syntax, Idx nest, reg_errcode_t *err)
 {
-  re_dfa_t *dfa = (re_dfa_t *) preg->buffer;
+  re_dfa_t *dfa = (re_dfa_t *) preg->re_buffer;
   bin_tree_t *tree, *branch = NULL;
   tree = parse_branch (regexp, preg, token, syntax, nest, err);
   if (BE (*err != REG_NOERROR && tree == NULL, 0))
@@ -2073,7 +2099,7 @@ parse_reg_exp (re_string_t *regexp, rege
 
   while (token->type == OP_ALT)
     {
-      fetch_token (token, regexp, syntax | RE_CARET_ANCHORS_HERE);
+      fetch_token (token, regexp, syntax | REG_CARET_ANCHORS_HERE);
       if (token->type != OP_ALT && token->type != END_OF_RE
          && (nest == 0 || token->type != OP_CLOSE_SUBEXP))
        {
@@ -2104,10 +2130,10 @@ parse_reg_exp (re_string_t *regexp, rege
 
 static bin_tree_t *
 parse_branch (re_string_t *regexp, regex_t *preg, re_token_t *token,
-             reg_syntax_t syntax, int nest, reg_errcode_t *err)
+             reg_syntax_t syntax, Idx nest, reg_errcode_t *err)
 {
   bin_tree_t *tree, *exp;
-  re_dfa_t *dfa = (re_dfa_t *) preg->buffer;
+  re_dfa_t *dfa = (re_dfa_t *) preg->re_buffer;
   tree = parse_expression (regexp, preg, token, syntax, nest, err);
   if (BE (*err != REG_NOERROR && tree == NULL, 0))
     return NULL;
@@ -2144,9 +2170,9 @@ parse_branch (re_string_t *regexp, regex
 
 static bin_tree_t *
 parse_expression (re_string_t *regexp, regex_t *preg, re_token_t *token,
-                 reg_syntax_t syntax, int nest, reg_errcode_t *err)
+                 reg_syntax_t syntax, Idx nest, reg_errcode_t *err)
 {
-  re_dfa_t *dfa = (re_dfa_t *) preg->buffer;
+  re_dfa_t *dfa = (re_dfa_t *) preg->re_buffer;
   bin_tree_t *tree;
   switch (token->type)
     {
@@ -2203,7 +2229,7 @@ parse_expression (re_string_t *regexp, r
       dfa->has_mb_node = 1;
       break;
     case OP_OPEN_DUP_NUM:
-      if (syntax & RE_CONTEXT_INVALID_DUP)
+      if (syntax & REG_CONTEXT_INVALID_DUP)
        {
          *err = REG_BADRPT;
          return NULL;
@@ -2212,12 +2238,12 @@ parse_expression (re_string_t *regexp, r
     case OP_DUP_ASTERISK:
     case OP_DUP_PLUS:
     case OP_DUP_QUESTION:
-      if (syntax & RE_CONTEXT_INVALID_OPS)
+      if (syntax & REG_CONTEXT_INVALID_OPS)
        {
          *err = REG_BADRPT;
          return NULL;
        }
-      else if (syntax & RE_CONTEXT_INDEP_OPS)
+      else if (syntax & REG_CONTEXT_INDEP_OPS)
        {
          fetch_token (token, regexp, syntax);
          return parse_expression (regexp, preg, token, syntax, nest, err);
@@ -2225,7 +2251,7 @@ parse_expression (re_string_t *regexp, r
       /* else fall through  */
     case OP_CLOSE_SUBEXP:
       if ((token->type == OP_CLOSE_SUBEXP) &&
-         !(syntax & RE_UNMATCHED_RIGHT_PAREN_ORD))
+         !(syntax & REG_UNMATCHED_RIGHT_PAREN_ORD))
        {
          *err = REG_ERPAREN;
          return NULL;
@@ -2339,7 +2365,7 @@ parse_expression (re_string_t *regexp, r
       if (BE (*err != REG_NOERROR && tree == NULL, 0))
        return NULL;
       /* In BRE consecutive duplications are not allowed.  */
-      if ((syntax & RE_CONTEXT_INVALID_DUP)
+      if ((syntax & REG_CONTEXT_INVALID_DUP)
          && (token->type == OP_DUP_ASTERISK
              || token->type == OP_OPEN_DUP_NUM))
        {
@@ -2360,14 +2386,14 @@ parse_expression (re_string_t *regexp, r
 
 static bin_tree_t *
 parse_sub_exp (re_string_t *regexp, regex_t *preg, re_token_t *token,
-              reg_syntax_t syntax, int nest, reg_errcode_t *err)
+              reg_syntax_t syntax, Idx nest, reg_errcode_t *err)
 {
-  re_dfa_t *dfa = (re_dfa_t *) preg->buffer;
+  re_dfa_t *dfa = (re_dfa_t *) preg->re_buffer;
   bin_tree_t *tree;
   size_t cur_nsub;
   cur_nsub = preg->re_nsub++;
 
-  fetch_token (token, regexp, syntax | RE_CARET_ANCHORS_HERE);
+  fetch_token (token, regexp, syntax | REG_CARET_ANCHORS_HERE);
 
   /* The subexpression may be a null string.  */
   if (token->type == OP_CLOSE_SUBEXP)
@@ -2380,7 +2406,9 @@ parse_sub_exp (re_string_t *regexp, rege
       if (BE (*err != REG_NOERROR, 0))
        return NULL;
     }
-  dfa->completed_bkref_map |= 1 << cur_nsub;
+
+  if (cur_nsub <= '9' - '1')
+    dfa->completed_bkref_map |= 1 << cur_nsub;
 
   tree = create_tree (dfa, tree, NULL, SUBEXP);
   if (BE (tree == NULL, 0))
@@ -2399,14 +2427,14 @@ parse_dup_op (bin_tree_t *elem, re_strin
              re_token_t *token, reg_syntax_t syntax, reg_errcode_t *err)
 {
   bin_tree_t *tree = NULL, *old_tree = NULL;
-  int i, start, end, start_idx = re_string_cur_idx (regexp);
+  Idx i, start, end, start_idx = re_string_cur_idx (regexp);
   re_token_t start_token = *token;
 
   if (token->type == OP_OPEN_DUP_NUM)
     {
       end = 0;
       start = fetch_number (regexp, token, syntax);
-      if (start == -1)
+      if (start == REG_MISSING)
        {
          if (token->type == CHARACTER && token->opr.c == ',')
            start = 0; /* We treat "{,m}" as "{0,m}".  */
@@ -2416,17 +2444,17 @@ parse_dup_op (bin_tree_t *elem, re_strin
              return NULL;
            }
        }
-      if (BE (start != -2, 1))
+      if (BE (start != REG_ERROR, 1))
        {
          /* We treat "{n}" as "{n,n}".  */
          end = ((token->type == OP_CLOSE_DUP_NUM) ? start
                 : ((token->type == CHARACTER && token->opr.c == ',')
-                   ? fetch_number (regexp, token, syntax) : -2));
+                   ? fetch_number (regexp, token, syntax) : REG_ERROR));
        }
-      if (BE (start == -2 || end == -2, 0))
+      if (BE (start == REG_ERROR || end == REG_ERROR, 0))
        {
          /* Invalid sequence.  */
-         if (BE (!(syntax & RE_INVALID_INTERVAL_ORD), 0))
+         if (BE (!(syntax & REG_INVALID_INTERVAL_ORD), 0))
            {
              if (token->type == END_OF_RE)
                *err = REG_EBRACE;
@@ -2445,7 +2473,7 @@ parse_dup_op (bin_tree_t *elem, re_strin
          return elem;
        }
 
-      if (BE (end != -1 && start > end, 0))
+      if (BE (end != REG_MISSING && start > end, 0))
        {
          /* First number greater than second.  */
          *err = REG_BADBR;
@@ -2455,7 +2483,7 @@ parse_dup_op (bin_tree_t *elem, re_strin
   else
     {
       start = (token->type == OP_DUP_PLUS) ? 1 : 0;
-      end = (token->type == OP_DUP_QUESTION) ? 1 : -1;
+      end = (token->type == OP_DUP_QUESTION) ? 1 : REG_MISSING;
     }
 
   fetch_token (token, regexp, syntax);
@@ -2493,24 +2521,26 @@ parse_dup_op (bin_tree_t *elem, re_strin
   if (elem->token.type == SUBEXP)
     postorder (elem, mark_opt_subexp, (void *) (long) elem->token.opr.idx);
 
-  tree = create_tree (dfa, elem, NULL, (end == -1 ? OP_DUP_ASTERISK : OP_ALT));
+  tree = create_tree (dfa, elem, NULL,
+                     (end == REG_MISSING ? OP_DUP_ASTERISK : OP_ALT));
   if (BE (tree == NULL, 0))
     goto parse_dup_op_espace;
 
-  /* This loop is actually executed only when end != -1,
+  /* This loop is actually executed only when end != REG_MISSING,
      to rewrite <re>{0,n} as (<re>(<re>...<re>?)?)?...  We have
      already created the start+1-th copy.  */
-  for (i = start + 2; i <= end; ++i)
-    {
-      elem = duplicate_tree (elem, dfa);
-      tree = create_tree (dfa, tree, elem, CONCAT);
-      if (BE (elem == NULL || tree == NULL, 0))
-        goto parse_dup_op_espace;
-
-      tree = create_tree (dfa, tree, NULL, OP_ALT);
-      if (BE (tree == NULL, 0))
-        goto parse_dup_op_espace;
-    }
+  if ((Idx) -1 < 0 || end != REG_MISSING)
+    for (i = start + 2; i <= end; ++i)
+      {
+       elem = duplicate_tree (elem, dfa);
+       tree = create_tree (dfa, tree, elem, CONCAT);
+       if (BE (elem == NULL || tree == NULL, 0))
+         goto parse_dup_op_espace;
+
+       tree = create_tree (dfa, tree, NULL, OP_ALT);
+       if (BE (tree == NULL, 0))
+         goto parse_dup_op_espace;
+      }
 
   if (old_tree)
     tree = create_tree (dfa, old_tree, tree, CONCAT);
@@ -2535,9 +2565,9 @@ parse_dup_op (bin_tree_t *elem, re_strin
      update it.  */
 
 static reg_errcode_t
-build_range_exp (re_bitset_ptr_t sbcset,
+build_range_exp (bitset sbcset,
 # ifdef RE_ENABLE_I18N
-                re_charset_t *mbcset, int *range_alloc,
+                re_charset_t *mbcset, Idx *range_alloc,
 # endif
                 bracket_elem_t *start_elem, bracket_elem_t *end_elem)
 {
@@ -2591,14 +2621,13 @@ build_range_exp (re_bitset_ptr_t sbcset,
           {
            /* There is not enough space, need realloc.  */
            wchar_t *new_array_start, *new_array_end;
-           int new_nranges;
+           Idx new_nranges;
 
-           /* +1 in case of mbcset->nranges is 0.  */
-           new_nranges = 2 * mbcset->nranges + 1;
+           new_nranges = mbcset->nranges;
            /* Use realloc since mbcset->range_starts and mbcset->range_ends
               are NULL if *range_alloc == 0.  */
-           new_array_start = re_realloc (mbcset->range_starts, wchar_t,
-                                         new_nranges);
+           new_array_start = re_x2realloc (mbcset->range_starts, wchar_t,
+                                           &new_nranges);
            new_array_end = re_realloc (mbcset->range_ends, wchar_t,
                                        new_nranges);
 
@@ -2652,9 +2681,9 @@ build_range_exp (re_bitset_ptr_t sbcset,
    pointer argument since we may update it.  */
 
 static reg_errcode_t
-build_collating_symbol (re_bitset_ptr_t sbcset,
+build_collating_symbol (bitset sbcset,
 # ifdef RE_ENABLE_I18N
-                       re_charset_t *mbcset, int *coll_sym_alloc,
+                       re_charset_t *mbcset, Idx *coll_sym_alloc,
 # endif
                        const unsigned char *name)
 {
@@ -2690,9 +2719,7 @@ parse_bracket_exp (re_string_t *regexp, 
 
   auto inline int32_t
   __attribute ((always_inline))
-  seek_collating_symbol_entry (name, name_len)
-        const unsigned char *name;
-        size_t name_len;
+  seek_collating_symbol_entry (const unsigned char *name, size_t name_len)
     {
       int32_t hash = elem_hash ((const char *) name, name_len);
       int32_t elem = hash % table_size;
@@ -2723,8 +2750,7 @@ parse_bracket_exp (re_string_t *regexp, 
 
   auto inline unsigned int
   __attribute ((always_inline))
-  lookup_collation_sequence_value (br_elem)
-        bracket_elem_t *br_elem;
+  lookup_collation_sequence_value (bracket_elem_t *br_elem)
     {
       if (br_elem->type == SB_CHAR)
        {
@@ -2791,11 +2817,9 @@ parse_bracket_exp (re_string_t *regexp, 
 
   auto inline reg_errcode_t
   __attribute ((always_inline))
-  build_range_exp (sbcset, mbcset, range_alloc, start_elem, end_elem)
-        re_charset_t *mbcset;
-        int *range_alloc;
-        re_bitset_ptr_t sbcset;
-        bracket_elem_t *start_elem, *end_elem;
+  build_range_exp (bitset sbcset, re_charset_t *mbcset,
+                  Idx *range_alloc,
+                  bracket_elem_t *start_elem, bracket_elem_t *end_elem)
     {
       unsigned int ch;
       uint32_t start_collseq;
@@ -2813,7 +2837,7 @@ parse_bracket_exp (re_string_t *regexp, 
       /* Check start/end collation sequence values.  */
       if (BE (start_collseq == UINT_MAX || end_collseq == UINT_MAX, 0))
        return REG_ECOLLATE;
-      if (BE ((syntax & RE_NO_EMPTY_RANGES) && start_collseq > end_collseq, 0))
+      if (BE ((syntax & REG_NO_EMPTY_RANGES) && start_collseq > end_collseq, 
0))
        return REG_ERANGE;
 
       /* Got valid collation sequence values, add them as a new entry.
@@ -2828,12 +2852,11 @@ parse_bracket_exp (re_string_t *regexp, 
              /* There is not enough space, need realloc.  */
              uint32_t *new_array_start;
              uint32_t *new_array_end;
-             int new_nranges;
+             Idx new_nranges;
 
-             /* +1 in case of mbcset->nranges is 0.  */
-             new_nranges = 2 * mbcset->nranges + 1;
-             new_array_start = re_realloc (mbcset->range_starts, uint32_t,
-                                           new_nranges);
+             new_nranges = mbcset->nranges;
+             new_array_start = re_x2realloc (mbcset->range_starts, uint32_t,
+                                             &new_nranges);
              new_array_end = re_realloc (mbcset->range_ends, uint32_t,
                                          new_nranges);
 
@@ -2874,11 +2897,8 @@ parse_bracket_exp (re_string_t *regexp, 
 
   auto inline reg_errcode_t
   __attribute ((always_inline))
-  build_collating_symbol (sbcset, mbcset, coll_sym_alloc, name)
-        re_charset_t *mbcset;
-        int *coll_sym_alloc;
-        re_bitset_ptr_t sbcset;
-        const unsigned char *name;
+  build_collating_symbol (bitset sbcset, re_charset_t *mbcset,
+                         Idx *coll_sym_alloc, const unsigned char *name)
     {
       int32_t elem, idx;
       size_t name_len = strlen ((const char *) name);
@@ -2907,12 +2927,11 @@ parse_bracket_exp (re_string_t *regexp, 
          if (BE (*coll_sym_alloc == mbcset->ncoll_syms, 0))
            {
              /* Not enough, realloc it.  */
-             /* +1 in case of mbcset->ncoll_syms is 0.  */
-             int new_coll_sym_alloc = 2 * mbcset->ncoll_syms + 1;
+             Idx new_coll_sym_alloc = mbcset->ncoll_syms;
              /* Use realloc since mbcset->coll_syms is NULL
                 if *alloc == 0.  */
-             int32_t *new_coll_syms = re_realloc (mbcset->coll_syms, int32_t,
-                                                  new_coll_sym_alloc);
+             int32_t *new_coll_syms = re_x2realloc (mbcset->coll_syms, int32_t,
+                                                    &new_coll_sym_alloc);
              if (BE (new_coll_syms == NULL, 0))
                return REG_ESPACE;
              mbcset->coll_syms = new_coll_syms;
@@ -2938,13 +2957,13 @@ parse_bracket_exp (re_string_t *regexp, 
   re_bitset_ptr_t sbcset;
 #ifdef RE_ENABLE_I18N
   re_charset_t *mbcset;
-  int coll_sym_alloc = 0, range_alloc = 0, mbchar_alloc = 0;
-  int equiv_class_alloc = 0, char_class_alloc = 0;
+  Idx coll_sym_alloc = 0, range_alloc = 0, mbchar_alloc = 0;
+  Idx equiv_class_alloc = 0, char_class_alloc = 0;
 #endif /* not RE_ENABLE_I18N */
-  int non_match = 0;
+  bool non_match = false;
   bin_tree_t *work_tree;
   int token_len;
-  int first_round = 1;
+  bool first_round = true;
 #ifdef _LIBC
   collseqmb = (const unsigned char *)
     _NL_CURRENT (LC_COLLATE, _NL_COLLATE_COLLSEQMB);
@@ -2962,9 +2981,9 @@ parse_bracket_exp (re_string_t *regexp, 
                                                   _NL_COLLATE_SYMB_EXTRAMB);
     }
 #endif
-  sbcset = (re_bitset_ptr_t) calloc (sizeof (unsigned int), BITSET_UINTS);
+  sbcset = re_calloc (bitset_word, BITSET_WORDS);
 #ifdef RE_ENABLE_I18N
-  mbcset = (re_charset_t *) calloc (sizeof (re_charset_t), 1);
+  mbcset = re_calloc (re_charset_t, 1);
 #endif /* RE_ENABLE_I18N */
 #ifdef RE_ENABLE_I18N
   if (BE (sbcset == NULL || mbcset == NULL, 0))
@@ -2987,8 +3006,8 @@ parse_bracket_exp (re_string_t *regexp, 
 #ifdef RE_ENABLE_I18N
       mbcset->non_match = 1;
 #endif /* not RE_ENABLE_I18N */
-      non_match = 1;
-      if (syntax & RE_HAT_LISTS_NOT_NEWLINE)
+      non_match = true;
+      if (syntax & REG_HAT_LISTS_NOT_NEWLINE)
        bitset_set (sbcset, '\0');
       re_string_skip_bytes (regexp, token_len); /* Skip a token.  */
       token_len = peek_token_bracket (token, regexp, syntax);
@@ -3009,7 +3028,8 @@ parse_bracket_exp (re_string_t *regexp, 
       unsigned char start_name_buf[BRACKET_NAME_BUF_SIZE];
       unsigned char end_name_buf[BRACKET_NAME_BUF_SIZE];
       reg_errcode_t ret;
-      int token_len2 = 0, is_range_exp = 0;
+      int token_len2 = 0;
+      bool is_range_exp = false;
       re_token_t token2;
 
       start_elem.opr.name = start_name_buf;
@@ -3020,7 +3040,7 @@ parse_bracket_exp (re_string_t *regexp, 
          *err = ret;
          goto parse_bracket_exp_free_return;
        }
-      first_round = 0;
+      first_round = false;
 
       /* Get information about the next token.  We need it in any case.  */
       token_len = peek_token_bracket (token, regexp, syntax);
@@ -3049,15 +3069,15 @@ parse_bracket_exp (re_string_t *regexp, 
                  token->type = CHARACTER;
                }
              else
-               is_range_exp = 1;
+               is_range_exp = true;
            }
        }
 
-      if (is_range_exp == 1)
+      if (is_range_exp == true)
        {
          end_elem.opr.name = end_name_buf;
          ret = parse_bracket_element (&end_elem, regexp, &token2, token_len2,
-                                      dfa, syntax, 1);
+                                      dfa, syntax, true);
          if (BE (ret != REG_NOERROR, 0))
            {
              *err = ret;
@@ -3095,11 +3115,10 @@ parse_bracket_exp (re_string_t *regexp, 
                {
                  wchar_t *new_mbchars;
                  /* Not enough, realloc it.  */
-                 /* +1 in case of mbcset->nmbchars is 0.  */
-                 mbchar_alloc = 2 * mbcset->nmbchars + 1;
+                 mbchar_alloc = mbcset->nmbchars;
                  /* Use realloc since array is NULL if *alloc == 0.  */
-                 new_mbchars = re_realloc (mbcset->mbchars, wchar_t,
-                                           mbchar_alloc);
+                 new_mbchars = re_x2realloc (mbcset->mbchars, wchar_t,
+                                             &mbchar_alloc);
                  if (BE (new_mbchars == NULL, 0))
                    goto parse_bracket_exp_espace;
                  mbcset->mbchars = new_mbchars;
@@ -3172,12 +3191,12 @@ parse_bracket_exp (re_string_t *regexp, 
       mbc_tree = create_token_tree (dfa, NULL, NULL, &br_token);
       if (BE (mbc_tree == NULL, 0))
        goto parse_bracket_exp_espace;
-      for (sbc_idx = 0; sbc_idx < BITSET_UINTS; ++sbc_idx)
+      for (sbc_idx = 0; sbc_idx < BITSET_WORDS; ++sbc_idx)
        if (sbcset[sbc_idx])
          break;
       /* If there are no bits set in sbcset, there is no point
         of having both SIMPLE_BRACKET and COMPLEX_BRACKET.  */
-      if (sbc_idx < BITSET_UINTS)
+      if (sbc_idx < BITSET_WORDS)
        {
           /* Build a tree for simple bracket.  */
           br_token.type = SIMPLE_BRACKET;
@@ -3227,7 +3246,7 @@ parse_bracket_exp (re_string_t *regexp, 
 static reg_errcode_t
 parse_bracket_element (bracket_elem_t *elem, re_string_t *regexp,
                       re_token_t *token, int token_len, re_dfa_t *dfa,
-                      reg_syntax_t syntax, int accept_hyphen)
+                      reg_syntax_t syntax, bool accept_hyphen)
 {
 #ifdef RE_ENABLE_I18N
   int cur_char_size;
@@ -3312,9 +3331,9 @@ parse_bracket_symbol (bracket_elem_t *el
      is a pointer argument sinse we may update it.  */
 
 static reg_errcode_t
-build_equiv_class (re_bitset_ptr_t sbcset,
+build_equiv_class (bitset sbcset,
 #ifdef RE_ENABLE_I18N
-                  re_charset_t *mbcset, int *equiv_class_alloc,
+                  re_charset_t *mbcset, Idx *equiv_class_alloc,
 #endif
                   const unsigned char *name)
 {
@@ -3373,12 +3392,11 @@ build_equiv_class (re_bitset_ptr_t sbcse
       if (BE (*equiv_class_alloc == mbcset->nequiv_classes, 0))
        {
          /* Not enough, realloc it.  */
-         /* +1 in case of mbcset->nequiv_classes is 0.  */
-         int new_equiv_class_alloc = 2 * mbcset->nequiv_classes + 1;
+         Idx new_equiv_class_alloc = mbcset->nequiv_classes;
          /* Use realloc since the array is NULL if *alloc == 0.  */
-         int32_t *new_equiv_classes = re_realloc (mbcset->equiv_classes,
-                                                  int32_t,
-                                                  new_equiv_class_alloc);
+         int32_t *new_equiv_classes = re_x2realloc (mbcset->equiv_classes,
+                                                    int32_t,
+                                                    &new_equiv_class_alloc);
          if (BE (new_equiv_classes == NULL, 0))
            return REG_ESPACE;
          mbcset->equiv_classes = new_equiv_classes;
@@ -3403,9 +3421,9 @@ build_equiv_class (re_bitset_ptr_t sbcse
      is a pointer argument sinse we may update it.  */
 
 static reg_errcode_t
-build_charclass (unsigned RE_TRANSLATE_TYPE trans, re_bitset_ptr_t sbcset,
+build_charclass (unsigned REG_TRANSLATE_TYPE trans, bitset sbcset,
 #ifdef RE_ENABLE_I18N
-                re_charset_t *mbcset, int *char_class_alloc,
+                re_charset_t *mbcset, Idx *char_class_alloc,
 #endif
                 const unsigned char *class_name, reg_syntax_t syntax)
 {
@@ -3414,7 +3432,7 @@ build_charclass (unsigned RE_TRANSLATE_T
 
   /* In case of REG_ICASE "upper" and "lower" match the both of
      upper and lower cases.  */
-  if ((syntax & RE_ICASE)
+  if ((syntax & REG_IGNORE_CASE)
       && (strcmp (name, "upper") == 0 || strcmp (name, "lower") == 0))
     name = "alpha";
 
@@ -3423,11 +3441,10 @@ build_charclass (unsigned RE_TRANSLATE_T
   if (BE (*char_class_alloc == mbcset->nchar_classes, 0))
     {
       /* Not enough, realloc it.  */
-      /* +1 in case of mbcset->nchar_classes is 0.  */
-      int new_char_class_alloc = 2 * mbcset->nchar_classes + 1;
+      Idx new_char_class_alloc = mbcset->nchar_classes;
       /* Use realloc since array is NULL if *alloc == 0.  */
-      wctype_t *new_char_classes = re_realloc (mbcset->char_classes, wctype_t,
-                                              new_char_class_alloc);
+      wctype_t *new_char_classes = re_x2realloc (mbcset->char_classes, 
wctype_t,
+                                                &new_char_class_alloc);
       if (BE (new_char_classes == NULL, 0))
        return REG_ESPACE;
       mbcset->char_classes = new_char_classes;
@@ -3477,23 +3494,23 @@ build_charclass (unsigned RE_TRANSLATE_T
 }
 
 static bin_tree_t *
-build_charclass_op (re_dfa_t *dfa, unsigned RE_TRANSLATE_TYPE trans,
+build_charclass_op (re_dfa_t *dfa, unsigned REG_TRANSLATE_TYPE trans,
                    const unsigned char *class_name,
                    const unsigned char *extra,
-                   int non_match, reg_errcode_t *err)
+                   bool non_match, reg_errcode_t *err)
 {
   re_bitset_ptr_t sbcset;
 #ifdef RE_ENABLE_I18N
   re_charset_t *mbcset;
-  int alloc = 0;
+  Idx alloc = 0;
 #endif /* not RE_ENABLE_I18N */
   reg_errcode_t ret;
   re_token_t br_token;
   bin_tree_t *tree;
 
-  sbcset = (re_bitset_ptr_t) calloc (sizeof (unsigned int), BITSET_UINTS);
+  sbcset = re_calloc (bitset_word, BITSET_WORDS);
 #ifdef RE_ENABLE_I18N
-  mbcset = (re_charset_t *) calloc (sizeof (re_charset_t), 1);
+  mbcset = re_calloc (re_charset_t, 1);
 #endif /* RE_ENABLE_I18N */
 
 #ifdef RE_ENABLE_I18N
@@ -3510,7 +3527,7 @@ build_charclass_op (re_dfa_t *dfa, unsig
     {
 #ifdef RE_ENABLE_I18N
       /*
-      if (syntax & RE_HAT_LISTS_NOT_NEWLINE)
+      if (syntax & REG_HAT_LISTS_NOT_NEWLINE)
        bitset_set(cset->sbcset, '\0');
       */
       mbcset->non_match = 1;
@@ -3590,25 +3607,27 @@ build_charclass_op (re_dfa_t *dfa, unsig
 
 /* This is intended for the expressions like "a{1,3}".
    Fetch a number from `input', and return the number.
-   Return -1, if the number field is empty like "{,1}".
-   Return -2, If an error is occured.  */
+   Return REG_MISSING if the number field is empty like "{,1}".
+   Return REG_ERROR if an error occurred.  */
 
-static int
+static Idx
 fetch_number (re_string_t *input, re_token_t *token, reg_syntax_t syntax)
 {
-  int num = -1;
+  Idx num = REG_MISSING;
   unsigned char c;
   while (1)
     {
       fetch_token (token, input, syntax);
       c = token->opr.c;
       if (BE (token->type == END_OF_RE, 0))
-       return -2;
+       return REG_ERROR;
       if (token->type == OP_CLOSE_DUP_NUM || c == ',')
        break;
-      num = ((token->type != CHARACTER || c < '0' || '9' < c || num == -2)
-            ? -2 : ((num == -1) ? c - '0' : num * 10 + c - '0'));
-      num = (num > RE_DUP_MAX) ? -2 : num;
+      num = ((token->type != CHARACTER || c < '0' || '9' < c
+             || num == REG_ERROR)
+            ? REG_ERROR
+            : ((num == REG_MISSING) ? c - '0' : num * 10 + c - '0'));
+      num = (num > REG_DUP_MAX) ? REG_ERROR : num;
     }
   return num;
 }
@@ -3667,7 +3686,7 @@ create_token_tree (re_dfa_t *dfa, bin_tr
   tree->token.opt_subexp = 0;
   tree->first = NULL;
   tree->next = NULL;
-  tree->node_idx = -1;
+  tree->node_idx = REG_MISSING;
 
   if (left != NULL)
     left->parent = tree;
@@ -3682,7 +3701,7 @@ create_token_tree (re_dfa_t *dfa, bin_tr
 static reg_errcode_t
 mark_opt_subexp (void *extra, bin_tree_t *node)
 {
-  int idx = (int) (long) extra;
+  Idx idx = (Idx) (long) extra;
   if (node->token.type == SUBEXP && node->token.opr.idx == idx)
     node->token.opt_subexp = 1;
 
Index: lib/regex.c
===================================================================
RCS file: /fetish/cu/lib/regex.c,v
retrieving revision 1.88
diff -p -u -r1.88 regex.c
--- lib/regex.c 23 Aug 2005 19:41:09 -0000      1.88
+++ lib/regex.c 9 Sep 2005 20:58:16 -0000
@@ -21,30 +21,6 @@
 #include "config.h"
 #endif
 
-#ifdef _AIX
-#pragma alloca
-#else
-# ifndef allocax           /* predefined by HP cc +Olibcalls */
-#  ifdef __GNUC__
-#   define alloca(size) __builtin_alloca (size)
-#  else
-#   if HAVE_ALLOCA_H
-#    include <alloca.h>
-#   else
-#    ifdef __hpux
-        void *alloca ();
-#    else
-#     if !defined __OS2__ && !defined WIN32
-        char *alloca ();
-#     else
-#      include <malloc.h>       /* OS/2 defines alloca in here */
-#     endif
-#    endif
-#   endif
-#  endif
-# endif
-#endif
-
 #ifdef _LIBC
 /* We have to keep the namespace clean.  */
 # define regfree(preg) __regfree (preg)
@@ -70,10 +46,6 @@
 # include "../locale/localeinfo.h"
 #endif
 
-/* POSIX says that <sys/types.h> must be included (by the caller) before
-   <regex.h>.  */
-#include <sys/types.h>
-
 /* On some systems, limits.h sets RE_DUP_MAX to a lower value than
    GNU regex allows.  Include it before <regex.h>, which correctly
    #undefs RE_DUP_MAX and sets it to the right value.  */
Index: lib/regex.h
===================================================================
RCS file: /fetish/cu/lib/regex.h,v
retrieving revision 1.24
diff -p -u -r1.24 regex.h
--- lib/regex.h 23 Aug 2005 19:41:09 -0000      1.24
+++ lib/regex.h 9 Sep 2005 20:58:16 -0000
@@ -28,15 +28,61 @@
 extern "C" {
 #endif
 
-/* POSIX says that <sys/types.h> must be included (by the caller) before
-   <regex.h>.  */
+/* Define _REGEX_SOURCE to get definitions that are incompatible with
+   POSIX.  */
+#if (!defined _REGEX_SOURCE                                    \
+     && (defined _GNU_SOURCE                                   \
+        || (!defined _POSIX_C_SOURCE && !defined _POSIX_SOURCE \
+            && !defined _XOPEN_SOURCE)))
+# define _REGEX_SOURCE 1
+#endif
 
-#if !defined _POSIX_C_SOURCE && !defined _POSIX_SOURCE && defined VMS
+#if defined _REGEX_SOURCE && defined VMS
 /* VMS doesn't have `size_t' in <sys/types.h>, even though POSIX says it
    should be there.  */
 # include <stddef.h>
 #endif
 
+#ifdef _REGEX_LARGE_OFFSETS
+
+/* Use types and values that are wide enough to represent signed and
+   unsigned byte offsets in memory.  This currently works only when
+   the regex code is used outside of the GNU C library; it is not yet
+   supported within glibc itself, and glibc users should not define
+   _REGEX_LARGE_OFFSETS.  */
+
+/* The type of the offset of a byte within a string.
+   For historical reasons POSIX 1003.1-2004 requires that regoff_t be
+   at least as wide as off_t.  This is a bit odd (and many common
+   POSIX platforms set it to the more-sensible ssize_t) but we might
+   as well conform.  We don't know of any hosts where ssize_t is wider
+   than off_t, so off_t is safe.  */
+typedef off_t regoff_t;
+
+/* The type of nonnegative object indexes.  Traditionally, GNU regex
+   uses 'int' for these.  Code that uses __re_idx_t should work
+   regardless of whether the type is signed.  */
+typedef size_t __re_idx_t;
+
+/* The type of object sizes.  */
+typedef size_t __re_size_t;
+
+/* The type of object sizes, in places where the traditional code
+   uses unsigned long int.  */
+typedef size_t __re_long_size_t;
+
+#else
+
+/* Use types that are binary-compatible with the traditional GNU regex
+   implementation, which mishandles strings longer than INT_MAX.  */
+
+typedef int regoff_t;
+typedef int __re_idx_t;
+typedef unsigned int __re_size_t;
+typedef unsigned long int __re_long_size_t;
+
+#endif
+
 /* The following two types have to be signed and unsigned integer type
    wide enough to hold a value of a pointer.  For most ANSI compilers
    ptrdiff_t and size_t should be likely OK.  Still size of these two
@@ -53,18 +99,18 @@ typedef unsigned long int reg_syntax_t;
 
 /* If this bit is not set, then \ inside a bracket expression is literal.
    If set, then such a \ quotes the following character.  */
-#define RE_BACKSLASH_ESCAPE_IN_LISTS ((unsigned long int) 1)
+#define REG_BACKSLASH_ESCAPE_IN_LISTS 1ul
 
 /* If this bit is not set, then + and ? are operators, and \+ and \? are
      literals.
    If set, then \+ and \? are operators and + and ? are literals.  */
-#define RE_BK_PLUS_QM (RE_BACKSLASH_ESCAPE_IN_LISTS << 1)
+#define REG_BK_PLUS_QM (1ul << 1)
 
 /* If this bit is set, then character classes are supported.  They are:
      [:alpha:], [:upper:], [:lower:],  [:digit:], [:alnum:], [:xdigit:],
      [:space:], [:print:], [:punct:], [:graph:], and [:cntrl:].
    If not set, then character classes are not supported.  */
-#define RE_CHAR_CLASSES (RE_BK_PLUS_QM << 1)
+#define REG_CHAR_CLASSES (1ul << 2)
 
 /* If this bit is set, then ^ and $ are always anchors (outside bracket
      expressions, of course).
@@ -74,11 +120,11 @@ typedef unsigned long int reg_syntax_t;
         $  is an anchor if it is at the end of a regular expression, or
            before a close-group or an alternation operator.
 
-   This bit could be (re)combined with RE_CONTEXT_INDEP_OPS, because
+   This bit could be (re)combined with REG_CONTEXT_INDEP_OPS, because
    POSIX draft 11.2 says that * etc. in leading positions is undefined.
    We already implemented a previous draft which made those constructs
    invalid, though, so we haven't changed the code back.  */
-#define RE_CONTEXT_INDEP_ANCHORS (RE_CHAR_CLASSES << 1)
+#define REG_CONTEXT_INDEP_ANCHORS (1ul << 3)
 
 /* If this bit is set, then special characters are always special
      regardless of where they are in the pattern.
@@ -86,70 +132,70 @@ typedef unsigned long int reg_syntax_t;
      some contexts; otherwise they are ordinary.  Specifically,
      * + ? and intervals are only special when not after the beginning,
      open-group, or alternation operator.  */
-#define RE_CONTEXT_INDEP_OPS (RE_CONTEXT_INDEP_ANCHORS << 1)
+#define REG_CONTEXT_INDEP_OPS (1ul << 4)
 
 /* If this bit is set, then *, +, ?, and { cannot be first in an re or
      immediately after an alternation or begin-group operator.  */
-#define RE_CONTEXT_INVALID_OPS (RE_CONTEXT_INDEP_OPS << 1)
+#define REG_CONTEXT_INVALID_OPS (1ul << 5)
 
 /* If this bit is set, then . matches newline.
    If not set, then it doesn't.  */
-#define RE_DOT_NEWLINE (RE_CONTEXT_INVALID_OPS << 1)
+#define REG_DOT_NEWLINE (1ul << 6)
 
 /* If this bit is set, then . doesn't match NUL.
    If not set, then it does.  */
-#define RE_DOT_NOT_NULL (RE_DOT_NEWLINE << 1)
+#define REG_DOT_NOT_NULL (1ul << 7)
 
 /* If this bit is set, nonmatching lists [^...] do not match newline.
    If not set, they do.  */
-#define RE_HAT_LISTS_NOT_NEWLINE (RE_DOT_NOT_NULL << 1)
+#define REG_HAT_LISTS_NOT_NEWLINE (1ul << 8)
 
 /* If this bit is set, either \{...\} or {...} defines an
-     interval, depending on RE_NO_BK_BRACES.
+     interval, depending on REG_NO_BK_BRACES.
    If not set, \{, \}, {, and } are literals.  */
-#define RE_INTERVALS (RE_HAT_LISTS_NOT_NEWLINE << 1)
+#define REG_INTERVALS (1ul << 9)
 
 /* If this bit is set, +, ? and | aren't recognized as operators.
    If not set, they are.  */
-#define RE_LIMITED_OPS (RE_INTERVALS << 1)
+#define REG_LIMITED_OPS (1ul << 10)
 
 /* If this bit is set, newline is an alternation operator.
    If not set, newline is literal.  */
-#define RE_NEWLINE_ALT (RE_LIMITED_OPS << 1)
+#define REG_NEWLINE_ALT (1ul << 11)
 
 /* If this bit is set, then `{...}' defines an interval, and \{ and \}
      are literals.
   If not set, then `\{...\}' defines an interval.  */
-#define RE_NO_BK_BRACES (RE_NEWLINE_ALT << 1)
+#define REG_NO_BK_BRACES (1ul << 12)
 
 /* If this bit is set, (...) defines a group, and \( and \) are literals.
    If not set, \(...\) defines a group, and ( and ) are literals.  */
-#define RE_NO_BK_PARENS (RE_NO_BK_BRACES << 1)
+#define REG_NO_BK_PARENS (1ul << 13)
 
 /* If this bit is set, then \<digit> matches <digit>.
    If not set, then \<digit> is a back-reference.  */
-#define RE_NO_BK_REFS (RE_NO_BK_PARENS << 1)
+#define REG_NO_BK_REFS (1ul << 14)
 
 /* If this bit is set, then | is an alternation operator, and \| is literal.
    If not set, then \| is an alternation operator, and | is literal.  */
-#define RE_NO_BK_VBAR (RE_NO_BK_REFS << 1)
+#define REG_NO_BK_VBAR (1ul << 15)
 
 /* If this bit is set, then an ending range point collating higher
      than the starting range point, as in [z-a], is invalid.
    If not set, the containing range is empty and does not match any string.  */
-#define RE_NO_EMPTY_RANGES (RE_NO_BK_VBAR << 1)
+#define REG_NO_EMPTY_RANGES (1ul << 16)
 
 /* If this bit is set, then an unmatched ) is ordinary.
    If not set, then an unmatched ) is invalid.  */
-#define RE_UNMATCHED_RIGHT_PAREN_ORD (RE_NO_EMPTY_RANGES << 1)
+#define REG_UNMATCHED_RIGHT_PAREN_ORD (1ul << 17)
 
 /* If this bit is set, succeed as soon as we match the whole pattern,
    without further backtracking.  */
-#define RE_NO_POSIX_BACKTRACKING (RE_UNMATCHED_RIGHT_PAREN_ORD << 1)
+#define REG_NO_POSIX_BACKTRACKING (1ul << 18)
 
 /* If this bit is set, do not process the GNU regex operators.
    If not set, then the GNU regex operators are recognized. */
-#define RE_NO_GNU_OPS (RE_NO_POSIX_BACKTRACKING << 1)
+#define REG_NO_GNU_OPS (1ul << 19)
 
 /* If this bit is set, turn on internal regex debugging.
    If not set, and debugging was on, turn it off.
@@ -157,29 +203,29 @@ typedef unsigned long int reg_syntax_t;
    We define this bit always, so that all that's needed to turn on
    debugging is to recompile regex.c; the calling code can always have
    this bit set, and it won't affect anything in the normal case. */
-#define RE_DEBUG (RE_NO_GNU_OPS << 1)
+#define REG_DEBUG (1ul << 20)
 
 /* If this bit is set, a syntactically invalid interval is treated as
    a string of ordinary characters.  For example, the ERE 'a{1' is
    treated as 'a\{1'.  */
-#define RE_INVALID_INTERVAL_ORD (RE_DEBUG << 1)
+#define REG_INVALID_INTERVAL_ORD (1ul << 21)
 
 /* If this bit is set, then ignore case when matching.
    If not set, then case is significant.  */
-#define RE_ICASE (RE_INVALID_INTERVAL_ORD << 1)
+#define REG_IGNORE_CASE (1ul << 22)
 
-/* This bit is used internally like RE_CONTEXT_INDEP_ANCHORS but only
+/* This bit is used internally like REG_CONTEXT_INDEP_ANCHORS but only
    for ^, because it is difficult to scan the regex backwards to find
    whether ^ should be special.  */
-#define RE_CARET_ANCHORS_HERE (RE_ICASE << 1)
+#define REG_CARET_ANCHORS_HERE (1ul << 23)
 
 /* If this bit is set, then \{ cannot be first in an bre or
    immediately after an alternation or begin-group operator.  */
-#define RE_CONTEXT_INVALID_DUP (RE_CARET_ANCHORS_HERE << 1)
+#define REG_CONTEXT_INVALID_DUP (1ul << 24)
 
 /* If this bit is set, then no_sub will be set to 1 during
    re_compile_pattern.  */
-#define RE_NO_SUB (RE_CONTEXT_INVALID_DUP << 1)
+#define REG_NO_SUB (1ul << 25)
 
 /* This global variable defines the particular regexp syntax to use (for
    some interfaces).  When a regexp is compiled, the syntax used is
@@ -191,81 +237,78 @@ extern reg_syntax_t re_syntax_options;
    (The [[[ comments delimit what gets put into the Texinfo file, so
    don't delete them!)  */
 /* [[[begin syntaxes]]] */
-#define RE_SYNTAX_EMACS 0
+#define REG_SYNTAX_EMACS 0
 
-#define RE_SYNTAX_AWK                                                  \
-  (RE_BACKSLASH_ESCAPE_IN_LISTS   | RE_DOT_NOT_NULL                    \
-   | RE_NO_BK_PARENS              | RE_NO_BK_REFS                      \
-   | RE_NO_BK_VBAR                | RE_NO_EMPTY_RANGES                 \
-   | RE_DOT_NEWLINE              | RE_CONTEXT_INDEP_ANCHORS            \
-   | RE_UNMATCHED_RIGHT_PAREN_ORD | RE_NO_GNU_OPS)
-
-#define RE_SYNTAX_GNU_AWK                                              \
-  ((RE_SYNTAX_POSIX_EXTENDED | RE_BACKSLASH_ESCAPE_IN_LISTS | RE_DEBUG)        
\
-   & ~(RE_DOT_NOT_NULL | RE_INTERVALS | RE_CONTEXT_INDEP_OPS           \
-       | RE_CONTEXT_INVALID_OPS ))
-
-#define RE_SYNTAX_POSIX_AWK                                            \
-  (RE_SYNTAX_POSIX_EXTENDED | RE_BACKSLASH_ESCAPE_IN_LISTS             \
-   | RE_INTERVALS          | RE_NO_GNU_OPS)
-
-#define RE_SYNTAX_GREP                                                 \
-  (RE_BK_PLUS_QM              | RE_CHAR_CLASSES                                
\
-   | RE_HAT_LISTS_NOT_NEWLINE | RE_INTERVALS                           \
-   | RE_NEWLINE_ALT)
-
-#define RE_SYNTAX_EGREP                                                        
\
-  (RE_CHAR_CLASSES        | RE_CONTEXT_INDEP_ANCHORS                   \
-   | RE_CONTEXT_INDEP_OPS | RE_HAT_LISTS_NOT_NEWLINE                   \
-   | RE_NEWLINE_ALT       | RE_NO_BK_PARENS                            \
-   | RE_NO_BK_VBAR)
-
-#define RE_SYNTAX_POSIX_EGREP                                          \
-  (RE_SYNTAX_EGREP | RE_INTERVALS | RE_NO_BK_BRACES                    \
-   | RE_INVALID_INTERVAL_ORD)
+#define REG_SYNTAX_AWK                                                 \
+  (REG_BACKSLASH_ESCAPE_IN_LISTS   | REG_DOT_NOT_NULL                  \
+   | REG_NO_BK_PARENS             | REG_NO_BK_REFS                     \
+   | REG_NO_BK_VBAR               | REG_NO_EMPTY_RANGES                \
+   | REG_DOT_NEWLINE              | REG_CONTEXT_INDEP_ANCHORS          \
+   | REG_UNMATCHED_RIGHT_PAREN_ORD | REG_NO_GNU_OPS)
+
+#define REG_SYNTAX_GNU_AWK                                             \
+  ((REG_SYNTAX_POSIX_EXTENDED | REG_BACKSLASH_ESCAPE_IN_LISTS          \
+    | REG_DEBUG)                                                       \
+   & ~(REG_DOT_NOT_NULL | REG_INTERVALS | REG_CONTEXT_INDEP_OPS                
\
+       | REG_CONTEXT_INVALID_OPS ))
+
+#define REG_SYNTAX_POSIX_AWK                                           \
+  (REG_SYNTAX_POSIX_EXTENDED | REG_BACKSLASH_ESCAPE_IN_LISTS           \
+   | REG_INTERVALS          | REG_NO_GNU_OPS)
+
+#define REG_SYNTAX_GREP                                                        
\
+  (REG_BK_PLUS_QM             | REG_CHAR_CLASSES                       \
+   | REG_HAT_LISTS_NOT_NEWLINE | REG_INTERVALS                         \
+   | REG_NEWLINE_ALT)
+
+#define REG_SYNTAX_EGREP                                               \
+  (REG_CHAR_CLASSES       | REG_CONTEXT_INDEP_ANCHORS                  \
+   | REG_CONTEXT_INDEP_OPS | REG_HAT_LISTS_NOT_NEWLINE                 \
+   | REG_NEWLINE_ALT      | REG_NO_BK_PARENS                           \
+   | REG_NO_BK_VBAR)
+
+#define REG_SYNTAX_POSIX_EGREP                                         \
+  (REG_SYNTAX_EGREP | REG_INTERVALS | REG_NO_BK_BRACES                 \
+   | REG_INVALID_INTERVAL_ORD)
 
 /* P1003.2/D11.2, section 4.20.7.1, lines 5078ff.  */
-#define RE_SYNTAX_ED RE_SYNTAX_POSIX_BASIC
+#define REG_SYNTAX_ED REG_SYNTAX_POSIX_BASIC
 
-#define RE_SYNTAX_SED RE_SYNTAX_POSIX_BASIC
+#define REG_SYNTAX_SED REG_SYNTAX_POSIX_BASIC
 
 /* Syntax bits common to both basic and extended POSIX regex syntax.  */
-#define _RE_SYNTAX_POSIX_COMMON                                                
\
-  (RE_CHAR_CLASSES | RE_DOT_NEWLINE      | RE_DOT_NOT_NULL             \
-   | RE_INTERVALS  | RE_NO_EMPTY_RANGES)
+#define _REG_SYNTAX_POSIX_COMMON                                       \
+  (REG_CHAR_CLASSES | REG_DOT_NEWLINE   | REG_DOT_NOT_NULL             \
+   | REG_INTERVALS  | REG_NO_EMPTY_RANGES)
 
-#define RE_SYNTAX_POSIX_BASIC                                          \
-  (_RE_SYNTAX_POSIX_COMMON | RE_BK_PLUS_QM | RE_CONTEXT_INVALID_DUP)
+#define REG_SYNTAX_POSIX_BASIC                                         \
+  (_REG_SYNTAX_POSIX_COMMON | REG_BK_PLUS_QM | REG_CONTEXT_INVALID_DUP)
 
-/* Differs from ..._POSIX_BASIC only in that RE_BK_PLUS_QM becomes
-   RE_LIMITED_OPS, i.e., \? \+ \| are not recognized.  Actually, this
+/* Differs from ..._POSIX_BASIC only in that REG_BK_PLUS_QM becomes
+   REG_LIMITED_OPS, i.e., \? \+ \| are not recognized.  Actually, this
    isn't minimal, since other operators, such as \`, aren't disabled.  */
-#define RE_SYNTAX_POSIX_MINIMAL_BASIC                                  \
-  (_RE_SYNTAX_POSIX_COMMON | RE_LIMITED_OPS)
+#define REG_SYNTAX_POSIX_MINIMAL_BASIC                                 \
+  (_REG_SYNTAX_POSIX_COMMON | REG_LIMITED_OPS)
 
-#define RE_SYNTAX_POSIX_EXTENDED                                       \
-  (_RE_SYNTAX_POSIX_COMMON  | RE_CONTEXT_INDEP_ANCHORS                 \
-   | RE_CONTEXT_INDEP_OPS   | RE_NO_BK_BRACES                          \
-   | RE_NO_BK_PARENS        | RE_NO_BK_VBAR                            \
-   | RE_CONTEXT_INVALID_OPS | RE_UNMATCHED_RIGHT_PAREN_ORD)
-
-/* Differs from ..._POSIX_EXTENDED in that RE_CONTEXT_INDEP_OPS is
-   removed and RE_NO_BK_REFS is added.  */
-#define RE_SYNTAX_POSIX_MINIMAL_EXTENDED                               \
-  (_RE_SYNTAX_POSIX_COMMON  | RE_CONTEXT_INDEP_ANCHORS                 \
-   | RE_CONTEXT_INVALID_OPS | RE_NO_BK_BRACES                          \
-   | RE_NO_BK_PARENS        | RE_NO_BK_REFS                            \
-   | RE_NO_BK_VBAR         | RE_UNMATCHED_RIGHT_PAREN_ORD)
+#define REG_SYNTAX_POSIX_EXTENDED                                      \
+  (_REG_SYNTAX_POSIX_COMMON  | REG_CONTEXT_INDEP_ANCHORS               \
+   | REG_CONTEXT_INDEP_OPS   | REG_NO_BK_BRACES                                
\
+   | REG_NO_BK_PARENS       | REG_NO_BK_VBAR                           \
+   | REG_CONTEXT_INVALID_OPS | REG_UNMATCHED_RIGHT_PAREN_ORD)
+
+/* Differs from ..._POSIX_EXTENDED in that REG_CONTEXT_INDEP_OPS is
+   removed and REG_NO_BK_REFS is added.  */
+#define REG_SYNTAX_POSIX_MINIMAL_EXTENDED                              \
+  (_REG_SYNTAX_POSIX_COMMON  | REG_CONTEXT_INDEP_ANCHORS               \
+   | REG_CONTEXT_INVALID_OPS | REG_NO_BK_BRACES                                
\
+   | REG_NO_BK_PARENS       | REG_NO_BK_REFS                           \
+   | REG_NO_BK_VBAR         | REG_UNMATCHED_RIGHT_PAREN_ORD)
 /* [[[end syntaxes]]] */
 
-/* Maximum number of duplicates an interval can allow.  Some systems
-   (erroneously) define this in other header files, but we want our
-   value, so remove any previous define.  */
-#ifdef RE_DUP_MAX
-# undef RE_DUP_MAX
-#endif
-/* If sizeof(int) == 2, then ((1 << 15) - 1) overflows.  */
-#define RE_DUP_MAX (0x7fff)
+/* Maximum number of duplicates an interval can allow.  This is
+   distinct from RE_DUP_MAX, to conform to POSIX name space rules and
+   to avoid collisions with <limits.h>.  */
+#define REG_DUP_MAX 32767
 
 
 /* POSIX `cflags' bits (i.e., information for `regcomp').  */
@@ -276,16 +319,16 @@ extern reg_syntax_t re_syntax_options;
 
 /* If this bit is set, then ignore case when matching.
    If not set, then case is significant.  */
-#define REG_ICASE (REG_EXTENDED << 1)
+#define REG_ICASE (1 << 1)
 
 /* If this bit is set, then anchors do not match at newline
      characters in the string.
    If not set, then anchors do match at newlines.  */
-#define REG_NEWLINE (REG_ICASE << 1)
+#define REG_NEWLINE (1 << 2)
 
 /* If this bit is set, then report only success or fail in regexec.
    If not set, then returns differ between not matching and errors.  */
-#define REG_NOSUB (REG_NEWLINE << 1)
+#define REG_NOSUB (1 << 3)
 
 
 /* POSIX `eflags' bits (i.e., information for regexec).  */
@@ -306,74 +349,131 @@ extern reg_syntax_t re_syntax_options;
 
 
 /* If any error codes are removed, changed, or added, update the
-   `re_error_msg' table in regex.c.  */
+   `__re_error_msgid' table in regcomp.c.  */
+
 typedef enum
 {
-#ifdef _XOPEN_SOURCE
-  REG_ENOSYS = -1,     /* This will never happen for this implementation.  */
-#endif
+  _REG_ENOSYS = -1,    /* This will never happen for this implementation.  */
+#define REG_ENOSYS     _REG_ENOSYS
+
+  _REG_NOERROR,                /* Success.  */
+#define REG_NOERROR    _REG_NOERROR
 
-  REG_NOERROR = 0,     /* Success.  */
-  REG_NOMATCH,         /* Didn't find a match (for regexec).  */
+  _REG_NOMATCH,                /* Didn't find a match (for regexec).  */
+#define REG_NOMATCH    _REG_NOMATCH
 
   /* POSIX regcomp return error codes.  (In the order listed in the
      standard.)  */
-  REG_BADPAT,          /* Invalid pattern.  */
-  REG_ECOLLATE,                /* Inalid collating element.  */
-  REG_ECTYPE,          /* Invalid character class name.  */
-  REG_EESCAPE,         /* Trailing backslash.  */
-  REG_ESUBREG,         /* Invalid back reference.  */
-  REG_EBRACK,          /* Unmatched left bracket.  */
-  REG_EPAREN,          /* Parenthesis imbalance.  */
-  REG_EBRACE,          /* Unmatched \{.  */
-  REG_BADBR,           /* Invalid contents of \{\}.  */
-  REG_ERANGE,          /* Invalid range end.  */
-  REG_ESPACE,          /* Ran out of memory.  */
-  REG_BADRPT,          /* No preceding re for repetition op.  */
+
+  _REG_BADPAT,         /* Invalid pattern.  */
+#define REG_BADPAT     _REG_BADPAT
+
+  _REG_ECOLLATE,       /* Inalid collating element.  */
+#define REG_ECOLLATE   _REG_ECOLLATE
+
+  _REG_ECTYPE,         /* Invalid character class name.  */
+#define REG_ECTYPE     _REG_ECTYPE
+
+  _REG_EESCAPE,                /* Trailing backslash.  */
+#define REG_EESCAPE    _REG_EESCAPE
+
+  _REG_ESUBREG,                /* Invalid back reference.  */
+#define REG_ESUBREG    _REG_ESUBREG
+
+  _REG_EBRACK,         /* Unmatched left bracket.  */
+#define REG_EBRACK     _REG_EBRACK
+
+  _REG_EPAREN,         /* Parenthesis imbalance.  */
+#define REG_EPAREN     _REG_EPAREN
+
+  _REG_EBRACE,         /* Unmatched \{.  */
+#define REG_EBRACE     _REG_EBRACE
+
+  _REG_BADBR,          /* Invalid contents of \{\}.  */
+#define REG_BADBR      _REG_BADBR
+
+  _REG_ERANGE,         /* Invalid range end.  */
+#define REG_ERANGE     _REG_ERANGE
+
+  _REG_ESPACE,         /* Ran out of memory.  */
+#define REG_ESPACE     _REG_ESPACE
+
+  _REG_BADRPT,         /* No preceding re for repetition op.  */
+#define REG_BADRPT     _REG_BADRPT
 
   /* Error codes we've added.  */
-  REG_EEND,            /* Premature end.  */
-  REG_ESIZE,           /* Compiled pattern bigger than 2^16 bytes.  */
-  REG_ERPAREN          /* Unmatched ) or \); not returned from regcomp.  */
+
+  _REG_EEND,           /* Premature end.  */
+#define REG_EEND       _REG_EEND
+
+  _REG_ESIZE,          /* Compiled pattern bigger than 2^16 bytes.  */
+#define REG_ESIZE      _REG_ESIZE
+
+  _REG_ERPAREN         /* Unmatched ) or \); not returned from regcomp.  */
+#define REG_ERPAREN    _REG_ERPAREN
+
 } reg_errcode_t;
 
+/* In the traditional GNU implementation, regex.h defined member names
+   like `buffer' that POSIX does not allow.  These members now have
+   names with leading `re_' (e.g., `re_buffer').  Support the old
+   names only if _REGEX_SOURCE is defined.  New programs should use
+   the new names.  */
+#ifdef _REGEX_SOURCE
+# define _REG_RE_NAME(id) id
+# define _REG_RM_NAME(id) id
+#else
+# define _REG_RE_NAME(id) re_##id
+# define _REG_RM_NAME(id) rm_##id
+#endif
+
+/* The user can specify the type of the re_translate member by
+   defining the macro REG_TRANSLATE_TYPE.  In the traditional GNU
+   implementation, this macro was named RE_TRANSLATE_TYPE, but POSIX
+   does not allow this.  Support the old name only if _REGEX_SOURCE
+   and if the new name is not defined. New programs should use the new
+   name.  */
+#ifndef REG_TRANSLATE_TYPE
+# if defined _REGEX_SOURCE && defined RE_TRANSLATE_TYPE
+#  define REG_TRANSLATE_TYPE RE_TRANSLATE_TYPE
+# else
+#  define REG_TRANSLATE_TYPE char *
+# endif
+#endif
+
 /* This data structure represents a compiled pattern.  Before calling
-   the pattern compiler, the fields `buffer', `allocated', `fastmap',
-   `translate', and `no_sub' can be set.  After the pattern has been
+   the pattern compiler), the fields `re_buffer', `re_allocated', `re_fastmap',
+   `re_translate', and `re_no_sub' can be set.  After the pattern has been
    compiled, the `re_nsub' field is available.  All other fields are
    private to the regex routines.  */
 
-#ifndef RE_TRANSLATE_TYPE
-# define RE_TRANSLATE_TYPE char *
-#endif
-
 struct re_pattern_buffer
 {
 /* [[[begin pattern_buffer]]] */
        /* Space that holds the compiled pattern.  It is declared as
           `unsigned char *' because its elements are
            sometimes used as array indexes.  */
-  unsigned char *buffer;
+  unsigned char *_REG_RE_NAME (buffer);
 
-       /* Number of bytes to which `buffer' points.  */
-  unsigned long int allocated;
+       /* Number of bytes to which `re_buffer' points.  */
+  __re_long_size_t _REG_RE_NAME (allocated);
 
-       /* Number of bytes actually used in `buffer'.  */
-  unsigned long int used;
+       /* Number of bytes actually used in `re_buffer'.  */
+  __re_long_size_t _REG_RE_NAME (used);
 
         /* Syntax setting with which the pattern was compiled.  */
-  reg_syntax_t syntax;
+  reg_syntax_t _REG_RE_NAME (syntax);
 
         /* Pointer to a fastmap, if any, otherwise zero.  re_search uses
            the fastmap, if there is one, to skip over impossible
            starting points for matches.  */
-  char *fastmap;
+  char *_REG_RE_NAME (fastmap);
 
         /* Either a translate table to apply to all characters before
            comparing them, or zero for no translation.  The translation
            is applied to a pattern when it is compiled and to a string
            when it is matched.  */
-  RE_TRANSLATE_TYPE translate;
+  REG_TRANSLATE_TYPE _REG_RE_NAME (translate);
 
        /* Number of subexpressions found by the compiler.  */
   size_t re_nsub;
@@ -383,59 +483,55 @@ struct re_pattern_buffer
            whether or not we should use the fastmap, so we don't set
            this absolutely perfectly; see `re_compile_fastmap' (the
            `duplicate' case).  */
-  unsigned can_be_null : 1;
+  unsigned int _REG_RE_NAME (can_be_null) : 1;
 
-        /* If REGS_UNALLOCATED, allocate space in the `regs' structure
-             for `max (RE_NREGS, re_nsub + 1)' groups.
-           If REGS_REALLOCATE, reallocate space if necessary.
-           If REGS_FIXED, use what's there.  */
-#define REGS_UNALLOCATED 0
-#define REGS_REALLOCATE 1
-#define REGS_FIXED 2
-  unsigned regs_allocated : 2;
+        /* If REG_UNALLOCATED, allocate space in the `regs' structure
+             for `max (REG_NREGS, re_nsub + 1)' groups.
+           If REG_REALLOCATE, reallocate space if necessary.
+           If REG_FIXED, use what's there.  */
+#define REG_UNALLOCATED 0
+#define REG_REALLOCATE 1
+#define REG_FIXED 2
+  unsigned int _REG_RE_NAME (regs_allocated) : 2;
 
         /* Set to zero when `regex_compile' compiles a pattern; set to one
            by `re_compile_fastmap' if it updates the fastmap.  */
-  unsigned fastmap_accurate : 1;
+  unsigned int _REG_RE_NAME (fastmap_accurate) : 1;
 
         /* If set, `re_match_2' does not return information about
            subexpressions.  */
-  unsigned no_sub : 1;
+  unsigned int _REG_RE_NAME (no_sub) : 1;
 
         /* If set, a beginning-of-line anchor doesn't match at the
            beginning of the string.  */
-  unsigned not_bol : 1;
+  unsigned int _REG_RE_NAME (not_bol) : 1;
 
         /* Similarly for an end-of-line anchor.  */
-  unsigned not_eol : 1;
+  unsigned int _REG_RE_NAME (not_eol) : 1;
 
         /* If true, an anchor at a newline matches.  */
-  unsigned newline_anchor : 1;
+  unsigned int _REG_RE_NAME (newline_anchor) : 1;
 
 /* [[[end pattern_buffer]]] */
 };
 
 typedef struct re_pattern_buffer regex_t;
 
-/* Type for byte offsets within the string.  POSIX mandates this.  */
-typedef int regoff_t;
-
-
 /* This is the structure we store register match data in.  See
    regex.texinfo for a full description of what registers match.  */
 struct re_registers
 {
-  unsigned num_regs;
-  regoff_t *start;
-  regoff_t *end;
+  __re_size_t _REG_RM_NAME (num_regs);
+  regoff_t *_REG_RM_NAME (start);
+  regoff_t *_REG_RM_NAME (end);
 };
 
 
-/* If `regs_allocated' is REGS_UNALLOCATED in the pattern buffer,
+/* If `regs_allocated' is REG_UNALLOCATED in the pattern buffer,
    `re_match_2' returns information about at least this many registers
    the first time a `regs' structure is passed.  */
-#ifndef RE_NREGS
-# define RE_NREGS 30
+#ifndef REG_NREGS
+# define REG_NREGS 30
 #endif
 
 
@@ -452,49 +548,55 @@ typedef struct
 
 /* Sets the current default syntax to SYNTAX, and return the old syntax.
    You can also simply assign to the `re_syntax_options' variable.  */
-extern reg_syntax_t re_set_syntax (reg_syntax_t syntax);
+extern reg_syntax_t re_set_syntax (reg_syntax_t __syntax);
 
 /* Compile the regular expression PATTERN, with length LENGTH
    and syntax given by the global `re_syntax_options', into the buffer
    BUFFER.  Return NULL if successful, and an error string if not.  */
-extern const char *re_compile_pattern (const char *pattern, size_t length,
-                                      struct re_pattern_buffer *buffer);
+extern const char *re_compile_pattern (const char *__pattern, size_t __length,
+                                      struct re_pattern_buffer *__buffer);
 
 
 /* Compile a fastmap for the compiled pattern in BUFFER; used to
    accelerate searches.  Return 0 if successful and -2 if was an
    internal error.  */
-extern int re_compile_fastmap (struct re_pattern_buffer *buffer);
+extern int re_compile_fastmap (struct re_pattern_buffer *__buffer);
 
 
 /* Search in the string STRING (with length LENGTH) for the pattern
    compiled into BUFFER.  Start searching at position START, for RANGE
    characters.  Return the starting position of the match, -1 for no
    match, or -2 for an internal error.  Also return register
-   information in REGS (if REGS and BUFFER->no_sub are nonzero).  */
-extern int re_search (struct re_pattern_buffer *buffer, const char *string,
-                     int length, int start, int range,
-                     struct re_registers *regs);
+   information in REGS (if REGS and BUFFER->re_no_sub are nonzero).  */
+extern regoff_t re_search (struct re_pattern_buffer *__buffer,
+                          const char *__string, __re_idx_t __length,
+                          __re_idx_t __start, regoff_t __range,
+                          struct re_registers *__regs);
 
 
 /* Like `re_search', but search in the concatenation of STRING1 and
    STRING2.  Also, stop searching at index START + STOP.  */
-extern int re_search_2 (struct re_pattern_buffer *buffer, const char *string1,
-                       int length1, const char *string2, int length2,
-                       int start, int range, struct re_registers *regs,
-                       int stop);
+extern regoff_t re_search_2 (struct re_pattern_buffer *__buffer,
+                            const char *__string1, __re_idx_t __length1,
+                            const char *__string2, __re_idx_t __length2,
+                            __re_idx_t __start, regoff_t __range,
+                            struct re_registers *__regs,
+                            __re_idx_t __stop);
 
 
 /* Like `re_search', but return how many characters in STRING the regexp
    in BUFFER matched, starting at position START.  */
-extern int re_match (struct re_pattern_buffer *buffer, const char *string,
-                    int length, int start, struct re_registers *regs);
+extern regoff_t re_match (struct re_pattern_buffer *__buffer,
+                         const char *__string, __re_idx_t __length,
+                         __re_idx_t __start, struct re_registers *__regs);
 
 
 /* Relates to `re_match' as `re_search_2' relates to `re_search'.  */
-extern int re_match_2 (struct re_pattern_buffer *buffer, const char *string1,
-                      int length1, const char *string2, int length2,
-                      int start, struct re_registers *regs, int stop);
+extern regoff_t re_match_2 (struct re_pattern_buffer *__buffer,
+                           const char *__string1, __re_idx_t __length1,
+                           const char *__string2, __re_idx_t __length2,
+                           __re_idx_t __start, struct re_registers *__regs,
+                           __re_idx_t __stop);
 
 
 /* Set REGS to hold NUM_REGS registers, storing them in STARTS and
@@ -509,9 +611,10 @@ extern int re_match_2 (struct re_pattern
    Unless this function is called, the first search or match using
    PATTERN_BUFFER will allocate its own register data, without
    freeing the old data.  */
-extern void re_set_registers (struct re_pattern_buffer *buffer,
-                             struct re_registers *regs, unsigned num_regs,
-                             regoff_t *starts, regoff_t *ends);
+extern void re_set_registers (struct re_pattern_buffer *__buffer,
+                             struct re_registers *__regs,
+                             __re_size_t __num_regs,
+                             regoff_t *__starts, regoff_t *__ends);
 
 #if defined _REGEX_RE_COMP || defined _LIBC
 # ifndef _CRAY
@@ -551,12 +654,106 @@ extern int regexec (const regex_t *__res
                    regmatch_t __pmatch[__restrict_arr],
                    int __eflags);
 
-extern size_t regerror (int __errcode, const regex_t *__preg,
-                       char *__errbuf, size_t __errbuf_size);
+extern size_t regerror (int __errcode, const regex_t *__restrict __preg,
+                       char *__restrict __errbuf, size_t __errbuf_size);
 
 extern void regfree (regex_t *__preg);
 
 
+#ifdef _REGEX_SOURCE
+
+/* Define the POSIX-compatible member names in terms of the
+   incompatible (and deprecated) names established by _REG_RE_NAME.
+   New programs should use the re_* names.  */
+
+# define re_allocated allocated
+# define re_buffer buffer
+# define re_can_be_null can_be_null
+# define re_fastmap fastmap
+# define re_fastmap_accurate fastmap_accurate
+# define re_newline_anchor newline_anchor
+# define re_no_sub no_sub
+# define re_not_bol not_bol
+# define re_not_eol not_eol
+# define re_regs_allocated regs_allocated
+# define re_syntax syntax
+# define re_translate translate
+# define re_used used
+
+/* Similarly for _REG_RM_NAME.  */
+
+# define rm_end end
+# define rm_num_regs num_regs
+# define rm_start start
+
+/* Undef RE_DUP_MAX first, in case the user has already included a
+   <limits.h> with an incompatible definition.
+
+   On GNU systems, the most common spelling for RE_DUP_MAX's value in
+   <limits.h> is (0x7ffff), so define RE_DUP_MAX to that, not to
+   REG_DUP_MAX.  This avoid some duplicate-macro-definition warnings
+   with programs that include <limits.h> after this file.
+
+   New programs should not assume that regex.h defines RE_DUP_MAX; to
+   get the value of RE_DUP_MAX, they should instead include <limits.h>
+   and possibly invoke the sysconf function.  */
+
+# undef RE_DUP_MAX
+# define RE_DUP_MAX (0x7fff)
+
+/* Define the following symbols for backward source compatibility.
+   These symbols violate the POSIX name space rules, and new programs
+   should avoid them.  */
+
+# define REGS_FIXED REG_FIXED
+# define REGS_REALLOCATE REG_REALLOCATE
+# define REGS_UNALLOCATED REG_UNALLOCATED
+# define RE_BACKSLASH_ESCAPE_IN_LISTS REG_BACKSLASH_ESCAPE_IN_LISTS
+# define RE_BK_PLUS_QM REG_BK_PLUS_QM
+# define RE_CARET_ANCHORS_HERE REG_CARET_ANCHORS_HERE
+# define RE_CHAR_CLASSES REG_CHAR_CLASSES
+# define RE_CONTEXT_INDEP_ANCHORS REG_CONTEXT_INDEP_ANCHORS
+# define RE_CONTEXT_INDEP_OPS REG_CONTEXT_INDEP_OPS
+# define RE_CONTEXT_INVALID_DUP REG_CONTEXT_INVALID_DUP
+# define RE_CONTEXT_INVALID_OPS REG_CONTEXT_INVALID_OPS
+# define RE_DEBUG REG_DEBUG
+# define RE_DOT_NEWLINE REG_DOT_NEWLINE
+# define RE_DOT_NOT_NULL REG_DOT_NOT_NULL
+# define RE_HAT_LISTS_NOT_NEWLINE REG_HAT_LISTS_NOT_NEWLINE
+# define RE_ICASE REG_IGNORE_CASE /* avoid collision with REG_ICASE */
+# define RE_INTERVALS REG_INTERVALS
+# define RE_INVALID_INTERVAL_ORD REG_INVALID_INTERVAL_ORD
+# define RE_LIMITED_OPS REG_LIMITED_OPS
+# define RE_NEWLINE_ALT REG_NEWLINE_ALT
+# define RE_NO_BK_BRACES REG_NO_BK_BRACES
+# define RE_NO_BK_PARENS REG_NO_BK_PARENS
+# define RE_NO_BK_REFS REG_NO_BK_REFS
+# define RE_NO_BK_VBAR REG_NO_BK_VBAR
+# define RE_NO_EMPTY_RANGES REG_NO_EMPTY_RANGES
+# define RE_NO_GNU_OPS REG_NO_GNU_OPS
+# define RE_NO_POSIX_BACKTRACKING REG_NO_POSIX_BACKTRACKING
+# define RE_NO_SUB REG_NO_SUB
+# define RE_NREGS REG_NREGS
+# define RE_SYNTAX_AWK REG_SYNTAX_AWK
+# define RE_SYNTAX_ED REG_SYNTAX_ED
+# define RE_SYNTAX_EGREP REG_SYNTAX_EGREP
+# define RE_SYNTAX_EMACS REG_SYNTAX_EMACS
+# define RE_SYNTAX_GNU_AWK REG_SYNTAX_GNU_AWK
+# define RE_SYNTAX_GREP REG_SYNTAX_GREP
+# define RE_SYNTAX_POSIX_AWK REG_SYNTAX_POSIX_AWK
+# define RE_SYNTAX_POSIX_BASIC REG_SYNTAX_POSIX_BASIC
+# define RE_SYNTAX_POSIX_EGREP REG_SYNTAX_POSIX_EGREP
+# define RE_SYNTAX_POSIX_EXTENDED REG_SYNTAX_POSIX_EXTENDED
+# define RE_SYNTAX_POSIX_MINIMAL_BASIC REG_SYNTAX_POSIX_MINIMAL_BASIC
+# define RE_SYNTAX_POSIX_MINIMAL_EXTENDED REG_SYNTAX_POSIX_MINIMAL_EXTENDED
+# define RE_SYNTAX_SED REG_SYNTAX_SED
+# define RE_UNMATCHED_RIGHT_PAREN_ORD REG_UNMATCHED_RIGHT_PAREN_ORD
+# ifndef RE_TRANSLATE_TYPE
+#  define RE_TRANSLATE_TYPE REG_TRANSLATE_TYPE
+# endif
+
+#endif /* defined _REGEX_SOURCE */
+
 #ifdef __cplusplus
 }
 #endif /* C++ */
Index: lib/regex_internal.c
===================================================================
RCS file: /fetish/cu/lib/regex_internal.c,v
retrieving revision 1.3
diff -p -u -r1.3 regex_internal.c
--- lib/regex_internal.c        23 Aug 2005 19:41:09 -0000      1.3
+++ lib/regex_internal.c        9 Sep 2005 20:58:16 -0000
@@ -17,17 +17,17 @@
    with this program; if not, write to the Free Software Foundation,
    Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
 
-static void re_string_construct_common (const char *str, int len,
+static void re_string_construct_common (const char *str, Idx len,
                                        re_string_t *pstr,
-                                       RE_TRANSLATE_TYPE trans, int icase,
+                                       REG_TRANSLATE_TYPE trans, bool icase,
                                        const re_dfa_t *dfa) internal_function;
-static re_dfastate_t *create_ci_newstate (re_dfa_t *dfa,
+static re_dfastate_t *create_ci_newstate (const re_dfa_t *dfa,
                                          const re_node_set *nodes,
-                                         unsigned int hash) internal_function;
-static re_dfastate_t *create_cd_newstate (re_dfa_t *dfa,
+                                         re_hashval_t hash) internal_function;
+static re_dfastate_t *create_cd_newstate (const re_dfa_t *dfa,
                                          const re_node_set *nodes,
                                          unsigned int context,
-                                         unsigned int hash) internal_function;
+                                         re_hashval_t hash) internal_function;
 
 /* Functions for string operation.  */
 
@@ -36,11 +36,11 @@ static re_dfastate_t *create_cd_newstate
 
 static reg_errcode_t
 internal_function
-re_string_allocate (re_string_t *pstr, const char *str, int len, int init_len,
-                   RE_TRANSLATE_TYPE trans, int icase, const re_dfa_t *dfa)
+re_string_allocate (re_string_t *pstr, const char *str, Idx len, Idx init_len,
+                   REG_TRANSLATE_TYPE trans, bool icase, const re_dfa_t *dfa)
 {
   reg_errcode_t ret;
-  int init_buf_len;
+  Idx init_buf_len;
 
   /* Ensure at least one character fits into the buffers.  */
   if (init_len < dfa->mb_cur_max)
@@ -64,8 +64,8 @@ re_string_allocate (re_string_t *pstr, c
 
 static reg_errcode_t
 internal_function
-re_string_construct (re_string_t *pstr, const char *str, int len,
-                    RE_TRANSLATE_TYPE trans, int icase, const re_dfa_t *dfa)
+re_string_construct (re_string_t *pstr, const char *str, Idx len,
+                    REG_TRANSLATE_TYPE trans, bool icase, const re_dfa_t *dfa)
 {
   reg_errcode_t ret;
   memset (pstr, '\0', sizeof (re_string_t));
@@ -127,18 +127,18 @@ re_string_construct (re_string_t *pstr, 
 
 static reg_errcode_t
 internal_function
-re_string_realloc_buffers (re_string_t *pstr, int new_buf_len)
+re_string_realloc_buffers (re_string_t *pstr, Idx new_buf_len)
 {
 #ifdef RE_ENABLE_I18N
   if (pstr->mb_cur_max > 1)
     {
-      wint_t *new_wcs = re_realloc (pstr->wcs, wint_t, new_buf_len);
+      wint_t *new_wcs = re_xrealloc (pstr->wcs, wint_t, new_buf_len);
       if (BE (new_wcs == NULL, 0))
        return REG_ESPACE;
       pstr->wcs = new_wcs;
       if (pstr->offsets != NULL)
        {
-         int *new_offsets = re_realloc (pstr->offsets, int, new_buf_len);
+         Idx *new_offsets = re_xrealloc (pstr->offsets, Idx, new_buf_len);
          if (BE (new_offsets == NULL, 0))
            return REG_ESPACE;
          pstr->offsets = new_offsets;
@@ -160,15 +160,15 @@ re_string_realloc_buffers (re_string_t *
 
 static void
 internal_function
-re_string_construct_common (const char *str, int len, re_string_t *pstr,
-                           RE_TRANSLATE_TYPE trans, int icase,
+re_string_construct_common (const char *str, Idx len, re_string_t *pstr,
+                           REG_TRANSLATE_TYPE trans, bool icase,
                            const re_dfa_t *dfa)
 {
   pstr->raw_mbs = (const unsigned char *) str;
   pstr->len = len;
   pstr->raw_len = len;
-  pstr->trans = (unsigned RE_TRANSLATE_TYPE) trans;
-  pstr->icase = icase ? 1 : 0;
+  pstr->trans = (unsigned REG_TRANSLATE_TYPE) trans;
+  pstr->icase = icase;
   pstr->mbs_allocated = (trans != NULL || icase);
   pstr->mb_cur_max = dfa->mb_cur_max;
   pstr->is_utf8 = dfa->is_utf8;
@@ -201,7 +201,7 @@ build_wcs_buffer (re_string_t *pstr)
   unsigned char buf[64];
 #endif
   mbstate_t prev_st;
-  int byte_idx, end_idx, remain_len;
+  Idx byte_idx, end_idx, remain_len;
   size_t mbclen;
 
   /* Build the buffers from pstr->valid_len to either pstr->len or
@@ -258,12 +258,12 @@ build_wcs_buffer (re_string_t *pstr)
 /* Build wide character buffer PSTR->WCS like build_wcs_buffer,
    but for REG_ICASE.  */
 
-static int
+static reg_errcode_t
 internal_function
 build_wcs_upper_buffer (re_string_t *pstr)
 {
   mbstate_t prev_st;
-  int src_idx, byte_idx, end_idx, remain_len;
+  Idx src_idx, byte_idx, end_idx, remain_len;
   size_t mbclen;
 #ifdef _LIBC
   char buf[MB_LEN_MAX];
@@ -301,7 +301,7 @@ build_wcs_upper_buffer (re_string_t *pst
          mbclen = mbrtowc (&wc,
                            ((const char *) pstr->raw_mbs + pstr->raw_mbs_idx
                             + byte_idx), remain_len, &pstr->cur_state);
-         if (BE (mbclen + 2 > 2, 1))
+         if (BE ((size_t) (mbclen + 2) > 2, 1))
            {
              wchar_t wcu = wc;
              if (iswlower (wc))
@@ -369,7 +369,7 @@ build_wcs_upper_buffer (re_string_t *pst
        else
          p = (const char *) pstr->raw_mbs + pstr->raw_mbs_idx + src_idx;
        mbclen = mbrtowc (&wc, p, remain_len, &pstr->cur_state);
-       if (BE (mbclen + 2 > 2, 1))
+       if (BE ((size_t) (mbclen + 2) > 2, 1))
          {
            wchar_t wcu = wc;
            if (iswlower (wc))
@@ -392,7 +392,7 @@ build_wcs_upper_buffer (re_string_t *pst
 
                    if (pstr->offsets == NULL)
                      {
-                       pstr->offsets = re_malloc (int, pstr->bufs_len);
+                       pstr->offsets = re_xmalloc (Idx, pstr->bufs_len);
 
                        if (pstr->offsets == NULL)
                          return REG_ESPACE;
@@ -474,12 +474,12 @@ build_wcs_upper_buffer (re_string_t *pst
 /* Skip characters until the index becomes greater than NEW_RAW_IDX.
    Return the index.  */
 
-static int
+static Idx
 internal_function
-re_string_skip_chars (re_string_t *pstr, int new_raw_idx, wint_t *last_wc)
+re_string_skip_chars (re_string_t *pstr, Idx new_raw_idx, wint_t *last_wc)
 {
   mbstate_t prev_st;
-  int rawbuf_idx;
+  Idx rawbuf_idx;
   size_t mbclen;
   wchar_t wc = 0;
 
@@ -487,7 +487,7 @@ re_string_skip_chars (re_string_t *pstr,
   for (rawbuf_idx = pstr->raw_mbs_idx + pstr->valid_raw_len;
        rawbuf_idx < new_raw_idx;)
     {
-      int remain_len;
+      Idx remain_len;
       remain_len = pstr->len - rawbuf_idx;
       prev_st = pstr->cur_state;
       mbclen = mbrtowc (&wc, (const char *) pstr->raw_mbs + rawbuf_idx,
@@ -513,7 +513,7 @@ static void
 internal_function
 build_upper_buffer (re_string_t *pstr)
 {
-  int char_idx, end_idx;
+  Idx char_idx, end_idx;
   end_idx = (pstr->bufs_len > pstr->len) ? pstr->len : pstr->bufs_len;
 
   for (char_idx = pstr->valid_len; char_idx < end_idx; ++char_idx)
@@ -536,7 +536,7 @@ static void
 internal_function
 re_string_translate_buffer (re_string_t *pstr)
 {
-  int buf_idx, end_idx;
+  Idx buf_idx, end_idx;
   end_idx = (pstr->bufs_len > pstr->len) ? pstr->len : pstr->bufs_len;
 
   for (buf_idx = pstr->valid_len; buf_idx < end_idx; ++buf_idx)
@@ -555,10 +555,13 @@ re_string_translate_buffer (re_string_t 
 
 static reg_errcode_t
 internal_function
-re_string_reconstruct (re_string_t *pstr, int idx, int eflags)
+re_string_reconstruct (re_string_t *pstr, Idx idx, int eflags)
 {
-  int offset = idx - pstr->raw_mbs_idx;
-  if (BE (offset < 0, 0))
+  Idx offset;
+
+  if (BE (pstr->raw_mbs_idx <= idx, 0))
+    offset = idx - pstr->raw_mbs_idx;
+  else
     {
       /* Reset buffer.  */
 #ifdef RE_ENABLE_I18N
@@ -621,7 +624,7 @@ re_string_reconstruct (re_string_t *pstr
 #ifdef RE_ENABLE_I18N
          if (pstr->mb_cur_max > 1)
            {
-             int wcs_idx;
+             Idx wcs_idx;
              wint_t wc = WEOF;
 
              if (pstr->is_utf8)
@@ -637,8 +640,9 @@ re_string_reconstruct (re_string_t *pstr
                      {
                        mbstate_t cur_state;
                        wchar_t wc2;
-                       int mlen = raw + pstr->len - p;
+                       Idx mlen = raw + pstr->len - p;
                        unsigned char buf[6];
+                       size_t mbclen;
 
                        q = p;
                        if (BE (pstr->trans != NULL, 0))
@@ -651,14 +655,13 @@ re_string_reconstruct (re_string_t *pstr
                        /* XXX Don't use mbrtowc, we know which conversion
                           to use (UTF-8 -> UCS4).  */
                        memset (&cur_state, 0, sizeof (cur_state));
-                       mlen = (mbrtowc (&wc2, (const char *) p, mlen,
-                                        &cur_state)
-                               - (raw + offset - p));
-                       if (mlen >= 0)
+                       mbclen = mbrtowc (&wc2, (const char *) p, mlen,
+                                         &cur_state);
+                       if (raw + offset - p <= mbclen && mbclen < (size_t) -2)
                          {
                            memset (&pstr->cur_state, '\0',
                                    sizeof (mbstate_t));
-                           pstr->valid_len = mlen;
+                           pstr->valid_len = mbclen - (raw + offset - p);
                            wc = wc2;
                          }
                        break;
@@ -672,7 +675,7 @@ re_string_reconstruct (re_string_t *pstr
                  for (wcs_idx = 0; wcs_idx < pstr->valid_len; ++wcs_idx)
                    pstr->wcs[wcs_idx] = WEOF;
                  if (pstr->mbs_allocated)
-                   memset (pstr->mbs, 255, pstr->valid_len);
+                   memset (pstr->mbs, -1, pstr->valid_len);
                }
              pstr->valid_raw_len = pstr->valid_len;
              pstr->tip_context = ((BE (pstr->word_ops_used != 0, 0)
@@ -707,7 +710,7 @@ re_string_reconstruct (re_string_t *pstr
     {
       if (pstr->icase)
        {
-         int ret = build_wcs_upper_buffer (pstr);
+         reg_errcode_t ret = build_wcs_upper_buffer (pstr);
          if (BE (ret != REG_NOERROR, 0))
            return ret;
        }
@@ -731,10 +734,11 @@ re_string_reconstruct (re_string_t *pstr
 }
 
 static unsigned char
-internal_function
-re_string_peek_byte_case (const re_string_t *pstr, int idx)
+internal_function __attribute ((pure))
+re_string_peek_byte_case (const re_string_t *pstr, Idx idx)
 {
-  int ch, off;
+  int ch;
+  Idx off;
 
   /* Handle the common (easiest) cases first.  */
   if (BE (!pstr->mbs_allocated, 1))
@@ -767,7 +771,7 @@ re_string_peek_byte_case (const re_strin
 }
 
 static unsigned char
-internal_function
+internal_function __attribute ((pure))
 re_string_fetch_byte_case (re_string_t *pstr)
 {
   if (BE (!pstr->mbs_allocated, 1))
@@ -776,7 +780,8 @@ re_string_fetch_byte_case (re_string_t *
 #ifdef RE_ENABLE_I18N
   if (pstr->offsets_needed)
     {
-      int off, ch;
+      Idx off;
+      int ch;
 
       /* For tr_TR.UTF-8 [[:islower:]] there is
         [[: CAPITAL LETTER I WITH DOT lower:]] in mbs.  Skip
@@ -819,10 +824,10 @@ re_string_destruct (re_string_t *pstr)
 
 static unsigned int
 internal_function
-re_string_context_at (const re_string_t *input, int idx, int eflags)
+re_string_context_at (const re_string_t *input, Idx idx, int eflags)
 {
   int c;
-  if (BE (idx < 0, 0))
+  if (BE (! REG_VALID_INDEX (idx), 0))
     /* In this case, we use the value stored in input->tip_context,
        since we can't know the character in input->mbs[-1] here.  */
     return input->tip_context;
@@ -833,15 +838,15 @@ re_string_context_at (const re_string_t 
   if (input->mb_cur_max > 1)
     {
       wint_t wc;
-      int wc_idx = idx;
+      Idx wc_idx = idx;
       while(input->wcs[wc_idx] == WEOF)
        {
 #ifdef DEBUG
          /* It must not happen.  */
-         assert (wc_idx >= 0);
+         assert (REG_VALID_INDEX (wc_idx));
 #endif
          --wc_idx;
-         if (wc_idx < 0)
+         if (! REG_VALID_INDEX (wc_idx))
            return input->tip_context;
        }
       wc = input->wcs[wc_idx];
@@ -864,11 +869,11 @@ re_string_context_at (const re_string_t 
 
 static reg_errcode_t
 internal_function
-re_node_set_alloc (re_node_set *set, int size)
+re_node_set_alloc (re_node_set *set, Idx size)
 {
   set->alloc = size;
   set->nelem = 0;
-  set->elems = re_malloc (int, size);
+  set->elems = re_xmalloc (Idx, size);
   if (BE (set->elems == NULL, 0))
     return REG_ESPACE;
   return REG_NOERROR;
@@ -876,11 +881,11 @@ re_node_set_alloc (re_node_set *set, int
 
 static reg_errcode_t
 internal_function
-re_node_set_init_1 (re_node_set *set, int elem)
+re_node_set_init_1 (re_node_set *set, Idx elem)
 {
   set->alloc = 1;
   set->nelem = 1;
-  set->elems = re_malloc (int, 1);
+  set->elems = re_malloc (Idx, 1);
   if (BE (set->elems == NULL, 0))
     {
       set->alloc = set->nelem = 0;
@@ -892,10 +897,10 @@ re_node_set_init_1 (re_node_set *set, in
 
 static reg_errcode_t
 internal_function
-re_node_set_init_2 (re_node_set *set, int elem1, int elem2)
+re_node_set_init_2 (re_node_set *set, Idx elem1, Idx elem2)
 {
   set->alloc = 2;
-  set->elems = re_malloc (int, 2);
+  set->elems = re_malloc (Idx, 2);
   if (BE (set->elems == NULL, 0))
     return REG_ESPACE;
   if (elem1 == elem2)
@@ -928,13 +933,13 @@ re_node_set_init_copy (re_node_set *dest
   if (src->nelem > 0)
     {
       dest->alloc = dest->nelem;
-      dest->elems = re_malloc (int, dest->alloc);
+      dest->elems = re_malloc (Idx, dest->alloc);
       if (BE (dest->elems == NULL, 0))
        {
          dest->alloc = dest->nelem = 0;
          return REG_ESPACE;
        }
-      memcpy (dest->elems, src->elems, src->nelem * sizeof (int));
+      memcpy (dest->elems, src->elems, src->nelem * sizeof dest->elems[0]);
     }
   else
     re_node_set_init_empty (dest);
@@ -950,7 +955,7 @@ internal_function
 re_node_set_add_intersect (re_node_set *dest, const re_node_set *src1,
                           const re_node_set *src2)
 {
-  int i1, i2, is, id, delta, sbase;
+  Idx i1, i2, is, id, delta, sbase;
   if (src1->nelem == 0 || src2->nelem == 0)
     return REG_NOERROR;
 
@@ -958,8 +963,13 @@ re_node_set_add_intersect (re_node_set *
      conservative estimate.  */
   if (src1->nelem + src2->nelem + dest->nelem > dest->alloc)
     {
-      int new_alloc = src1->nelem + src2->nelem + dest->alloc;
-      int *new_elems = re_realloc (dest->elems, int, new_alloc);
+      Idx new_alloc = src1->nelem + src2->nelem + dest->alloc;
+      Idx *new_elems;
+      if (sizeof (Idx) < 3
+         && (new_alloc < dest->alloc
+             || ((Idx) (src1->nelem + src2->nelem) < src1->nelem)))
+       return REG_ESPACE;
+      new_elems = re_xrealloc (dest->elems, Idx, new_alloc);
       if (BE (new_elems == NULL, 0))
         return REG_ESPACE;
       dest->elems = new_elems;
@@ -977,25 +987,25 @@ re_node_set_add_intersect (re_node_set *
       if (src1->elems[i1] == src2->elems[i2])
        {
          /* Try to find the item in DEST.  Maybe we could binary search?  */
-         while (id >= 0 && dest->elems[id] > src1->elems[i1])
+         while (REG_VALID_INDEX (id) && dest->elems[id] > src1->elems[i1])
            --id;
 
-          if (id < 0 || dest->elems[id] != src1->elems[i1])
+          if (! REG_VALID_INDEX (id) || dest->elems[id] != src1->elems[i1])
             dest->elems[--sbase] = src1->elems[i1];
 
-         if (--i1 < 0 || --i2 < 0)
+         if (! REG_VALID_INDEX (--i1) || ! REG_VALID_INDEX (--i2))
            break;
        }
 
       /* Lower the highest of the two items.  */
       else if (src1->elems[i1] < src2->elems[i2])
        {
-         if (--i2 < 0)
+         if (! REG_VALID_INDEX (--i2))
            break;
        }
       else
        {
-         if (--i1 < 0)
+         if (! REG_VALID_INDEX (--i1))
            break;
        }
     }
@@ -1008,7 +1018,7 @@ re_node_set_add_intersect (re_node_set *
      DEST elements are already in place; this is more or
      less the same loop that is in re_node_set_merge.  */
   dest->nelem += delta;
-  if (delta > 0 && id >= 0)
+  if (delta > 0 && REG_VALID_INDEX (id))
     for (;;)
       {
         if (dest->elems[is] > dest->elems[id])
@@ -1022,13 +1032,13 @@ re_node_set_add_intersect (re_node_set *
           {
             /* Slide from the bottom.  */
             dest->elems[id + delta] = dest->elems[id];
-            if (--id < 0)
+            if (! REG_VALID_INDEX (--id))
               break;
           }
       }
 
   /* Copy remaining SRC elements.  */
-  memcpy (dest->elems, dest->elems + sbase, delta * sizeof (int));
+  memcpy (dest->elems, dest->elems + sbase, delta * sizeof dest->elems[0]);
 
   return REG_NOERROR;
 }
@@ -1041,11 +1051,13 @@ internal_function
 re_node_set_init_union (re_node_set *dest, const re_node_set *src1,
                        const re_node_set *src2)
 {
-  int i1, i2, id;
+  Idx i1, i2, id;
   if (src1 != NULL && src1->nelem > 0 && src2 != NULL && src2->nelem > 0)
     {
       dest->alloc = src1->nelem + src2->nelem;
-      dest->elems = re_malloc (int, dest->alloc);
+      if (sizeof (Idx) < 2 && dest->alloc < src1->nelem)
+       return REG_ESPACE;
+      dest->elems = re_xmalloc (Idx, dest->alloc);
       if (BE (dest->elems == NULL, 0))
        return REG_ESPACE;
     }
@@ -1073,13 +1085,13 @@ re_node_set_init_union (re_node_set *des
   if (i1 < src1->nelem)
     {
       memcpy (dest->elems + id, src1->elems + i1,
-            (src1->nelem - i1) * sizeof (int));
+            (src1->nelem - i1) * sizeof dest->elems[0]);
       id += src1->nelem - i1;
     }
   else if (i2 < src2->nelem)
     {
       memcpy (dest->elems + id, src2->elems + i2,
-            (src2->nelem - i2) * sizeof (int));
+            (src2->nelem - i2) * sizeof dest->elems[0]);
       id += src2->nelem - i2;
     }
   dest->nelem = id;
@@ -1093,13 +1105,20 @@ static reg_errcode_t
 internal_function
 re_node_set_merge (re_node_set *dest, const re_node_set *src)
 {
-  int is, id, sbase, delta;
+  Idx is, id, sbase, delta;
   if (src == NULL || src->nelem == 0)
     return REG_NOERROR;
+  if (sizeof (Idx) < 3
+      && ((Idx) (2 * src->nelem) < src->nelem
+         || (Idx) (2 * src->nelem + dest->nelem) < dest->nelem))
+    return REG_ESPACE;
   if (dest->alloc < 2 * src->nelem + dest->nelem)
     {
-      int new_alloc = 2 * (src->nelem + dest->alloc);
-      int *new_buffer = re_realloc (dest->elems, int, new_alloc);
+      Idx new_alloc = src->nelem + dest->alloc;
+      Idx *new_buffer;
+      if (sizeof (Idx) < 4 && new_alloc < dest->alloc)
+       return REG_ESPACE;
+      new_buffer = re_x2realloc (dest->elems, Idx, &new_alloc);
       if (BE (new_buffer == NULL, 0))
        return REG_ESPACE;
       dest->elems = new_buffer;
@@ -1109,14 +1128,15 @@ re_node_set_merge (re_node_set *dest, co
   if (BE (dest->nelem == 0, 0))
     {
       dest->nelem = src->nelem;
-      memcpy (dest->elems, src->elems, src->nelem * sizeof (int));
+      memcpy (dest->elems, src->elems, src->nelem * sizeof dest->elems[0]);
       return REG_NOERROR;
     }
 
   /* Copy into the top of DEST the items of SRC that are not
      found in DEST.  Maybe we could binary search in DEST?  */
   for (sbase = dest->nelem + 2 * src->nelem,
-       is = src->nelem - 1, id = dest->nelem - 1; is >= 0 && id >= 0; )
+       is = src->nelem - 1, id = dest->nelem - 1;
+       REG_VALID_INDEX (is) && REG_VALID_INDEX (id); )
     {
       if (dest->elems[id] == src->elems[is])
         is--, id--;
@@ -1126,11 +1146,12 @@ re_node_set_merge (re_node_set *dest, co
         --id;
     }
 
-  if (is >= 0)
+  if (REG_VALID_INDEX (is))
     {
       /* If DEST is exhausted, the remaining items of SRC must be unique.  */
       sbase -= is + 1;
-      memcpy (dest->elems + sbase, src->elems, (is + 1) * sizeof (int));
+      memcpy (dest->elems + sbase, src->elems,
+             (is + 1) * sizeof dest->elems[0]);
     }
 
   id = dest->nelem - 1;
@@ -1155,11 +1176,11 @@ re_node_set_merge (re_node_set *dest, co
         {
           /* Slide from the bottom.  */
           dest->elems[id + delta] = dest->elems[id];
-         if (--id < 0)
+         if (! REG_VALID_INDEX (--id))
            {
              /* Copy remaining SRC elements.  */
              memcpy (dest->elems, dest->elems + sbase,
-                     delta * sizeof (int));
+                     delta * sizeof dest->elems[0]);
              break;
            }
        }
@@ -1170,38 +1191,31 @@ re_node_set_merge (re_node_set *dest, co
 
 /* Insert the new element ELEM to the re_node_set* SET.
    SET should not already have ELEM.
-   return -1 if an error is occured, return 1 otherwise.  */
+   Return true if successful.  */
 
-static int
+static bool
 internal_function
-re_node_set_insert (re_node_set *set, int elem)
+re_node_set_insert (re_node_set *set, Idx elem)
 {
-  int idx;
+  Idx idx;
   /* In case the set is empty.  */
   if (set->alloc == 0)
-    {
-      if (BE (re_node_set_init_1 (set, elem) == REG_NOERROR, 1))
-       return 1;
-      else
-       return -1;
-    }
+    return re_node_set_init_1 (set, elem) == REG_NOERROR;
 
   if (BE (set->nelem, 0) == 0)
     {
       /* We already guaranteed above that set->alloc != 0.  */
       set->elems[0] = elem;
       ++set->nelem;
-      return 1;
+      return true;
     }
 
   /* Realloc if we need.  */
   if (set->alloc == set->nelem)
     {
-      int *new_elems;
-      set->alloc = set->alloc * 2;
-      new_elems = re_realloc (set->elems, int, set->alloc);
+      Idx *new_elems = re_x2realloc (set->elems, Idx, &set->alloc);
       if (BE (new_elems == NULL, 0))
-       return -1;
+       return false;
       set->elems = new_elems;
     }
 
@@ -1222,57 +1236,56 @@ re_node_set_insert (re_node_set *set, in
   /* Insert the new element.  */
   set->elems[idx] = elem;
   ++set->nelem;
-  return 1;
+  return true;
 }
 
 /* Insert the new element ELEM to the re_node_set* SET.
    SET should not already have any element greater than or equal to ELEM.
-   Return -1 if an error is occured, return 1 otherwise.  */
+   Return true if successful.  */
 
-static int
+static bool
 internal_function
-re_node_set_insert_last (re_node_set *set, int elem)
+re_node_set_insert_last (re_node_set *set, Idx elem)
 {
   /* Realloc if we need.  */
   if (set->alloc == set->nelem)
     {
-      int *new_elems;
-      set->alloc = (set->alloc + 1) * 2;
-      new_elems = re_realloc (set->elems, int, set->alloc);
+      Idx *new_elems;
+      new_elems = re_x2realloc (set->elems, Idx, &set->alloc);
       if (BE (new_elems == NULL, 0))
-       return -1;
+       return false;
       set->elems = new_elems;
     }
 
   /* Insert the new element.  */
   set->elems[set->nelem++] = elem;
-  return 1;
+  return true;
 }
 
 /* Compare two node sets SET1 and SET2.
-   return 1 if SET1 and SET2 are equivalent, return 0 otherwise.  */
+   Return true if SET1 and SET2 are equivalent.  */
 
-static int
-internal_function
+static bool
+internal_function __attribute ((pure))
 re_node_set_compare (const re_node_set *set1, const re_node_set *set2)
 {
-  int i;
+  Idx i;
   if (set1 == NULL || set2 == NULL || set1->nelem != set2->nelem)
-    return 0;
-  for (i = set1->nelem ; --i >= 0 ; )
+    return false;
+  for (i = set1->nelem ; REG_VALID_INDEX (--i) ; )
     if (set1->elems[i] != set2->elems[i])
-      return 0;
-  return 1;
+      return false;
+  return true;
 }
 
 /* Return (idx + 1) if SET contains the element ELEM, return 0 otherwise.  */
 
-static int
-internal_function
-re_node_set_contains (const re_node_set *set, int elem)
+static Idx
+internal_function __attribute ((pure))
+re_node_set_contains (const re_node_set *set, Idx elem)
 {
-  unsigned int idx, right, mid;
-  if (set->nelem <= 0)
+  __re_size_t idx, right, mid;
+  if (! REG_VALID_NONZERO_INDEX (set->nelem))
     return 0;
 
   /* Binary search the element.  */
@@ -1291,7 +1304,7 @@ re_node_set_contains (const re_node_set 
 
 static void
 internal_function
-re_node_set_remove_at (re_node_set *set, int idx)
+re_node_set_remove_at (re_node_set *set, Idx idx)
 {
   if (idx < 0 || idx >= set->nelem)
     return;
@@ -1302,31 +1315,31 @@ re_node_set_remove_at (re_node_set *set,
 
 
 /* Add the token TOKEN to dfa->nodes, and return the index of the token.
-   Or return -1, if an error will be occured.  */
+   Or return REG_MISSING if an error occurred.  */
 
-static int
+static Idx
 internal_function
 re_dfa_add_node (re_dfa_t *dfa, re_token_t token)
 {
   int type = token.type;
   if (BE (dfa->nodes_len >= dfa->nodes_alloc, 0))
     {
-      int new_nodes_alloc = dfa->nodes_alloc * 2;
-      int *new_nexts, *new_indices;
+      Idx new_nodes_alloc = dfa->nodes_alloc;
+      Idx *new_nexts, *new_indices;
       re_node_set *new_edests, *new_eclosures;
 
-      re_token_t *new_nodes = re_realloc (dfa->nodes, re_token_t,
-                                         new_nodes_alloc);
+      re_token_t *new_nodes = re_x2realloc (dfa->nodes, re_token_t,
+                                           &new_nodes_alloc);
       if (BE (new_nodes == NULL, 0))
-       return -1;
+       return REG_MISSING;
       dfa->nodes = new_nodes;
-      new_nexts = re_realloc (dfa->nexts, int, new_nodes_alloc);
-      new_indices = re_realloc (dfa->org_indices, int, new_nodes_alloc);
-      new_edests = re_realloc (dfa->edests, re_node_set, new_nodes_alloc);
+      new_nexts = re_realloc (dfa->nexts, Idx, new_nodes_alloc);
+      new_indices = re_realloc (dfa->org_indices, Idx, new_nodes_alloc);
+      new_edests = re_xrealloc (dfa->edests, re_node_set, new_nodes_alloc);
       new_eclosures = re_realloc (dfa->eclosures, re_node_set, 
new_nodes_alloc);
       if (BE (new_nexts == NULL || new_indices == NULL
              || new_edests == NULL || new_eclosures == NULL, 0))
-       return -1;
+       return REG_MISSING;
       dfa->nexts = new_nexts;
       dfa->org_indices = new_indices;
       dfa->edests = new_edests;
@@ -1339,18 +1352,18 @@ re_dfa_add_node (re_dfa_t *dfa, re_token
   dfa->nodes[dfa->nodes_len].accept_mb =
     (type == OP_PERIOD && dfa->mb_cur_max > 1) || type == COMPLEX_BRACKET;
 #endif
-  dfa->nexts[dfa->nodes_len] = -1;
+  dfa->nexts[dfa->nodes_len] = REG_MISSING;
   re_node_set_init_empty (dfa->edests + dfa->nodes_len);
   re_node_set_init_empty (dfa->eclosures + dfa->nodes_len);
   return dfa->nodes_len++;
 }
 
-static inline unsigned int
+static inline re_hashval_t
 internal_function
 calc_state_hash (const re_node_set *nodes, unsigned int context)
 {
-  unsigned int hash = nodes->nelem + context;
-  int i;
+  re_hashval_t hash = nodes->nelem + context;
+  Idx i;
   for (i = 0 ; i < nodes->nelem ; i++)
     hash += nodes->elems[i];
   return hash;
@@ -1369,10 +1382,10 @@ static re_dfastate_t*
 internal_function
 re_acquire_state (reg_errcode_t *err, re_dfa_t *dfa, const re_node_set *nodes)
 {
-  unsigned int hash;
+  re_hashval_t hash;
   re_dfastate_t *new_state;
   struct re_state_table_entry *spot;
-  int i;
+  Idx i;
 #ifdef lint
   /* Suppress bogus uninitialized-variable warnings.  */
   *err = REG_NOERROR;
@@ -1420,10 +1433,10 @@ internal_function
 re_acquire_state_context (reg_errcode_t *err, re_dfa_t *dfa,
                          const re_node_set *nodes, unsigned int context)
 {
-  unsigned int hash;
+  re_hashval_t hash;
   re_dfastate_t *new_state;
   struct re_state_table_entry *spot;
-  int i;
+  Idx i;
 #ifdef lint
   /* Suppress bogus uninitialized-variable warnings.  */
   *err = REG_NOERROR;
@@ -1461,11 +1474,11 @@ re_acquire_state_context (reg_errcode_t 
 
 static reg_errcode_t
 internal_function
-register_state (re_dfa_t *dfa, re_dfastate_t *newstate, unsigned int hash)
+register_state (const re_dfa_t *dfa, re_dfastate_t *newstate, re_hashval_t 
hash)
 {
   struct re_state_table_entry *spot;
   reg_errcode_t err;
-  int i;
+  Idx i;
 
   newstate->hash = hash;
   err = re_node_set_alloc (&newstate->non_eps_nodes, newstate->nodes.nelem);
@@ -1473,17 +1486,21 @@ register_state (re_dfa_t *dfa, re_dfasta
     return REG_ESPACE;
   for (i = 0; i < newstate->nodes.nelem; i++)
     {
-      int elem = newstate->nodes.elems[i];
+      Idx elem = newstate->nodes.elems[i];
       if (!IS_EPSILON_NODE (dfa->nodes[elem].type))
-        re_node_set_insert_last (&newstate->non_eps_nodes, elem);
+       {
+         bool ok = re_node_set_insert_last (&newstate->non_eps_nodes, elem);
+         if (BE (! ok, 0))
+           return REG_ESPACE;
+       }
     }
 
   spot = dfa->state_table + (hash & dfa->state_hash_mask);
   if (BE (spot->alloc <= spot->num, 0))
     {
-      int new_alloc = 2 * spot->num + 2;
-      re_dfastate_t **new_array = re_realloc (spot->array, re_dfastate_t *,
-                                             new_alloc);
+      Idx new_alloc = spot->num;
+      re_dfastate_t **new_array = re_x2realloc (spot->array, re_dfastate_t *,
+                                               &new_alloc);
       if (BE (new_array == NULL, 0))
        return REG_ESPACE;
       spot->array = new_array;
@@ -1498,13 +1515,14 @@ register_state (re_dfa_t *dfa, re_dfasta
 
 static re_dfastate_t *
 internal_function
-create_ci_newstate (re_dfa_t *dfa, const re_node_set *nodes, unsigned int hash)
+create_ci_newstate (const re_dfa_t *dfa, const re_node_set *nodes,
+                   re_hashval_t hash)
 {
-  int i;
+  Idx i;
   reg_errcode_t err;
   re_dfastate_t *newstate;
 
-  newstate = (re_dfastate_t *) calloc (sizeof (re_dfastate_t), 1);
+  newstate = re_calloc (re_dfastate_t, 1);
   if (BE (newstate == NULL, 0))
     return NULL;
   err = re_node_set_init_copy (&newstate->nodes, nodes);
@@ -1547,14 +1565,14 @@ create_ci_newstate (re_dfa_t *dfa, const
 
 static re_dfastate_t *
 internal_function
-create_cd_newstate (re_dfa_t *dfa, const re_node_set *nodes,
-                   unsigned int context, unsigned int hash)
+create_cd_newstate (const re_dfa_t *dfa, const re_node_set *nodes,
+                   unsigned int context, re_hashval_t hash)
 {
-  int i, nctx_nodes = 0;
+  Idx i, nctx_nodes = 0;
   reg_errcode_t err;
   re_dfastate_t *newstate;
 
-  newstate = (re_dfastate_t *) calloc (sizeof (re_dfastate_t), 1);
+  newstate = re_calloc (re_dfastate_t, 1);
   if (BE (newstate == NULL, 0))
     return NULL;
   err = re_node_set_init_copy (&newstate->nodes, nodes);
Index: lib/regex_internal.h
===================================================================
RCS file: /fetish/cu/lib/regex_internal.h,v
retrieving revision 1.2
diff -p -u -r1.2 regex_internal.h
--- lib/regex_internal.h        23 Aug 2005 19:41:09 -0000      1.2
+++ lib/regex_internal.h        9 Sep 2005 20:58:16 -0000
@@ -22,6 +22,7 @@
 
 #include <assert.h>
 #include <ctype.h>
+#include <stdbool.h>
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
@@ -90,8 +91,6 @@
 # define inline
 #endif
 
-/* Number of bits in a byte.  */
-#define BYTE_BITS 8
 /* Number of single byte character.  */
 #define SBC_MAX 256
 
@@ -123,26 +122,73 @@
 extern const char __re_error_msgid[] attribute_hidden;
 extern const size_t __re_error_msgid_idx[] attribute_hidden;
 
-/* Number of bits in an unsinged int.  */
-#define UINT_BITS (sizeof (unsigned int) * BYTE_BITS)
-/* Number of unsigned int in an bit_set.  */
-#define BITSET_UINTS ((SBC_MAX + UINT_BITS - 1) / UINT_BITS)
-typedef unsigned int bitset[BITSET_UINTS];
-typedef unsigned int *re_bitset_ptr_t;
-typedef const unsigned int *re_const_bitset_ptr_t;
-
-#define bitset_set(set,i) (set[i / UINT_BITS] |= 1 << i % UINT_BITS)
-#define bitset_clear(set,i) (set[i / UINT_BITS] &= ~(1 << i % UINT_BITS))
-#define bitset_contain(set,i) (set[i / UINT_BITS] & (1 << i % UINT_BITS))
-#define bitset_empty(set) memset (set, 0, sizeof (unsigned int) * BITSET_UINTS)
-#define bitset_set_all(set) \
-  memset (set, 255, sizeof (unsigned int) * BITSET_UINTS)
-#define bitset_copy(dest,src) \
-  memcpy (dest, src, sizeof (unsigned int) * BITSET_UINTS)
-static inline void bitset_not (bitset set);
-static inline void bitset_merge (bitset dest, const bitset src);
-static inline void bitset_not_merge (bitset dest, const bitset src);
-static inline void bitset_mask (bitset dest, const bitset src);
+typedef __re_idx_t Idx;
+
+/* Special return value for failure to match.  */
+#define REG_MISSING ((Idx) -1)
+
+/* Special return value for internal error.  */
+#define REG_ERROR ((Idx) -2)
+
+/* Test whether N is a valid index, and is not one of the above.  */
+#ifdef _REGEX_LARGE_OFFSETS
+# define REG_VALID_INDEX(n) ((Idx) (n) < REG_ERROR)
+#else
+# define REG_VALID_INDEX(n) (0 <= (n))
+#endif
+
+/* Test whether N is a valid nonzero index.  */
+#ifdef _REGEX_LARGE_OFFSETS
+# define REG_VALID_NONZERO_INDEX(n) ((Idx) ((n) - 1) < (Idx) (REG_ERROR - 1))
+#else
+# define REG_VALID_NONZERO_INDEX(n) (0 < (n))
+#endif
+
+/* A hash value, suitable for computing hash tables.  */
+typedef __re_size_t re_hashval_t;
+
+/* An integer used to represent a set of bits.  It must be unsigned,
+   and must be at least as wide as unsigned int.  */
+typedef unsigned long int bitset_word;
+
+/* Maximum value of a bitset word.  It must be useful in preprocessor
+   contexts, and must be consistent with bitset_word.  */
+#define BITSET_WORD_MAX ULONG_MAX
+
+/* Number of bits in a bitset word.  Avoid greater-than-32-bit
+   integers and unconditional shifts by more than 31 bits, as they're
+   not portable.  */
+#if BITSET_WORD_MAX == 0xffffffff
+# define BITSET_WORD_BITS 32
+#elif BITSET_WORD_MAX >> 31 >> 5 == 1
+# define BITSET_WORD_BITS 36
+#elif BITSET_WORD_MAX >> 31 >> 16 == 1
+# define BITSET_WORD_BITS 48
+#elif BITSET_WORD_MAX >> 31 >> 28 == 1
+# define BITSET_WORD_BITS 60
+#elif BITSET_WORD_MAX >> 31 >> 31 >> 1 == 1
+# define BITSET_WORD_BITS 64
+#elif BITSET_WORD_MAX >> 31 >> 31 >> 9 == 1
+# define BITSET_WORD_BITS 72
+#elif BITSET_WORD_MAX >> 31 >> 31 >> 31 >> 31 >> 3 == 1
+# define BITSET_WORD_BITS 128
+#elif BITSET_WORD_MAX >> 31 >> 31 >> 31 >> 31 >> 31 >> 31 >> 31 >> 31 >> 7 == 1
+# define BITSET_WORD_BITS 256
+#elif BITSET_WORD_MAX >> 31 >> 31 >> 31 >> 31 >> 31 >> 31 >> 31 >> 31 >> 7 > 1
+# define BITSET_WORD_BITS 257 /* any value > SBC_MAX will do here */
+# if BITSET_WORD_BITS <= SBC_MAX
+#  error "Invalid SBC_MAX"
+# endif
+#else
+# error "Add case for new bitset_word size"
+#endif
+
+/* Number of bitset words in a bitset.  */
+#define BITSET_WORDS ((SBC_MAX + BITSET_WORD_BITS - 1) / BITSET_WORD_BITS)
+
+typedef bitset_word bitset[BITSET_WORDS];
+typedef bitset_word *re_bitset_ptr_t;
+typedef const bitset_word *re_const_bitset_ptr_t;
 
 #define PREV_WORD_CONSTRAINT 0x0001
 #define PREV_NOTWORD_CONSTRAINT 0x0002
@@ -171,9 +217,9 @@ typedef enum
 
 typedef struct
 {
-  int alloc;
-  int nelem;
-  int *elems;
+  Idx alloc;
+  Idx nelem;
+  Idx *elems;
 } re_node_set;
 
 typedef enum
@@ -259,19 +305,19 @@ typedef struct
   unsigned int non_match : 1;
 
   /* # of multibyte characters.  */
-  int nmbchars;
+  Idx nmbchars;
 
   /* # of collating symbols.  */
-  int ncoll_syms;
+  Idx ncoll_syms;
 
   /* # of equivalence classes. */
-  int nequiv_classes;
+  Idx nequiv_classes;
 
   /* # of range expressions. */
-  int nranges;
+  Idx nranges;
 
   /* # of character classes. */
-  int nchar_classes;
+  Idx nchar_classes;
 } re_charset_t;
 #endif /* RE_ENABLE_I18N */
 
@@ -284,7 +330,7 @@ typedef struct
 #ifdef RE_ENABLE_I18N
     re_charset_t *mbcset;      /* for COMPLEX_BRACKET */
 #endif /* RE_ENABLE_I18N */
-    int idx;                   /* for BACK_REF */
+    Idx idx;                   /* for BACK_REF */
     re_context_type ctx_type;  /* for ANCHOR */
   } opr;
 #if __GNUC__ >= 2
@@ -318,40 +364,40 @@ struct re_string_t
 #ifdef RE_ENABLE_I18N
   /* Store the wide character string which is corresponding to MBS.  */
   wint_t *wcs;
-  int *offsets;
+  Idx *offsets;
   mbstate_t cur_state;
 #endif
   /* Index in RAW_MBS.  Each character mbs[i] corresponds to
      raw_mbs[raw_mbs_idx + i].  */
-  int raw_mbs_idx;
+  Idx raw_mbs_idx;
   /* The length of the valid characters in the buffers.  */
-  int valid_len;
+  Idx valid_len;
   /* The corresponding number of bytes in raw_mbs array.  */
-  int valid_raw_len;
+  Idx valid_raw_len;
   /* The length of the buffers MBS and WCS.  */
-  int bufs_len;
+  Idx bufs_len;
   /* The index in MBS, which is updated by re_string_fetch_byte.  */
-  int cur_idx;
+  Idx cur_idx;
   /* length of RAW_MBS array.  */
-  int raw_len;
+  Idx raw_len;
   /* This is RAW_LEN - RAW_MBS_IDX + VALID_LEN - VALID_RAW_LEN.  */
-  int len;
+  Idx len;
   /* End of the buffer may be shorter than its length in the cases such
      as re_match_2, re_search_2.  Then, we use STOP for end of the buffer
      instead of LEN.  */
-  int raw_stop;
+  Idx raw_stop;
   /* This is RAW_STOP - RAW_MBS_IDX adjusted through OFFSETS.  */
-  int stop;
+  Idx stop;
 
   /* The context of mbs[0].  We store the context independently, since
      the context of mbs[0] may be different from raw_mbs[0], which is
      the beginning of the input string.  */
   unsigned int tip_context;
   /* The translation passed as a part of an argument of re_compile_pattern.  */
-  unsigned RE_TRANSLATE_TYPE trans;
+  unsigned REG_TRANSLATE_TYPE trans;
   /* Copy of re_dfa_t's word_char.  */
   re_const_bitset_ptr_t word_char;
-  /* 1 if REG_ICASE.  */
+  /* true if REG_ICASE.  */
   unsigned char icase;
   unsigned char is_utf8;
   unsigned char map_notascii;
@@ -375,42 +421,18 @@ typedef struct re_dfa_t re_dfa_t;
 # endif
 #endif
 
-static reg_errcode_t re_string_allocate (re_string_t *pstr, const char *str,
-                                        int len, int init_len,
-                                        RE_TRANSLATE_TYPE trans, int icase,
-                                        const re_dfa_t *dfa)
-     internal_function;
-static reg_errcode_t re_string_construct (re_string_t *pstr, const char *str,
-                                         int len, RE_TRANSLATE_TYPE trans,
-                                         int icase, const re_dfa_t *dfa)
-     internal_function;
-static reg_errcode_t re_string_reconstruct (re_string_t *pstr, int idx,
-                                           int eflags) internal_function;
 static reg_errcode_t re_string_realloc_buffers (re_string_t *pstr,
-                                               int new_buf_len)
+                                               Idx new_buf_len)
      internal_function;
 #ifdef RE_ENABLE_I18N
 static void build_wcs_buffer (re_string_t *pstr) internal_function;
-static int build_wcs_upper_buffer (re_string_t *pstr) internal_function;
+static reg_errcode_t build_wcs_upper_buffer (re_string_t *pstr)
+     internal_function;
 #endif /* RE_ENABLE_I18N */
 static void build_upper_buffer (re_string_t *pstr) internal_function;
 static void re_string_translate_buffer (re_string_t *pstr) internal_function;
-static void re_string_destruct (re_string_t *pstr) internal_function;
-#ifdef RE_ENABLE_I18N
-static int re_string_elem_size_at (const re_string_t *pstr, int idx)
-     internal_function __attribute ((pure));
-static inline int re_string_char_size_at (const re_string_t *pstr, int idx)
-     internal_function __attribute ((pure));
-static inline wint_t re_string_wchar_at (const re_string_t *pstr, int idx)
-     internal_function __attribute ((pure));
-#endif /* RE_ENABLE_I18N */
-static unsigned int re_string_context_at (const re_string_t *input, int idx,
-                                         int eflags)
-     internal_function __attribute ((pure));
-static unsigned char re_string_peek_byte_case (const re_string_t *pstr,
-                                              int idx)
-     internal_function __attribute ((pure));
-static unsigned char re_string_fetch_byte_case (re_string_t *pstr)
+static unsigned int re_string_context_at (const re_string_t *input,
+                                         Idx idx, int eflags)
      internal_function __attribute ((pure));
 
 #define re_string_peek_byte(pstr, offset) \
@@ -430,10 +452,86 @@ static unsigned char re_string_fetch_byt
 #define re_string_skip_bytes(pstr,idx) ((pstr)->cur_idx += (idx))
 #define re_string_set_index(pstr,idx) ((pstr)->cur_idx = (idx))
 
+#include <alloca.h>
+
+#ifndef _LIBC
+# if HAVE_ALLOCA
+/* The OS usually guarantees only one guard page at the bottom of the stack,
+   and a page size can be as small as 4096 bytes.  So we cannot safely
+   allocate anything larger than 4096 bytes.  Also care for the possibility
+   of a few compiler-allocated temporary stack slots.  */
+#  define __libc_use_alloca(n) ((n) < 4032)
+# else
+/* alloca is implemented with malloc, so just use malloc.  */
+#  define __libc_use_alloca(n) 0
+# endif
+#endif
+
 #define re_malloc(t,n) ((t *) malloc ((n) * sizeof (t)))
+#define re_xmalloc(t,n) ((t *) re_xnmalloc (n, sizeof (t)))
+#define re_calloc(t,n) ((t *) calloc (n, sizeof (t)))
 #define re_realloc(p,t,n) ((t *) realloc (p, (n) * sizeof (t)))
+#define re_xrealloc(p,t,n) ((t *) re_xnrealloc (p, n, sizeof (t)))
+#define re_x2realloc(p,t,pn) ((t *) re_x2nrealloc (p, pn, sizeof (t)))
 #define re_free(p) free (p)
 
+#ifndef SIZE_MAX
+# define SIZE_MAX ((size_t) -1)
+#endif
+
+/* Return true if an array of N objects, each of size S, cannot exist
+   due to size arithmetic overflow.  S must be nonzero.  */
+static inline bool
+re_alloc_oversized (size_t n, size_t s)
+{
+  return BE (SIZE_MAX / s < n, 0);
+}
+
+/* Return true if an array of (2 * N + 1) objects, each of size S,
+   cannot exist due to size arithmetic overflow.  S must be nonzero.  */
+static inline bool
+re_x2alloc_oversized (size_t n, size_t s)
+{
+  return BE ((SIZE_MAX / s - 1) / 2 < n, 0);
+}
+
+/* Allocate an array of N objects, each with S bytes of memory,
+   dynamically, with error checking.  S must be nonzero.  */
+static inline void *
+re_xnmalloc (size_t n, size_t s)
+{
+  return re_alloc_oversized (n, s) ? NULL : malloc (n * s);
+}
+
+/* Change the size of an allocated block of memory P to an array of N
+   objects each of S bytes, with error checking.  S must be nonzero.  */
+static inline void *
+re_xnrealloc (void *p, size_t n, size_t s)
+{
+  return re_alloc_oversized (n, s) ? NULL : realloc (p, n * s);
+}
+
+/* Reallocate a block of memory P to an array of (2 * (*PN) + 1)
+   objects each of S bytes, with error checking.  S must be nonzero.
+   If the allocation is successful, set *PN to the new allocation
+   count and return the resulting pointer.  Otherwise, return
+   NULL.  */
+static inline void *
+re_x2nrealloc (void *p, size_t *pn, size_t s)
+{
+  if (re_x2alloc_oversized (*pn, s))
+    return NULL;
+  else
+    {
+      /* Add 1 in case *PN is zero.  */
+      size_t n1 = 2 * *pn + 1;
+      p = realloc (p, n1 * s);
+      if (BE (p != NULL, 1))
+       *pn = n1;
+      return p;
+    }
+}
+
 struct bin_tree_t
 {
   struct bin_tree_t *parent;
@@ -446,7 +544,7 @@ struct bin_tree_t
 
   /* `node_idx' is the index in dfa->nodes, if `type' == 0.
      Otherwise `type' indicate the type of this node.  */
-  int node_idx;
+  Idx node_idx;
 };
 typedef struct bin_tree_t bin_tree_t;
 
@@ -490,7 +588,7 @@ typedef struct bin_tree_storage_t bin_tr
 
 struct re_dfastate_t
 {
-  unsigned int hash;
+  re_hashval_t hash;
   re_node_set nodes;
   re_node_set non_eps_nodes;
   re_node_set inveclosure;
@@ -510,8 +608,8 @@ typedef struct re_dfastate_t re_dfastate
 
 struct re_state_table_entry
 {
-  int num;
-  int alloc;
+  Idx num;
+  Idx alloc;
   re_dfastate_t **array;
 };
 
@@ -519,8 +617,8 @@ struct re_state_table_entry
 
 typedef struct
 {
-  int next_idx;
-  int alloc;
+  Idx next_idx;
+  Idx alloc;
   re_dfastate_t **array;
 } state_array_t;
 
@@ -528,8 +626,8 @@ typedef struct
 
 typedef struct
 {
-  int node;
-  int str_idx; /* The position NODE match at.  */
+  Idx node;
+  Idx str_idx; /* The position NODE match at.  */
   state_array_t path;
 } re_sub_match_last_t;
 
@@ -539,21 +637,20 @@ typedef struct
 
 typedef struct
 {
-  int str_idx;
-  int node;
-  int next_last_offset;
+  Idx str_idx;
+  Idx node;
   state_array_t *path;
-  int alasts; /* Allocation size of LASTS.  */
-  int nlasts; /* The number of LASTS.  */
+  Idx alasts; /* Allocation size of LASTS.  */
+  Idx nlasts; /* The number of LASTS.  */
   re_sub_match_last_t **lasts;
 } re_sub_match_top_t;
 
 struct re_backref_cache_entry
 {
-  int node;
-  int str_idx;
-  int subexp_from;
-  int subexp_to;
+  Idx node;
+  Idx str_idx;
+  Idx subexp_from;
+  Idx subexp_to;
   char more;
   char unused;
   unsigned short int eps_reachable_subexps_map;
@@ -571,18 +668,18 @@ typedef struct
   /* EFLAGS of the argument of regexec.  */
   int eflags;
   /* Where the matching ends.  */
-  int match_last;
-  int last_node;
+  Idx match_last;
+  Idx last_node;
   /* The state log used by the matcher.  */
   re_dfastate_t **state_log;
-  int state_log_top;
+  Idx state_log_top;
   /* Back reference cache.  */
-  int nbkref_ents;
-  int abkref_ents;
+  Idx nbkref_ents;
+  Idx abkref_ents;
   struct re_backref_cache_entry *bkref_ents;
   int max_mb_elem_len;
-  int nsub_tops;
-  int asub_tops;
+  Idx nsub_tops;
+  Idx asub_tops;
   re_sub_match_top_t **sub_tops;
 } re_match_context_t;
 
@@ -590,33 +687,33 @@ typedef struct
 {
   re_dfastate_t **sifted_states;
   re_dfastate_t **limited_states;
-  int last_node;
-  int last_str_idx;
+  Idx last_node;
+  Idx last_str_idx;
   re_node_set limits;
 } re_sift_context_t;
 
 struct re_fail_stack_ent_t
 {
-  int idx;
-  int node;
+  Idx idx;
+  Idx node;
   regmatch_t *regs;
   re_node_set eps_via_nodes;
 };
 
 struct re_fail_stack_t
 {
-  int num;
-  int alloc;
+  Idx num;
+  Idx alloc;
   struct re_fail_stack_ent_t *stack;
 };
 
 struct re_dfa_t
 {
   re_token_t *nodes;
-  int nodes_alloc;
-  int nodes_len;
-  int *nexts;
-  int *org_indices;
+  Idx nodes_alloc;
+  Idx nodes_len;
+  Idx *nexts;
+  Idx *org_indices;
   re_node_set *edests;
   re_node_set *eclosures;
   re_node_set *inveclosures;
@@ -631,14 +728,13 @@ struct re_dfa_t
   int str_tree_storage_idx;
 
   /* number of subexpressions `re_nsub' is in regex_t.  */
-  unsigned int state_hash_mask;
-  int states_alloc;
-  int init_node;
-  int nbackref; /* The number of backreference in this dfa.  */
+  re_hashval_t state_hash_mask;
+  Idx init_node;
+  Idx nbackref; /* The number of backreference in this dfa.  */
 
   /* Bitmap expressing which backreference is used.  */
-  unsigned int used_bkref_map;
-  unsigned int completed_bkref_map;
+  bitset_word used_bkref_map;
+  bitset_word completed_bkref_map;
 
   unsigned int has_plural_match : 1;
   /* If this dfa has "multibyte node", which is a backreference or
@@ -651,51 +747,20 @@ struct re_dfa_t
   int mb_cur_max;
   bitset word_char;
   reg_syntax_t syntax;
-  int *subexp_map;
+  Idx *subexp_map;
 #ifdef DEBUG
   char* re_str;
 #endif
   __libc_lock_define (, lock)
 };
 
-#ifndef RE_NO_INTERNAL_PROTOTYPES
-static reg_errcode_t re_node_set_alloc (re_node_set *set, int size) 
internal_function;
-static reg_errcode_t re_node_set_init_1 (re_node_set *set, int elem) 
internal_function;
-static reg_errcode_t re_node_set_init_2 (re_node_set *set, int elem1,
-                                        int elem2) internal_function;
 #define re_node_set_init_empty(set) memset (set, '\0', sizeof (re_node_set))
-static reg_errcode_t re_node_set_init_copy (re_node_set *dest,
-                                           const re_node_set *src) 
internal_function;
-static reg_errcode_t re_node_set_add_intersect (re_node_set *dest,
-                                               const re_node_set *src1,
-                                               const re_node_set *src2) 
internal_function;
-static reg_errcode_t re_node_set_init_union (re_node_set *dest,
-                                            const re_node_set *src1,
-                                            const re_node_set *src2) 
internal_function;
-static reg_errcode_t re_node_set_merge (re_node_set *dest,
-                                       const re_node_set *src) 
internal_function;
-static int re_node_set_insert (re_node_set *set, int elem) internal_function;
-static int re_node_set_insert_last (re_node_set *set,
-                                   int elem) internal_function;
-static int re_node_set_compare (const re_node_set *set1,
-                               const re_node_set *set2)
-     internal_function __attribute ((pure));
-static int re_node_set_contains (const re_node_set *set, int elem)
-     internal_function __attribute ((pure));
-static void re_node_set_remove_at (re_node_set *set, int idx) 
internal_function;
 #define re_node_set_remove(set,id) \
   (re_node_set_remove_at (set, re_node_set_contains (set, id) - 1))
 #define re_node_set_empty(p) ((p)->nelem = 0)
 #define re_node_set_free(set) re_free ((set)->elems)
-static int re_dfa_add_node (re_dfa_t *dfa, re_token_t token) internal_function;
-static re_dfastate_t *re_acquire_state (reg_errcode_t *err, re_dfa_t *dfa,
-                                       const re_node_set *nodes) 
internal_function;
-static re_dfastate_t *re_acquire_state_context (reg_errcode_t *err,
-                                               re_dfa_t *dfa,
-                                               const re_node_set *nodes,
-                                               unsigned int context) 
internal_function;
+
 static void free_state (re_dfastate_t *state) internal_function;
-#endif
 
 
 typedef enum
@@ -720,43 +785,79 @@ typedef struct
 
 
 /* Inline functions for bitset operation.  */
+
 static inline void
-bitset_not (bitset set)
+bitset_set (bitset set, Idx i)
 {
-  int bitset_i;
-  for (bitset_i = 0; bitset_i < BITSET_UINTS; ++bitset_i)
-    set[bitset_i] = ~set[bitset_i];
+  set[i / BITSET_WORD_BITS] |= (bitset_word) 1 << i % BITSET_WORD_BITS;
 }
 
 static inline void
-bitset_merge (bitset dest, const bitset src)
+bitset_clear (bitset set, Idx i)
+{
+  set[i / BITSET_WORD_BITS] &= ~ ((bitset_word) 1 << i % BITSET_WORD_BITS);
+}
+
+static inline bool
+bitset_contain (const bitset set, Idx i)
 {
-  int bitset_i;
-  for (bitset_i = 0; bitset_i < BITSET_UINTS; ++bitset_i)
-    dest[bitset_i] |= src[bitset_i];
+  return (set[i / BITSET_WORD_BITS] >> i % BITSET_WORD_BITS) & 1;
 }
 
 static inline void
-bitset_not_merge (bitset dest, const bitset src)
+bitset_empty (bitset set)
+{
+  memset (set, 0, sizeof (bitset));
+}
+
+static inline void
+bitset_set_all (bitset set)
+{
+  memset (set, -1, sizeof (bitset_word) * (SBC_MAX / BITSET_WORD_BITS));
+  if (SBC_MAX % BITSET_WORD_BITS != 0)
+    set[BITSET_WORDS - 1] =
+      ((bitset_word) 1 << SBC_MAX % BITSET_WORD_BITS) - 1;
+}
+
+static inline void
+bitset_copy (bitset dest, const bitset src)
+{
+  memcpy (dest, src, sizeof (bitset));
+}
+
+static inline void
+bitset_not (bitset set)
+{
+  int i;
+  for (i = 0; i < SBC_MAX / BITSET_WORD_BITS; ++i)
+    set[i] = ~set[i];
+  if (SBC_MAX % BITSET_WORD_BITS != 0)
+    set[BITSET_WORDS - 1] =
+      ((((bitset_word) 1 << SBC_MAX % BITSET_WORD_BITS) - 1)
+       & ~set[BITSET_WORDS - 1]);
+}
+
+static inline void
+bitset_merge (bitset dest, const bitset src)
 {
   int i;
-  for (i = 0; i < BITSET_UINTS; ++i)
-    dest[i] |= ~src[i];
+  for (i = 0; i < BITSET_WORDS; ++i)
+    dest[i] |= src[i];
 }
 
 static inline void
 bitset_mask (bitset dest, const bitset src)
 {
-  int bitset_i;
-  for (bitset_i = 0; bitset_i < BITSET_UINTS; ++bitset_i)
-    dest[bitset_i] &= src[bitset_i];
+  int i;
+  for (i = 0; i < BITSET_WORDS; ++i)
+    dest[i] &= src[i];
 }
 
-#if defined RE_ENABLE_I18N && !defined RE_NO_INTERNAL_PROTOTYPES
+#if defined RE_ENABLE_I18N
 /* Inline functions for re_string.  */
 static inline int
-internal_function
-re_string_char_size_at (const re_string_t *pstr, int idx)
+internal_function __attribute ((pure))
+re_string_char_size_at (const re_string_t *pstr, Idx idx)
 {
   int byte_idx;
   if (pstr->mb_cur_max == 1)
@@ -768,8 +869,8 @@ re_string_char_size_at (const re_string_
 }
 
 static inline wint_t
-internal_function
-re_string_wchar_at (const re_string_t *pstr, int idx)
+internal_function __attribute ((pure))
+re_string_wchar_at (const re_string_t *pstr, Idx idx)
 {
   if (pstr->mb_cur_max == 1)
     return (wint_t) pstr->mbs[idx];
@@ -777,8 +878,8 @@ re_string_wchar_at (const re_string_t *p
 }
 
 static int
-internal_function
-re_string_elem_size_at (const re_string_t *pstr, int idx)
+internal_function __attribute ((pure))
+re_string_elem_size_at (const re_string_t *pstr, Idx idx)
 {
 #ifdef _LIBC
   const unsigned char *p, *extra;
Index: lib/regexec.c
===================================================================
RCS file: /fetish/cu/lib/regexec.c,v
retrieving revision 1.3
diff -p -u -r1.3 regexec.c
--- lib/regexec.c       23 Aug 2005 19:41:09 -0000      1.3
+++ lib/regexec.c       9 Sep 2005 20:58:17 -0000
@@ -18,98 +18,101 @@
    Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
 
 static reg_errcode_t match_ctx_init (re_match_context_t *cache, int eflags,
-                                    int n) internal_function;
+                                    Idx n) internal_function;
 static void match_ctx_clean (re_match_context_t *mctx) internal_function;
 static void match_ctx_free (re_match_context_t *cache) internal_function;
-static reg_errcode_t match_ctx_add_entry (re_match_context_t *cache, int node,
-                                         int str_idx, int from, int to)
+static reg_errcode_t match_ctx_add_entry (re_match_context_t *cache, Idx node,
+                                         Idx str_idx, Idx from, Idx to)
      internal_function;
-static int search_cur_bkref_entry (re_match_context_t *mctx, int str_idx)
+static Idx search_cur_bkref_entry (const re_match_context_t *mctx, Idx str_idx)
      internal_function;
-static reg_errcode_t match_ctx_add_subtop (re_match_context_t *mctx, int node,
-                                          int str_idx) internal_function;
+static reg_errcode_t match_ctx_add_subtop (re_match_context_t *mctx, Idx node,
+                                          Idx str_idx) internal_function;
 static re_sub_match_last_t * match_ctx_add_sublast (re_sub_match_top_t *subtop,
-                                                  int node, int str_idx)
+                                                   Idx node, Idx str_idx)
      internal_function;
 static void sift_ctx_init (re_sift_context_t *sctx, re_dfastate_t **sifted_sts,
-                          re_dfastate_t **limited_sts, int last_node,
-                          int last_str_idx)
+                          re_dfastate_t **limited_sts, Idx last_node,
+                          Idx last_str_idx)
      internal_function;
 static reg_errcode_t re_search_internal (const regex_t *preg,
-                                        const char *string, int length,
-                                        int start, int range, int stop,
+                                        const char *string, Idx length,
+                                        Idx start, Idx last_start, Idx stop,
                                         size_t nmatch, regmatch_t pmatch[],
                                         int eflags) internal_function;
-static int re_search_2_stub (struct re_pattern_buffer *bufp,
-                            const char *string1, int length1,
-                            const char *string2, int length2,
-                            int start, int range, struct re_registers *regs,
-                            int stop, int ret_len) internal_function;
-static int re_search_stub (struct re_pattern_buffer *bufp,
-                          const char *string, int length, int start,
-                          int range, int stop, struct re_registers *regs,
-                          int ret_len) internal_function;
+static regoff_t re_search_2_stub (struct re_pattern_buffer *bufp,
+                                 const char *string1, Idx length1,
+                                 const char *string2, Idx length2,
+                                 Idx start, regoff_t range,
+                                 struct re_registers *regs,
+                                 Idx stop, bool ret_len) internal_function;
+static regoff_t re_search_stub (struct re_pattern_buffer *bufp,
+                               const char *string, Idx length, Idx start,
+                               regoff_t range, Idx stop,
+                               struct re_registers *regs,
+                               bool ret_len) internal_function;
 static unsigned re_copy_regs (struct re_registers *regs, regmatch_t *pmatch,
-                             int nregs, int regs_allocated) internal_function;
+                             Idx nregs, int regs_allocated) internal_function;
 static reg_errcode_t prune_impossible_nodes (re_match_context_t *mctx)
      internal_function;
-static int check_matching (re_match_context_t *mctx, int fl_longest_match,
-                          int *p_match_first)
+static Idx check_matching (re_match_context_t *mctx, bool fl_longest_match,
+                          Idx *p_match_first)
      internal_function;
-static int check_halt_state_context (const re_match_context_t *mctx,
-                                    const re_dfastate_t *state, int idx)
+static Idx check_halt_state_context (const re_match_context_t *mctx,
+                                    const re_dfastate_t *state, Idx idx)
      internal_function;
 static void update_regs (re_dfa_t *dfa, regmatch_t *pmatch,
-                        regmatch_t *prev_idx_match, int cur_node,
-                        int cur_idx, int nmatch) internal_function;
+                        regmatch_t *prev_idx_match, Idx cur_node,
+                        Idx cur_idx, Idx nmatch) internal_function;
 static reg_errcode_t push_fail_stack (struct re_fail_stack_t *fs,
-                                     int str_idx, int dest_node, int nregs,
+                                     Idx str_idx, Idx dest_node, Idx nregs,
                                      regmatch_t *regs,
                                      re_node_set *eps_via_nodes) 
internal_function;
 static reg_errcode_t set_regs (const regex_t *preg,
                               const re_match_context_t *mctx,
                               size_t nmatch, regmatch_t *pmatch,
-                              int fl_backtrack) internal_function;
+                              bool fl_backtrack) internal_function;
 static reg_errcode_t free_fail_stack_return (struct re_fail_stack_t *fs) 
internal_function;
 
 #ifdef RE_ENABLE_I18N
 static int sift_states_iter_mb (const re_match_context_t *mctx,
                                re_sift_context_t *sctx,
-                               int node_idx, int str_idx, int max_str_idx) 
internal_function;
+                               Idx node_idx, Idx str_idx, Idx max_str_idx) 
internal_function;
 #endif /* RE_ENABLE_I18N */
 static reg_errcode_t sift_states_backward (re_match_context_t *mctx,
                                           re_sift_context_t *sctx) 
internal_function;
 static reg_errcode_t build_sifted_states (re_match_context_t *mctx,
-                                         re_sift_context_t *sctx, int str_idx,
+                                         re_sift_context_t *sctx, Idx str_idx,
                                          re_node_set *cur_dest) 
internal_function;
 static reg_errcode_t update_cur_sifted_state (re_match_context_t *mctx,
                                              re_sift_context_t *sctx,
-                                             int str_idx,
+                                             Idx str_idx,
                                              re_node_set *dest_nodes) 
internal_function;
 static reg_errcode_t add_epsilon_src_nodes (re_dfa_t *dfa,
                                            re_node_set *dest_nodes,
                                            const re_node_set *candidates) 
internal_function;
-static int check_dst_limits (re_match_context_t *mctx, re_node_set *limits,
-                            int dst_node, int dst_idx, int src_node,
-                            int src_idx) internal_function;
-static int check_dst_limits_calc_pos_1 (re_match_context_t *mctx,
-                                       int boundaries, int subexp_idx,
-                                       int from_node, int bkref_idx) 
internal_function;
-static int check_dst_limits_calc_pos (re_match_context_t *mctx,
-                                     int limit, int subexp_idx,
-                                     int node, int str_idx,
-                                     int bkref_idx) internal_function;
+static bool check_dst_limits (const re_match_context_t *mctx,
+                             const re_node_set *limits,
+                             Idx dst_node, Idx dst_idx, Idx src_node,
+                             Idx src_idx) internal_function;
+static int check_dst_limits_calc_pos_1 (const re_match_context_t *mctx,
+                                       int boundaries, Idx subexp_idx,
+                                       Idx from_node, Idx bkref_idx) 
internal_function;
+static int check_dst_limits_calc_pos (const re_match_context_t *mctx,
+                                     Idx limit, Idx subexp_idx,
+                                     Idx node, Idx str_idx,
+                                     Idx bkref_idx) internal_function;
 static reg_errcode_t check_subexp_limits (re_dfa_t *dfa,
                                          re_node_set *dest_nodes,
                                          const re_node_set *candidates,
                                          re_node_set *limits,
                                          struct re_backref_cache_entry 
*bkref_ents,
-                                         int str_idx) internal_function;
+                                         Idx str_idx) internal_function;
 static reg_errcode_t sift_states_bkref (re_match_context_t *mctx,
                                        re_sift_context_t *sctx,
-                                       int str_idx, const re_node_set 
*candidates) internal_function;
+                                       Idx str_idx, const re_node_set 
*candidates) internal_function;
 static reg_errcode_t merge_state_array (re_dfa_t *dfa, re_dfastate_t **dst,
-                                       re_dfastate_t **src, int num) 
internal_function;
+                                       re_dfastate_t **src, Idx num) 
internal_function;
 static re_dfastate_t *find_recover_state (reg_errcode_t *err,
                                         re_match_context_t *mctx) 
internal_function;
 static re_dfastate_t *transit_state (reg_errcode_t *err,
@@ -120,7 +123,7 @@ static re_dfastate_t *merge_state_with_l
                                            re_dfastate_t *next_state) 
internal_function;
 static reg_errcode_t check_subexp_matching_top (re_match_context_t *mctx,
                                                re_node_set *cur_nodes,
-                                               int str_idx) internal_function;
+                                               Idx str_idx) internal_function;
 #if 0
 static re_dfastate_t *transit_state_sb (reg_errcode_t *err,
                                        re_match_context_t *mctx,
@@ -133,47 +136,48 @@ static reg_errcode_t transit_state_mb (r
 static reg_errcode_t transit_state_bkref (re_match_context_t *mctx,
                                          const re_node_set *nodes) 
internal_function;
 static reg_errcode_t get_subexp (re_match_context_t *mctx,
-                                int bkref_node, int bkref_str_idx) 
internal_function;
+                                Idx bkref_node, Idx bkref_str_idx) 
internal_function;
 static reg_errcode_t get_subexp_sub (re_match_context_t *mctx,
                                     const re_sub_match_top_t *sub_top,
                                     re_sub_match_last_t *sub_last,
-                                    int bkref_node, int bkref_str) 
internal_function;
-static int find_subexp_node (const re_dfa_t *dfa, const re_node_set *nodes,
-                            int subexp_idx, int type) internal_function;
+                                    Idx bkref_node, Idx bkref_str) 
internal_function;
+static Idx find_subexp_node (const re_dfa_t *dfa, const re_node_set *nodes,
+                            Idx subexp_idx, int type) internal_function;
 static reg_errcode_t check_arrival (re_match_context_t *mctx,
-                                   state_array_t *path, int top_node,
-                                   int top_str, int last_node, int last_str,
+                                   state_array_t *path, Idx top_node,
+                                   Idx top_str, Idx last_node, Idx last_str,
                                    int type) internal_function;
 static reg_errcode_t check_arrival_add_next_nodes (re_match_context_t *mctx,
-                                                  int str_idx,
+                                                  Idx str_idx,
                                                   re_node_set *cur_nodes,
                                                   re_node_set *next_nodes) 
internal_function;
 static reg_errcode_t check_arrival_expand_ecl (re_dfa_t *dfa,
                                               re_node_set *cur_nodes,
-                                              int ex_subexp, int type) 
internal_function;
+                                              Idx ex_subexp, int type) 
internal_function;
 static reg_errcode_t check_arrival_expand_ecl_sub (re_dfa_t *dfa,
                                                   re_node_set *dst_nodes,
-                                                  int target, int ex_subexp,
+                                                  Idx target, Idx ex_subexp,
                                                   int type) internal_function;
 static reg_errcode_t expand_bkref_cache (re_match_context_t *mctx,
-                                        re_node_set *cur_nodes, int cur_str,
-                                        int subexp_num, int type) 
internal_function;
-static int build_trtable (re_dfa_t *dfa,
-                         re_dfastate_t *state) internal_function;
+                                        re_node_set *cur_nodes, Idx cur_str,
+                                        Idx subexp_num, int type) 
internal_function;
+static bool build_trtable (re_dfa_t *dfa,
+                          re_dfastate_t *state) internal_function;
 #ifdef RE_ENABLE_I18N
-static int check_node_accept_bytes (re_dfa_t *dfa, int node_idx,
-                                   const re_string_t *input, int idx) 
internal_function;
+static int check_node_accept_bytes (re_dfa_t *dfa, Idx node_idx,
+                                   const re_string_t *input, Idx idx) 
internal_function;
 # ifdef _LIBC
 static unsigned int find_collation_sequence_value (const unsigned char *mbs,
                                                   size_t name_len) 
internal_function;
 # endif /* _LIBC */
 #endif /* RE_ENABLE_I18N */
-static int group_nodes_into_DFAstates (re_dfa_t *dfa,
+static Idx group_nodes_into_DFAstates (const re_dfa_t *dfa,
                                       const re_dfastate_t *state,
                                       re_node_set *states_node,
                                       bitset *states_ch) internal_function;
-static int check_node_accept (const re_match_context_t *mctx,
-                             const re_token_t *node, int idx) 
internal_function;
+static bool check_node_accept (const re_match_context_t *mctx,
+                              const re_token_t *node, Idx idx)
+     internal_function;
 static reg_errcode_t extend_buffers (re_match_context_t *mctx) 
internal_function;
 
 /* Entry point for POSIX code.  */
@@ -197,9 +201,9 @@ regexec (const regex_t *__restrict preg,
         size_t nmatch, regmatch_t pmatch[], int eflags)
 {
   reg_errcode_t err;
-  int start, length;
+  Idx start, length;
 #ifdef _LIBC
-  re_dfa_t *dfa = (re_dfa_t *)preg->buffer;
+  re_dfa_t *dfa = (re_dfa_t *) preg->re_buffer;
 #endif
 
   if (eflags & ~(REG_NOTBOL | REG_NOTEOL | REG_STARTEND))
@@ -217,11 +221,11 @@ regexec (const regex_t *__restrict preg,
     }
 
   __libc_lock_lock (dfa->lock);
-  if (preg->no_sub)
-    err = re_search_internal (preg, string, length, start, length - start,
+  if (preg->re_no_sub)
+    err = re_search_internal (preg, string, length, start, length,
                              length, 0, NULL, eflags);
   else
-    err = re_search_internal (preg, string, length, start, length - start,
+    err = re_search_internal (preg, string, length, start, length,
                              length, nmatch, pmatch, eflags);
   __libc_lock_unlock (dfa->lock);
   return err != REG_NOERROR;
@@ -267,7 +271,7 @@ compat_symbol (libc, __compat_regexec, r
    the first STOP characters of the concatenation of the strings should be
    concerned.
 
-   If REGS is not NULL, and BUFP->no_sub is not set, the offsets of the match
+   If REGS is not NULL, and BUFP->re_no_sub is not set, the offsets of the 
match
    and all groups is stroed in REGS.  (For the "_2" variants, the offsets are
    computed relative to the concatenation, not relative to the individual
    strings.)
@@ -276,80 +280,80 @@ compat_symbol (libc, __compat_regexec, r
    return the position of the start of the match.  Return value -1 means no
    match was found and -2 indicates an internal error.  */
 
-int
+regoff_t
 re_match (struct re_pattern_buffer *bufp, const char *string,
-         int length, int start, struct re_registers *regs)
+         Idx length, Idx start, struct re_registers *regs)
 {
-  return re_search_stub (bufp, string, length, start, 0, length, regs, 1);
+  return re_search_stub (bufp, string, length, start, 0, length, regs, true);
 }
 #ifdef _LIBC
 weak_alias (__re_match, re_match)
 #endif
 
-int
+regoff_t
 re_search (struct re_pattern_buffer *bufp, const char *string,
-          int length, int start, int range, struct re_registers *regs)
+          Idx length, Idx start, regoff_t range, struct re_registers *regs)
 {
-  return re_search_stub (bufp, string, length, start, range, length, regs, 0);
+  return re_search_stub (bufp, string, length, start, range, length, regs,
+                        false);
 }
 #ifdef _LIBC
 weak_alias (__re_search, re_search)
 #endif
 
-int
+regoff_t
 re_match_2 (struct re_pattern_buffer *bufp,
-           const char *string1, int length1,
-           const char *string2, int length2,
-           int start, struct re_registers *regs, int stop)
+           const char *string1, Idx length1,
+           const char *string2, Idx length2,
+           Idx start, struct re_registers *regs, Idx stop)
 {
   return re_search_2_stub (bufp, string1, length1, string2, length2,
-                          start, 0, regs, stop, 1);
+                          start, 0, regs, stop, true);
 }
 #ifdef _LIBC
 weak_alias (__re_match_2, re_match_2)
 #endif
 
-int
+regoff_t
 re_search_2 (struct re_pattern_buffer *bufp,
-            const char *string1, int length1,
-            const char *string2, int length2,
-            int start, int range, struct re_registers *regs, int stop)
+            const char *string1, Idx length1,
+            const char *string2, Idx length2,
+            Idx start, regoff_t range, struct re_registers *regs, Idx stop)
 {
   return re_search_2_stub (bufp, string1, length1, string2, length2,
-                          start, range, regs, stop, 0);
+                          start, range, regs, stop, false);
 }
 #ifdef _LIBC
 weak_alias (__re_search_2, re_search_2)
 #endif
 
-static int
+static regoff_t
 internal_function
 re_search_2_stub (struct re_pattern_buffer *bufp,
-                 const char *string1, int length1,
-                 const char *string2, int length2,
-                 int start, int range, struct re_registers *regs, int stop,
-                 int ret_len)
+                 const char *string1, Idx length1,
+                 const char *string2, Idx length2,
+                 Idx start, regoff_t range, struct re_registers *regs,
+                 Idx stop, bool ret_len)
 {
   const char *str;
-  int rval;
-  int len = length1 + length2;
-  int free_str = 0;
+  regoff_t rval;
+  Idx len = length1 + length2;
+  char *s = NULL;
 
-  if (BE (length1 < 0 || length2 < 0 || stop < 0, 0))
+  if (BE (length1 < 0 || length2 < 0 || stop < 0 || len < length1, 0))
     return -2;
 
   /* Concatenate the strings.  */
   if (length2 > 0)
     if (length1 > 0)
       {
-       char *s = re_malloc (char, len);
+       s = re_malloc (char, len);
 
        if (BE (s == NULL, 0))
          return -2;
        memcpy (s, string1, length1);
        memcpy (s + length1, string2, length2);
        str = s;
-       free_str = 1;
       }
     else
       str = string2;
@@ -358,58 +362,81 @@ re_search_2_stub (struct re_pattern_buff
 
   rval = re_search_stub (bufp, str, len, start, range, stop, regs,
                         ret_len);
-  if (free_str)
-    re_free ((char *) str);
+  re_free (s);
   return rval;
 }
 
 /* The parameters have the same meaning as those of re_search.
    Additional parameters:
-   If RET_LEN is nonzero the length of the match is returned (re_match style);
+   If RET_LEN is true the length of the match is returned (re_match style);
    otherwise the position of the match is returned.  */
 
-static int
+static regoff_t
 internal_function
 re_search_stub (struct re_pattern_buffer *bufp,
-               const char *string, int length,
-               int start, int range, int stop, struct re_registers *regs,
-               int ret_len)
+               const char *string, Idx length,
+               Idx start, regoff_t range, Idx stop, struct re_registers *regs,
+               bool ret_len)
 {
   reg_errcode_t result;
   regmatch_t *pmatch;
-  int nregs, rval;
+  Idx nregs;
+  regoff_t rval;
   int eflags = 0;
 #ifdef _LIBC
-  re_dfa_t *dfa = (re_dfa_t *)bufp->buffer;
+  re_dfa_t *dfa = (re_dfa_t *) bufp->re_buffer;
 #endif
+  Idx last_start = start + range;
 
   /* Check for out-of-range.  */
   if (BE (start < 0 || start > length, 0))
     return -1;
-  if (BE (start + range > length, 0))
-    range = length - start;
-  else if (BE (start + range < 0, 0))
-    range = -start;
+  if (sizeof start < sizeof range)
+    {
+      regoff_t length_offset = length;
+      regoff_t start_offset = start;
+      if (BE (length_offset - start_offset < range, 0))
+       last_start = length;
+      else if (BE (range < - start_offset, 0))
+       last_start = 0;
+    }
+  else
+    {
+      if (BE ((last_start < start) != (range < 0), 0))
+       {
+         /* Overflow occurred when computing last_start; substitute
+            the extreme value.  */
+         last_start = range < 0 ? 0 : length;
+       }
+      else
+       {
+         if (BE (length < last_start, 0))
+           last_start = length;
+         else if (BE (last_start < 0, 0))
+           last_start = 0;
+       }
+    }
 
   __libc_lock_lock (dfa->lock);
 
-  eflags |= (bufp->not_bol) ? REG_NOTBOL : 0;
-  eflags |= (bufp->not_eol) ? REG_NOTEOL : 0;
+  eflags |= (bufp->re_not_bol) ? REG_NOTBOL : 0;
+  eflags |= (bufp->re_not_eol) ? REG_NOTEOL : 0;
 
   /* Compile fastmap if we haven't yet.  */
-  if (range > 0 && bufp->fastmap != NULL && !bufp->fastmap_accurate)
+  if (start < last_start && bufp->re_fastmap != NULL
+      && !bufp->re_fastmap_accurate)
     re_compile_fastmap (bufp);
 
-  if (BE (bufp->no_sub, 0))
+  if (BE (bufp->re_no_sub, 0))
     regs = NULL;
 
   /* We need at least 1 register.  */
   if (regs == NULL)
     nregs = 1;
-  else if (BE (bufp->regs_allocated == REGS_FIXED &&
-              regs->num_regs < bufp->re_nsub + 1, 0))
+  else if (BE (bufp->re_regs_allocated == REG_FIXED
+              && regs->rm_num_regs <= bufp->re_nsub, 0))
     {
-      nregs = regs->num_regs;
+      nregs = regs->rm_num_regs;
       if (BE (nregs < 1, 0))
        {
          /* Nothing can be copied to regs.  */
@@ -419,14 +446,14 @@ re_search_stub (struct re_pattern_buffer
     }
   else
     nregs = bufp->re_nsub + 1;
-  pmatch = re_malloc (regmatch_t, nregs);
+  pmatch = re_xmalloc (regmatch_t, nregs);
   if (BE (pmatch == NULL, 0))
     {
       rval = -2;
       goto out;
     }
 
-  result = re_search_internal (bufp, string, length, start, range, stop,
+  result = re_search_internal (bufp, string, length, start, last_start, stop,
                               nregs, pmatch, eflags);
 
   rval = 0;
@@ -437,9 +464,9 @@ re_search_stub (struct re_pattern_buffer
   else if (regs != NULL)
     {
       /* If caller wants register contents data back, copy them.  */
-      bufp->regs_allocated = re_copy_regs (regs, pmatch, nregs,
-                                          bufp->regs_allocated);
-      if (BE (bufp->regs_allocated == REGS_UNALLOCATED, 0))
+      bufp->re_regs_allocated = re_copy_regs (regs, pmatch, nregs,
+                                             bufp->re_regs_allocated);
+      if (BE (bufp->re_regs_allocated == REG_UNALLOCATED, 0))
        rval = -2;
     }
 
@@ -461,55 +488,56 @@ re_search_stub (struct re_pattern_buffer
 
 static unsigned
 internal_function
-re_copy_regs (struct re_registers *regs, regmatch_t *pmatch, int nregs,
+re_copy_regs (struct re_registers *regs, regmatch_t *pmatch, Idx nregs,
              int regs_allocated)
 {
-  int rval = REGS_REALLOCATE;
-  int i;
-  int need_regs = nregs + 1;
-  /* We need one extra element beyond `num_regs' for the `-1' marker GNU code
+  int rval = REG_REALLOCATE;
+  Idx i;
+  Idx need_regs = nregs + 1;
+  /* We need one extra element beyond `rm_num_regs' for the `-1' marker GNU 
code
      uses.  */
 
   /* Have the register data arrays been allocated?  */
-  if (regs_allocated == REGS_UNALLOCATED)
+  if (regs_allocated == REG_UNALLOCATED)
     { /* No.  So allocate them with malloc.  */
-      regs->start = re_malloc (regoff_t, need_regs);
-      regs->end = re_malloc (regoff_t, need_regs);
-      if (BE (regs->start == NULL, 0) || BE (regs->end == NULL, 0))
-       return REGS_UNALLOCATED;
-      regs->num_regs = need_regs;
+      regs->rm_start = re_xmalloc (regoff_t, need_regs);
+      regs->rm_end = re_malloc (regoff_t, need_regs);
+      if (BE (regs->rm_start == NULL, 0) || BE (regs->rm_end == NULL, 0))
+       return REG_UNALLOCATED;
+      regs->rm_num_regs = need_regs;
     }
-  else if (regs_allocated == REGS_REALLOCATE)
+  else if (regs_allocated == REG_REALLOCATE)
     { /* Yes.  If we need more elements than were already
         allocated, reallocate them.  If we need fewer, just
         leave it alone.  */
-      if (BE (need_regs > regs->num_regs, 0))
+      if (BE (need_regs > regs->rm_num_regs, 0))
        {
-         regoff_t *new_start = re_realloc (regs->start, regoff_t, need_regs);
-         regoff_t *new_end = re_realloc (regs->end, regoff_t, need_regs);
+         regoff_t *new_start =
+           re_xrealloc (regs->rm_start, regoff_t, need_regs);
+         regoff_t *new_end = re_realloc (regs->rm_end, regoff_t, need_regs);
          if (BE (new_start == NULL, 0) || BE (new_end == NULL, 0))
-           return REGS_UNALLOCATED;
-         regs->start = new_start;
-         regs->end = new_end;
-         regs->num_regs = need_regs;
+           return REG_UNALLOCATED;
+         regs->rm_start = new_start;
+         regs->rm_end = new_end;
+         regs->rm_num_regs = need_regs;
        }
     }
   else
     {
-      assert (regs_allocated == REGS_FIXED);
-      /* This function may not be called with REGS_FIXED and nregs too big.  */
-      assert (regs->num_regs >= nregs);
-      rval = REGS_FIXED;
+      assert (regs_allocated == REG_FIXED);
+      /* This function may not be called with REG_FIXED and nregs too big.  */
+      assert (regs->rm_num_regs >= nregs);
+      rval = REG_FIXED;
     }
 
   /* Copy the regs.  */
   for (i = 0; i < nregs; ++i)
     {
-      regs->start[i] = pmatch[i].rm_so;
-      regs->end[i] = pmatch[i].rm_eo;
+      regs->rm_start[i] = pmatch[i].rm_so;
+      regs->rm_end[i] = pmatch[i].rm_eo;
     }
-  for ( ; i < regs->num_regs; ++i)
-    regs->start[i] = regs->end[i] = -1;
+  for ( ; i < regs->rm_num_regs; ++i)
+    regs->rm_start[i] = regs->rm_end[i] = -1;
 
   return rval;
 }
@@ -529,20 +557,20 @@ re_copy_regs (struct re_registers *regs,
 
 void
 re_set_registers (struct re_pattern_buffer *bufp, struct re_registers *regs,
-                 unsigned int num_regs, regoff_t *starts, regoff_t *ends)
+                 __re_size_t num_regs, regoff_t *starts, regoff_t *ends)
 {
   if (num_regs)
     {
-      bufp->regs_allocated = REGS_REALLOCATE;
-      regs->num_regs = num_regs;
-      regs->start = starts;
-      regs->end = ends;
+      bufp->re_regs_allocated = REG_REALLOCATE;
+      regs->rm_num_regs = num_regs;
+      regs->rm_start = starts;
+      regs->rm_end = ends;
     }
   else
     {
-      bufp->regs_allocated = REGS_UNALLOCATED;
-      regs->num_regs = 0;
-      regs->start = regs->end = (regoff_t *) 0;
+      bufp->re_regs_allocated = REG_UNALLOCATED;
+      regs->rm_num_regs = 0;
+      regs->rm_start = regs->rm_end = NULL;
     }
 }
 #ifdef _LIBC
@@ -557,8 +585,7 @@ int
 # ifdef _LIBC
 weak_function
 # endif
-re_exec (s)
-     const char *s;
+re_exec (const char *s)
 {
   return 0 == regexec (&re_comp_buf, s, 0, NULL, 0);
 }
@@ -568,35 +595,41 @@ re_exec (s)
 
 /* Searches for a compiled pattern PREG in the string STRING, whose
    length is LENGTH.  NMATCH, PMATCH, and EFLAGS have the same
-   mingings with regexec.  START, and RANGE have the same meanings
-   with re_search.
+   meaning as with regexec.  LAST_START is START + RANGE, where
+   START and RANGE have the same meaning as with re_search.
    Return REG_NOERROR if we find a match, and REG_NOMATCH if not,
    otherwise return the error code.
    Note: We assume front end functions already check ranges.
-   (START + RANGE >= 0 && START + RANGE <= LENGTH)  */
+   (0 <= LAST_START && LAST_START <= LENGTH)  */
 
 static reg_errcode_t
 internal_function
 re_search_internal (const regex_t *preg,
-                   const char *string, int length,
-                   int start, int range, int stop,
+                   const char *string, Idx length,
+                   Idx start, Idx last_start, Idx stop,
                    size_t nmatch, regmatch_t pmatch[],
                    int eflags)
 {
   reg_errcode_t err;
-  re_dfa_t *dfa = (re_dfa_t *)preg->buffer;
-  int left_lim, right_lim, incr;
-  int fl_longest_match, match_first, match_kind, match_last = -1;
-  int extra_nmatch;
-  int sb, ch;
+  re_dfa_t *dfa = (re_dfa_t *) preg->re_buffer;
+  Idx left_lim, right_lim;
+  int incr;
+  bool fl_longest_match;
+  int match_kind;
+  Idx match_first, match_last = REG_MISSING;
+  Idx extra_nmatch;
+  bool sb;
+  int ch;
 #if defined _LIBC || (defined __STDC_VERSION__ && __STDC_VERSION__ >= 199901L)
   re_match_context_t mctx = { .dfa = dfa };
 #else
   re_match_context_t mctx;
 #endif
-  char *fastmap = (preg->fastmap != NULL && preg->fastmap_accurate
-                  && range && !preg->can_be_null) ? preg->fastmap : NULL;
-  unsigned RE_TRANSLATE_TYPE t = (unsigned RE_TRANSLATE_TYPE) preg->translate;
+  char *fastmap = ((preg->re_fastmap != NULL && preg->re_fastmap_accurate
+                   && start != last_start && !preg->re_can_be_null)
+                  ? preg->re_fastmap : NULL);
+  unsigned REG_TRANSLATE_TYPE t =
+    (unsigned REG_TRANSLATE_TYPE) preg->re_translate;
 
 #if !(defined _LIBC || (defined __STDC_VERSION__ && __STDC_VERSION__ >= 
199901L))
   memset (&mctx, '\0', sizeof (re_match_context_t));
@@ -607,39 +640,40 @@ re_search_internal (const regex_t *preg,
   nmatch -= extra_nmatch;
 
   /* Check if the DFA haven't been compiled.  */
-  if (BE (preg->used == 0 || dfa->init_state == NULL
+  if (BE (preg->re_used == 0 || dfa->init_state == NULL
          || dfa->init_state_word == NULL || dfa->init_state_nl == NULL
          || dfa->init_state_begbuf == NULL, 0))
     return REG_NOMATCH;
 
 #ifdef DEBUG
   /* We assume front-end functions already check them.  */
-  assert (start + range >= 0 && start + range <= length);
+  assert (0 <= last_start && last_start <= length);
 #endif
 
   /* If initial states with non-begbuf contexts have no elements,
-     the regex must be anchored.  If preg->newline_anchor is set,
+     the regex must be anchored.  If preg->re_newline_anchor is set,
      we'll never use init_state_nl, so do not check it.  */
   if (dfa->init_state->nodes.nelem == 0
       && dfa->init_state_word->nodes.nelem == 0
       && (dfa->init_state_nl->nodes.nelem == 0
-         || !preg->newline_anchor))
+         || !preg->re_newline_anchor))
     {
-      if (start != 0 && start + range != 0)
+      if (start != 0 && last_start != 0)
         return REG_NOMATCH;
-      start = range = 0;
+      start = last_start = 0;
     }
 
   /* We must check the longest matching, if nmatch > 0.  */
   fl_longest_match = (nmatch != 0 || dfa->nbackref);
 
   err = re_string_allocate (&mctx.input, string, length, dfa->nodes_len + 1,
-                           preg->translate, preg->syntax & RE_ICASE, dfa);
+                           preg->re_translate,
+                           preg->re_syntax & REG_IGNORE_CASE, dfa);
   if (BE (err != REG_NOERROR, 0))
     goto free_return;
   mctx.input.stop = stop;
   mctx.input.raw_stop = stop;
-  mctx.input.newline_anchor = preg->newline_anchor;
+  mctx.input.newline_anchor = preg->re_newline_anchor;
 
   err = match_ctx_init (&mctx, eflags, dfa->nbackref * 2);
   if (BE (err != REG_NOERROR, 0))
@@ -651,7 +685,7 @@ re_search_internal (const regex_t *preg,
      multi character collating element.  */
   if (nmatch > 1 || dfa->has_mb_node)
     {
-      mctx.state_log = re_malloc (re_dfastate_t *, mctx.input.bufs_len + 1);
+      mctx.state_log = re_xmalloc (re_dfastate_t *, mctx.input.bufs_len + 1);
       if (BE (mctx.state_log == NULL, 0))
        {
          err = REG_ESPACE;
@@ -666,14 +700,14 @@ re_search_internal (const regex_t *preg,
                           : CONTEXT_NEWLINE | CONTEXT_BEGBUF;
 
   /* Check incrementally whether of not the input string match.  */
-  incr = (range < 0) ? -1 : 1;
-  left_lim = (range < 0) ? start + range : start;
-  right_lim = (range < 0) ? start : start + range;
+  incr = (last_start < start) ? -1 : 1;
+  left_lim = (last_start < start) ? last_start : start;
+  right_lim = (last_start < start) ? start : last_start;
   sb = dfa->mb_cur_max == 1;
   match_kind =
     (fastmap
-     ? ((sb || !(preg->syntax & RE_ICASE || t) ? 4 : 0)
-       | (range >= 0 ? 2 : 0)
+     ? ((sb || !(preg->re_syntax & REG_IGNORE_CASE || t) ? 4 : 0)
+       | (start <= last_start ? 2 : 0)
        | (t != NULL ? 1 : 0))
      : 8);
 
@@ -740,8 +774,8 @@ re_search_internal (const regex_t *preg,
            {
              /* If MATCH_FIRST is out of the valid range, reconstruct the
                 buffers.  */
-             unsigned int offset = match_first - mctx.input.raw_mbs_idx;
-             if (BE (offset >= (unsigned int) mctx.input.valid_raw_len, 0))
+             __re_size_t offset = match_first - mctx.input.raw_mbs_idx;
+             if (BE (offset >= (__re_size_t) mctx.input.valid_raw_len, 0))
                {
                  err = re_string_reconstruct (&mctx.input, match_first,
                                               eflags);
@@ -783,10 +817,10 @@ re_search_internal (const regex_t *preg,
       /* We assume that the matching starts from 0.  */
       mctx.state_log_top = mctx.nbkref_ents = mctx.max_mb_elem_len = 0;
       match_last = check_matching (&mctx, fl_longest_match,
-                                  range >= 0 ? &match_first : NULL);
-      if (match_last != -1)
+                                  start <= last_start ? &match_first : NULL);
+      if (match_last != REG_MISSING)
        {
-         if (BE (match_last == -2, 0))
+         if (BE (match_last == REG_ERROR, 0))
            {
              err = REG_ESPACE;
              goto free_return;
@@ -794,13 +828,13 @@ re_search_internal (const regex_t *preg,
          else
            {
              mctx.match_last = match_last;
-             if ((!preg->no_sub && nmatch > 1) || dfa->nbackref)
+             if ((!preg->re_no_sub && nmatch > 1) || dfa->nbackref)
                {
                  re_dfastate_t *pstate = mctx.state_log[match_last];
                  mctx.last_node = check_halt_state_context (&mctx, pstate,
                                                             match_last);
                }
-             if ((!preg->no_sub && nmatch > 1 && dfa->has_plural_match)
+             if ((!preg->re_no_sub && nmatch > 1 && dfa->has_plural_match)
                  || dfa->nbackref)
                {
                  err = prune_impossible_nodes (&mctx);
@@ -808,7 +842,7 @@ re_search_internal (const regex_t *preg,
                    break;
                  if (BE (err != REG_NOMATCH, 0))
                    goto free_return;
-                 match_last = -1;
+                 match_last = REG_MISSING;
                }
              else
                break; /* We found a match.  */
@@ -819,14 +853,14 @@ re_search_internal (const regex_t *preg,
     }
 
 #ifdef DEBUG
-  assert (match_last != -1);
+  assert (match_last != REG_MISSING);
   assert (err == REG_NOERROR);
 #endif
 
   /* Set pmatch[] if we need.  */
   if (nmatch > 0)
     {
-      int reg_idx;
+      Idx reg_idx;
 
       /* Initialize registers.  */
       for (reg_idx = 1; reg_idx < nmatch; ++reg_idx)
@@ -835,8 +869,11 @@ re_search_internal (const regex_t *preg,
       /* Set the points where matching start/end.  */
       pmatch[0].rm_so = 0;
       pmatch[0].rm_eo = mctx.match_last;
+      /* FIXME: This function should fail if mctx.match_last exceeds
+        the maximum possible regoff_t value.  We need a new error
+        code REG_OVERFLOW.  */
 
-      if (!preg->no_sub && nmatch > 1)
+      if (!preg->re_no_sub && nmatch > 1)
        {
          err = set_regs (preg, &mctx, nmatch, pmatch,
                          dfa->has_plural_match && dfa->nbackref > 0);
@@ -853,14 +890,14 @@ re_search_internal (const regex_t *preg,
 #ifdef RE_ENABLE_I18N
            if (BE (mctx.input.offsets_needed != 0, 0))
              {
-               if (pmatch[reg_idx].rm_so == mctx.input.valid_len)
-                 pmatch[reg_idx].rm_so += mctx.input.valid_raw_len - 
mctx.input.valid_len;
-               else
-                 pmatch[reg_idx].rm_so = 
mctx.input.offsets[pmatch[reg_idx].rm_so];
-               if (pmatch[reg_idx].rm_eo == mctx.input.valid_len)
-                 pmatch[reg_idx].rm_eo += mctx.input.valid_raw_len - 
mctx.input.valid_len;
-               else
-                 pmatch[reg_idx].rm_eo = 
mctx.input.offsets[pmatch[reg_idx].rm_eo];
+               pmatch[reg_idx].rm_so =
+                 (pmatch[reg_idx].rm_so == mctx.input.valid_len
+                  ? mctx.input.valid_raw_len
+                  : mctx.input.offsets[pmatch[reg_idx].rm_so]);
+               pmatch[reg_idx].rm_eo =
+                 (pmatch[reg_idx].rm_eo == mctx.input.valid_len
+                  ? mctx.input.valid_raw_len
+                  : mctx.input.offsets[pmatch[reg_idx].rm_eo]);
              }
 #else
            assert (mctx.input.offsets_needed == 0);
@@ -898,7 +935,7 @@ internal_function
 prune_impossible_nodes (re_match_context_t *mctx)
 {
   re_dfa_t *const dfa = mctx->dfa;
-  int halt_node, match_last;
+  Idx halt_node, match_last;
   reg_errcode_t ret;
   re_dfastate_t **sifted_states;
   re_dfastate_t **lim_states = NULL;
@@ -908,7 +945,7 @@ prune_impossible_nodes (re_match_context
 #endif
   match_last = mctx->match_last;
   halt_node = mctx->last_node;
-  sifted_states = re_malloc (re_dfastate_t *, match_last + 1);
+  sifted_states = re_xmalloc (re_dfastate_t *, match_last + 1);
   if (BE (sifted_states == NULL, 0))
     {
       ret = REG_ESPACE;
@@ -916,7 +953,7 @@ prune_impossible_nodes (re_match_context
     }
   if (dfa->nbackref)
     {
-      lim_states = re_malloc (re_dfastate_t *, match_last + 1);
+      lim_states = re_xmalloc (re_dfastate_t *, match_last + 1);
       if (BE (lim_states == NULL, 0))
        {
          ret = REG_ESPACE;
@@ -937,7 +974,7 @@ prune_impossible_nodes (re_match_context
          do
            {
              --match_last;
-             if (match_last < 0)
+             if (! REG_VALID_INDEX (match_last))
                {
                  ret = REG_NOMATCH;
                  goto free_return;
@@ -982,7 +1019,7 @@ prune_impossible_nodes (re_match_context
 static inline re_dfastate_t *
 __attribute ((always_inline)) internal_function
 acquire_init_state_context (reg_errcode_t *err, const re_match_context_t *mctx,
-                           int idx)
+                           Idx idx)
 {
   re_dfa_t *const dfa = mctx->dfa;
   if (dfa->init_state->has_constraint)
@@ -1013,27 +1050,27 @@ acquire_init_state_context (reg_errcode_
 }
 
 /* Check whether the regular expression match input string INPUT or not,
-   and return the index where the matching end, return -1 if not match,
-   or return -2 in case of an error.
+   and return the index where the matching end.  Return REG_MISSING if
+   there is no match, and return REG_ERROR in case of an error.
    FL_LONGEST_MATCH means we want the POSIX longest matching.
    If P_MATCH_FIRST is not NULL, and the match fails, it is set to the
    next place where we may want to try matching.
    Note that the matcher assume that the maching starts from the current
    index of the buffer.  */
 
-static int
+static Idx
 internal_function
-check_matching (re_match_context_t *mctx, int fl_longest_match,
-               int *p_match_first)
+check_matching (re_match_context_t *mctx, bool fl_longest_match,
+               Idx *p_match_first)
 {
   re_dfa_t *const dfa = mctx->dfa;
   reg_errcode_t err;
-  int match = 0;
-  int match_last = -1;
-  int cur_str_idx = re_string_cur_idx (&mctx->input);
+  Idx match = 0;
+  Idx match_last = REG_MISSING;
+  Idx cur_str_idx = re_string_cur_idx (&mctx->input);
   re_dfastate_t *cur_state;
-  int at_init_state = p_match_first != NULL;
-  int next_start_idx = cur_str_idx;
+  bool at_init_state = p_match_first != NULL;
+  Idx next_start_idx = cur_str_idx;
 
   err = REG_NOERROR;
   cur_state = acquire_init_state_context (&err, mctx, cur_str_idx);
@@ -1041,7 +1078,7 @@ check_matching (re_match_context_t *mctx
   if (BE (cur_state == NULL, 0))
     {
       assert (err == REG_ESPACE);
-      return -2;
+      return REG_ERROR;
     }
 
   if (mctx->state_log != NULL)
@@ -1052,7 +1089,7 @@ check_matching (re_match_context_t *mctx
         later.  E.g. Processing back references.  */
       if (BE (dfa->nbackref, 0))
        {
-         at_init_state = 0;
+         at_init_state = false;
          err = check_subexp_matching_top (mctx, &cur_state->nodes, 0);
          if (BE (err != REG_NOERROR, 0))
            return err;
@@ -1085,7 +1122,7 @@ check_matching (re_match_context_t *mctx
   while (!re_string_eoi (&mctx->input))
     {
       re_dfastate_t *old_state = cur_state;
-      int next_char_idx = re_string_cur_idx (&mctx->input) + 1;
+      Idx next_char_idx = re_string_cur_idx (&mctx->input) + 1;
 
       if (BE (next_char_idx >= mctx->input.bufs_len, 0)
           || (BE (next_char_idx >= mctx->input.valid_len, 0)
@@ -1095,7 +1132,7 @@ check_matching (re_match_context_t *mctx
           if (BE (err != REG_NOERROR, 0))
            {
              assert (err == REG_ESPACE);
-             return -2;
+             return REG_ERROR;
            }
         }
 
@@ -1109,7 +1146,7 @@ check_matching (re_match_context_t *mctx
             state using the state log, if available and if we have not
             already found a valid (even if not the longest) match.  */
          if (BE (err != REG_NOERROR, 0))
-           return -2;
+           return REG_ERROR;
 
          if (mctx->state_log == NULL
              || (match && !fl_longest_match)
@@ -1122,7 +1159,7 @@ check_matching (re_match_context_t *mctx
          if (old_state == cur_state)
            next_start_idx = next_char_idx;
          else
-           at_init_state = 0;
+           at_init_state = false;
        }
 
       if (cur_state->halt)
@@ -1153,31 +1190,31 @@ check_matching (re_match_context_t *mctx
 
 /* Check NODE match the current context.  */
 
-static int
+static bool
 internal_function
-check_halt_node_context (const re_dfa_t *dfa, int node, unsigned int context)
+check_halt_node_context (const re_dfa_t *dfa, Idx node, unsigned int context)
 {
   re_token_type_t type = dfa->nodes[node].type;
   unsigned int constraint = dfa->nodes[node].constraint;
   if (type != END_OF_RE)
-    return 0;
+    return false;
   if (!constraint)
-    return 1;
+    return true;
   if (NOT_SATISFY_NEXT_CONSTRAINT (constraint, context))
-    return 0;
-  return 1;
+    return false;
+  return true;
 }
 
 /* Check the halt state STATE match the current context.
    Return 0 if not match, if the node, STATE has, is a halt node and
    match the context, return the node.  */
 
-static int
+static Idx
 internal_function
 check_halt_state_context (const re_match_context_t *mctx,
-                         const re_dfastate_t *state, int idx)
+                         const re_dfastate_t *state, Idx idx)
 {
-  int i;
+  Idx i;
   unsigned int context;
 #ifdef DEBUG
   assert (state->halt);
@@ -1191,32 +1228,34 @@ check_halt_state_context (const re_match
 
 /* Compute the next node to which "NFA" transit from NODE("NFA" is a NFA
    corresponding to the DFA).
-   Return the destination node, and update EPS_VIA_NODES, return -1 in case
-   of errors.  */
+   Return the destination node, and update EPS_VIA_NODES;
+   return REG_MISSING in case of errors.  */
 
-static int
+static Idx
 internal_function
 proceed_next_node (const re_match_context_t *mctx,
-                  int nregs, regmatch_t *regs, int *pidx, int node,
+                  Idx nregs, regmatch_t *regs, Idx *pidx, Idx node,
                   re_node_set *eps_via_nodes, struct re_fail_stack_t *fs)
 {
   re_dfa_t *const dfa = mctx->dfa;
-  int i, err;
+  Idx i;
+  bool ok;
   if (IS_EPSILON_NODE (dfa->nodes[node].type))
     {
       re_node_set *cur_nodes = &mctx->state_log[*pidx]->nodes;
       re_node_set *edests = &dfa->edests[node];
-      int dest_node;
-      err = re_node_set_insert (eps_via_nodes, node);
-      if (BE (err < 0, 0))
-       return -2;
-      /* Pick up a valid destination, or return -1 if none is found.  */
-      for (dest_node = -1, i = 0; i < edests->nelem; ++i)
+      Idx dest_node;
+      ok = re_node_set_insert (eps_via_nodes, node);
+      if (BE (! ok, 0))
+       return REG_ERROR;
+      /* Pick up a valid destination, or return REG_MISSING if none
+        is found.  */
+      for (dest_node = REG_MISSING, i = 0; i < edests->nelem; ++i)
        {
-         int candidate = edests->elems[i];
+         Idx candidate = edests->elems[i];
          if (!re_node_set_contains (cur_nodes, candidate))
            continue;
-          if (dest_node == -1)
+          if (dest_node == REG_MISSING)
            dest_node = candidate;
 
           else
@@ -1230,7 +1269,7 @@ proceed_next_node (const re_match_contex
              else if (fs != NULL
                       && push_fail_stack (fs, *pidx, candidate, nregs, regs,
                                           eps_via_nodes))
-               return -2;
+               return REG_ERROR;
 
              /* We know we are going to exit.  */
              break;
@@ -1240,7 +1279,7 @@ proceed_next_node (const re_match_contex
     }
   else
     {
-      int naccepted = 0;
+      Idx naccepted = 0;
       re_token_type_t type = dfa->nodes[node].type;
 
 #ifdef RE_ENABLE_I18N
@@ -1250,27 +1289,27 @@ proceed_next_node (const re_match_contex
 #endif /* RE_ENABLE_I18N */
       if (type == OP_BACK_REF)
        {
-         int subexp_idx = dfa->nodes[node].opr.idx + 1;
+         Idx subexp_idx = dfa->nodes[node].opr.idx + 1;
          naccepted = regs[subexp_idx].rm_eo - regs[subexp_idx].rm_so;
          if (fs != NULL)
            {
              if (regs[subexp_idx].rm_so == -1 || regs[subexp_idx].rm_eo == -1)
-               return -1;
+               return REG_MISSING;
              else if (naccepted)
                {
                  char *buf = (char *) re_string_get_buffer (&mctx->input);
                  if (memcmp (buf + regs[subexp_idx].rm_so, buf + *pidx,
                              naccepted) != 0)
-                   return -1;
+                   return REG_MISSING;
                }
            }
 
          if (naccepted == 0)
            {
-             int dest_node;
-             err = re_node_set_insert (eps_via_nodes, node);
-             if (BE (err < 0, 0))
-               return -2;
+             Idx dest_node;
+             ok = re_node_set_insert (eps_via_nodes, node);
+             if (BE (! ok, 0))
+               return REG_ERROR;
              dest_node = dfa->edests[node].elems[0];
              if (re_node_set_contains (&mctx->state_log[*pidx]->nodes,
                                        dest_node))
@@ -1281,39 +1320,37 @@ proceed_next_node (const re_match_contex
       if (naccepted != 0
          || check_node_accept (mctx, dfa->nodes + node, *pidx))
        {
-         int dest_node = dfa->nexts[node];
+         Idx dest_node = dfa->nexts[node];
          *pidx = (naccepted == 0) ? *pidx + 1 : *pidx + naccepted;
          if (fs && (*pidx > mctx->match_last || mctx->state_log[*pidx] == NULL
                     || !re_node_set_contains (&mctx->state_log[*pidx]->nodes,
                                               dest_node)))
-           return -1;
+           return REG_MISSING;
          re_node_set_empty (eps_via_nodes);
          return dest_node;
        }
     }
-  return -1;
+  return REG_MISSING;
 }
 
 static reg_errcode_t
 internal_function
-push_fail_stack (struct re_fail_stack_t *fs, int str_idx, int dest_node,
-                int nregs, regmatch_t *regs, re_node_set *eps_via_nodes)
+push_fail_stack (struct re_fail_stack_t *fs, Idx str_idx, Idx dest_node,
+                Idx nregs, regmatch_t *regs, re_node_set *eps_via_nodes)
 {
   reg_errcode_t err;
-  int num = fs->num++;
+  Idx num = fs->num++;
   if (fs->num == fs->alloc)
     {
-      struct re_fail_stack_ent_t *new_array;
-      new_array = realloc (fs->stack, (sizeof (struct re_fail_stack_ent_t)
-                                      * fs->alloc * 2));
+      struct re_fail_stack_ent_t *new_array =
+       re_x2realloc (fs->stack, struct re_fail_stack_ent_t, &fs->alloc);
       if (new_array == NULL)
        return REG_ESPACE;
-      fs->alloc *= 2;
       fs->stack = new_array;
     }
   fs->stack[num].idx = str_idx;
   fs->stack[num].node = dest_node;
-  fs->stack[num].regs = re_malloc (regmatch_t, nregs);
+  fs->stack[num].regs = re_xmalloc (regmatch_t, nregs);
   if (fs->stack[num].regs == NULL)
     return REG_ESPACE;
   memcpy (fs->stack[num].regs, regs, sizeof (regmatch_t) * nregs);
@@ -1321,13 +1358,13 @@ push_fail_stack (struct re_fail_stack_t 
   return err;
 }
 
-static int
+static Idx
 internal_function
-pop_fail_stack (struct re_fail_stack_t *fs, int *pidx,
-               int nregs, regmatch_t *regs, re_node_set *eps_via_nodes)
+pop_fail_stack (struct re_fail_stack_t *fs, Idx *pidx,
+               Idx nregs, regmatch_t *regs, re_node_set *eps_via_nodes)
 {
-  int num = --fs->num;
-  assert (num >= 0);
+  Idx num = --fs->num;
+  assert (REG_VALID_INDEX (num));
   *pidx = fs->stack[num].idx;
   memcpy (regs, fs->stack[num].regs, sizeof (regmatch_t) * nregs);
   re_node_set_free (eps_via_nodes);
@@ -1344,14 +1381,15 @@ pop_fail_stack (struct re_fail_stack_t *
 static reg_errcode_t
 internal_function
 set_regs (const regex_t *preg, const re_match_context_t *mctx,
-         size_t nmatch, regmatch_t *pmatch, int fl_backtrack)
+         size_t nmatch, regmatch_t *pmatch, bool fl_backtrack)
 {
-  re_dfa_t *dfa = (re_dfa_t *) preg->buffer;
-  int idx, cur_node;
+  re_dfa_t *dfa = (re_dfa_t *) preg->re_buffer;
+  Idx idx, cur_node;
   re_node_set eps_via_nodes;
   struct re_fail_stack_t *fs;
   struct re_fail_stack_t fs_body = { 0, 2, NULL };
   regmatch_t *prev_idx_match;
+  bool prev_idx_match_malloced = false;
 
 #ifdef DEBUG
   assert (nmatch > 1);
@@ -1360,7 +1398,7 @@ set_regs (const regex_t *preg, const re_
   if (fl_backtrack)
     {
       fs = &fs_body;
-      fs->stack = re_malloc (struct re_fail_stack_ent_t, fs->alloc);
+      fs->stack = re_xmalloc (struct re_fail_stack_ent_t, fs->alloc);
       if (fs->stack == NULL)
        return REG_ESPACE;
     }
@@ -1370,7 +1408,23 @@ set_regs (const regex_t *preg, const re_
   cur_node = dfa->init_node;
   re_node_set_init_empty (&eps_via_nodes);
 
-  prev_idx_match = (regmatch_t *) alloca (sizeof (regmatch_t) * nmatch);
+  if (re_alloc_oversized (nmatch, sizeof (regmatch_t)))
+    {
+      free_fail_stack_return (fs);
+      return REG_ESPACE;
+    }
+  if (__libc_use_alloca (nmatch * sizeof (regmatch_t)))
+    prev_idx_match = (regmatch_t *) alloca (nmatch * sizeof (regmatch_t));
+  else
+    {
+      prev_idx_match = re_malloc (regmatch_t, nmatch);
+      if (prev_idx_match == NULL)
+       {
+         free_fail_stack_return (fs);
+         return REG_ESPACE;
+       }
+      prev_idx_match_malloced = true;
+    }
   memcpy (prev_idx_match, pmatch, sizeof (regmatch_t) * nmatch);
 
   for (idx = pmatch[0].rm_so; idx <= pmatch[0].rm_eo ;)
@@ -1379,7 +1433,7 @@ set_regs (const regex_t *preg, const re_
 
       if (idx == pmatch[0].rm_eo && cur_node == mctx->last_node)
        {
-         int reg_idx;
+         Idx reg_idx;
          if (fs)
            {
              for (reg_idx = 0; reg_idx < nmatch; ++reg_idx)
@@ -1388,6 +1442,8 @@ set_regs (const regex_t *preg, const re_
              if (reg_idx == nmatch)
                {
                  re_node_set_free (&eps_via_nodes);
+                 if (prev_idx_match_malloced)
+                   re_free (prev_idx_match);
                  return free_fail_stack_return (fs);
                }
              cur_node = pop_fail_stack (fs, &idx, nmatch, pmatch,
@@ -1396,6 +1452,8 @@ set_regs (const regex_t *preg, const re_
          else
            {
              re_node_set_free (&eps_via_nodes);
+             if (prev_idx_match_malloced)
+               re_free (prev_idx_match);
              return REG_NOERROR;
            }
        }
@@ -1404,11 +1462,13 @@ set_regs (const regex_t *preg, const re_
       cur_node = proceed_next_node (mctx, nmatch, pmatch, &idx, cur_node,
                                    &eps_via_nodes, fs);
 
-      if (BE (cur_node < 0, 0))
+      if (BE (! REG_VALID_INDEX (cur_node), 0))
        {
-         if (BE (cur_node == -2, 0))
+         if (BE (cur_node == REG_ERROR, 0))
            {
              re_node_set_free (&eps_via_nodes);
+             if (prev_idx_match_malloced)
+               re_free (prev_idx_match);
              free_fail_stack_return (fs);
              return REG_ESPACE;
            }
@@ -1418,11 +1478,15 @@ set_regs (const regex_t *preg, const re_
          else
            {
              re_node_set_free (&eps_via_nodes);
+             if (prev_idx_match_malloced)
+               re_free (prev_idx_match);
              return REG_NOMATCH;
            }
        }
     }
   re_node_set_free (&eps_via_nodes);
+  if (prev_idx_match_malloced)
+    re_free (prev_idx_match);
   return free_fail_stack_return (fs);
 }
 
@@ -1432,7 +1496,7 @@ free_fail_stack_return (struct re_fail_s
 {
   if (fs)
     {
-      int fs_idx;
+      Idx fs_idx;
       for (fs_idx = 0; fs_idx < fs->num; ++fs_idx)
        {
          re_node_set_free (&fs->stack[fs_idx].eps_via_nodes);
@@ -1446,12 +1510,12 @@ free_fail_stack_return (struct re_fail_s
 static void
 internal_function
 update_regs (re_dfa_t *dfa, regmatch_t *pmatch, regmatch_t *prev_idx_match,
-            int cur_node, int cur_idx, int nmatch)
+            Idx cur_node, Idx cur_idx, Idx nmatch)
 {
   int type = dfa->nodes[cur_node].type;
   if (type == OP_OPEN_SUBEXP)
     {
-      int reg_num = dfa->nodes[cur_node].opr.idx + 1;
+      Idx reg_num = dfa->nodes[cur_node].opr.idx + 1;
 
       /* We are at the first node of this sub expression.  */
       if (reg_num < nmatch)
@@ -1462,7 +1526,7 @@ update_regs (re_dfa_t *dfa, regmatch_t *
     }
   else if (type == OP_CLOSE_SUBEXP)
     {
-      int reg_num = dfa->nodes[cur_node].opr.idx + 1;
+      Idx reg_num = dfa->nodes[cur_node].opr.idx + 1;
       if (reg_num < nmatch)
        {
          /* We are at the last node of this sub expression.  */
@@ -1521,7 +1585,7 @@ sift_states_backward (re_match_context_t
 {
   reg_errcode_t err;
   int null_cnt = 0;
-  int str_idx = sctx->last_str_idx;
+  Idx str_idx = sctx->last_str_idx;
   re_node_set cur_dest;
 
 #ifdef DEBUG
@@ -1576,11 +1640,11 @@ sift_states_backward (re_match_context_t
 static reg_errcode_t
 internal_function
 build_sifted_states (re_match_context_t *mctx, re_sift_context_t *sctx,
-                    int str_idx, re_node_set *cur_dest)
+                    Idx str_idx, re_node_set *cur_dest)
 {
   re_dfa_t *const dfa = mctx->dfa;
   re_node_set *cur_src = &mctx->state_log[str_idx]->non_eps_nodes;
-  int i;
+  Idx i;
 
   /* Then build the next sifted state.
      We build the next sifted state on `cur_dest', and update
@@ -1591,9 +1655,9 @@ build_sifted_states (re_match_context_t 
      (with the epsilon nodes pre-filtered out).  */
   for (i = 0; i < cur_src->nelem; i++)
     {
-      int prev_node = cur_src->elems[i];
+      Idx prev_node = cur_src->elems[i];
       int naccepted = 0;
-      int ret;
+      bool ok;
 
 #ifdef DEBUG
       re_token_type_t type = dfa->nodes[prev_node].type;
@@ -1619,14 +1683,14 @@ build_sifted_states (re_match_context_t 
 
       if (sctx->limits.nelem)
        {
-         int to_idx = str_idx + naccepted;
+         Idx to_idx = str_idx + naccepted;
          if (check_dst_limits (mctx, &sctx->limits,
                                dfa->nexts[prev_node], to_idx,
                                prev_node, str_idx))
            continue;
        }
-      ret = re_node_set_insert (cur_dest, prev_node);
-      if (BE (ret == -1, 0))
+      ok = re_node_set_insert (cur_dest, prev_node);
+      if (BE (! ok, 0))
        return REG_ESPACE;
     }
 
@@ -1637,9 +1701,9 @@ build_sifted_states (re_match_context_t 
 
 static reg_errcode_t
 internal_function
-clean_state_log_if_needed (re_match_context_t *mctx, int next_state_log_idx)
+clean_state_log_if_needed (re_match_context_t *mctx, Idx next_state_log_idx)
 {
-  int top = mctx->state_log_top;
+  Idx top = mctx->state_log_top;
 
   if (next_state_log_idx >= mctx->input.bufs_len
       || (next_state_log_idx >= mctx->input.valid_len
@@ -1663,9 +1727,9 @@ clean_state_log_if_needed (re_match_cont
 static reg_errcode_t
 internal_function
 merge_state_array (re_dfa_t *dfa, re_dfastate_t **dst, re_dfastate_t **src,
-                  int num)
+                  Idx num)
 {
-  int st_idx;
+  Idx st_idx;
   reg_errcode_t err;
   for (st_idx = 0; st_idx < num; ++st_idx)
     {
@@ -1690,7 +1754,7 @@ merge_state_array (re_dfa_t *dfa, re_dfa
 static reg_errcode_t
 internal_function
 update_cur_sifted_state (re_match_context_t *mctx, re_sift_context_t *sctx,
-                        int str_idx, re_node_set *dest_nodes)
+                        Idx str_idx, re_node_set *dest_nodes)
 {
   re_dfa_t *const dfa = mctx->dfa;
   reg_errcode_t err;
@@ -1740,7 +1804,7 @@ add_epsilon_src_nodes (re_dfa_t *dfa, re
                       const re_node_set *candidates)
 {
   reg_errcode_t err = REG_NOERROR;
-  int i;
+  Idx i;
 
   re_dfastate_t *state = re_acquire_state (&err, dfa, dest_nodes);
   if (BE (err != REG_NOERROR, 0))
@@ -1761,27 +1825,27 @@ add_epsilon_src_nodes (re_dfa_t *dfa, re
 
 static reg_errcode_t
 internal_function
-sub_epsilon_src_nodes (re_dfa_t *dfa, int node, re_node_set *dest_nodes,
+sub_epsilon_src_nodes (re_dfa_t *dfa, Idx node, re_node_set *dest_nodes,
                       const re_node_set *candidates)
 {
-    int ecl_idx;
+    Idx ecl_idx;
     reg_errcode_t err;
     re_node_set *inv_eclosure = dfa->inveclosures + node;
     re_node_set except_nodes;
     re_node_set_init_empty (&except_nodes);
     for (ecl_idx = 0; ecl_idx < inv_eclosure->nelem; ++ecl_idx)
       {
-       int cur_node = inv_eclosure->elems[ecl_idx];
+       Idx cur_node = inv_eclosure->elems[ecl_idx];
        if (cur_node == node)
          continue;
        if (IS_EPSILON_NODE (dfa->nodes[cur_node].type))
          {
-           int edst1 = dfa->edests[cur_node].elems[0];
-           int edst2 = ((dfa->edests[cur_node].nelem > 1)
-                        ? dfa->edests[cur_node].elems[1] : -1);
+           Idx edst1 = dfa->edests[cur_node].elems[0];
+           Idx edst2 = ((dfa->edests[cur_node].nelem > 1)
+                        ? dfa->edests[cur_node].elems[1] : REG_MISSING);
            if ((!re_node_set_contains (inv_eclosure, edst1)
                 && re_node_set_contains (dest_nodes, edst1))
-               || (edst2 > 0
+               || (REG_VALID_NONZERO_INDEX (edst2)
                    && !re_node_set_contains (inv_eclosure, edst2)
                    && re_node_set_contains (dest_nodes, edst2)))
              {
@@ -1797,10 +1861,10 @@ sub_epsilon_src_nodes (re_dfa_t *dfa, in
       }
     for (ecl_idx = 0; ecl_idx < inv_eclosure->nelem; ++ecl_idx)
       {
-       int cur_node = inv_eclosure->elems[ecl_idx];
+       Idx cur_node = inv_eclosure->elems[ecl_idx];
        if (!re_node_set_contains (&except_nodes, cur_node))
          {
-           int idx = re_node_set_contains (dest_nodes, cur_node) - 1;
+           Idx idx = re_node_set_contains (dest_nodes, cur_node) - 1;
            re_node_set_remove_at (dest_nodes, idx);
          }
       }
@@ -1808,19 +1872,19 @@ sub_epsilon_src_nodes (re_dfa_t *dfa, in
     return REG_NOERROR;
 }
 
-static int
+static bool
 internal_function
-check_dst_limits (re_match_context_t *mctx, re_node_set *limits,
-                 int dst_node, int dst_idx, int src_node, int src_idx)
+check_dst_limits (const re_match_context_t *mctx, const re_node_set *limits,
+                 Idx dst_node, Idx dst_idx, Idx src_node, Idx src_idx)
 {
   re_dfa_t *const dfa = mctx->dfa;
-  int lim_idx, src_pos, dst_pos;
+  Idx lim_idx, src_pos, dst_pos;
 
-  int dst_bkref_idx = search_cur_bkref_entry (mctx, dst_idx);
-  int src_bkref_idx = search_cur_bkref_entry (mctx, src_idx);
+  Idx dst_bkref_idx = search_cur_bkref_entry (mctx, dst_idx);
+  Idx src_bkref_idx = search_cur_bkref_entry (mctx, src_idx);
   for (lim_idx = 0; lim_idx < limits->nelem; ++lim_idx)
     {
-      int subexp_idx;
+      Idx subexp_idx;
       struct re_backref_cache_entry *ent;
       ent = mctx->bkref_ents + limits->elems[lim_idx];
       subexp_idx = dfa->nodes[ent->node].opr.idx;
@@ -1839,40 +1903,42 @@ check_dst_limits (re_match_context_t *mc
       if (src_pos == dst_pos)
        continue; /* This is unrelated limitation.  */
       else
-       return 1;
+       return true;
     }
-  return 0;
+  return false;
 }
 
 static int
 internal_function
-check_dst_limits_calc_pos_1 (re_match_context_t *mctx, int boundaries,
-                            int subexp_idx, int from_node, int bkref_idx)
+check_dst_limits_calc_pos_1 (const re_match_context_t *mctx, int boundaries,
+                            Idx subexp_idx, Idx from_node, Idx bkref_idx)
 {
   re_dfa_t *const dfa = mctx->dfa;
   re_node_set *eclosures = dfa->eclosures + from_node;
-  int node_idx;
+  Idx node_idx;
 
   /* Else, we are on the boundary: examine the nodes on the epsilon
      closure.  */
   for (node_idx = 0; node_idx < eclosures->nelem; ++node_idx)
     {
-      int node = eclosures->elems[node_idx];
+      Idx node = eclosures->elems[node_idx];
       switch (dfa->nodes[node].type)
        {
        case OP_BACK_REF:
-         if (bkref_idx != -1)
+         if (bkref_idx != REG_MISSING)
            {
              struct re_backref_cache_entry *ent = mctx->bkref_ents + bkref_idx;
              do
                {
-                 int dst, cpos;
+                 Idx dst;
+                 int cpos;
 
                  if (ent->node != node)
                    continue;
 
-                 if (subexp_idx <= 8 * sizeof (ent->eps_reachable_subexps_map)
-                     && !(ent->eps_reachable_subexps_map & (1 << subexp_idx)))
+                 if (subexp_idx < BITSET_WORD_BITS
+                     && !(ent->eps_reachable_subexps_map
+                          & ((bitset_word) 1 << subexp_idx)))
                    continue;
 
                  /* Recurse trying to reach the OP_OPEN_SUBEXP and
@@ -1898,7 +1964,9 @@ check_dst_limits_calc_pos_1 (re_match_co
                  if (cpos == 0 && (boundaries & 2))
                    return 0;
 
-                 ent->eps_reachable_subexps_map &= ~(1 << subexp_idx);
+                 if (subexp_idx < BITSET_WORD_BITS)
+                   ent->eps_reachable_subexps_map &=
+                     ~ ((bitset_word) 1 << subexp_idx);
                }
              while (ent++->more);
            }
@@ -1924,8 +1992,9 @@ check_dst_limits_calc_pos_1 (re_match_co
 
 static int
 internal_function
-check_dst_limits_calc_pos (re_match_context_t *mctx, int limit, int subexp_idx,
-                          int from_node, int str_idx, int bkref_idx)
+check_dst_limits_calc_pos (const re_match_context_t *mctx,
+                          Idx limit, Idx subexp_idx,
+                          Idx from_node, Idx str_idx, Idx bkref_idx)
 {
   struct re_backref_cache_entry *lim = mctx->bkref_ents + limit;
   int boundaries;
@@ -1955,14 +2024,14 @@ static reg_errcode_t
 internal_function
 check_subexp_limits (re_dfa_t *dfa, re_node_set *dest_nodes,
                     const re_node_set *candidates, re_node_set *limits,
-                    struct re_backref_cache_entry *bkref_ents, int str_idx)
+                    struct re_backref_cache_entry *bkref_ents, Idx str_idx)
 {
   reg_errcode_t err;
-  int node_idx, lim_idx;
+  Idx node_idx, lim_idx;
 
   for (lim_idx = 0; lim_idx < limits->nelem; ++lim_idx)
     {
-      int subexp_idx;
+      Idx subexp_idx;
       struct re_backref_cache_entry *ent;
       ent = bkref_ents + limits->elems[lim_idx];
 
@@ -1972,11 +2041,11 @@ check_subexp_limits (re_dfa_t *dfa, re_n
       subexp_idx = dfa->nodes[ent->node].opr.idx;
       if (ent->subexp_to == str_idx)
        {
-         int ops_node = -1;
-         int cls_node = -1;
+         Idx ops_node = REG_MISSING;
+         Idx cls_node = REG_MISSING;
          for (node_idx = 0; node_idx < dest_nodes->nelem; ++node_idx)
            {
-             int node = dest_nodes->elems[node_idx];
+             Idx node = dest_nodes->elems[node_idx];
              re_token_type_t type = dfa->nodes[node].type;
              if (type == OP_OPEN_SUBEXP
                  && subexp_idx == dfa->nodes[node].opr.idx)
@@ -1988,7 +2057,7 @@ check_subexp_limits (re_dfa_t *dfa, re_n
 
          /* Check the limitation of the open subexpression.  */
          /* Note that (ent->subexp_to = str_idx != ent->subexp_from).  */
-         if (ops_node >= 0)
+         if (REG_VALID_INDEX (ops_node))
            {
              err = sub_epsilon_src_nodes (dfa, ops_node, dest_nodes,
                                           candidates);
@@ -1997,10 +2066,10 @@ check_subexp_limits (re_dfa_t *dfa, re_n
            }
 
          /* Check the limitation of the close subexpression.  */
-         if (cls_node >= 0)
+         if (REG_VALID_INDEX (cls_node))
            for (node_idx = 0; node_idx < dest_nodes->nelem; ++node_idx)
              {
-               int node = dest_nodes->elems[node_idx];
+               Idx node = dest_nodes->elems[node_idx];
                if (!re_node_set_contains (dfa->inveclosures + node,
                                           cls_node)
                    && !re_node_set_contains (dfa->eclosures + node,
@@ -2020,7 +2089,7 @@ check_subexp_limits (re_dfa_t *dfa, re_n
        {
          for (node_idx = 0; node_idx < dest_nodes->nelem; ++node_idx)
            {
-             int node = dest_nodes->elems[node_idx];
+             Idx node = dest_nodes->elems[node_idx];
              re_token_type_t type = dfa->nodes[node].type;
              if (type == OP_CLOSE_SUBEXP || type == OP_OPEN_SUBEXP)
                {
@@ -2042,22 +2111,22 @@ check_subexp_limits (re_dfa_t *dfa, re_n
 static reg_errcode_t
 internal_function
 sift_states_bkref (re_match_context_t *mctx, re_sift_context_t *sctx,
-                  int str_idx, const re_node_set *candidates)
+                  Idx str_idx, const re_node_set *candidates)
 {
   re_dfa_t *const dfa = mctx->dfa;
   reg_errcode_t err;
-  int node_idx, node;
+  Idx node_idx, node;
   re_sift_context_t local_sctx;
-  int first_idx = search_cur_bkref_entry (mctx, str_idx);
+  Idx first_idx = search_cur_bkref_entry (mctx, str_idx);
 
-  if (first_idx == -1)
+  if (first_idx == REG_MISSING)
     return REG_NOERROR;
 
   local_sctx.sifted_states = NULL; /* Mark that it hasn't been initialized.  */
 
   for (node_idx = 0; node_idx < candidates->nelem; ++node_idx)
     {
-      int enabled_idx;
+      Idx enabled_idx;
       re_token_type_t type;
       struct re_backref_cache_entry *entry;
       node = candidates->elems[node_idx];
@@ -2072,7 +2141,8 @@ sift_states_bkref (re_match_context_t *m
       enabled_idx = first_idx;
       do
        {
-         int subexp_len, to_idx, dst_node, ret;
+         bool ok;
+         Idx subexp_len, to_idx, dst_node;
          re_dfastate_t *cur_state;
 
          if (entry->node != node)
@@ -2098,8 +2168,8 @@ sift_states_bkref (re_match_context_t *m
            }
          local_sctx.last_node = node;
          local_sctx.last_str_idx = str_idx;
-         ret = re_node_set_insert (&local_sctx.limits, enabled_idx);
-         if (BE (ret < 0, 0))
+         ok = re_node_set_insert (&local_sctx.limits, enabled_idx);
+         if (BE (! ok, 0))
            {
              err = REG_ESPACE;
              goto free_return;
@@ -2139,7 +2209,7 @@ sift_states_bkref (re_match_context_t *m
 static int
 internal_function
 sift_states_iter_mb (const re_match_context_t *mctx, re_sift_context_t *sctx,
-                    int node_idx, int str_idx, int max_str_idx)
+                    Idx node_idx, Idx str_idx, Idx max_str_idx)
 {
   re_dfa_t *const dfa = mctx->dfa;
   int naccepted;
@@ -2230,7 +2300,7 @@ merge_state_with_log (reg_errcode_t *err
                      re_dfastate_t *next_state)
 {
   re_dfa_t *const dfa = mctx->dfa;
-  int cur_idx = re_string_cur_idx (&mctx->input);
+  Idx cur_idx = re_string_cur_idx (&mctx->input);
 
   if (cur_idx > mctx->state_log_top)
     {
@@ -2310,8 +2380,8 @@ find_recover_state (reg_errcode_t *err, 
   re_dfastate_t *cur_state = NULL;
   do
     {
-      int max = mctx->state_log_top;
-      int cur_str_idx = re_string_cur_idx (&mctx->input);
+      Idx max = mctx->state_log_top;
+      Idx cur_str_idx = re_string_cur_idx (&mctx->input);
 
       do
        {
@@ -2323,7 +2393,7 @@ find_recover_state (reg_errcode_t *err, 
 
       cur_state = merge_state_with_log (err, mctx, NULL);
     }
-  while (err == REG_NOERROR && cur_state == NULL);
+  while (*err == REG_NOERROR && cur_state == NULL);
   return cur_state;
 }
 
@@ -2337,10 +2407,10 @@ find_recover_state (reg_errcode_t *err, 
 static reg_errcode_t
 internal_function
 check_subexp_matching_top (re_match_context_t *mctx, re_node_set *cur_nodes,
-                          int str_idx)
+                          Idx str_idx)
 {
   re_dfa_t *const dfa = mctx->dfa;
-  int node_idx;
+  Idx node_idx;
   reg_errcode_t err;
 
   /* TODO: This isn't efficient.
@@ -2350,10 +2420,11 @@ check_subexp_matching_top (re_match_cont
           E.g. RE: (a){2}  */
   for (node_idx = 0; node_idx < cur_nodes->nelem; ++node_idx)
     {
-      int node = cur_nodes->elems[node_idx];
+      Idx node = cur_nodes->elems[node_idx];
       if (dfa->nodes[node].type == OP_OPEN_SUBEXP
-         && dfa->nodes[node].opr.idx < (8 * sizeof (dfa->used_bkref_map))
-         && dfa->used_bkref_map & (1 << dfa->nodes[node].opr.idx))
+         && dfa->nodes[node].opr.idx < BITSET_WORD_BITS
+         && (dfa->used_bkref_map
+             & ((bitset_word) 1 << dfa->nodes[node].opr.idx)))
        {
          err = match_ctx_add_subtop (mctx, node, str_idx);
          if (BE (err != REG_NOERROR, 0))
@@ -2368,15 +2439,13 @@ check_subexp_matching_top (re_match_cont
    accepting the current input byte.  */
 
 static re_dfastate_t *
-transit_state_sb (err, mctx, state)
-     reg_errcode_t *err;
-     re_match_context_t *mctx;
-     re_dfastate_t *state;
+transit_state_sb (reg_errcode_t *err, re_match_context_t *mctx,
+                 re_dfastate_t *state)
 {
   re_dfa_t *const dfa = mctx->dfa;
   re_node_set next_nodes;
   re_dfastate_t *next_state;
-  int node_cnt, cur_str_idx = re_string_cur_idx (&mctx->input);
+  Idx node_cnt, cur_str_idx = re_string_cur_idx (&mctx->input);
   unsigned int context;
 
   *err = re_node_set_alloc (&next_nodes, state->nodes.nelem + 1);
@@ -2384,7 +2453,7 @@ transit_state_sb (err, mctx, state)
     return NULL;
   for (node_cnt = 0; node_cnt < state->nodes.nelem; ++node_cnt)
     {
-      int cur_node = state->nodes.elems[node_cnt];
+      Idx cur_node = state->nodes.elems[node_cnt];
       if (check_node_accept (mctx, dfa->nodes + cur_node, cur_str_idx))
        {
          *err = re_node_set_merge (&next_nodes,
@@ -2414,13 +2483,14 @@ transit_state_mb (re_match_context_t *mc
 {
   re_dfa_t *const dfa = mctx->dfa;
   reg_errcode_t err;
-  int i;
+  Idx i;
 
   for (i = 0; i < pstate->nodes.nelem; ++i)
     {
       re_node_set dest_nodes, *new_nodes;
-      int cur_node_idx = pstate->nodes.elems[i];
-      int naccepted, dest_idx;
+      Idx cur_node_idx = pstate->nodes.elems[i];
+      int naccepted;
+      Idx dest_idx;
       unsigned int context;
       re_dfastate_t *dest_state;
 
@@ -2451,7 +2521,7 @@ transit_state_mb (re_match_context_t *mc
       if (BE (err != REG_NOERROR, 0))
        return err;
 #ifdef DEBUG
-      assert (dfa->nexts[cur_node_idx] != -1);
+      assert (dfa->nexts[cur_node_idx] != REG_MISSING);
 #endif
       new_nodes = dfa->eclosures + dfa->nexts[cur_node_idx];
 
@@ -2483,13 +2553,13 @@ transit_state_bkref (re_match_context_t 
 {
   re_dfa_t *const dfa = mctx->dfa;
   reg_errcode_t err;
-  int i;
-  int cur_str_idx = re_string_cur_idx (&mctx->input);
+  Idx i;
+  Idx cur_str_idx = re_string_cur_idx (&mctx->input);
 
   for (i = 0; i < nodes->nelem; ++i)
     {
-      int dest_str_idx, prev_nelem, bkc_idx;
-      int node_idx = nodes->elems[i];
+      Idx dest_str_idx, prev_nelem, bkc_idx;
+      Idx node_idx = nodes->elems[i];
       unsigned int context;
       const re_token_t *node = dfa->nodes + node_idx;
       re_node_set *new_dest_nodes;
@@ -2516,11 +2586,11 @@ transit_state_bkref (re_match_context_t 
       /* And add the epsilon closures (which is `new_dest_nodes') of
         the backreference to appropriate state_log.  */
 #ifdef DEBUG
-      assert (dfa->nexts[node_idx] != -1);
+      assert (dfa->nexts[node_idx] != REG_MISSING);
 #endif
       for (; bkc_idx < mctx->nbkref_ents; ++bkc_idx)
        {
-         int subexp_len;
+         Idx subexp_len;
          re_dfastate_t *dest_state;
          struct re_backref_cache_entry *bkref_ent;
          bkref_ent = mctx->bkref_ents + bkc_idx;
@@ -2593,14 +2663,14 @@ transit_state_bkref (re_match_context_t 
 
 static reg_errcode_t
 internal_function
-get_subexp (re_match_context_t *mctx, int bkref_node, int bkref_str_idx)
+get_subexp (re_match_context_t *mctx, Idx bkref_node, Idx bkref_str_idx)
 {
   re_dfa_t *const dfa = mctx->dfa;
-  int subexp_num, sub_top_idx;
+  Idx subexp_num, sub_top_idx;
   const char *buf = (const char *) re_string_get_buffer (&mctx->input);
   /* Return if we have already checked BKREF_NODE at BKREF_STR_IDX.  */
-  int cache_idx = search_cur_bkref_entry (mctx, bkref_str_idx);
-  if (cache_idx != -1)
+  Idx cache_idx = search_cur_bkref_entry (mctx, bkref_str_idx);
+  if (cache_idx != REG_MISSING)
     {
       const struct re_backref_cache_entry *entry = mctx->bkref_ents + 
cache_idx;
       do
@@ -2617,7 +2687,7 @@ get_subexp (re_match_context_t *mctx, in
       reg_errcode_t err;
       re_sub_match_top_t *sub_top = mctx->sub_tops[sub_top_idx];
       re_sub_match_last_t *sub_last;
-      int sub_last_idx, sl_str, bkref_str_off;
+      Idx sub_last_idx, sl_str, bkref_str_off;
 
       if (dfa->nodes[sub_top->node].opr.idx != subexp_num)
        continue; /* It isn't related.  */
@@ -2628,7 +2698,7 @@ get_subexp (re_match_context_t *mctx, in
         evaluated.  */
       for (sub_last_idx = 0; sub_last_idx < sub_top->nlasts; ++sub_last_idx)
        {
-         int sl_str_diff;
+         regoff_t sl_str_diff;
          sub_last = sub_top->lasts[sub_last_idx];
          sl_str_diff = sub_last->str_idx - sl_str;
          /* The matched string by the sub expression match with the substring
@@ -2673,7 +2743,8 @@ get_subexp (re_match_context_t *mctx, in
       /* Then, search for the other last nodes of the sub expression.  */
       for (; sl_str <= bkref_str_idx; ++sl_str)
        {
-         int cls_node, sl_str_off;
+         Idx cls_node;
+         regoff_t sl_str_off;
          const re_node_set *nodes;
          sl_str_off = sl_str - sub_top->str_idx;
          /* The matched string by the sub expression match with the substring
@@ -2701,12 +2772,12 @@ get_subexp (re_match_context_t *mctx, in
          /* Does this state have a ')' of the sub expression?  */
          nodes = &mctx->state_log[sl_str]->nodes;
          cls_node = find_subexp_node (dfa, nodes, subexp_num, OP_CLOSE_SUBEXP);
-         if (cls_node == -1)
+         if (cls_node == REG_MISSING)
            continue; /* No.  */
          if (sub_top->path == NULL)
            {
-             sub_top->path = calloc (sizeof (state_array_t),
-                                     sl_str - sub_top->str_idx + 1);
+             sub_top->path = re_calloc (state_array_t,
+                                        sl_str - sub_top->str_idx + 1);
              if (sub_top->path == NULL)
                return REG_ESPACE;
            }
@@ -2739,10 +2810,10 @@ get_subexp (re_match_context_t *mctx, in
 static reg_errcode_t
 internal_function
 get_subexp_sub (re_match_context_t *mctx, const re_sub_match_top_t *sub_top,
-               re_sub_match_last_t *sub_last, int bkref_node, int bkref_str)
+               re_sub_match_last_t *sub_last, Idx bkref_node, Idx bkref_str)
 {
   reg_errcode_t err;
-  int to_idx;
+  Idx to_idx;
   /* Can the subexpression arrive the back reference?  */
   err = check_arrival (mctx, &sub_last->path, sub_last->node,
                       sub_last->str_idx, bkref_node, bkref_str, 
OP_OPEN_SUBEXP);
@@ -2764,21 +2835,21 @@ get_subexp_sub (re_match_context_t *mctx
         nodes.
         E.g. RE: (a){2}  */
 
-static int
+static Idx
 internal_function
 find_subexp_node (const re_dfa_t *dfa, const re_node_set *nodes,
-                 int subexp_idx, int type)
+                 Idx subexp_idx, int type)
 {
-  int cls_idx;
+  Idx cls_idx;
   for (cls_idx = 0; cls_idx < nodes->nelem; ++cls_idx)
     {
-      int cls_node = nodes->elems[cls_idx];
+      Idx cls_node = nodes->elems[cls_idx];
       const re_token_t *node = dfa->nodes + cls_node;
       if (node->type == type
          && node->opr.idx == subexp_idx)
        return cls_node;
     }
-  return -1;
+  return REG_MISSING;
 }
 
 /* Check whether the node TOP_NODE at TOP_STR can arrive to the node
@@ -2789,12 +2860,12 @@ find_subexp_node (const re_dfa_t *dfa, c
 static reg_errcode_t
 internal_function
 check_arrival (re_match_context_t *mctx, state_array_t *path,
-              int top_node, int top_str, int last_node, int last_str,
+              Idx top_node, Idx top_str, Idx last_node, Idx last_str,
               int type)
 {
   re_dfa_t *const dfa = mctx->dfa;
   reg_errcode_t err;
-  int subexp_num, backup_cur_idx, str_idx, null_cnt;
+  Idx subexp_num, backup_cur_idx, str_idx, null_cnt;
   re_dfastate_t *cur_state = NULL;
   re_node_set *cur_nodes, next_nodes;
   re_dfastate_t **backup_state_log;
@@ -2805,17 +2876,17 @@ check_arrival (re_match_context_t *mctx,
   if (BE (path->alloc < last_str + mctx->max_mb_elem_len + 1, 0))
     {
       re_dfastate_t **new_array;
-      int old_alloc = path->alloc;
-      path->alloc += last_str + mctx->max_mb_elem_len + 1;
-      new_array = re_realloc (path->array, re_dfastate_t *, path->alloc);
-      if (new_array == NULL)
-       {
-         path->alloc = old_alloc;
-         return REG_ESPACE;
-       }
+      Idx old_alloc = path->alloc;
+      Idx new_alloc = old_alloc + last_str + mctx->max_mb_elem_len + 1;
+      if (BE (new_alloc < old_alloc, 0))
+       return REG_ESPACE;
+      new_array = re_xrealloc (path->array, re_dfastate_t *, new_alloc);
+      if (BE (new_array == NULL, 0))
+       return REG_ESPACE;
       path->array = new_array;
+      path->alloc = new_alloc;
       memset (new_array + old_alloc, '\0',
-             sizeof (re_dfastate_t *) * (path->alloc - old_alloc));
+             sizeof (re_dfastate_t *) * (new_alloc - old_alloc));
     }
 
   str_idx = path->next_idx == 0 ? top_str : path->next_idx;
@@ -2949,20 +3020,20 @@ check_arrival (re_match_context_t *mctx,
 
 static reg_errcode_t
 internal_function
-check_arrival_add_next_nodes (re_match_context_t *mctx, int str_idx,
+check_arrival_add_next_nodes (re_match_context_t *mctx, Idx str_idx,
                              re_node_set *cur_nodes,
                              re_node_set *next_nodes)
 {
   re_dfa_t *const dfa = mctx->dfa;
-  int result;
-  int cur_idx;
+  bool ok;
+  Idx cur_idx;
   reg_errcode_t err;
   re_node_set union_set;
   re_node_set_init_empty (&union_set);
   for (cur_idx = 0; cur_idx < cur_nodes->nelem; ++cur_idx)
     {
       int naccepted = 0;
-      int cur_node = cur_nodes->elems[cur_idx];
+      Idx cur_node = cur_nodes->elems[cur_idx];
 #ifdef DEBUG
       re_token_type_t type = dfa->nodes[cur_node].type;
       assert (!IS_EPSILON_NODE (type));
@@ -2976,8 +3047,8 @@ check_arrival_add_next_nodes (re_match_c
          if (naccepted > 1)
            {
              re_dfastate_t *dest_state;
-             int next_node = dfa->nexts[cur_node];
-             int next_idx = str_idx + naccepted;
+             Idx next_node = dfa->nexts[cur_node];
+             Idx next_idx = str_idx + naccepted;
              dest_state = mctx->state_log[next_idx];
              re_node_set_empty (&union_set);
              if (dest_state)
@@ -2989,8 +3060,8 @@ check_arrival_add_next_nodes (re_match_c
                      return err;
                    }
                }
-             result = re_node_set_insert (&union_set, next_node);
-             if (BE (result < 0, 0))
+             ok = re_node_set_insert (&union_set, next_node);
+             if (BE (! ok, 0))
                {
                  re_node_set_free (&union_set);
                  return REG_ESPACE;
@@ -3009,8 +3080,8 @@ check_arrival_add_next_nodes (re_match_c
       if (naccepted
          || check_node_accept (mctx, dfa->nodes + cur_node, str_idx))
        {
-         result = re_node_set_insert (next_nodes, dfa->nexts[cur_node]);
-         if (BE (result < 0, 0))
+         ok = re_node_set_insert (next_nodes, dfa->nexts[cur_node]);
+         if (BE (! ok, 0))
            {
              re_node_set_free (&union_set);
              return REG_ESPACE;
@@ -3030,10 +3101,10 @@ check_arrival_add_next_nodes (re_match_c
 static reg_errcode_t
 internal_function
 check_arrival_expand_ecl (re_dfa_t *dfa, re_node_set *cur_nodes,
-                         int ex_subexp, int type)
+                         Idx ex_subexp, int type)
 {
   reg_errcode_t err;
-  int idx, outside_node;
+  Idx idx, outside_node;
   re_node_set new_nodes;
 #ifdef DEBUG
   assert (cur_nodes->nelem);
@@ -3046,10 +3117,10 @@ check_arrival_expand_ecl (re_dfa_t *dfa,
 
   for (idx = 0; idx < cur_nodes->nelem; ++idx)
     {
-      int cur_node = cur_nodes->elems[idx];
+      Idx cur_node = cur_nodes->elems[idx];
       re_node_set *eclosure = dfa->eclosures + cur_node;
       outside_node = find_subexp_node (dfa, eclosure, ex_subexp, type);
-      if (outside_node == -1)
+      if (outside_node == REG_MISSING)
        {
          /* There are no problematic nodes, just merge them.  */
          err = re_node_set_merge (&new_nodes, eclosure);
@@ -3083,36 +3154,37 @@ check_arrival_expand_ecl (re_dfa_t *dfa,
 static reg_errcode_t
 internal_function
 check_arrival_expand_ecl_sub (re_dfa_t *dfa, re_node_set *dst_nodes,
-                             int target, int ex_subexp, int type)
+                             Idx target, Idx ex_subexp, int type)
 {
-  int cur_node;
+  Idx cur_node;
   for (cur_node = target; !re_node_set_contains (dst_nodes, cur_node);)
     {
-      int err;
+      bool ok;
 
       if (dfa->nodes[cur_node].type == type
          && dfa->nodes[cur_node].opr.idx == ex_subexp)
        {
          if (type == OP_CLOSE_SUBEXP)
            {
-             err = re_node_set_insert (dst_nodes, cur_node);
-             if (BE (err == -1, 0))
+             ok = re_node_set_insert (dst_nodes, cur_node);
+             if (BE (! ok, 0))
                return REG_ESPACE;
            }
          break;
        }
-      err = re_node_set_insert (dst_nodes, cur_node);
-      if (BE (err == -1, 0))
+      ok = re_node_set_insert (dst_nodes, cur_node);
+      if (BE (! ok, 0))
        return REG_ESPACE;
       if (dfa->edests[cur_node].nelem == 0)
        break;
       if (dfa->edests[cur_node].nelem == 2)
        {
-         err = check_arrival_expand_ecl_sub (dfa, dst_nodes,
-                                             dfa->edests[cur_node].elems[1],
-                                             ex_subexp, type);
-         if (BE (err != REG_NOERROR, 0))
-           return err;
+         reg_errcode_t ret =
+           check_arrival_expand_ecl_sub (dfa, dst_nodes,
+                                         dfa->edests[cur_node].elems[1],
+                                         ex_subexp, type);
+         if (BE (ret != REG_NOERROR, 0))
+           return ret;
        }
       cur_node = dfa->edests[cur_node].elems[0];
     }
@@ -3127,21 +3199,21 @@ check_arrival_expand_ecl_sub (re_dfa_t *
 static reg_errcode_t
 internal_function
 expand_bkref_cache (re_match_context_t *mctx, re_node_set *cur_nodes,
-                   int cur_str, int subexp_num, int type)
+                   Idx cur_str, Idx subexp_num, int type)
 {
   re_dfa_t *const dfa = mctx->dfa;
   reg_errcode_t err;
-  int cache_idx_start = search_cur_bkref_entry (mctx, cur_str);
+  Idx cache_idx_start = search_cur_bkref_entry (mctx, cur_str);
   struct re_backref_cache_entry *ent;
 
-  if (cache_idx_start == -1)
+  if (cache_idx_start == REG_MISSING)
     return REG_NOERROR;
 
  restart:
   ent = mctx->bkref_ents + cache_idx_start;
   do
     {
-      int to_idx, next_node;
+      Idx to_idx, next_node;
 
       /* Is this entry ENT is appropriate?  */
       if (!re_node_set_contains (cur_nodes, ent->node))
@@ -3179,14 +3251,14 @@ expand_bkref_cache (re_match_context_t *
          next_node = dfa->nexts[ent->node];
          if (mctx->state_log[to_idx])
            {
-             int ret;
+             bool ok;
              if (re_node_set_contains (&mctx->state_log[to_idx]->nodes,
                                        next_node))
                continue;
              err = re_node_set_init_copy (&union_set,
                                           &mctx->state_log[to_idx]->nodes);
-             ret = re_node_set_insert (&union_set, next_node);
-             if (BE (err != REG_NOERROR || ret < 0, 0))
+             ok = re_node_set_insert (&union_set, next_node);
+             if (BE (err != REG_NOERROR || ! ok, 0))
                {
                  re_node_set_free (&union_set);
                  err = err != REG_NOERROR ? err : REG_ESPACE;
@@ -3211,41 +3283,46 @@ expand_bkref_cache (re_match_context_t *
 }
 
 /* Build transition table for the state.
-   Return 1 if succeeded, otherwise return NULL.  */
+   Return true if successful.  */
 
-static int
+static bool
 internal_function
 build_trtable (re_dfa_t *dfa, re_dfastate_t *state)
 {
   reg_errcode_t err;
-  int i, j, ch, need_word_trtable = 0;
-  unsigned int elem, mask;
-  int dests_node_malloced = 0, dest_states_malloced = 0;
-  int ndests; /* Number of the destination states from `state'.  */
+  Idx i, j;
+  int ch;
+  bool need_word_trtable = false;
+  bitset_word elem, mask;
+  bool dests_node_malloced = false, dest_states_malloced = false;
+  Idx ndests; /* Number of the destination states from `state'.  */
   re_dfastate_t **trtable;
   re_dfastate_t **dest_states = NULL, **dest_states_word, **dest_states_nl;
   re_node_set follows, *dests_node;
   bitset *dests_ch;
   bitset acceptable;
 
+  struct dests_alloc
+  {
+    re_node_set dests_node[SBC_MAX];
+    bitset dests_ch[SBC_MAX];
+  } *dests_alloc;
+
   /* We build DFA states which corresponds to the destination nodes
      from `state'.  `dests_node[i]' represents the nodes which i-th
      destination state contains, and `dests_ch[i]' represents the
      characters which i-th destination state accepts.  */
-#ifdef _LIBC
-  if (__libc_use_alloca ((sizeof (re_node_set) + sizeof (bitset)) * SBC_MAX))
-    dests_node = (re_node_set *)
-      alloca ((sizeof (re_node_set) + sizeof (bitset)) * SBC_MAX);
+  if (__libc_use_alloca (sizeof (struct dests_alloc)))
+    dests_alloc = (struct dests_alloc *) alloca (sizeof dests_alloc[0]);
   else
-#endif
     {
-      dests_node = (re_node_set *)
-       malloc ((sizeof (re_node_set) + sizeof (bitset)) * SBC_MAX);
-      if (BE (dests_node == NULL, 0))
-       return 0;
-      dests_node_malloced = 1;
+      dests_alloc = re_malloc (struct dests_alloc, 1);
+      if (BE (dests_alloc == NULL, 0))
+       return false;
+      dests_node_malloced = true;
     }
-  dests_ch = (bitset *) (dests_node + SBC_MAX);
+  dests_node = dests_alloc->dests_node;
+  dests_ch = dests_alloc->dests_ch;
 
   /* Initialize transiton table.  */
   state->word_trtable = state->trtable = NULL;
@@ -3253,31 +3330,33 @@ build_trtable (re_dfa_t *dfa, re_dfastat
   /* At first, group all nodes belonging to `state' into several
      destinations.  */
   ndests = group_nodes_into_DFAstates (dfa, state, dests_node, dests_ch);
-  if (BE (ndests <= 0, 0))
+  if (BE (! REG_VALID_NONZERO_INDEX (ndests), 0))
     {
       if (dests_node_malloced)
-       free (dests_node);
-      /* Return 0 in case of an error, 1 otherwise.  */
+       free (dests_alloc);
       if (ndests == 0)
        {
-         state->trtable = (re_dfastate_t **)
-           calloc (sizeof (re_dfastate_t *), SBC_MAX);
-         return 1;
+         state->trtable = re_calloc (re_dfastate_t *, SBC_MAX);
+         return true;
        }
-      return 0;
+      return false;
     }
 
   err = re_node_set_alloc (&follows, ndests + 1);
   if (BE (err != REG_NOERROR, 0))
     goto out_free;
 
-#ifdef _LIBC
+  /* Avoid arithmetic overflow in size calculation.  */
+  if (BE (((SIZE_MAX - (sizeof (re_node_set) + sizeof (bitset)) * SBC_MAX)
+          / (3 * sizeof (re_dfastate_t *)))
+         < ndests, 0))
+    goto out_free;
+
   if (__libc_use_alloca ((sizeof (re_node_set) + sizeof (bitset)) * SBC_MAX
                         + ndests * 3 * sizeof (re_dfastate_t *)))
     dest_states = (re_dfastate_t **)
       alloca (ndests * 3 * sizeof (re_dfastate_t *));
   else
-#endif
     {
       dest_states = (re_dfastate_t **)
        malloc (ndests * 3 * sizeof (re_dfastate_t *));
@@ -3290,10 +3369,10 @@ out_free:
          for (i = 0; i < ndests; ++i)
            re_node_set_free (dests_node + i);
          if (dests_node_malloced)
-           free (dests_node);
-         return 0;
+           free (dests_alloc);
+         return false;
        }
-      dest_states_malloced = 1;
+      dest_states_malloced = true;
     }
   dest_states_word = dest_states + ndests;
   dest_states_nl = dest_states_word + ndests;
@@ -3302,13 +3381,13 @@ out_free:
   /* Then build the states for all destinations.  */
   for (i = 0; i < ndests; ++i)
     {
-      int next_node;
+      Idx next_node;
       re_node_set_empty (&follows);
       /* Merge the follows of this destination states.  */
       for (j = 0; j < dests_node[i].nelem; ++j)
        {
          next_node = dfa->nexts[dests_node[i].elems[j]];
-         if (next_node != -1)
+         if (next_node != REG_MISSING)
            {
              err = re_node_set_merge (&follows, dfa->eclosures + next_node);
              if (BE (err != REG_NOERROR, 0))
@@ -3328,7 +3407,7 @@ out_free:
            goto out_free;
 
          if (dest_states[i] != dest_states_word[i] && dfa->mb_cur_max > 1)
-           need_word_trtable = 1;
+           need_word_trtable = true;
 
          dest_states_nl[i] = re_acquire_state_context (&err, dfa, &follows,
                                                        CONTEXT_NEWLINE);
@@ -3349,14 +3428,13 @@ out_free:
         character, or we are in a single-byte character set so we can
         discern by looking at the character code: allocate a
         256-entry transition table.  */
-      trtable = state->trtable =
-       (re_dfastate_t **) calloc (sizeof (re_dfastate_t *), SBC_MAX);
+      trtable = state->trtable = re_calloc (re_dfastate_t *, SBC_MAX);
       if (BE (trtable == NULL, 0))
        goto out_free;
 
       /* For all characters ch...:  */
-      for (i = 0; i < BITSET_UINTS; ++i)
-       for (ch = i * UINT_BITS, elem = acceptable[i], mask = 1;
+      for (i = 0; i < BITSET_WORDS; ++i)
+       for (ch = i * BITSET_WORD_BITS, elem = acceptable[i], mask = 1;
             elem;
             mask <<= 1, elem >>= 1, ++ch)
          if (BE (elem & 1, 0))
@@ -3380,14 +3458,13 @@ out_free:
         by looking at the character code: build two 256-entry
         transition tables, one starting at trtable[0] and one
         starting at trtable[SBC_MAX].  */
-      trtable = state->word_trtable =
-       (re_dfastate_t **) calloc (sizeof (re_dfastate_t *), 2 * SBC_MAX);
+      trtable = state->word_trtable = re_calloc (re_dfastate_t *, 2 * SBC_MAX);
       if (BE (trtable == NULL, 0))
        goto out_free;
 
       /* For all characters ch...:  */
-      for (i = 0; i < BITSET_UINTS; ++i)
-       for (ch = i * UINT_BITS, elem = acceptable[i], mask = 1;
+      for (i = 0; i < BITSET_WORDS; ++i)
+       for (ch = i * BITSET_WORD_BITS, elem = acceptable[i], mask = 1;
             elem;
             mask <<= 1, elem >>= 1, ++ch)
          if (BE (elem & 1, 0))
@@ -3428,9 +3505,9 @@ out_free:
     re_node_set_free (dests_node + i);
 
   if (dests_node_malloced)
-    free (dests_node);
+    free (dests_alloc);
 
-  return 1;
+  return true;
 }
 
 /* Group all nodes belonging to STATE into several destinations.
@@ -3438,15 +3515,15 @@ out_free:
    to DESTS_NODE[i] and set the characters accepted by the destination
    to DEST_CH[i].  This function return the number of destinations.  */
 
-static int
+static Idx
 internal_function
-group_nodes_into_DFAstates (re_dfa_t *dfa, const re_dfastate_t *state,
+group_nodes_into_DFAstates (const re_dfa_t *dfa, const re_dfastate_t *state,
                            re_node_set *dests_node, bitset *dests_ch)
 {
   reg_errcode_t err;
-  int result;
-  int i, j, k;
-  int ndests; /* Number of the destinations from `state'.  */
+  bool ok;
+  Idx i, j, k;
+  Idx ndests; /* Number of the destinations from `state'.  */
   bitset accepts; /* Characters a node can accept.  */
   const re_node_set *cur_nodes = &state->nodes;
   bitset_empty (accepts);
@@ -3474,18 +3551,21 @@ group_nodes_into_DFAstates (re_dfa_t *df
          else
 #endif
            bitset_set_all (accepts);
-         if (!(dfa->syntax & RE_DOT_NEWLINE))
+         if (!(dfa->syntax & REG_DOT_NEWLINE))
            bitset_clear (accepts, '\n');
-         if (dfa->syntax & RE_DOT_NOT_NULL)
+         if (dfa->syntax & REG_DOT_NOT_NULL)
            bitset_clear (accepts, '\0');
        }
 #ifdef RE_ENABLE_I18N
       else if (type == OP_UTF8_PERIOD)
         {
-         memset (accepts, 255, sizeof (unsigned int) * BITSET_UINTS / 2);
-         if (!(dfa->syntax & RE_DOT_NEWLINE))
+         if (SBC_MAX / 2 % BITSET_WORD_BITS == 0)
+           memset (accepts, -1, sizeof accepts / 2);
+         else
+           bitset_merge (accepts, utf8_sb_map);
+         if (!(dfa->syntax & REG_DOT_NEWLINE))
            bitset_clear (accepts, '\n');
-         if (dfa->syntax & RE_DOT_NOT_NULL)
+         if (dfa->syntax & REG_DOT_NOT_NULL)
            bitset_clear (accepts, '\0');
         }
 #endif
@@ -3498,7 +3578,7 @@ group_nodes_into_DFAstates (re_dfa_t *df
        {
          if (constraint & NEXT_NEWLINE_CONSTRAINT)
            {
-             int accepts_newline = bitset_contain (accepts, NEWLINE_CHAR);
+             bool accepts_newline = bitset_contain (accepts, NEWLINE_CHAR);
              bitset_empty (accepts);
              if (accepts_newline)
                bitset_set (accepts, NEWLINE_CHAR);
@@ -3513,7 +3593,7 @@ group_nodes_into_DFAstates (re_dfa_t *df
 
          if (constraint & NEXT_WORD_CONSTRAINT)
            {
-             unsigned int any_set = 0;
+             bitset_word any_set = 0;
              if (type == CHARACTER && !node->word_char)
                {
                  bitset_empty (accepts);
@@ -3521,18 +3601,18 @@ group_nodes_into_DFAstates (re_dfa_t *df
                }
 #ifdef RE_ENABLE_I18N
              if (dfa->mb_cur_max > 1)
-               for (j = 0; j < BITSET_UINTS; ++j)
+               for (j = 0; j < BITSET_WORDS; ++j)
                  any_set |= (accepts[j] &= (dfa->word_char[j] | 
~dfa->sb_char[j]));
              else
 #endif
-               for (j = 0; j < BITSET_UINTS; ++j)
+               for (j = 0; j < BITSET_WORDS; ++j)
                  any_set |= (accepts[j] &= dfa->word_char[j]);
              if (!any_set)
                continue;
            }
          if (constraint & NEXT_NOTWORD_CONSTRAINT)
            {
-             unsigned int any_set = 0;
+             bitset_word any_set = 0;
              if (type == CHARACTER && node->word_char)
                {
                  bitset_empty (accepts);
@@ -3540,11 +3620,11 @@ group_nodes_into_DFAstates (re_dfa_t *df
                }
 #ifdef RE_ENABLE_I18N
              if (dfa->mb_cur_max > 1)
-               for (j = 0; j < BITSET_UINTS; ++j)
+               for (j = 0; j < BITSET_WORDS; ++j)
                  any_set |= (accepts[j] &= ~(dfa->word_char[j] & 
dfa->sb_char[j]));
              else
 #endif
-               for (j = 0; j < BITSET_UINTS; ++j)
+               for (j = 0; j < BITSET_WORDS; ++j)
                  any_set |= (accepts[j] &= ~dfa->word_char[j]);
              if (!any_set)
                continue;
@@ -3558,7 +3638,7 @@ group_nodes_into_DFAstates (re_dfa_t *df
          bitset intersec; /* Intersection sets, see below.  */
          bitset remains;
          /* Flags, see below.  */
-         int has_intersec, not_subset, not_consumed;
+         bitset_word has_intersec, not_subset, not_consumed;
 
          /* Optimization, skip if this state doesn't accept the character.  */
          if (type == CHARACTER && !bitset_contain (dests_ch[j], node->opr.c))
@@ -3566,7 +3646,7 @@ group_nodes_into_DFAstates (re_dfa_t *df
 
          /* Enumerate the intersection set of this state and `accepts'.  */
          has_intersec = 0;
-         for (k = 0; k < BITSET_UINTS; ++k)
+         for (k = 0; k < BITSET_WORDS; ++k)
            has_intersec |= intersec[k] = accepts[k] & dests_ch[j][k];
          /* And skip if the intersection set is empty.  */
          if (!has_intersec)
@@ -3574,7 +3654,7 @@ group_nodes_into_DFAstates (re_dfa_t *df
 
          /* Then check if this state is a subset of `accepts'.  */
          not_subset = not_consumed = 0;
-         for (k = 0; k < BITSET_UINTS; ++k)
+         for (k = 0; k < BITSET_WORDS; ++k)
            {
              not_subset |= remains[k] = ~accepts[k] & dests_ch[j][k];
              not_consumed |= accepts[k] = accepts[k] & ~dests_ch[j][k];
@@ -3593,8 +3673,8 @@ group_nodes_into_DFAstates (re_dfa_t *df
            }
 
          /* Put the position in the current group. */
-         result = re_node_set_insert (&dests_node[j], cur_nodes->elems[i]);
-         if (BE (result < 0, 0))
+         ok = re_node_set_insert (&dests_node[j], cur_nodes->elems[i]);
+         if (BE (! ok, 0))
            goto error_return;
 
          /* If all characters are consumed, go to next node. */
@@ -3616,7 +3696,7 @@ group_nodes_into_DFAstates (re_dfa_t *df
  error_return:
   for (j = 0; j < ndests; ++j)
     re_node_set_free (dests_node + j);
-  return -1;
+  return REG_MISSING;
 }
 
 #ifdef RE_ENABLE_I18N
@@ -3630,12 +3710,12 @@ group_nodes_into_DFAstates (re_dfa_t *df
 
 static int
 internal_function
-check_node_accept_bytes (re_dfa_t *dfa, int node_idx,
-                        const re_string_t *input, int str_idx)
+check_node_accept_bytes (re_dfa_t *dfa, Idx node_idx,
+                        const re_string_t *input, Idx str_idx)
 {
   const re_token_t *node = dfa->nodes + node_idx;
   int char_len, elem_len;
-  int i;
+  Idx i;
 
   if (BE (node->type == OP_UTF8_PERIOD, 0))
     {
@@ -3696,9 +3776,9 @@ check_node_accept_bytes (re_dfa_t *dfa, 
       /* FIXME: I don't think this if is needed, as both '\n'
         and '\0' are char_len == 1.  */
       /* '.' accepts any one character except the following two cases.  */
-      if ((!(dfa->syntax & RE_DOT_NEWLINE) &&
+      if ((!(dfa->syntax & REG_DOT_NEWLINE) &&
           re_string_byte_at (input, str_idx) == '\n') ||
-         ((dfa->syntax & RE_DOT_NOT_NULL) &&
+         ((dfa->syntax & REG_DOT_NOT_NULL) &&
           re_string_byte_at (input, str_idx) == '\0'))
        return 0;
       return char_len;
@@ -3714,7 +3794,7 @@ check_node_accept_bytes (re_dfa_t *dfa, 
 # ifdef _LIBC
       const unsigned char *pin
        = ((const unsigned char *) re_string_get_buffer (input) + str_idx);
-      int j;
+      Idx j;
       uint32_t nrules;
 # endif /* _LIBC */
       int match_len = 0;
@@ -3813,7 +3893,7 @@ check_node_accept_bytes (re_dfa_t *dfa, 
                    size_t weight_len = weights[idx];
                    if (weight_len == weights[equiv_class_idx])
                      {
-                       int cnt = 0;
+                       Idx cnt = 0;
                        while (cnt <= weight_len
                               && (weights[equiv_class_idx + 1 + cnt]
                                   == weights[idx + 1 + cnt]))
@@ -3865,9 +3945,7 @@ check_node_accept_bytes (re_dfa_t *dfa, 
 
 # ifdef _LIBC
 static unsigned int
-find_collation_sequence_value (mbs, mbs_len)
-    const unsigned char *mbs;
-    size_t mbs_len;
+find_collation_sequence_value (const unsigned char *mbs, size_t mbs_len)
 {
   uint32_t nrules = _NL_CURRENT_WORD (LC_COLLATE, _NL_COLLATE_NRULES);
   if (nrules == 0)
@@ -3891,7 +3969,8 @@ find_collation_sequence_value (mbs, mbs_
 
       for (idx = 0; idx < extrasize;)
        {
-         int mbs_cnt, found = 0;
+         int mbs_cnt;
+         bool found = false;
          int32_t elem_mbs_len;
          /* Skip the name of collating element name.  */
          idx = idx + extra[idx] + 1;
@@ -3903,7 +3982,7 @@ find_collation_sequence_value (mbs, mbs_
                  break;
              if (mbs_cnt == elem_mbs_len)
                /* Found the entry.  */
-               found = 1;
+               found = true;
            }
          /* Skip the byte sequence of the collating element.  */
          idx += elem_mbs_len;
@@ -3928,10 +4007,10 @@ find_collation_sequence_value (mbs, mbs_
 /* Check whether the node accepts the byte which is IDX-th
    byte of the INPUT.  */
 
-static int
+static bool
 internal_function
 check_node_accept (const re_match_context_t *mctx, const re_token_t *node,
-                  int idx)
+                  Idx idx)
 {
   unsigned char ch;
   ch = re_string_byte_at (&mctx->input, idx);
@@ -3939,28 +4018,28 @@ check_node_accept (const re_match_contex
     {
     case CHARACTER:
       if (node->opr.c != ch)
-        return 0;
+        return false;
       break;
 
     case SIMPLE_BRACKET:
       if (!bitset_contain (node->opr.sbcset, ch))
-        return 0;
+        return false;
       break;
 
 #ifdef RE_ENABLE_I18N
     case OP_UTF8_PERIOD:
       if (ch >= 0x80)
-        return 0;
+        return false;
       /* FALLTHROUGH */
 #endif
     case OP_PERIOD:
-      if ((ch == '\n' && !(mctx->dfa->syntax & RE_DOT_NEWLINE))
-         || (ch == '\0' && (mctx->dfa->syntax & RE_DOT_NOT_NULL)))
-       return 0;
+      if ((ch == '\n' && !(mctx->dfa->syntax & REG_DOT_NEWLINE))
+         || (ch == '\0' && (mctx->dfa->syntax & REG_DOT_NOT_NULL)))
+       return false;
       break;
 
     default:
-      return 0;
+      return false;
     }
 
   if (node->constraint)
@@ -3970,10 +4049,10 @@ check_node_accept (const re_match_contex
       unsigned int context = re_string_context_at (&mctx->input, idx,
                                                   mctx->eflags);
       if (NOT_SATISFY_NEXT_CONSTRAINT (node->constraint, context))
-       return 0;
+       return false;
     }
 
-  return 1;
+  return true;
 }
 
 /* Extend the buffers, if the buffers have run out.  */
@@ -3996,8 +4075,8 @@ extend_buffers (re_match_context_t *mctx
       /* XXX We have no indication of the size of this buffer.  If this
         allocation fail we have no indication that the state_log array
         does not have the right size.  */
-      re_dfastate_t **new_array = re_realloc (mctx->state_log, re_dfastate_t *,
-                                             pstr->bufs_len + 1);
+      re_dfastate_t **new_array = re_xrealloc (mctx->state_log, re_dfastate_t 
*,
+                                              pstr->bufs_len + 1);
       if (BE (new_array == NULL, 0))
        return REG_ESPACE;
       mctx->state_log = new_array;
@@ -4039,14 +4118,14 @@ extend_buffers (re_match_context_t *mctx
 
 static reg_errcode_t
 internal_function
-match_ctx_init (re_match_context_t *mctx, int eflags, int n)
+match_ctx_init (re_match_context_t *mctx, int eflags, Idx n)
 {
   mctx->eflags = eflags;
-  mctx->match_last = -1;
+  mctx->match_last = REG_MISSING;
   if (n > 0)
     {
-      mctx->bkref_ents = re_malloc (struct re_backref_cache_entry, n);
-      mctx->sub_tops = re_malloc (re_sub_match_top_t *, n);
+      mctx->bkref_ents = re_xmalloc (struct re_backref_cache_entry, n);
+      mctx->sub_tops = re_xmalloc (re_sub_match_top_t *, n);
       if (BE (mctx->bkref_ents == NULL || mctx->sub_tops == NULL, 0))
        return REG_ESPACE;
     }
@@ -4069,10 +4148,10 @@ static void
 internal_function
 match_ctx_clean (re_match_context_t *mctx)
 {
-  int st_idx;
+  Idx st_idx;
   for (st_idx = 0; st_idx < mctx->nsub_tops; ++st_idx)
     {
-      int sl_idx;
+      Idx sl_idx;
       re_sub_match_top_t *top = mctx->sub_tops[st_idx];
       for (sl_idx = 0; sl_idx < top->nlasts; ++sl_idx)
        {
@@ -4112,14 +4191,14 @@ match_ctx_free (re_match_context_t *mctx
 
 static reg_errcode_t
 internal_function
-match_ctx_add_entry (re_match_context_t *mctx, int node, int str_idx,
-                    int from, int to)
+match_ctx_add_entry (re_match_context_t *mctx, Idx node, Idx str_idx,
+                    Idx from, Idx to)
 {
   if (mctx->nbkref_ents >= mctx->abkref_ents)
     {
       struct re_backref_cache_entry* new_entry;
-      new_entry = re_realloc (mctx->bkref_ents, struct re_backref_cache_entry,
-                             mctx->abkref_ents * 2);
+      new_entry = re_x2realloc (mctx->bkref_ents, struct 
re_backref_cache_entry,
+                               &mctx->abkref_ents);
       if (BE (new_entry == NULL, 0))
        {
          re_free (mctx->bkref_ents);
@@ -4127,8 +4206,8 @@ match_ctx_add_entry (re_match_context_t 
        }
       mctx->bkref_ents = new_entry;
       memset (mctx->bkref_ents + mctx->nbkref_ents, '\0',
-             sizeof (struct re_backref_cache_entry) * mctx->abkref_ents);
-      mctx->abkref_ents *= 2;
+             (sizeof (struct re_backref_cache_entry)
+              * (mctx->abkref_ents - mctx->nbkref_ents)));
     }
   if (mctx->nbkref_ents > 0
       && mctx->bkref_ents[mctx->nbkref_ents - 1].str_idx == str_idx)
@@ -4148,7 +4227,7 @@ match_ctx_add_entry (re_match_context_t 
      A backreference does not epsilon-transition unless it is empty, so set
      to all zeros if FROM != TO.  */
   mctx->bkref_ents[mctx->nbkref_ents].eps_reachable_subexps_map
-    = (from == to ? ~0 : 0);
+    = (from == to ? -1 : 0);
 
   mctx->bkref_ents[mctx->nbkref_ents++].more = 0;
   if (mctx->max_mb_elem_len < to - from)
@@ -4156,14 +4235,14 @@ match_ctx_add_entry (re_match_context_t 
   return REG_NOERROR;
 }
 
-/* Search for the first entry which has the same str_idx, or -1 if none is
+/* Return the first entry with the same str_idx, or REG_MISSING if none is
    found.  Note that MCTX->BKREF_ENTS is already sorted by MCTX->STR_IDX.  */
 
-static int
+static Idx
 internal_function
-search_cur_bkref_entry (re_match_context_t *mctx, int str_idx)
+search_cur_bkref_entry (const re_match_context_t *mctx, Idx str_idx)
 {
-  int left, right, mid, last;
+  Idx left, right, mid, last;
   last = right = mctx->nbkref_ents;
   for (left = 0; left < right;)
     {
@@ -4176,7 +4255,7 @@ search_cur_bkref_entry (re_match_context
   if (left < last && mctx->bkref_ents[left].str_idx == str_idx)
     return left;
   else
-    return -1;
+    return REG_MISSING;
 }
 
 /* Register the node NODE, whose type is OP_OPEN_SUBEXP, and which matches
@@ -4184,7 +4263,7 @@ search_cur_bkref_entry (re_match_context
 
 static reg_errcode_t
 internal_function
-match_ctx_add_subtop (re_match_context_t *mctx, int node, int str_idx)
+match_ctx_add_subtop (re_match_context_t *mctx, Idx node, Idx str_idx)
 {
 #ifdef DEBUG
   assert (mctx->sub_tops != NULL);
@@ -4192,16 +4271,16 @@ match_ctx_add_subtop (re_match_context_t
 #endif
   if (BE (mctx->nsub_tops == mctx->asub_tops, 0))
     {
-      int new_asub_tops = mctx->asub_tops * 2;
-      re_sub_match_top_t **new_array = re_realloc (mctx->sub_tops,
-                                                  re_sub_match_top_t *,
-                                                  new_asub_tops);
+      Idx new_asub_tops = mctx->asub_tops;
+      re_sub_match_top_t **new_array = re_x2realloc (mctx->sub_tops,
+                                                    re_sub_match_top_t *,
+                                                    &new_asub_tops);
       if (BE (new_array == NULL, 0))
        return REG_ESPACE;
       mctx->sub_tops = new_array;
       mctx->asub_tops = new_asub_tops;
     }
-  mctx->sub_tops[mctx->nsub_tops] = calloc (1, sizeof (re_sub_match_top_t));
+  mctx->sub_tops[mctx->nsub_tops] = re_calloc (re_sub_match_top_t, 1);
   if (BE (mctx->sub_tops[mctx->nsub_tops] == NULL, 0))
     return REG_ESPACE;
   mctx->sub_tops[mctx->nsub_tops]->node = node;
@@ -4214,21 +4293,21 @@ match_ctx_add_subtop (re_match_context_t
 
 static re_sub_match_last_t *
 internal_function
-match_ctx_add_sublast (re_sub_match_top_t *subtop, int node, int str_idx)
+match_ctx_add_sublast (re_sub_match_top_t *subtop, Idx node, Idx str_idx)
 {
   re_sub_match_last_t *new_entry;
   if (BE (subtop->nlasts == subtop->alasts, 0))
     {
-      int new_alasts = 2 * subtop->alasts + 1;
-      re_sub_match_last_t **new_array = re_realloc (subtop->lasts,
-                                                   re_sub_match_last_t *,
-                                                   new_alasts);
+      Idx new_alasts = subtop->alasts;
+      re_sub_match_last_t **new_array = re_x2realloc (subtop->lasts,
+                                                     re_sub_match_last_t *,
+                                                     &new_alasts);
       if (BE (new_array == NULL, 0))
        return NULL;
       subtop->lasts = new_array;
       subtop->alasts = new_alasts;
     }
-  new_entry = calloc (1, sizeof (re_sub_match_last_t));
+  new_entry = re_calloc (re_sub_match_last_t, 1);
   if (BE (new_entry != NULL, 1))
     {
       subtop->lasts[subtop->nlasts] = new_entry;
@@ -4244,7 +4323,7 @@ internal_function
 sift_ctx_init (re_sift_context_t *sctx,
               re_dfastate_t **sifted_sts,
               re_dfastate_t **limited_sts,
-              int last_node, int last_str_idx)
+              Idx last_node, Idx last_str_idx)
 {
   sctx->sifted_states = sifted_sts;
   sctx->limited_states = limited_sts;
Index: m4/regex.m4
===================================================================
RCS file: /fetish/cu/m4/regex.m4,v
retrieving revision 1.39
diff -p -u -r1.39 regex.m4
--- m4/regex.m4 25 Jul 2005 19:29:34 -0000      1.39
+++ m4/regex.m4 9 Sep 2005 20:58:17 -0000
@@ -1,4 +1,4 @@
-#serial 24
+#serial 29
 
 # Copyright (C) 1996, 1997, 1998, 1999, 2000, 2001, 2003, 2004, 2005 Free
 # Software Foundation, Inc.
@@ -10,121 +10,125 @@
 dnl Initially derived from code in GNU grep.
 dnl Mostly written by Jim Meyering.
 
+AC_PREREQ([2.50])
+
 AC_DEFUN([gl_REGEX],
 [
-  gl_INCLUDED_REGEX([lib/regex.c])
-])
-
-dnl Usage: gl_INCLUDED_REGEX([lib/regex.c])
-dnl
-AC_DEFUN([gl_INCLUDED_REGEX],
-  [
-    AC_LIBSOURCES(
-      [regcomp.c, regex.c, regex.h,
-       regex_internal.c, regex_internal.h, regexec.c])
-
-    dnl Even packages that don't use regex.c can use this macro.
-    dnl Of course, for them it doesn't do anything.
-
-    # Assume we'll default to using the included regex.c.
-    ac_use_included_regex=yes
-
-    # However, if the system regex support is good enough that it passes the
-    # the following run test, then default to *not* using the included regex.c.
+  AC_REQUIRE([AC_SYS_LARGEFILE]) dnl for a sufficently-wide off_t
+  AC_DEFINE([_REGEX_LARGE_OFFSETS], 1,
+    [Define if you want regoff_t to be at least as wide POSIX requires.])
+
+  AC_LIBSOURCES(
+    [regcomp.c, regex.c, regex.h,
+     regex_internal.c, regex_internal.h, regexec.c])
+
+  AC_ARG_WITH([included-regex],
+    [AC_HELP_STRING([--without-included-regex],
+                   [don't compile regex; this is the default on
+                    systems with recent-enough versions of the GNU C
+                    Library (use with caution on other systems)])])
+
+  case $with_included_regex in
+  yes|no) ac_use_included_regex=$with_included_regex
+       ;;
+  '')
+    # If the system regex support is good enough that it passes the the
+    # following run test, then default to *not* using the included regex.c.
     # If cross compiling, assume the test would fail and use the included
     # regex.c.  The first failing regular expression is from `Spencer ere
     # test #75' in grep-2.3.
     AC_CACHE_CHECK([for working re_compile_pattern],
-                  [gl_cv_func_working_re_compile_pattern],
+                  [gl_cv_func_re_compile_pattern_broken],
       [AC_RUN_IFELSE(
-        [AC_LANG_PROGRAM(
-           [AC_INCLUDES_DEFAULT
-            #include <regex.h>],
-           [[static struct re_pattern_buffer regex;
-             const char *s;
-             struct re_registers regs;
-             re_set_syntax (RE_SYNTAX_POSIX_EGREP);
-             memset (&regex, 0, sizeof (regex));
-             s = re_compile_pattern ("a[:@:>@:]b\n", 9, &regex);
-             /* This should fail with _Invalid character class name_ error.  */
-             if (!s)
-               exit (1);
-
-             /* This should succeed, but does not for e.g. glibc-2.1.3.  */
-             memset (&regex, 0, sizeof (regex));
-             s = re_compile_pattern ("{1", 2, &regex);
-
-             if (s)
-               exit (1);
-
-             /* The following example is derived from a problem report
-                against gawk from Jorge Stolfi <address@hidden>.  */
-             memset (&regex, 0, sizeof (regex));
-             s = re_compile_pattern ("[an\371]*n", 7, &regex);
-             if (s)
-               exit (1);
-
-             /* This should match, but does not for e.g. glibc-2.2.1.  */
-             if (re_match (&regex, "an", 2, 0, &regs) != 2)
-               exit (1);
-
-             memset (&regex, 0, sizeof (regex));
-             s = re_compile_pattern ("x", 1, &regex);
-             if (s)
-               exit (1);
-
-             /* The version of regex.c in e.g. GNU libc-2.2.93 did not
-                work with a negative RANGE argument.  */
-             if (re_search (&regex, "wxy", 3, 2, -2, &regs) != 1)
-               exit (1);
-
-             /* The version of regex.c in older versions of gnulib
-              * ignored RE_ICASE.  Detect that problem too. */
-             memset (&regex, 0, sizeof (regex));
-             re_set_syntax(RE_SYNTAX_EMACS|RE_ICASE);
-             s = re_compile_pattern ("x", 1, &regex);
-             if (s)
-               exit (1);
-
-             if (re_search (&regex, "WXY", 3, 0, 3, &regs) < 0)
-               exit (1);
-
-             /* REG_STARTEND was added to glibc on 2004-01-15.
-                Reject older versions.  */
-             if (! REG_STARTEND)
-               exit (1);
-
-             exit (0);]])],
-        [gl_cv_func_working_re_compile_pattern=yes],
-        [gl_cv_func_working_re_compile_pattern=no],
-        dnl When crosscompiling, assume it is broken.
-        [gl_cv_func_working_re_compile_pattern=no])])
-    if test $gl_cv_func_working_re_compile_pattern = yes; then
-      ac_use_included_regex=no
-    fi
-
-    test -n "$1" || AC_MSG_ERROR([missing argument])
-    m4_syscmd([test -f '$1'])
-    ifelse(m4_sysval, 0,
-      [
-       AC_ARG_WITH([included-regex],
-         [  --without-included-regex don't compile regex; this is the default 
on
-                          systems with recent-enough versions of the GNU C
-                          Library (use with caution on other systems)],
-         [gl_with_regex=$withval],
-         [gl_with_regex=$ac_use_included_regex])
-       if test "X$gl_with_regex" = Xyes; then
-         AC_LIBOBJ([regex])
-         gl_PREREQ_REGEX
-       fi
-      ],
-    )
-  ]
-)
+       [AC_LANG_PROGRAM(
+         [AC_INCLUDES_DEFAULT
+          #include <regex.h>],
+         [[static struct re_pattern_buffer regex;
+           const char *s;
+           struct re_registers regs;
+           /* Use the POSIX-compliant spelling with leading REG_,
+              rather than the traditional GNU spelling with leading RE_,
+              so that we reject older libc implementations.  */
+           re_set_syntax (REG_SYNTAX_POSIX_EGREP);
+           memset (&regex, 0, sizeof (regex));
+           s = re_compile_pattern ("a[:@:>@:]b\n", 9, &regex);
+           /* This should fail with _Invalid character class name_ error.  */
+           if (!s)
+             exit (1);
+
+           /* This should succeed, but does not for e.g. glibc-2.1.3.  */
+           memset (&regex, 0, sizeof (regex));
+           s = re_compile_pattern ("{1", 2, &regex);
+
+           if (s)
+             exit (1);
+
+           /* The following example is derived from a problem report
+              against gawk from Jorge Stolfi <address@hidden>.  */
+           memset (&regex, 0, sizeof (regex));
+           s = re_compile_pattern ("[an\371]*n", 7, &regex);
+           if (s)
+             exit (1);
+
+           /* This should match, but does not for e.g. glibc-2.2.1.  */
+           if (re_match (&regex, "an", 2, 0, &regs) != 2)
+             exit (1);
+
+           memset (&regex, 0, sizeof (regex));
+           s = re_compile_pattern ("x", 1, &regex);
+           if (s)
+             exit (1);
+
+           /* The version of regex.c in e.g. GNU libc-2.2.93 did not
+              work with a negative RANGE argument.  */
+           if (re_search (&regex, "wxy", 3, 2, -2, &regs) != 1)
+             exit (1);
+
+           /* The version of regex.c in older versions of gnulib
+              ignored REG_IGNORE_CASE (which was then called RE_ICASE).
+              Detect that problem too.  */
+           memset (&regex, 0, sizeof (regex));
+           re_set_syntax (REG_SYNTAX_EMACS | REG_IGNORE_CASE);
+           s = re_compile_pattern ("x", 1, &regex);
+           if (s)
+             exit (1);
+
+           if (re_search (&regex, "WXY", 3, 0, 3, &regs) < 0)
+             exit (1);
+
+           /* REG_STARTEND was added to glibc on 2004-01-15.
+              Reject older versions.  */
+           if (! REG_STARTEND)
+             exit (1);
+
+           /* Reject hosts whose regoff_t values are too narrow.
+              These include glibc 2.3.5 on hosts with 64-bit off_t
+              and 32-bit int, and Solaris 10 on hosts with 32-bit int
+              and _FILE_OFFSET_BITS=64.  */
+           if (sizeof (regoff_t) < sizeof (off_t))
+             exit (1);
+
+           exit (0);]])],
+       [gl_cv_func_re_compile_pattern_broken=no],
+       [gl_cv_func_re_compile_pattern_broken=yes],
+       dnl When crosscompiling, assume it is broken.
+       [gl_cv_func_re_compile_pattern_broken=yes])])
+    ac_use_included_regex=$gl_cv_func_re_compile_pattern_broken
+    ;;
+  *) AC_MSG_ERROR([Invalid value for --with-included-regex: 
$with_included_regex])
+    ;;
+  esac
+
+  if test $ac_use_included_regex = yes; then
+    AC_LIBOBJ([regex])
+    gl_PREREQ_REGEX
+  fi
+])
 
 # Prerequisites of lib/regex.c and lib/regex_internal.c.
 AC_DEFUN([gl_PREREQ_REGEX],
 [
+  AC_REQUIRE([AC_GNU_SOURCE])
   AC_REQUIRE([gl_C_RESTRICT])
   AC_REQUIRE([AM_LANGINFO_CODESET])
   AC_CHECK_HEADERS_ONCE([locale.h wchar.h wctype.h])




reply via email to

[Prev in Thread] Current Thread [Next in Thread]