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

Re: How to detect hash randomization?

Thread Previous | Thread Next
Elizabeth Mattijsen
September 8, 2003 02:28
Re: How to detect hash randomization?
Message ID:
At 01:04 +0200 9/8/03, Rafael Garcia-Suarez wrote:
>Elizabeth Mattijsen wrote:
>  > If I build 21021 with:
>  > >    -Accflags=-DNO_HASH_SEED
>You can always do 'perl -V:ccflags'. That should be there.

You learn something every day.  Thanks, Rafael.

I concocted the following check routine.  Maybe this needs a home 
somewhere in a Util core library somewhere?  If so, I'll supply a 
patch for the indicated library.

I also made it print something in void context, so you'll be able to 
do one-liners such as:

   perl -Mwhatever -e is_hash_randomized

(if is_hash_randomized in module would be exported, of course)


The code:

# is_hash_randomized
# OUT: 1 undef (random sequence) or defined (repeatable with seed)
# Prints a string when called in void context.

sub is_hash_randomized {

# If we're called in void context
#  Call ourselves to get status
#  Show the world what it is
#  We're done now

     if (!defined wantarray) {
         my $seed = is_hash_randomized();
         print defined $seed ?
          qq{Repeatable hash sequences with seed "$seed".\n} :
          qq{Unrepeatable hash sequences.\n};

# We don't have any random sequences yet, as if seed "0"

     return 0 if $] < 5.008001;

# Get the -Accflags= settings using this Perl executor

     open my $handle, "$^X -V:ccflags |"
      or die "Could not execute $^X -V:ccflags: $!\n";
     my $ccflags = <$handle>;
     close $handle;

# Return repeatable default if random sequences compiled out
# Return random if switch settings reversed and environment variable set
# Return whatever the environment variable indicates

     return 0 if $ccflags =~ m#-DNO_HASH_SEED#;
     return   if $ccflags =~ m#-DUSE_HASH_SEED_EXPLICIT#
                  and exists $ENV{PERL_HASH_SEED};
} #is_hash_randomized

Thread Previous | Thread Next Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at | Group listing | About