Skip to content

HV_FETCH_ISSTORE and G_DISCARD have the same value #24537

Description

@mauke

Description

I don't have time to investigate this more closely at the moment, but I don't want to lose track of it either; hence this issue.

perldoc perlapi says the hash deletion functions (hv_delete, hv_deletes, hv_delete_ent) can take a flags value of G_DISCARD to make them return NULL (instead of the value that was deleted from the hash).

Internally, hv_delete(hv, key, klen, flags) forwards to hv_common_key_len(hv, key, klen, flags | HV_DELETE, NULL, 0), which in turn calls hv_common(hv, NULL, key, abs(klen), klen < 0 ? HVhek_UTF8 : 0, flags, NULL, 0), which is a macro wrapper for Perl_hv_common. Perl_hv_common then takes the flags argument (confusingly named action in this function) to determine what to do, comparing bit tests against HV_DELETE, HV_FETCH_ISSTORE, HV_FETCH_ISEXISTS, etc.

These constants are defined in hv.h next to a comment that says their values must not overlap with G_DISCARD:

#define HV_DISABLE_UVAR_XKEY	0x01
/* We need to ensure that these don't clash with G_DISCARD, which is 2, as it
   is documented as being passed to hv_delete().  */
#define HV_FETCH_ISSTORE	0x04
#define HV_FETCH_ISEXISTS	0x08

This makes sense because callers can just bitwise-or with G_DISCARD and we don't want Perl_hv_common to get confused about what was passed.

However, G_DISCARD is not 2; it is 4 and so overlaps with HV_FETCH_ISSTORE:

#define G_DISCARD         0x4	/* Call FREETMPS.
                                   Don't change this without consulting the
                                   hash actions codes defined in hv.h */

The comments in cop.h and hv.h were added in commit b54b483 on 2007-09-19, and then shortly after disregarded in commit 2f8edad on 2008-01-21, when G_DISCARD was changed from 2 to 4 without fixing up the constants in hv.h.

Steps to Reproduce

I haven't yet figured out what, if anything, this breaks. :-(

Expected behavior

The comments should match the code.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions