summaryrefslogtreecommitdiff
path: root/src/regex-emacs.c
diff options
context:
space:
mode:
authorPaul Eggert <eggert@cs.ucla.edu>2020-08-26 00:07:08 -0700
committerPaul Eggert <eggert@cs.ucla.edu>2020-08-26 00:20:31 -0700
commit768bea30cbc0a5e7851fa60ad1ea7ec14cf2cdd8 (patch)
treef21a0af8dddb6376f460593c85b31628afb320ba /src/regex-emacs.c
parent438975bbaa25b7de74993e7928c45cf5779442b8 (diff)
downloademacs-768bea30cbc0a5e7851fa60ad1ea7ec14cf2cdd8.tar.gz
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].
Diffstat (limited to 'src/regex-emacs.c')
-rw-r--r--src/regex-emacs.c26
1 files changed, 13 insertions, 13 deletions
diff --git a/src/regex-emacs.c b/src/regex-emacs.c
index 954a193371a..da73f58f503 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. */