diff options
author | Father Chrysostomos <sprout@cpan.org> | 2011-06-12 14:46:44 -0700 |
---|---|---|
committer | Father Chrysostomos <sprout@cpan.org> | 2011-06-12 14:46:44 -0700 |
commit | 7d6175ef71f6339fae97e36c1cdae9e4f47f74d0 (patch) | |
tree | a222a3dd9597443219d01c6b5976128dd51a7f8f /hv.c | |
parent | 30b0736d971c494432c032b4ca9fd2e8dcd31680 (diff) | |
download | perl-7d6175ef71f6339fae97e36c1cdae9e4f47f74d0.tar.gz |
Completely free hashes containing nulls
This fixes a regression introduced since 5.14.0, by commit e0171a1a3.
The new Perl_hfree_next_entry function that that commit introduced
returns the value of the hash element, or NULL if there are none left.
If the value of the hash element is NULL, the two cases are indistin-
guishable.
Before e0171a1a3, all the hash code took null values into account.
mro_package_moved took advantage of that, stealing values out of a
hash and leaving it to the freeing code to delete the elements.
The two places that call Perl_hfree_next_entry (there was only one,
S_hfreeentries, with commit e0171a1a3, but the following commit,
104d7b699c, made sv_clear call it, too) were not accounting for NULL
values’ being returned, and could terminate early, resulting in mem-
ory leaks.
One could argue that the perl core should not be assigning nulls to
HeVAL, but HeVAL is part of the public API and there could be CPAN
code assigning NULL to it, too.
So the safest approach seems to be to modify Perl_hfree_next_entry’s
callers to check the number of keys and not to attribute a signifi-
cance to a returned NULL.
Diffstat (limited to 'hv.c')
-rw-r--r-- | hv.c | 10 |
1 files changed, 6 insertions, 4 deletions
@@ -1662,12 +1662,12 @@ STATIC void S_hfreeentries(pTHX_ HV *hv) { STRLEN index = 0; - SV* sv; + XPVHV * const xhv = (XPVHV*)SvANY(hv); PERL_ARGS_ASSERT_HFREEENTRIES; - while ( ((sv = Perl_hfree_next_entry(aTHX_ hv, &index))) ) { - SvREFCNT_dec(sv); + while (xhv->xhv_keys) { + SvREFCNT_dec(Perl_hfree_next_entry(aTHX_ hv, &index)); } } @@ -1675,7 +1675,9 @@ S_hfreeentries(pTHX_ HV *hv) /* hfree_next_entry() * For use only by S_hfreeentries() and sv_clear(). * Delete the next available HE from hv and return the associated SV. - * Returns null on empty hash. + * Returns null on empty hash. Nevertheless null is not a reliable + * indicator that the hash is empty, as the deleted entry may have a + * null value. * indexp is a pointer to the current index into HvARRAY. The index should * initially be set to 0. hfree_next_entry() may update it. */ |