Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Re: Odd Perl bitwise-AND & MySQL problem?

Reply
Thread Tools

Re: Odd Perl bitwise-AND & MySQL problem?

 
 
dohnut
Guest
Posts: n/a
 
      10-21-2003
Here's some working code that I created to show the problem.

First the MySQL database dump
-------------------------------------------------------------
CREATE DATABASE /*!32312 IF NOT EXISTS*/ pbug;

USE pbug;

CREATE TABLE hosts (
id int(11) NOT NULL auto_increment,
ip int(4) unsigned default NULL,
password varchar(32) default NULL,
description varchar(255) default '',
clone int(4) unsigned default '0',
conn int(4) unsigned default '0',
status int(4) unsigned default '0',
tdate timestamp(14) NOT NULL,
alarms int(11) unsigned default '0',
monitored int(11) unsigned default '0',
installed int(11) unsigned default '0',
soft_ver_major int(4) default '0',
soft_ver_minor int(4) default '0',
soft_ver_incr int(4) default '0',
hard_ver_major int(4) default '0',
hard_ver_minor int(4) default '0',
hard_ver_incr int(4) default '0',
case_temp int(4) default '-1',
cpu1_temp int(4) default '-1',
cpu2_temp int(4) default '-1',
ext_temp int(4) default '-1',
PRIMARY KEY (id),
UNIQUE KEY ip (ip)
) TYPE=MyISAM;


INSERT INTO hosts VALUES (1,168430090,'crystal','Demo
XXXXXXXX',0,129,0,20031020210116,0,61,7,0,0,0,0,0, 0,29,-1,-1,-1);
INSERT INTO hosts VALUES (2,185273099,'password','Test XXXXXXXX
#1\r\n',0,129,0,20031020210117,57,2045,2047,0,0,0, 0,0,0,31,-1,-1,-1);
INSERT INTO hosts VALUES (3,202116108,'password','Another
XXXXXXXX\r\n\r\n',0,129,0,20031020210118,3,7,63,0, 0,0,0,0,0,30,-1,-1,-1);
--------------------------------------------

Put this into a file and add it to your mysql server.
type: mysql -u <username> -p<password> < pbug.sql

Ok, and here's my little perl module that demonstrates the error (or
my stupidity).
------------------------------------------------
#!/usr/bin/perl -w

use strict;
use warnings;
use DBI;


foreach (1..3)
{
my @dat = &GetHostStatus( $_ );

print "$_: ";
#printf "0x%x & 0x%x & 0x%x & 0x7fff = ", $dat[3], $dat[4],
$dat[5];
print $dat[3] & $dat[4] & $dat[5] & 0x7fff;
print "\n";
}

1;

################################################## ######################

sub GetHostStatus()
{
my $id = shift || return ();

my $db = DBI->connect( 'dbi:mysqlbug','bla','bla' ) || die(
DBI->errstr );

my $query = "SELECT ip, password, description, alarms, monitored,
installed, case_temp, cpu1_temp, cpu2_temp, ext_temp FROM hosts WHERE
id = $id";

my $sth = $db->prepare( $query ) || die( DBI->errstr );

$sth->execute() || die( DBI->errstr );

my @row = $sth->fetchrow_array();

$sth->finish;
$db->disconnect;

return @row;
}
--------------------------------------------------

When I run this on my system, here is my output:

1: 0
2: 0
3: 2

Clearly incorrect. Now, when I uncomment that printf line:

1: 0x0 & 0x3d & 0x7 & 0x7fff = 0
2: 0x39 & 0x7fd & 0x7ff & 0x7fff = 57
3: 0x3 & 0x7 & 0x3f & 0x7fff = 3

Then it works.. Hmm...

I've tested this on 2 separate systems now with the same results, I'd
be interested to see if any of you guys can duplicate it also? Or
tell me where I screwed up, that would be better (and more likely).

Regardless, I'd appreciate any feedback.

- Chris
 
Reply With Quote
 
 
 
 
Sam Holden
Guest
Posts: n/a
 
      10-21-2003
On 20 Oct 2003 20:16:05 -0700, dohnut <(E-Mail Removed)> wrote:
> Here's some working code that I created to show the problem.
>
> First the MySQL database dump

[snip the dump]
> ------------------------------------------------
> #!/usr/bin/perl -w
>
> use strict;
> use warnings;
> use DBI;
>
>
> foreach (1..3)
> {
> my @dat = &GetHostStatus( $_ );


Why are you using the & there?

>
> print "$_: ";
> #printf "0x%x & 0x%x & 0x%x & 0x7fff = ", $dat[3], $dat[4],
> $dat[5];
> print $dat[3] & $dat[4] & $dat[5] & 0x7fff;
> print "\n";
> }


[snip the GetHostStatus code]

> --------------------------------------------------
>
> When I run this on my system, here is my output:
>
> 1: 0
> 2: 0
> 3: 2
>
> Clearly incorrect. Now, when I uncomment that printf line:
>
> 1: 0x0 & 0x3d & 0x7 & 0x7fff = 0
> 2: 0x39 & 0x7fd & 0x7ff & 0x7fff = 57
> 3: 0x3 & 0x7 & 0x3f & 0x7fff = 3
>
> Then it works.. Hmm...


The values are strings. Perl defines a bitwise & operator for
strings and hence uses it. Doing the printf causes perl to generate
a numeric version of the value, so then the & operator operates
on the numbers.

The database stuff is all irrelevant (aside from the fact it is returning
strings):

# start with numbers
my ($x,$y,$z) = (0x39,0x7fd,0x77f);
print $x & $y & $z & 0x7fff, "\n";

# create string variables containing the numbers as strings.
my ($sx, $sy, $sz) = ("$x", "$y", "$z");
print $sx & $sy & $sz & 0x7fff, "\n";

# create numeric 'version's of the strings...
# adding 0 seems clearer than the printf side effect
$_ += 0 for ($sx, $sy, $sz);
print $sx & $sy & $sz & 0x7fff, "\n";

Things like this is why the knowledgable people here always argue against
posters who quote variables for no reason (replace the '("$x", "$y", "$z")'
in that code with '($z, $y, $z)' and the bahaviour will change).

--
Sam Holden

 
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
Odd behavior with odd code Michael Speer C Programming 33 02-18-2007 07:31 AM
DBD:mysql doesn't read mysql option file /etc/my.cnf file JL Perl 0 01-28-2005 03:19 AM
Odd Perl bitwise-AND & MySQL problem? dohnut Perl Misc 7 10-21-2003 05:29 AM
Re: Odd Perl bitwise-AND & MySQL problem? dohnut Perl 0 10-21-2003 03:55 AM
Odd Perl bitwise-AND & MySQL problem? dohnut Perl 0 10-20-2003 09:26 PM



Advertisments