Potential bug ?

Discussion in 'Perl Misc' started by kspecial@linuxmail.org, Sep 30, 2004.

  1. Guest

    Hey, i've got some code here that i've been having problems with, I
    have crunched my brain and have resorted to this very place. For some
    reason the variable @logins in the following code is being undefined
    when there is absolutely nothing that is using @logins in such a way
    that it would be undefined, the two functions are out of a lot longer
    bit of code but I have put them together here in such a manor that you
    can run the following code yourself as a file and get an eye to what's
    going on, please excuse any bad syntax, unless it's having to do with
    something i've done wrong that causes this problem. Here is the code:

    --- CODE ---

    @logins = ('K-sPecial perl.freak o (kspecial)');

    logout_user(\@logins, "K-sPecial",
    "d41d8cd98f00b204e9800998ecf8427e");

    sub logout_user {
    my ($logins, $name, $pass) = @_;
    print "Logins ($logins) in logout_user: @$logins\r\n";
    foreach (@$logins) {
    if (m/^\s*$name\s+/i) {
    #my $rname = get_rname($logins, "$name", ' ',
    1);
    my $md5 = ub_value ("$rname", 1);
    print "Now logins ($logins) in logout_user:
    @$logins\r\n";
    print "Got $rname and $name then $md5 and
    $pass\r\n";
    if ("$pass" eq "$md5") {
    #my $return = del_login($logins,
    "$name");
    return ($return);
    }
    }
    return(0);
    }
    }

    sub ub_value {
    my ($user, @values) = @_;
    #$user = rem_re($user);
    my $userline;
    open (FH, "<fool.txt") or return (undef);
    while (<FH>) {
    $userline = $_ if m/^$user:/i;
    }
    close(FH);
    print "Userline is $userline";
    if ($userline) {
    my @results;
    foreach(@values) {
    push(@results, (split(':', "$userline"))[$_]);
    }
    print "Gots the @results\r\n";
    return ("$results[0]") if scalar(@values) == 1;
    return("@results");
    }
    else {
    print "returning 0\r\n";
    return(0);
    }
    }

    --- END CODE ---

    So i'm going to run that code exactly as is and show you it's output:

    --- OUTPUT ---

    kspecial@xzziroz:~$ perl borked.pl
    Logins (ARRAY(0x814490c)) in logout_user: K-sPecial perl.freak o
    (kspecial)
    Userline is returning 0
    Now logins (ARRAY(0x814490c)) in logout_user:
    Got and K-sPecial then 0 and d41d8cd98f00b204e9800998ecf8427e

    --- END OUTPUT ---

    Look! @logins is completely being undefined with no apparent reason!
    Now try taking out the call to ub_value ( my $md5 = ub_value
    ("$rname", 1); ) now @logins is fine. It prints this:

    --- OUTPUT ---
    kspecial@xzziroz:~$ perl borked.pl
    Logins (ARRAY(0x814490c)) in logout_user: K-sPecial perl.freak o
    (kspecial)
    Now logins (ARRAY(0x814490c)) in logout_user: K-sPecial perl.freak o
    (kspecial)
    Got and K-sPecial then and d41d8cd98f00b204e9800998ecf8427e

    --- END OUTPUT ---

    Just as it should... now i'm not very good with the perl debugger but
    I did manage to get a glimpse at what was happening, at the user
    level.

    Here is the debugging:

    --- DEBUGGING ---
    main::ub_value(borked.pl:24): my ($user, @values) = @_;
    DB<3> s
    main::ub_value(borked.pl:26): my $userline;
    DB<3> p "@logins"
    K-sPecial perl.freak o (kspecial)
    DB<4> s
    main::ub_value(borked.pl:27): open (FH, "<fool.txt") or
    return (undef);
    DB<4> p "@logins"
    K-sPecial perl.freak o (kspecial)
    DB<5> s
    main::ub_value(borked.pl:28): while (<FH>) {
    DB<5> p "@logins"
    K-sPecial perl.freak o (kspecial)
    DB<6> s
    main::ub_value(borked.pl:29): $userline = $_ if
    m/^$user:/i;
    DB<6> p "@logins"
    kspecial:d41d8cd98f00b204e9800998ecf8427e:eek:w:-1:::
    DB<7> s
    main::ub_value(borked.pl:29): $userline = $_ if
    m/^$user:/i;
    DB<7> p "@logins"
    xemp:c4ca4238a0b923820dcc509a6f75849b:eek::1000:::
    DB<8> s
    main::ub_value(borked.pl:29): $userline = $_ if
    m/^$user:/i;
    DB<8> p "@logins"
    coprime:c81e728d9d4c2f636f067f89cc14862c:v:9999:::

    --- END DEBUGGING ---

    Of course that's assuming your fool.txt looks like this:

    --- START fool.txt ---
    kspecial:d41d8cd98f00b204e9800998ecf8427e:eek:w:-1:::
    xemp:c4ca4238a0b923820dcc509a6f75849b:eek::1000:::
    coprime:c81e728d9d4c2f636f067f89cc14862c:v:9999:::
    --- END fool.txt ---

    Oddly the line " $userline = $_ if m/^$user:/i " is assigning $_ to
    @logins on every loop......I'm gonig to also send this same message
    using 'perlbug' if i'm able to.

    --K-sPecial
     
    , Sep 30, 2004
    #1
    1. Advertising

  2. Eric Bohlman Guest

    () wrote in
    news::

    > Hey, i've got some code here that i've been having problems with, I
    > have crunched my brain and have resorted to this very place. For some
    > reason the variable @logins in the following code is being undefined
    > when there is absolutely nothing that is using @logins in such a way
    > that it would be undefined, the two functions are out of a lot longer


    Actually, there is.

    > @logins = ('K-sPecial perl.freak o (kspecial)');
    >
    > logout_user(\@logins, "K-sPecial",
    > "d41d8cd98f00b204e9800998ecf8427e");
    >
    > sub logout_user {
    > my ($logins, $name, $pass) = @_;
    > print "Logins ($logins) in logout_user: @$logins\r\n";
    > foreach (@$logins) {


    Throughout this loop $_ will be an *alias* to the appropriate element of
    @logins. That means, _inter alia_, that any assignment to $_ within
    the loop will change the corresponding element of @logins.

    > if (m/^\s*$name\s+/i) {
    > #my $rname = get_rname($logins, "$name", ' ',
    > 1);
    > my $md5 = ub_value ("$rname", 1);
    > print "Now logins ($logins) in logout_user:
    > @$logins\r\n";
    > print "Got $rname and $name then $md5 and
    > $pass\r\n";
    > if ("$pass" eq "$md5") {
    > #my $return = del_login($logins,
    > "$name");
    > return ($return);
    > }
    > }
    > return(0);
    > }


    So far we haven't seen anything *directly* affecting $_, but there were a
    bunch of sub calls...

    > }
    >
    > sub ub_value {
    > my ($user, @values) = @_;
    > #$user = rem_re($user);
    > my $userline;
    > open (FH, "<fool.txt") or return (undef);
    > while (<FH>) {


    OOPS! Remember that $_ is *not* automatically localized during sub calls,
    so each line read is overwriting an element of @logins. And, of course,
    the very last read will put undef there.

    > $userline = $_ if m/^$user:/i;
    > }
    > close(FH);
    > print "Userline is $userline";
    > if ($userline) {
    > my @results;
    > foreach(@values) {


    Here $_ is actually being localized, so no further trashing is going on.

    > push(@results, (split(':', "$userline"))[$_]);
    > }
    > print "Gots the @results\r\n";
    > return ("$results[0]") if scalar(@values) == 1;
    > return("@results");
    > }
    > else {
    > print "returning 0\r\n";
    > return(0);
    > }
    > }


    Moral of the story: if you need to loop over an array or list and you're
    going to be doing something non-trivial (such as calling subs) in the loop,
    use an explicit lexical ("my") loop variable rather than implicitly using
    $_ (which, being a global variable, has all the problems associated with
    global variables).
     
    Eric Bohlman, Sep 30, 2004
    #2
    1. Advertising

  3. Guest

    Eric Bohlman <> wrote in message news:<Xns9573D6B6B4488ebohlmanomsdevcom@130.133.1.4>...
    > () wrote in
    > news::
    >
    > > Hey, i've got some code here that i've been having problems with, I
    > > have crunched my brain and have resorted to this very place. For some
    > > reason the variable @logins in the following code is being undefined
    > > when there is absolutely nothing that is using @logins in such a way
    > > that it would be undefined, the two functions are out of a lot longer

    >
    > Actually, there is.
    >
    > > @logins = ('K-sPecial perl.freak o (kspecial)');
    > >
    > > logout_user(\@logins, "K-sPecial",
    > > "d41d8cd98f00b204e9800998ecf8427e");
    > >
    > > sub logout_user {
    > > my ($logins, $name, $pass) = @_;
    > > print "Logins ($logins) in logout_user: @$logins\r\n";
    > > foreach (@$logins) {

    >
    > Throughout this loop $_ will be an *alias* to the appropriate element of
    > @logins. That means, _inter alia_, that any assignment to $_ within
    > the loop will change the corresponding element of @logins.
    >
    > > if (m/^\s*$name\s+/i) {
    > > #my $rname = get_rname($logins, "$name", ' ',
    > > 1);
    > > my $md5 = ub_value ("$rname", 1);
    > > print "Now logins ($logins) in logout_user:
    > > @$logins\r\n";
    > > print "Got $rname and $name then $md5 and
    > > $pass\r\n";
    > > if ("$pass" eq "$md5") {
    > > #my $return = del_login($logins,
    > > "$name");
    > > return ($return);
    > > }
    > > }
    > > return(0);
    > > }

    >
    > So far we haven't seen anything *directly* affecting $_, but there were a
    > bunch of sub calls...
    >
    > > }
    > >
    > > sub ub_value {
    > > my ($user, @values) = @_;
    > > #$user = rem_re($user);
    > > my $userline;
    > > open (FH, "<fool.txt") or return (undef);
    > > while (<FH>) {

    >
    > OOPS! Remember that $_ is *not* automatically localized during sub calls,
    > so each line read is overwriting an element of @logins. And, of course,
    > the very last read will put undef there.
    >
    > > $userline = $_ if m/^$user:/i;
    > > }
    > > close(FH);
    > > print "Userline is $userline";
    > > if ($userline) {
    > > my @results;
    > > foreach(@values) {

    >
    > Here $_ is actually being localized, so no further trashing is going on.
    >
    > > push(@results, (split(':', "$userline"))[$_]);
    > > }
    > > print "Gots the @results\r\n";
    > > return ("$results[0]") if scalar(@values) == 1;
    > > return("@results");
    > > }
    > > else {
    > > print "returning 0\r\n";
    > > return(0);
    > > }
    > > }

    >
    > Moral of the story: if you need to loop over an array or list and you're
    > going to be doing something non-trivial (such as calling subs) in the loop,
    > use an explicit lexical ("my") loop variable rather than implicitly using
    > $_ (which, being a global variable, has all the problems associated with
    > global variables).


    Wow loads of help. I was under the impression that while (<FH>) {
    would make it's own aliased $_ not at all related to the foreach ()'s
    $_ in logout_user... but I read in my book and they do basicly
    describe exactly what you tould me... so it's only my fault and nobody
    elses. Luckily my ISP blocks port 25 outbound so I never got the
    chance to report with perlbug..Thanks alot,

    --K-sPecial
     
    , Sep 30, 2004
    #3
  4. 187 Guest

    Perl.org locked down? (was Re: Potential bug ?)

    wrote:
    [...]
    > Luckily my ISP blocks port 25 outbound so I never got the
    > chance to report with perlbug..Thanks alot,


    Is this an attempt to stop spammers? If so, sounds like a good idea,
    though I'm thinking you meant 25 inbound is blocked, as the outbound
    port used by your computer when *sending* mail is a random outgoing
    port; the server recieving the mail is always on the fixed port of 25
    TCP.

    I think the problem is the Perl.org servers lately seeming won't acccept
    any new posts of mail (ie PerlBug gets posted there), as if it's been
    locked down for some reason? I mean I can read posts, but suddenly
    nothing seems to be acceoted in the way of posts and emails (like
    PerlBug.)

    Maybe someone else knows more?
     
    187, Sep 30, 2004
    #4
  5. Greg Schmidt Guest

    Re: Perl.org locked down? (was Re: Potential bug ?)

    On Thu, 30 Sep 2004 09:07:45 -0700, 187 wrote:

    > wrote:
    > [...]
    >> Luckily my ISP blocks port 25 outbound so I never got the
    >> chance to report with perlbug..Thanks alot,

    >
    > Is this an attempt to stop spammers? If so, sounds like a good idea,
    > though I'm thinking you meant 25 inbound is blocked, as the outbound
    > port used by your computer when *sending* mail is a random outgoing
    > port; the server recieving the mail is always on the fixed port of 25
    > TCP.


    My sister's ISP (Sprint Canada, I think) blocks outbound connections to
    port 25 to anything but their own servers. Took a while to figure this out
    (she uses my mail server instead of theirs), but it does seem like a very
    good defense against spammers and viruses that send out emails.

    --
    Greg Schmidt
    Trawna Publications http://www.trawna.com/
     
    Greg Schmidt, Sep 30, 2004
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Robert May

    Potential ASP.NET bug

    Robert May, Oct 16, 2003, in forum: ASP .Net
    Replies:
    1
    Views:
    414
    Robert May
    Oct 16, 2003
  2. Andy Leszczynski
    Replies:
    0
    Views:
    270
    Andy Leszczynski
    Mar 1, 2005
  3. =?Utf-8?B?QXZp?=
    Replies:
    1
    Views:
    517
    =?Utf-8?B?TWlsb3N6IFNrYWxlY2tpIFtNQ0FEXQ==?=
    May 21, 2007
  4. Todd S.
    Replies:
    3
    Views:
    159
    Matthew Moss
    Jan 26, 2006
  5. Replies:
    2
    Views:
    113
Loading...

Share This Page