Mailing List Archive

Example code in perlguts doc probably leaks memory
Hi,

In illumos we do have the following piece of C code[1]:

690 /* Create and bless a hash for the tie, if necessary */
691 if (new) {
692 SV *tieref;
693 HV *stash;
694
695 tie = newHV();
696 tieref = newRV_noinc((SV *)tie);
697 stash = gv_stashpv("Sun::Solaris::Kstat::_Stat", TRUE);
698 sv_bless(tieref, stash);
699
700 /* Add TIEHASH magic */
701 hv_magic(hash, (GV *)tieref, 'P');
702 SvREADONLY_on(hash);
703
704 /* Otherwise, just find the existing tied hash */
705 } else {

It was found that the code leaks memory[2] when it is run
with Perl 5.36. The proposed fix is to add

SvREFCNT_dec(tieref);

after the hv_magic() call above.

Further analysis showed that the code in question was very likely
borrowed from the perlguts documentation page[3] where we can see the
following example:

SV*
mytie()
PREINIT:
HV *hash;
HV *stash;
SV *tie;
CODE:
hash = newHV();
tie = newRV_noinc((SV*)newHV());
stash = gv_stashpv("MyTie", GV_ADD);
sv_bless(tie, stash);
hv_magic(hash, (GV*)tie, PERL_MAGIC_tied);
RETVAL = newRV_noinc(hash);
OUTPUT:
RETVAL

Should the example in the perlguts documentation be updated to include
a SvREFCNT_dec() call as well?


Thank you for any hints and comments.


References:
[1] https://kernelsources.org/source/xref/illumos-gate/usr/src/cmd/perl/contrib/Sun/Solaris/Kstat/Kstat.xs?r=8af765f5#690
[2] https://www.illumos.org/issues/16149
[3] https://perldoc.perl.org/perlguts#Understanding-the-Magic-of-Tied-Hashes-and-Arrays


--
+-------------------------------------------+
| Marcel Telka e-mail: marcel@telka.sk |
| homepage: http://telka.sk/ |
+-------------------------------------------+
Re: Example code in perlguts doc probably leaks memory [ In reply to ]
On Wed, Jan 03, 2024 at 12:34:57PM +0100, Marcel Telka wrote:
> Should the example in the perlguts documentation be updated to include
> a SvREFCNT_dec() call as well?

No, because the XS parser automatically adds an sv_2mortal(RETVAL)
for a return type of SV*

--
My Dad used to say 'always fight fire with fire', which is probably why
he got thrown out of the fire brigade.
Re: Example code in perlguts doc probably leaks memory [ In reply to ]
On Wed, Jan 03, 2024 at 12:24:51PM +0000, Dave Mitchell wrote:
> On Wed, Jan 03, 2024 at 12:34:57PM +0100, Marcel Telka wrote:
> > Should the example in the perlguts documentation be updated to include
> > a SvREFCNT_dec() call as well?
>
> No, because the XS parser automatically adds an sv_2mortal(RETVAL)
> for a return type of SV*

The question is about tie, not about RETVAL. So it looks like in the
perlguts example there is missing SvREFCNT_dec(tie) call.


Thank you.

--
+-------------------------------------------+
| Marcel Telka e-mail: marcel@telka.sk |
| homepage: http://telka.sk/ |
+-------------------------------------------+
Re: Example code in perlguts doc probably leaks memory [ In reply to ]
On Wed, Jan 03, 2024 at 01:40:12PM +0100, Marcel Telka wrote:
> On Wed, Jan 03, 2024 at 12:24:51PM +0000, Dave Mitchell wrote:
> > On Wed, Jan 03, 2024 at 12:34:57PM +0100, Marcel Telka wrote:
> > > Should the example in the perlguts documentation be updated to include
> > > a SvREFCNT_dec() call as well?
> >
> > No, because the XS parser automatically adds an sv_2mortal(RETVAL)
> > for a return type of SV*
>
> The question is about tie, not about RETVAL. So it looks like in the
> perlguts example there is missing SvREFCNT_dec(tie) call.

Ah yeah. I agree with your diagnosis, and have updated perlguts via
v5.39.6-66-g2100da0d75

--
In economics, the exam questions are the same every year.
They just change the answers.