?

Log in

No account? Create an account
Pop quiz. - Chaz Meyers [entries|archive|friends|userinfo]
Chaz Meyers

[ website | chazmeyers.com ]
[ profile | view profile ]
[ archive | view archive ]

Links
[Links:| chazmeyers.com Twitter ]

Pop quiz. [Mar. 1st, 2005|01:42 pm]
Chaz Meyers
Can you spot why the following Perl function is buggy?



The sad part is this code is paraphrased from a commercial product. And, people can pay up to $400 for this product.
LinkReply

Comments:
[User Picture]From: kellygrape
2005-03-01 11:18 am (UTC)
No, but I wish I could. I just don't know Perl.

Now I'm curious. What's the problem?
(Reply) (Thread)
[User Picture]From: cpm
2005-03-01 06:32 pm (UTC)
my $dbrecord = SomeClass->get_by_id($arg);

This looks up a database record with an id of $arg. There are some weird things about that.

For starters, the name of this function is "get record id". If we're passing the id to the function, don't we already know the id? Redundant, but not buggy.

Now consider the following line:
$dbrecord = SomeClass->get_by_name($arg) unless $dbrecord;

This is the same as saying:
if( ! $dbrecord ) { $dbrecord = SomeClass->get_by_name($arg) }


Basically, if we couldn't find the record by id, we try find it by name. Name, obviously, would be a string.

Now, consider a database record that looks like:
name = "1st Database Record!"
id = 5

and you call the function like:
$record = get_record_id ("1st Database Record!");


When Perl reaches the
SomeClass->getById("1st Database Record!")
, that function will return the database record with id = 1 if one exists. That's because when Perl is given a string and it needs a number, it will try to parse a number out of it. When you have something like
" 2" + 1
, it's useful. In this case, though, it results in a bug.

Because we already have the record with id = 1, we never call
SomeClass->getByName("1st Database Record!")
.
(Reply) (Parent) (Thread)
[User Picture]From: kellygrape
2005-03-01 10:02 pm (UTC)
Oh wow, I'll have to read this again when it's NOT 1 in the morning.

What program/product is this from?
(Reply) (Parent) (Thread)
(Deleted comment)
[User Picture]From: cpm
2005-03-01 08:08 pm (UTC)
I'm more of a programmer than an HTML guy, but I know enough to get by. Hit me up on aim sometime this weekend and we can talk about it, OK? :)
(Reply) (Parent) (Thread)
[User Picture]From: instantdharma
2005-03-01 11:59 am (UTC)
I know! It's the part with the underscore, dollar sign, and the incoherent jargon!
(Reply) (Thread)
From: psikoticsilver
2005-03-01 12:02 pm (UTC)
Because you touch yourself at night?
(Reply) (Thread)
From: psikoticsilver
2005-03-01 12:03 pm (UTC)
It's really because you used and extra

;
(Reply) (Parent) (Thread)
[User Picture]From: duckssaymip
2005-03-01 03:09 pm (UTC)
I don't know, but I'm more than willing to bet that the reason for the mistake lies in your eating of currency. You bastard!

:)
(Reply) (Thread)
From: froggidyke
2005-03-01 03:13 pm (UTC)
my guess is the part with return ''
(Reply) (Thread)
[User Picture]From: cpm
2005-03-01 06:32 pm (UTC)
No, but good guess!
(Reply) (Parent) (Thread)
From: froggidyke
2005-03-02 02:14 pm (UTC)
hhah well damn.
(Reply) (Parent) (Thread)
(Deleted comment)
[User Picture]From: cpm
2005-03-01 06:33 pm (UTC)
Pfft. This is perl. It could be cut down to one line. :-P
(Reply) (Parent) (Thread)
[User Picture]From: finnell1912
2005-03-01 03:46 pm (UTC)
Is it setting dbrecord to something, then changing it before the first thing can be used?
(Reply) (Thread)
[User Picture]From: cpm
2005-03-01 06:34 pm (UTC)
Nope.

http://www.livejournal.com/users/cpm/331809.html?thread=706337#t706337

More than you ever wanted to know about the inner workings of your help ticket system. :-P
(Reply) (Parent) (Thread)