Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask

comment on

( #3333=superdoc: print w/replies, xml ) Need Help??

At $work a co-worker recently sent an obfu out to the tech list that relied on keys() returning the keys of a hash in the same order every time the piece of code was executed. My perl is 5.8.8, compiled without "-DUSE_HASH_SEED_EXPLICIT", and I don't have the "PERL_HASH_SEED" environment variable set.

Given my interpretation of `perldoc -f keys`, and `perldoc perlrun`, every time I call keys() on a hash, even between runs of perl, I will get a different order. This is the relevant piece of documentation from `perldoc -f keys`:

Since Perl 5.8.1 the ordering is different even between different runs of Perl for security reasons (see "Algorithmic Complexity Attacks" in perlsec).

But when I run:

#!/usr/bin/perl use warnings; use strict; my %hash = ( A => "B", C => "D", E => "F", ); print keys %hash;

"ACE" is printed every time (at least every time that I've run it).

Is there a bug in the way I'm reading the documentation? If I remember correctly (which is unlikely), this was kind of a big deal when 5.8.1 came out, as there was a fair bit of code in the wild that disobeyed the documentation and relied on hash ordering.

My understanding of the current implementation given my version, compilation options and environment variables was that I could rely on hashes *not* being ordered the same between runs of perl. I'm clearly missing something. Can anyone shed some light?


-- Douglas Hunter

In reply to hash randomization and keys() by dug

Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":

  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?

What's my password?
Create A New User
Domain Nodelet?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others perusing the Monastery: (1)
As of 2023-09-24 16:52 GMT
Find Nodes?
    Voting Booth?

    No recent polls found