]> code.delx.au - gnu-emacs/blobdiff - src/regex.c
Merge remote-tracking branch 'origin/emacs-24'
[gnu-emacs] / src / regex.c
index 06a9db2c55c6b16346a63823149c343e9955b188..766ad26e709dbe43ad16126d219b1f583640a748 100644 (file)
@@ -2,7 +2,7 @@
    0.12.  (Implements POSIX draft P1003.2/D11.2, except for some of the
    internationalization features.)
 
-   Copyright (C) 1993-2013 Free Software Foundation, Inc.
+   Copyright (C) 1993-2014 Free Software Foundation, Inc.
 
    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
@@ -43,7 +43,7 @@
 # endif
 #endif
 
-#if 4 < __GNUC__ + (5 <= __GNUC_MINOR__) && ! defined __clang__
+#if 4 < __GNUC__ + (6 <= __GNUC_MINOR__) && ! defined __clang__
 # pragma GCC diagnostic ignored "-Wunused-but-set-variable"
 #endif
 
 # include "character.h"
 # include "buffer.h"
 
-/* Make syntax table lookup grant data in gl_state.  */
-# define SYNTAX_ENTRY_VIA_PROPERTY
-
 # include "syntax.h"
 # include "category.h"
 
+/* Make syntax table lookup grant data in gl_state.  */
+# define SYNTAX(c) syntax_property (c, 1)
+
 # ifdef malloc
 #  undef malloc
 # endif
@@ -257,15 +257,10 @@ xrealloc (void *block, size_t size)
 enum syntaxcode { Swhitespace = 0, Sword = 1, Ssymbol = 2 };
 
 /* Dummy macros for non-Emacs environments.  */
-# define CHAR_CHARSET(c) 0
-# define CHARSET_LEADING_CODE_BASE(c) 0
 # define MAX_MULTIBYTE_LENGTH 1
 # define RE_MULTIBYTE_P(x) 0
 # define RE_TARGET_MULTIBYTE_P(x) 0
 # define WORD_BOUNDARY_P(c1, c2) (0)
-# define CHAR_HEAD_P(p) (1)
-# define SINGLE_BYTE_CHAR_P(c) (1)
-# define SAME_CHARSET_P(c1, c2) (1)
 # define BYTES_BY_CHAR_HEAD(p) (1)
 # define PREV_CHAR_BOUNDARY(p, limit) ((p)--)
 # define STRING_CHAR(p) (*(p))
@@ -279,8 +274,6 @@ enum syntaxcode { Swhitespace = 0, Sword = 1, Ssymbol = 2 };
   (c = ((p) == (str2) ? *((end1) - 1) : *((p) - 1)))
 # define GET_CHAR_AFTER(c, p, len)     \
   (c = *p, len = 1)
-# define MAKE_CHAR(charset, c1, c2) (c1)
-# define BYTE8_TO_CHAR(c) (c)
 # define CHAR_BYTE8_P(c) (0)
 # define CHAR_LEADING_CODE(c) (c)
 
@@ -464,11 +457,17 @@ init_syntax_once (void)
 
 # endif /* not alloca */
 
-# define REGEX_ALLOCATE alloca
+# ifdef emacs
+#  define REGEX_USE_SAFE_ALLOCA USE_SAFE_ALLOCA
+#  define REGEX_SAFE_FREE() SAFE_FREE ()
+#  define REGEX_ALLOCATE SAFE_ALLOCA
+# else
+#  define REGEX_ALLOCATE alloca
+# endif
 
 /* Assumes a `char *destination' variable.  */
 # define REGEX_REALLOCATE(source, osize, nsize)                                \
-  (destination = alloca (nsize),                                       \
+  (destination = REGEX_ALLOCATE (nsize),                               \
    memcpy (destination, source, osize))
 
 /* No need to do anything to free, after alloca.  */
@@ -476,6 +475,11 @@ init_syntax_once (void)
 
 #endif /* not REGEX_MALLOC */
 
+#ifndef REGEX_USE_SAFE_ALLOCA
+# define REGEX_USE_SAFE_ALLOCA ((void) 0)
+# define REGEX_SAFE_FREE() ((void) 0)
+#endif
+
 /* Define how to allocate the failure stack.  */
 
 #if defined REL_ALLOC && defined REGEX_MALLOC
@@ -489,22 +493,10 @@ init_syntax_once (void)
 
 #else /* not using relocating allocator */
 
-# ifdef REGEX_MALLOC
-
-#  define REGEX_ALLOCATE_STACK malloc
-#  define REGEX_REALLOCATE_STACK(source, osize, nsize) realloc (source, nsize)
-#  define REGEX_FREE_STACK free
-
-# else /* not REGEX_MALLOC */
+# define REGEX_ALLOCATE_STACK(size) REGEX_ALLOCATE (size)
+# define REGEX_REALLOCATE_STACK(source, o, n) REGEX_REALLOCATE (source, o, n)
+# define REGEX_FREE_STACK(ptr) REGEX_FREE (ptr)
 
-#  define REGEX_ALLOCATE_STACK alloca
-
-#  define REGEX_REALLOCATE_STACK(source, osize, nsize)                 \
-   REGEX_REALLOCATE (source, osize, nsize)
-/* No need to explicitly free anything.  */
-#  define REGEX_FREE_STACK(arg) ((void)0)
-
-# endif /* not REGEX_MALLOC */
 #endif /* not using relocating allocator */
 
 
@@ -523,10 +515,12 @@ init_syntax_once (void)
 
 #define STREQ(s1, s2) ((strcmp (s1, s2) == 0))
 
-#undef MAX
-#undef MIN
-#define MAX(a, b) ((a) > (b) ? (a) : (b))
-#define MIN(a, b) ((a) < (b) ? (a) : (b))
+#ifndef emacs  
+# undef max
+# undef min
+# define max(a, b) ((a) > (b) ? (a) : (b))
+# define min(a, b) ((a) < (b) ? (a) : (b))
+#endif
 
 /* Type of source-pattern and string chars.  */
 #ifdef _MSC_VER
@@ -720,7 +714,8 @@ typedef enum
 static int
 extract_number (re_char *source)
 {
-  return (SIGN_EXTEND_CHAR (source[1]) << 8) + source[0];
+  unsigned leading_byte = SIGN_EXTEND_CHAR (source[1]);
+  return (leading_byte << 8) + source[0];
 }
 
 /* Same as EXTRACT_NUMBER, except increment SOURCE to after the number.
@@ -775,10 +770,12 @@ extract_number_and_incr (re_char **source)
    and the 2 bytes of flags at the start of the range table.  */
 #define CHARSET_RANGE_TABLE(p) (&(p)[4 + CHARSET_BITMAP_SIZE (p)])
 
+#ifdef emacs
 /* Extract the bit flags that start a range table.  */
 #define CHARSET_RANGE_TABLE_BITS(p)            \
   ((p)[2 + CHARSET_BITMAP_SIZE (p)]            \
    + (p)[3 + CHARSET_BITMAP_SIZE (p)] * 0x100)
+#endif
 
 /* Return the address of end of RANGE_TABLE.  COUNT is number of
    ranges (which is a pair of (start, end)) in the RANGE_TABLE.  `* 2'
@@ -1194,12 +1191,7 @@ print_double_string (re_char *where, re_char *string1, ssize_t size1,
 # define assert(e)
 
 # define DEBUG_STATEMENT(e)
-# if __STDC_VERSION__ < 199901L
-#  define DEBUG_COMPILES_ARGUMENTS
-#  define DEBUG_PRINT /* 'DEBUG_PRINT (x, y)' discards X and Y.  */ (void)
-# else
-#  define DEBUG_PRINT(...)
-# endif
+# define DEBUG_PRINT(...)
 # define DEBUG_PRINT_COMPILED_PATTERN(p, s, e)
 # define DEBUG_PRINT_DOUBLE_STRING(w, s1, sz1, s2, sz2)
 
@@ -1238,12 +1230,12 @@ re_set_syntax (reg_syntax_t syntax)
 WEAK_ALIAS (__re_set_syntax, re_set_syntax)
 
 /* Regexp to use to replace spaces, or NULL meaning don't.  */
-static re_char *whitespace_regexp;
+static const_re_char *whitespace_regexp;
 
 void
 re_set_whitespace_regexp (const char *regexp)
 {
-  whitespace_regexp = (re_char *) regexp;
+  whitespace_regexp = (const_re_char *) regexp;
 }
 WEAK_ALIAS (__re_set_syntax, re_set_syntax)
 \f
@@ -1404,14 +1396,14 @@ typedef struct
    : ((fail_stack).stack                                               \
       = REGEX_REALLOCATE_STACK ((fail_stack).stack,                    \
          (fail_stack).size * sizeof (fail_stack_elt_t),                \
-         MIN (re_max_failures * TYPICAL_FAILURE_SIZE,                  \
+         min (re_max_failures * TYPICAL_FAILURE_SIZE,                  \
               ((fail_stack).size * sizeof (fail_stack_elt_t)           \
                * FAIL_STACK_GROWTH_FACTOR))),                          \
                                                                        \
       (fail_stack).stack == NULL                                       \
       ? 0                                                              \
       : ((fail_stack).size                                             \
-        = (MIN (re_max_failures * TYPICAL_FAILURE_SIZE,                \
+        = (min (re_max_failures * TYPICAL_FAILURE_SIZE,                \
                 ((fail_stack).size * sizeof (fail_stack_elt_t)         \
                  * FAIL_STACK_GROWTH_FACTOR))                          \
            / sizeof (fail_stack_elt_t)),                               \
@@ -1830,6 +1822,8 @@ struct range_table_work_area
   int bits;                    /* flag to record character classes */
 };
 
+#ifdef emacs
+
 /* Make sure that WORK_AREA can hold more N multibyte characters.
    This is used only in set_image_of_range and set_image_of_range_1.
    It expects WORK_AREA to be a pointer.
@@ -1848,15 +1842,6 @@ struct range_table_work_area
 #define SET_RANGE_TABLE_WORK_AREA_BIT(work_area, bit)          \
   (work_area).bits |= (bit)
 
-/* Bits used to implement the multibyte-part of the various character classes
-   such as [:alnum:] in a charset's range table.  */
-#define BIT_WORD       0x1
-#define BIT_LOWER      0x2
-#define BIT_PUNCT      0x4
-#define BIT_SPACE      0x8
-#define BIT_UPPER      0x10
-#define BIT_MULTIBYTE  0x20
-
 /* Set a range (RANGE_START, RANGE_END) to WORK_AREA.  */
 #define SET_RANGE_TABLE_WORK_AREA(work_area, range_start, range_end)   \
   do {                                                                 \
@@ -1865,6 +1850,8 @@ struct range_table_work_area
     (work_area).table[(work_area).used++] = (range_end);               \
   } while (0)
 
+#endif /* emacs */
+
 /* Free allocated memory for WORK_AREA.  */
 #define FREE_RANGE_TABLE_WORK_AREA(work_area)  \
   do {                                         \
@@ -1876,6 +1863,15 @@ struct range_table_work_area
 #define RANGE_TABLE_WORK_USED(work_area) ((work_area).used)
 #define RANGE_TABLE_WORK_BITS(work_area) ((work_area).bits)
 #define RANGE_TABLE_WORK_ELT(work_area, i) ((work_area).table[i])
+
+/* Bits used to implement the multibyte-part of the various character classes
+   such as [:alnum:] in a charset's range table.  */
+#define BIT_WORD       0x1
+#define BIT_LOWER      0x2
+#define BIT_PUNCT      0x4
+#define BIT_SPACE      0x8
+#define BIT_UPPER      0x10
+#define BIT_MULTIBYTE  0x20
 \f
 
 /* Set the bit for character C in a list.  */
@@ -1990,7 +1986,7 @@ struct range_table_work_area
 #endif /* emacs */
 
 /* Get the next unsigned number in the uncompiled pattern.  */
-#define GET_UNSIGNED_NUMBER(num)                                       \
+#define GET_INTERVAL_COUNT(num)                                        \
   do {                                                                 \
     if (p == pend)                                                     \
       FREE_STACK_RETURN (REG_EBRACE);                                  \
@@ -1999,13 +1995,11 @@ struct range_table_work_area
        PATFETCH (c);                                                   \
        while ('0' <= c && c <= '9')                                    \
          {                                                             \
-           int prev;                                                   \
            if (num < 0)                                                \
              num = 0;                                                  \
-           prev = num;                                                 \
-           num = num * 10 + c - '0';                                   \
-           if (num / 10 != prev)                                       \
+           if (RE_DUP_MAX / 10 - (RE_DUP_MAX % 10 < c - '0') < num)    \
              FREE_STACK_RETURN (REG_BADBR);                            \
+           num = num * 10 + c - '0';                                   \
            if (p == pend)                                              \
              FREE_STACK_RETURN (REG_EBRACE);                           \
            PATFETCH (c);                                               \
@@ -2317,8 +2311,8 @@ set_image_of_range (struct range_table_work_area *work_area,
                cmin = c, cmax = c;
              else
                {
-                 cmin = MIN (cmin, c);
-                 cmax = MAX (cmax, c);
+                 cmin = min (cmin, c);
+                 cmax = max (cmax, c);
                }
            }
        }
@@ -2997,7 +2991,7 @@ regex_compile (const_re_char *pattern, size_t size, reg_syntax_t syntax,
 #else  /* emacs */
                    if (c < 128)
                      {
-                       ch = MIN (127, c1);
+                       ch = min (127, c1);
                        SETUP_ASCII_RANGE (range_table_work, c, ch);
                        c = ch + 1;
                        if (CHAR_BYTE8_P (c1))
@@ -3311,16 +3305,16 @@ regex_compile (const_re_char *pattern, size_t size, reg_syntax_t syntax,
 
                beg_interval = p;
 
-               GET_UNSIGNED_NUMBER (lower_bound);
+               GET_INTERVAL_COUNT (lower_bound);
 
                if (c == ',')
-                 GET_UNSIGNED_NUMBER (upper_bound);
+                 GET_INTERVAL_COUNT (upper_bound);
                else
                  /* Interval such as `{1}' => match exactly once. */
                  upper_bound = lower_bound;
 
-               if (lower_bound < 0 || upper_bound > RE_DUP_MAX
-                   || (upper_bound >= 0 && lower_bound > upper_bound))
+               if (lower_bound < 0
+                   || (0 <= upper_bound && upper_bound < lower_bound))
                  FREE_STACK_RETURN (REG_BADBR);
 
                if (!(syntax & RE_NO_BK_BRACES))
@@ -4349,8 +4343,7 @@ re_search_2 (struct re_pattern_buffer *bufp, const char *str1, size_t size1,
 
          if (range > 0)        /* Searching forwards.  */
            {
-             register int lim = 0;
-             ssize_t irange = range;
+             ssize_t irange = range, lim = 0;
 
              if (startpos < size1 && startpos + range >= size1)
                lim = range - (size1 - startpos);
@@ -4587,6 +4580,7 @@ static int bcmp_translate (re_char *s1, re_char *s2,
     FREE_VAR (regend);                                                 \
     FREE_VAR (best_regstart);                                          \
     FREE_VAR (best_regend);                                            \
+    REGEX_SAFE_FREE ();                                                        \
   } while (0)
 #else
 # define FREE_VARIABLES() ((void)0) /* Do nothing!  But inhibit gcc warning.  */
@@ -5026,6 +5020,8 @@ re_match_2_internal (struct re_pattern_buffer *bufp, const_re_char *string1,
 
   DEBUG_PRINT ("\n\nEntering re_match_2.\n");
 
+  REGEX_USE_SAFE_ALLOCA;
+
   INIT_FAIL_STACK ();
 
 #ifdef MATCH_MAY_ALLOCATE
@@ -5216,7 +5212,7 @@ re_match_2_internal (struct re_pattern_buffer *bufp, const_re_char *string1,
                { /* No.  So allocate them with malloc.  We need one
                     extra element beyond `num_regs' for the `-1' marker
                     GNU code uses.  */
-                 regs->num_regs = MAX (RE_NREGS, num_regs + 1);
+                 regs->num_regs = max (RE_NREGS, num_regs + 1);
                  regs->start = TALLOC (regs->num_regs, regoff_t);
                  regs->end = TALLOC (regs->num_regs, regoff_t);
                  if (regs->start == NULL || regs->end == NULL)
@@ -5260,7 +5256,7 @@ re_match_2_internal (struct re_pattern_buffer *bufp, const_re_char *string1,
 
              /* Go through the first `min (num_regs, regs->num_regs)'
                 registers, since that is all we initialized.  */
-             for (reg = 1; reg < MIN (num_regs, regs->num_regs); reg++)
+             for (reg = 1; reg < min (num_regs, regs->num_regs); reg++)
                {
                  if (REG_UNSET (regstart[reg]) || REG_UNSET (regend[reg]))
                    regs->start[reg] = regs->end[reg] = -1;
@@ -5908,7 +5904,7 @@ re_match_2_internal (struct re_pattern_buffer *bufp, const_re_char *string1,
            EXTRACT_NUMBER_AND_INCR (mcnt, p);
            /* Here, we discard `const', making re_match non-reentrant.  */
            p2 = (unsigned char*) p + mcnt;
-           /* Signedness doesn't matter since we only copy MCNT's bits .  */
+           /* Signedness doesn't matter since we only copy MCNT's bits.  */
            EXTRACT_NUMBER_AND_INCR (mcnt, p);
            DEBUG_PRINT ("  Setting %p to %d.\n", p2, mcnt);
            PUSH_NUMBER (p2, mcnt);