emacs-diffs
[Top][All Lists]
Advanced

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

master 768bea3 2/6: regex-emacs omit allocation of 3 slots


From: Paul Eggert
Subject: master 768bea3 2/6: regex-emacs omit allocation of 3 slots
Date: Wed, 26 Aug 2020 03:20:39 -0400 (EDT)

branch: master
commit 768bea30cbc0a5e7851fa60ad1ea7ec14cf2cdd8
Author: Paul Eggert <eggert@cs.ucla.edu>
Commit: Paul Eggert <eggert@cs.ucla.edu>

    regex-emacs omit allocation of 3 slots
    
    * src/regex-emacs.c (re_match_2_internal): Avoid
    unnecessary allocation of REGEND[0], BEST_REGSTART[0],
    BEST_REGEND[0].
---
 src/regex-emacs.c | 26 +++++++++++++-------------
 1 file changed, 13 insertions(+), 13 deletions(-)

diff --git a/src/regex-emacs.c b/src/regex-emacs.c
index 954a193..da73f58 100644
--- a/src/regex-emacs.c
+++ b/src/regex-emacs.c
@@ -3923,8 +3923,7 @@ re_match_2_internal (struct re_pattern_buffer *bufp,
      attempt) by a subexpression part of the pattern, that is, the
      regnum-th regstart pointer points to where in the pattern we began
      matching and the regnum-th regend points to right after where we
-     stopped matching the regnum-th subexpression.  (The zeroth register
-     keeps track of what the whole pattern matches.)  */
+     stopped matching the regnum-th subexpression.  */
   re_char **regstart UNINIT, **regend UNINIT;
 
   /* The following record the register info as found in the above
@@ -3973,20 +3972,21 @@ re_match_2_internal (struct re_pattern_buffer *bufp,
   /* Do not bother to initialize all the register variables if there are
      no groups in the pattern, as it takes a fair amount of time.  If
      there are groups, we include space for register 0 (the whole
-     pattern), even though we never use it, since it simplifies the
-     array indexing.  We should fix this.  */
-  if (bufp->re_nsub)
+     pattern) in REGSTART[0], even though we never use it, to avoid
+     the undefined behavior of subtracting 1 from REGSTART.  */
+  ptrdiff_t re_nsub = num_regs - 1;
+  if (0 < re_nsub)
     {
-      regstart = SAFE_ALLOCA (num_regs * 4 * sizeof *regstart);
+      regstart = SAFE_ALLOCA ((re_nsub * 4 + 1) * sizeof *regstart);
       regend = regstart + num_regs;
-      best_regstart = regend + num_regs;
-      best_regend = best_regstart + num_regs;
-    }
+      best_regstart = regend + re_nsub;
+      best_regend = best_regstart + re_nsub;
 
-  /* Initialize subexpression text positions to -1 to mark ones that no
-     start_memory/stop_memory has been seen for.  */
-  for (ptrdiff_t reg = 1; reg < num_regs; reg++)
-    regstart[reg] = regend[reg] = NULL;
+      /* Initialize subexpression text positions to unset, to mark ones
+        that no start_memory/stop_memory has been seen for.  */
+      for (re_char **apos = regstart + 1; apos < best_regstart + 1; apos++)
+       *apos = NULL;
+    }
 
   /* We move 'string1' into 'string2' if the latter's empty -- but not if
      'string1' is null.  */



reply via email to

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