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

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

Thread Next
Marcus Holland-Moritz
August 28, 2003 12:48
[PATCH op.c] Perl_newCONSTSUB() related memory leaks
Message ID:
Attached patch fixes the memory leaks related to Perl_newCONSTSUB().

  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.

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

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

  if (stash) {

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


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 Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at | Group listing | About