From 4327152a8da835b489f2314100b405920cc614ea Mon Sep 17 00:00:00 2001 From: Ilya Zakharevich Date: Sun, 7 Feb 1999 10:07:27 -0500 Subject: [PATCH] patch for change#2822, done right; add PERL_OBJECT stuff; regen headers Message-ID: <19990207150726.A571@monk.mps.ohio-state.edu> Subject: Re: fixing memory leaks in REx compilation p4raw-link: @2822 on //depot/perl: 34184a49c8a0771dbea73b3f0519126fd5b22c2f p4raw-id: //depot/perl@2826 --- embed.h | 1 + embed.pl | 1 + objXSUB.h | 2 ++ proto.h | 1 + regcomp.c | 14 +++++++++++++- regcomp.h | 8 ++++---- regexec.c | 6 +++--- 7 files changed, 25 insertions(+), 8 deletions(-) diff --git a/embed.h b/embed.h index ef311c6..78c28e2 100644 --- a/embed.h +++ b/embed.h @@ -1080,6 +1080,7 @@ #define ck_subr CPerlObj::Perl_ck_subr #define ck_svconst CPerlObj::Perl_ck_svconst #define ck_trunc CPerlObj::Perl_ck_trunc +#define clear_re CPerlObj::Perl_clear_re #define condpair_magic CPerlObj::Perl_condpair_magic #define convert CPerlObj::Perl_convert #define croak CPerlObj::Perl_croak diff --git a/embed.pl b/embed.pl index 46df6c3..3aabd9f 100755 --- a/embed.pl +++ b/embed.pl @@ -356,6 +356,7 @@ my @staticfuncs = qw( add_data re_croak2 regpposixcc + clear_re regmatch regrepeat regrepeat_hard diff --git a/objXSUB.h b/objXSUB.h index 9c8460f..71aa211 100644 --- a/objXSUB.h +++ b/objXSUB.h @@ -971,6 +971,8 @@ #define ck_svconst pPerl->Perl_ck_svconst #undef ck_trunc #define ck_trunc pPerl->Perl_ck_trunc +#undef clear_re +#define clear_re pPerl->Perl_clear_re #undef condpair_magic #define condpair_magic pPerl->Perl_condpair_magic #undef convert diff --git a/proto.h b/proto.h index e536733..93517d2 100644 --- a/proto.h +++ b/proto.h @@ -869,6 +869,7 @@ I32 study_chunk _((regnode **scanp, I32 *deltap, regnode *last, scan_data_t *dat I32 add_data _((I32 n, char *s)); void re_croak2 _((const char* pat1,const char* pat2,...)) __attribute__((noreturn)); char* regpposixcc _((I32 value)); +void clear_re _((void *r)); I32 regmatch _((regnode *prog)); I32 regrepeat _((regnode *p, I32 max)); I32 regrepeat_hard _((regnode *p, I32 max, I32 *lp)); diff --git a/regcomp.c b/regcomp.c index f78388b..91f9d7b 100644 --- a/regcomp.c +++ b/regcomp.c @@ -147,6 +147,7 @@ static char* regwhite _((char *, char *)); static char* nextchar _((void)); static void re_croak2 _((const char* pat1,const char* pat2,...)) __attribute__((noreturn)); static char* regpposixcc _((I32 value)); +static void clear_re _((void *r)); #endif /* Length of a variant. */ @@ -207,6 +208,12 @@ static scan_data_t zero_scan_data = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }; #define CHR_DIST(a,b) (UTF ? utf8_distance(a,b) : a - b) STATIC void +clear_re(void *r) +{ + ReREFCNT_dec((regexp *)r); +} + +STATIC void scan_commit(scan_data_t *data) { dTHR; @@ -868,7 +875,12 @@ pregcomp(char *exp, char *xend, PMOP *pm) r->prelen = xend - exp; r->precomp = PL_regprecomp; r->subbeg = r->subbase = NULL; - r->nparens = PL_regnpar - 1; /* set early to validate backrefs */ + r->nparens = PL_regnpar - 1; /* set early to validate backrefs */ + + r->substrs = 0; /* Useful during FAIL. */ + r->startp = 0; /* Useful during FAIL. */ + r->endp = 0; /* Useful during FAIL. */ + PL_regcomp_rx = r; /* Second pass: emit code. */ diff --git a/regcomp.h b/regcomp.h index 1a139c5..1538f8a 100644 --- a/regcomp.h +++ b/regcomp.h @@ -188,15 +188,15 @@ struct regnode_2 { #define FAIL(m) \ STMT_START { \ - /*if (!SIZE_ONLY) \ - ReREFCNT_dec(PL_regcomp_rx);*/ \ + if (!SIZE_ONLY) \ + SAVEDESTRUCTOR(clear_re,(void*)PL_regcomp_rx); \ croak ("/%.127s/: %s", PL_regprecomp,m); \ } STMT_END #define FAIL2(pat,m) \ STMT_START { \ - /*if (!SIZE_ONLY) \ - ReREFCNT_dec(PL_regcomp_rx);*/ \ + if (!SIZE_ONLY) \ + SAVEDESTRUCTOR(clear_re,(void*)PL_regcomp_rx); \ re_croak2("/%.127s/: ",pat,PL_regprecomp,m); \ } STMT_END diff --git a/regexec.c b/regexec.c index f7210a0..f53567e 100644 --- a/regexec.c +++ b/regexec.c @@ -329,7 +329,7 @@ regexec_flags(register regexp *prog, char *stringarg, register char *strend, /* Check validity of program. */ if (UCHARAT(prog->program) != REG_MAGIC) { - FAIL("corrupted regexp program"); + croak("corrupted regexp program"); } PL_reg_flags = 0; @@ -2445,7 +2445,7 @@ regmatch(regnode *prog) default: PerlIO_printf(PerlIO_stderr(), "%lx %d\n", (unsigned long)scan, OP(scan)); - FAIL("regexp memory corruption"); + croak("regexp memory corruption"); } scan = next; } @@ -2454,7 +2454,7 @@ regmatch(regnode *prog) * We get here only if there's trouble -- normally "case END" is * the terminating point. */ - FAIL("corrupted regexp pointers"); + croak("corrupted regexp pointers"); /*NOTREACHED*/ sayNO; -- 1.8.3.1