jlhsgcib has asked for the wisdom of the Perl Monks concerning the following question:

Hi there!

I've skimmed the posting rules beforehand but this is my very first post here so excuse me if there is any mistake in my interpretation of them.

I've already posted this question in another Perl related forum without success and PerlMonks has eventually been pointed out.

I have the following snippet of code (the code is not as pedantic as I'd like it to be, but it's not mine and I'll dump it as-is in case I've overlooked something):

18: sub getPasswordStatus { 18: my ($login, $password) = @_; 20: my$quotedLogin = quotemeta($login); 21: my $Passwd; 22: 23: if( $password && $password !~ /\?{7,7}/ ) { 24: if ($password =~ m/^NO PASSWORD$/i){ 25: $Passwd = 'empty'; 26: } elsif ($password =~ /^PASSWORD$/i){ 27: $Passwd = 'password'; 28: } elsif ($password =~ /^$quotedLogin$/i){ 29: $Passwd = 'login'; 30: } else { 31: $Passwd = 'weak'; 32: } 33: } else { 34: $Passwd = 'ok'; 35: } 36: return $Passwd; 37: }

As you can see, this is not a very tricky piece of code. But I have a very weird behaviour though. For some passwords, I get the following message:

        Use of uninitialized value in string eq at lib/Pwc/Policy.pm line 24.

It's worth noting that it always happens for the same 7 or 8 passwords among more than 20000. Interestingly, the guilty passwords don't seem to differ from the others. All I can say is they are always using the same scheme: an upper case letter, then a few lower case letters and finally some numbers. But this doesn't mean that passwords matching this scheme will trigger the warning! For instance the password "Alcatel1" generates an error but the password "Algerie1" doesn't.

Even more interestingly, the error is only generated for the first test line 24, not for the others on line 26 and 28, although the passwords causing this error obviously don't match the first test and thus should go through the others.

I tried to change the test on line 23 to

if( defined $password && $password !~ /\?{7,7}/ )
but it doesn't change anything and it shouldn't actually because if $password is undefined, then "$password" is false, and we don't enter in the guilty block.

I've also tried to change the regular expression match to a simple eq, but I have the exact same behaviour.

I've printed the passwords in hexadecimal in case there would be non-printable characters, but there isn't. But hey! How could I print the password if it is undefined? It's crazy, isn't it?

I'm really puzzled on this bug. My leaning goes toward a Perl bug... Any idea to prove me I'm wrong?

For what it's worth, this error happens on ActivePerl:

        This is perl, v5.8.8 built for MSWin32-x86-multi-thread

Thank you very much for you help.

Best regards,

Replies are listed 'Best First'.
Re: Puzzling warning "Use of unitialized value" in very simple code
by kyle (Abbot) on Aug 04, 2008 at 16:57 UTC

    I'd look at $login and $quotedLogin. When perl encounters that warning at some point later in an elsif ladder, it'll report it as happening on the line where the if starts it all, even if it's much later.

    Updated with an example:

    my $def1 = 'true'; my $def2 = 'foo'; my $def3 = 'blue'; my $undef1; if ( $def1 =~ /X/ ) { # <-- problem reported here print 'not here'; } elsif ( $def2 =~ /Y/ ) { print 'not here either'; } elsif ( $undef1 =~ /$def3/ ) { # <-- problem exists here print 'oh noes'; } else { print "happiness ensues\n"; }
      <AOL>Me too!</AOL>
      I think kyle nailed it. Put:
      use Data::Dumper; print Dumper([$login,$quotedLogin,$password]);
      on line 22 and see the results...
      []s, HTH, Massa (κς,πμ,πλ)

      Dear kyle, moritz,

      The line number displayed for the warning was indeed misleading me. It actually occurs because of the undefined $email argument.

      By the way, Fletch remark was pertinent: the warning was related to the "eq" operator, not to the "m//" one.

      Thank you for your help.
      Best regards,
      -- Jeremie

Re: Puzzling warning "Use of unitialized value" in very simple code
by Fletch (Bishop) on Aug 04, 2008 at 16:56 UTC

    I'm very suspicious as to this actually being the code causing that message since there's no use of eq in it anywhere; I'd expect to see Use of uninitialized value in pattern match (m//) at ... (or Use of uninitialized value in regexp compilation at ..., depending on the side of the =~ the undef is showing up) instead.

    Update: Added other possible expected error message.

    The cake is a lie.
    The cake is a lie.
    The cake is a lie.

Re: Puzzling warning "Use of unitialized value" in very simple code
by moritz (Cardinal) on Aug 04, 2008 at 16:57 UTC
    There are a few known bugs in perl 5.8.8 where the line number of a warning is incorrectly reported. Also warnings are reported for the first line in which an if starts, and warnings from subsequent conditions (sometimes?) wrongly report to be on that same line.

    Perl 5.10.0 has the nice feature that it tells you exactly which variable is actually undefined, so running your script under perl 5.10.0 might shed some light on your problem.

    It might also help to give us an example password (not a real one ;-) ) that throws the "uninitialized" warning.

      Moritz, thank you! You have saved my sanity.
      Also warnings are reported for the first line in which an if starts, and warnings from subsequent conditions (sometimes?) wrongly report to be on that same line.
      I was getting the exact same error, and searching fruitlessly for the answer hence landing here, and sure enough, it was a typo farther down the eslif's and not at the line in question. I thought I was going nuts there. :) (running 5.8.5 on solaris 9 if anyone cares)
Re: Puzzling warning "Use of unitialized value" in very simple code
by CountZero (Bishop) on Aug 04, 2008 at 19:04 UTC
    I am not commenting on the reason why your code behaves as it does as that seems to be discovered already, but rather on the code itself, or more particularly, on what appears to be a very strange password policy.

    I ran the following code against this subroutine:

    foreach ('test_NO PASSWORD', 'test_PASSWORD', 'test_test', 'test', 'te +st_abc???????def') { print "$_: ", getPasswordStatus(split /_/), "\n"; }
    and got the following result:
    test_NO PASSWORD: empty test_PASSWORD: password test_test: login test: ok test_abc???????def: ok test_A gOoD PASsword: weak
    <p So if I understand this password-policy it is:
    1. an empty password is OK
    2. a password with 7 consecutive question marks is OK
    3. all other passwords are weak or are unacceptable for other reasons.
    Of all the hare-brained password policies I have ever seen, this must rank within the top 3!

    CountZero

    A program should be light and agile, its subroutines connected like a string of pearls. The spirit and intent of the program should be retained throughout. There should be neither too little or too much, neither needless loops nor useless variables, neither lack of structure nor overwhelming rigidity." - The Tao of Programming, 4.1 - Geoffrey James

      Hey CountZero,

      Thanks for giving me a so high place in your ranking :D.

      Actually the password argument is the result of the cracking phase. It should have been given a better name, I admit but I'm not the author of this code.

      I hope this enlightens your mind :).

      Regards,
      -- Jeremie

Re: Puzzling warning "Use of unitialized value" in very simple code
by GrandFather (Saint) on Aug 04, 2008 at 21:07 UTC

    If you haven't the answer you need yet post an example showing the error. You've given us a good test and bad test case, but plugging those into the code you have provided doesn't demonstrate the error you assert you are getting.

    CountZero's reply Re: Puzzling warning "Use of unitialized value" in very simple code gives a good framework for the test. Using 'test_Alcatel1', 'test_Algerie1' in the test list reports both passwords as weak, but no errors or warnings (as expected).

    Trying trimming down the relevant bits of code to the point where you have just enough stand alone code using the two test cases provided to demonstrate the problem. If you've not found the problem by then come back and show us what you've got.


    Perl reduces RSI - it saves typing
Re: Puzzling warning "Use of unitialized value" in very simple code
by rurban (Scribe) on Aug 07, 2008 at 15:31 UTC
    This is a known limitation/bug, that with elsif series the line numbers are wrong. No nextstate ops for the needed cops are generated for elsif. #37302 and #42664

    http://rt.perl.org/rt3/Ticket/Display.html?id=37302
    http://rt.perl.org/rt3/Ticket/Display.html?id=42664

    Your real error is at line 28 of course. undefined $quotedLogin