Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > Potential bug ?

Reply
Thread Tools

Potential bug ?

 
 
kspecial@linuxmail.org
Guest
Posts: n/a
 
      09-30-2004
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:2: 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:d41d8cd98f00b204e9800998ecf8427ew:-1:::
DB<7> s
main::ub_value(borked.pl:29): $userline = $_ if
m/^$user:/i;
DB<7> p "@logins"
xemp:c4ca4238a0b923820dcc509a6f75849b: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:d41d8cd98f00b204e9800998ecf8427ew:-1:::
xemp:c4ca4238a0b923820dcc509a6f75849b: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
 
Reply With Quote
 
 
 
 
Eric Bohlman
Guest
Posts: n/a
 
      09-30-2004
http://www.velocityreviews.com/forums/(E-Mail Removed) ((E-Mail Removed)) wrote in
news:(E-Mail Removed) om:

> 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).

 
Reply With Quote
 
 
 
 
kspecial@linuxmail.org
Guest
Posts: n/a
 
      09-30-2004
Eric Bohlman <(E-Mail Removed)> wrote in message news:<Xns9573D6B6B4488ebohlmanomsdevcom@130.133.1. 4>...
> (E-Mail Removed) ((E-Mail Removed)) wrote in
> news:(E-Mail Removed) om:
>
> > 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
 
Reply With Quote
 
187
Guest
Posts: n/a
 
      09-30-2004
(E-Mail Removed) 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?


 
Reply With Quote
 
Greg Schmidt
Guest
Posts: n/a
 
      09-30-2004
On Thu, 30 Sep 2004 09:07:45 -0700, 187 wrote:

> (E-Mail Removed) 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 (E-Mail Removed)
Trawna Publications http://www.trawna.com/
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Potential bug: ASP.NET session variables and file watcher service =?Utf-8?B?QXZp?= ASP .Net 1 05-21-2007 11:13 AM
*bug* *bug* *bug* David Raleigh Arnold Firefox 12 04-02-2007 03:13 AM
Need a ruby math genius - potential ruby bug. Todd S. Ruby 3 01-26-2006 09:41 PM
[Twisted] potential bug in the reactor's handling events loop Andy Leszczynski Python 0 03-01-2005 03:07 PM
Potential ASP.NET bug Robert May ASP .Net 1 10-16-2003 09:30 PM



Advertisments