Skip to content

Commit 2dd6733

Browse files
committed
Minor fixes to improve regex debugging code.
When REG_DEBUG is defined, ensure that an un-filled "struct cnfa" is all-zeroes, not just that it has nstates == 0. This is mainly so that looking at "struct subre" structs in gdb doesn't distract one with a lot of garbage fields during regex compilation. Adjust some places that print debug output to have suitable fflush calls afterwards. In passing, correct an erroneous ancient comment: the concatenation subre-s created by parsebranch() have op == '.' not ','. Noted while fooling around with some regex performance improvements.
1 parent c7ecd6a commit 2dd6733

File tree

3 files changed

+15
-4
lines changed

3 files changed

+15
-4
lines changed

src/backend/regex/regc_nfa.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2951,11 +2951,11 @@ carc_cmp(const void *a, const void *b)
29512951
static void
29522952
freecnfa(struct cnfa *cnfa)
29532953
{
2954-
assert(cnfa->nstates != 0); /* not empty already */
2955-
cnfa->nstates = 0;
2954+
assert(!NULLCNFA(*cnfa)); /* not empty already */
29562955
FREE(cnfa->stflags);
29572956
FREE(cnfa->states);
29582957
FREE(cnfa->arcs);
2958+
ZAPCNFA(*cnfa);
29592959
}
29602960

29612961
/*
@@ -3012,13 +3012,13 @@ dumpstate(struct state *s,
30123012
fprintf(f, "\tno out arcs\n");
30133013
else
30143014
dumparcs(s, f);
3015-
fflush(f);
30163015
for (a = s->ins; a != NULL; a = a->inchain)
30173016
{
30183017
if (a->to != s)
30193018
fprintf(f, "\tlink from %d to %d on %d's in-chain\n",
30203019
a->from->no, a->to->no, s->no);
30213020
}
3021+
fflush(f);
30223022
}
30233023

30243024
/*

src/backend/regex/regcomp.c

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -479,7 +479,10 @@ pg_regcomp(regex_t *re,
479479

480480
#ifdef REG_DEBUG
481481
if (flags & REG_DUMP)
482+
{
482483
dump(re, stdout);
484+
fflush(stdout);
485+
}
483486
#endif
484487

485488
assert(v->err == 0);
@@ -721,7 +724,7 @@ parse(struct vars *v,
721724
*
722725
* This mostly manages concatenation, working closely with parseqatom().
723726
* Concatenated things are bundled up as much as possible, with separate
724-
* ',' nodes introduced only when necessary due to substructure.
727+
* '.' nodes introduced only when necessary due to substructure.
725728
*/
726729
static struct subre *
727730
parsebranch(struct vars *v,

src/include/regex/regguts.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -368,7 +368,15 @@ struct cnfa
368368
struct carc *arcs; /* the area for the lists */
369369
};
370370

371+
/*
372+
* When debugging, it's helpful if an un-filled CNFA is all-zeroes.
373+
* In production, though, we only require nstates to be zero.
374+
*/
375+
#ifdef REG_DEBUG
376+
#define ZAPCNFA(cnfa) memset(&(cnfa), 0, sizeof(cnfa))
377+
#else
371378
#define ZAPCNFA(cnfa) ((cnfa).nstates = 0)
379+
#endif
372380
#define NULLCNFA(cnfa) ((cnfa).nstates == 0)
373381

374382
/*

0 commit comments

Comments
 (0)