]> git.ipfire.org Git - thirdparty/gcc.git/blobdiff - libiberty/regex.c
libbacktrace: add DLLS as they are loaded
[thirdparty/gcc.git] / libiberty / regex.c
index 779998278b9dbcc03b535816e4df50677d560766..67747e6ce5a1ccf41f65cf3bc9a5782161678bdf 100644 (file)
@@ -2,7 +2,8 @@
    version 0.12.
    (Implements POSIX draft P1003.2/D11.2, except for some of the
    internationalization features.)
-   Copyright (C) 1993-1999, 2000, 2001, 2002 Free Software Foundation, Inc.
+
+   Copyright (C) 1993-2024 Free Software Foundation, Inc.
    This file is part of the GNU C Library.
 
    The GNU C Library is free software; you can redistribute it and/or
@@ -17,8 +18,8 @@
 
    You should have received a copy of the GNU Lesser General Public
    License along with the GNU C Library; if not, write to the Free
-   Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
-   02111-1307 USA.  */
+   Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+   02110-1301 USA.  */
 
 /* This file has been modified for usage in libiberty.  It includes "xregex.h"
    instead of <regex.h>.  The "xregex.h" header file renames all external
   #pragma alloca
 #endif
 
+#if __GNUC__ >= 12
+#  pragma GCC diagnostic ignored "-Wuse-after-free"
+#endif
+
 #undef _GNU_SOURCE
 #define _GNU_SOURCE
 
 
 # if defined STDC_HEADERS && !defined emacs
 #  include <stddef.h>
+#  define PTR_INT_TYPE ptrdiff_t
 # else
 /* We need this for `regex.h', and perhaps for the Emacs include files.  */
 #  include <sys/types.h>
+#  define PTR_INT_TYPE long
 # endif
 
 # define WIDE_CHAR_SUPPORT (HAVE_WCTYPE_H && HAVE_WCHAR_H && HAVE_BTOWC)
@@ -147,7 +154,7 @@ char *realloc ();
 #    include <string.h>
 #    ifndef bzero
 #     ifndef _LIBC
-#      define bzero(s, n)      (memset (s, '\0', n), (s))
+#      define bzero(s, n)      ((void) memset (s, '\0', n))
 #     else
 #      define bzero(s, n)      __bzero (s, n)
 #     endif
@@ -681,7 +688,7 @@ typedef enum
 #  define EXTRACT_NUMBER(destination, source)                          \
   do {                                                                 \
     (destination) = *(source) & 0377;                                  \
-    (destination) += SIGN_EXTEND_CHAR (*((source) + 1)) << 8;          \
+    (destination) += ((unsigned) SIGN_EXTEND_CHAR (*((source) + 1))) << 8; \
   } while (0)
 # endif
 
@@ -1918,7 +1925,7 @@ static reg_errcode_t byte_compile_range (unsigned int range_start,
    ? (char) translate[(unsigned char) (d)] : (d))
 # else /* BYTE */
 #   define TRANSLATE(d) \
-  (translate ? (char) translate[(unsigned char) (d)] : (d))
+  (translate ? (char) translate[(unsigned char) (d)] : (char) (d))
 #  endif /* WCHAR */
 # endif
 
@@ -2043,7 +2050,7 @@ static reg_errcode_t byte_compile_range (unsigned int range_start,
     /* How many characters the new buffer can have?  */                        \
     wchar_count = bufp->allocated / sizeof(UCHAR_T);                   \
     if (wchar_count == 0) wchar_count = 1;                             \
-    /* Truncate the buffer to CHAR_T align.  */                        \
+    /* Truncate the buffer to CHAR_T align.  */                                \
     bufp->allocated = wchar_count * sizeof(UCHAR_T);                   \
     RETALLOC (COMPILED_BUFFER_VAR, wchar_count, UCHAR_T);              \
     bufp->buffer = (char*)COMPILED_BUFFER_VAR;                         \
@@ -2052,7 +2059,7 @@ static reg_errcode_t byte_compile_range (unsigned int range_start,
     /* If the buffer moved, move all the pointers into it.  */         \
     if (old_buffer != COMPILED_BUFFER_VAR)                             \
       {                                                                        \
-       int incr = COMPILED_BUFFER_VAR - old_buffer;                    \
+       PTR_INT_TYPE incr = COMPILED_BUFFER_VAR - old_buffer;           \
        MOVE_BUFFER_POINTER (b);                                        \
        MOVE_BUFFER_POINTER (begalt);                                   \
        if (fixup_alt_jump)                                             \
@@ -2080,7 +2087,7 @@ static reg_errcode_t byte_compile_range (unsigned int range_start,
     /* If the buffer moved, move all the pointers into it.  */         \
     if (old_buffer != COMPILED_BUFFER_VAR)                             \
       {                                                                        \
-       int incr = COMPILED_BUFFER_VAR - old_buffer;                    \
+       PTR_INT_TYPE incr = COMPILED_BUFFER_VAR - old_buffer;           \
        MOVE_BUFFER_POINTER (b);                                        \
        MOVE_BUFFER_POINTER (begalt);                                   \
        if (fixup_alt_jump)                                             \
@@ -2489,6 +2496,7 @@ PREFIX(regex_compile) (const char *ARG_PREFIX(pattern),
           if ((syntax & RE_BK_PLUS_QM)
               || (syntax & RE_LIMITED_OPS))
             goto normal_char;
+         /* Fall through.  */
         handle_plus:
         case '*':
           /* If there is no previous pattern... */
@@ -3392,7 +3400,7 @@ PREFIX(regex_compile) (const char *ARG_PREFIX(pattern),
                               class.  */
                            PATFETCH (c);
 
-                           /* Now we have to go throught the whole table
+                           /* Now we have to go through the whole table
                               and find all characters which have the same
                               first level weight.
 
@@ -4968,7 +4976,7 @@ weak_alias (__re_search_2, re_search_2)
 #ifdef MATCH_MAY_ALLOCATE
 # define FREE_VAR(var) if (var) REGEX_FREE (var); var = NULL
 #else
-# define FREE_VAR(var) if (var) free (var); var = NULL
+# define FREE_VAR(var) free (var); var = NULL
 #endif
 
 #ifdef WCHAR
@@ -5589,7 +5597,7 @@ byte_re_match_2_internal (struct re_pattern_buffer *bufp,
      to resume scanning the pattern; the second one is where to resume
      scanning the strings.  If the latter is zero, the failure point is
      a ``dummy''; if a failure happens and the failure point is a dummy,
-     it gets discarded and the next next one is tried.  */
+     it gets discarded and the next one is tried.  */
 #ifdef MATCH_MAY_ALLOCATE /* otherwise, this is global.  */
   PREFIX(fail_stack_type) fail_stack;
 #endif
@@ -5908,11 +5916,14 @@ byte_re_match_2_internal (struct re_pattern_buffer *bufp,
            {
              /* 1 if this match ends in the same string (string1 or string2)
                 as the best previous match.  */
-             boolean same_str_p = (FIRST_STRING_P (match_end)
-                                   == MATCHING_IN_FIRST_STRING);
+             boolean same_str_p;
+
              /* 1 if this match is the best seen so far.  */
              boolean best_match_p;
 
+              same_str_p = (FIRST_STRING_P (match_end)
+                            == MATCHING_IN_FIRST_STRING);
+
              /* AIX compiler got confused when this was combined
                 with the previous declaration.  */
              if (same_str_p)
@@ -6172,9 +6183,9 @@ byte_re_match_2_internal (struct re_pattern_buffer *bufp,
            uint32_t nrules;
 # endif /* _LIBC */
 #endif /* WCHAR */
-           boolean not = (re_opcode_t) *(p - 1) == charset_not;
+           boolean negate = (re_opcode_t) *(p - 1) == charset_not;
 
-            DEBUG_PRINT2 ("EXECUTING charset%s.\n", not ? "_not" : "");
+            DEBUG_PRINT2 ("EXECUTING charset%s.\n", negate ? "_not" : "");
            PREFETCH ();
            c = TRANSLATE (*d); /* The character to match.  */
 #ifdef WCHAR
@@ -6541,20 +6552,20 @@ byte_re_match_2_internal (struct re_pattern_buffer *bufp,
              if (c == *workp)
                goto char_set_matched;
 
-           not = !not;
+           negate = !negate;
 
          char_set_matched:
-           if (not) goto fail;
+           if (negate) goto fail;
 #else
             /* Cast to `unsigned' instead of `unsigned char' in case the
                bit list is a full 32 bytes long.  */
            if (c < (unsigned) (*p * BYTEWIDTH)
                && p[1 + c / BYTEWIDTH] & (1 << (c % BYTEWIDTH)))
-             not = !not;
+             negate = !negate;
 
            p += 1 + *p;
 
-           if (!not) goto fail;
+           if (!negate) goto fail;
 #undef WORK_BUFFER_SIZE
 #endif /* WCHAR */
            SET_REGS_MATCHED ();
@@ -6690,6 +6701,7 @@ byte_re_match_2_internal (struct re_pattern_buffer *bufp,
                 {
                   case jump_n:
                    is_a_jump_n = true;
+                   /* Fall through.  */
                   case pop_failure_jump:
                  case maybe_pop_jump:
                  case jump:
@@ -7044,15 +7056,15 @@ byte_re_match_2_internal (struct re_pattern_buffer *bufp,
                else if ((re_opcode_t) p1[3] == charset
                         || (re_opcode_t) p1[3] == charset_not)
                  {
-                   int not = (re_opcode_t) p1[3] == charset_not;
+                   int negate = (re_opcode_t) p1[3] == charset_not;
 
                    if (c < (unsigned) (p1[4] * BYTEWIDTH)
                        && p1[5 + c / BYTEWIDTH] & (1 << (c % BYTEWIDTH)))
-                     not = !not;
+                     negate = !negate;
 
-                    /* `not' is equal to 1 if c would match, which means
+                    /* `negate' is equal to 1 if c would match, which means
                         that we can't change to pop_failure_jump.  */
-                   if (!not)
+                   if (!negate)
                       {
                        p[-3] = (unsigned char) pop_failure_jump;
                         DEBUG_PRINT1 ("  No match => pop_failure_jump.\n");
@@ -7118,7 +7130,7 @@ byte_re_match_2_internal (struct re_pattern_buffer *bufp,
               DEBUG_PRINT1 ("  Match => jump.\n");
              goto unconditional_jump;
            }
-        /* Note fall through.  */
+        /* Fall through.  */
 
 
        /* The end of a simple repeat has a pop_failure_jump back to
@@ -7135,15 +7147,15 @@ byte_re_match_2_internal (struct re_pattern_buffer *bufp,
                register from the stack, since lowest will == highest in
                `pop_failure_point'.  */
             active_reg_t dummy_low_reg, dummy_high_reg;
-            UCHAR_T *pdummy = NULL;
-            const CHAR_T *sdummy = NULL;
+            UCHAR_T *pdummy ATTRIBUTE_UNUSED = NULL;
+            const CHAR_T *sdummy ATTRIBUTE_UNUSED = NULL;
 
             DEBUG_PRINT1 ("EXECUTING pop_failure_jump.\n");
             POP_FAILURE_POINT (sdummy, pdummy,
                                dummy_low_reg, dummy_high_reg,
                                reg_dummy, reg_dummy, reg_info_dummy);
           }
-         /* Note fall through.  */
+         /* Fall through.  */
 
        unconditional_jump:
 #ifdef _LIBC
@@ -7446,6 +7458,7 @@ byte_re_match_2_internal (struct re_pattern_buffer *bufp,
                 {
                 case jump_n:
                   is_a_jump_n = true;
+                 /* Fall through.  */
                 case maybe_pop_jump:
                 case pop_failure_jump:
                 case jump:
@@ -7711,6 +7724,7 @@ PREFIX(common_op_match_null_string_p) (UCHAR_T **p, UCHAR_T *end,
 
     case set_number_at:
       p1 += 2 * OFFSET_ADDRESS_SIZE;
+      return false;
 
     default:
       /* All other opcodes mean we cannot match the empty string.  */
@@ -7814,7 +7828,7 @@ re_comp (const char *s)
   if (!s)
     {
       if (!re_comp_buf.buffer)
-       return gettext ("No previous regular expression");
+       return (char *) gettext ("No previous regular expression");
       return 0;
     }
 
@@ -7921,7 +7935,7 @@ regcomp (regex_t *preg, const char *pattern, int cflags)
 
   if (cflags & REG_ICASE)
     {
-      unsigned i;
+      int i;
 
       preg->translate
        = (RE_TRANSLATE_TYPE) malloc (CHAR_SET_SIZE
@@ -7931,7 +7945,7 @@ regcomp (regex_t *preg, const char *pattern, int cflags)
 
       /* Map uppercase characters to corresponding lowercase ones.  */
       for (i = 0; i < CHAR_SET_SIZE; i++)
-        preg->translate[i] = ISUPPER (i) ? TOLOWER (i) : (int) i;
+        preg->translate[i] = ISUPPER (i) ? TOLOWER (i) : i;
     }
   else
     preg->translate = NULL;
@@ -8086,12 +8100,12 @@ regerror (int errcode, const regex_t *preg ATTRIBUTE_UNUSED,
 #if defined HAVE_MEMPCPY || defined _LIBC
          *((char *) mempcpy (errbuf, msg, errbuf_size - 1)) = '\0';
 #else
-          memcpy (errbuf, msg, errbuf_size - 1);
+          (void) memcpy (errbuf, msg, errbuf_size - 1);
           errbuf[errbuf_size - 1] = 0;
 #endif
         }
       else
-        memcpy (errbuf, msg, msg_size);
+        (void) memcpy (errbuf, msg, msg_size);
     }
 
   return msg_size;
@@ -8106,20 +8120,17 @@ weak_alias (__regerror, regerror)
 void
 regfree (regex_t *preg)
 {
-  if (preg->buffer != NULL)
-    free (preg->buffer);
+  free (preg->buffer);
   preg->buffer = NULL;
 
   preg->allocated = 0;
   preg->used = 0;
 
-  if (preg->fastmap != NULL)
-    free (preg->fastmap);
+  free (preg->fastmap);
   preg->fastmap = NULL;
   preg->fastmap_accurate = 0;
 
-  if (preg->translate != NULL)
-    free (preg->translate);
+  free (preg->translate);
   preg->translate = NULL;
 }
 #ifdef _LIBC