develooper Front page | perl.perl5.porters | Postings from August 2003

[PATCH op.c] Perl_newCONSTSUB() related memory leaks

Thread Next
From:
Marcus Holland-Moritz
Date:
August 28, 2003 12:48
Subject:
[PATCH op.c] Perl_newCONSTSUB() related memory leaks
Message ID:
006301c36d9d$335976a0$e400a8c0@R2D2
Attached patch fixes the memory leaks related to Perl_newCONSTSUB().

  0:malloc
  1:Perl_safesysmalloc (util.c:70)
  2:Perl_savepv (util.c:753)
  3:Perl_newCONSTSUB (op.c:4445)

This leaks memory because in the call

  cv = newXS(name, const_sv_xsub, savepv(CopFILE(PL_curcop)));

the filename CopFILE(PL_curcop) is copied, but Perl_newXS
assumes that filename is static (and thus doesn't copy it).
Perl_cv_undef() knows about that and doesn't free CvFILE(cv)
for XSUBs. So the copied filename isn't freed. The patched
code checks if it's a CONSTSUB and frees the memory.

  0:malloc
  1:Perl_savesharedpv (util.c:802)
  2:Perl_newCONSTSUB (op.c:4442)

This leaks because in Perl_newCONSTSUB() (only interesting bits)

  if (stash) {
      SAVECOPSTASH(PL_curcop);
      CopSTASH_set(PL_curcop,stash);
  }

  cv = newXS(name, const_sv_xsub, savepv(CopFILE(PL_curcop)));

  LEAVE;

CopSTASHPV(PL_curcop) is first saved, then it is set to a
freshly allocated block, and at the end of the routine LEAVE
restores the old cop_stashpv, and there's our leak. Perl_newXS()
needs CopSTASH(PL_curcop) only once (within Perl_gv_fetchpv(),
to look up the stash), but it won't keep a reference on it.
The patched code will CopSTASH_free(PL_curcop) before LEAVEing.

-- Marcus

Thread Next


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About