Bitwise comparison failing

C

corky

I have a function that manages security for various hyperlinks on the
web. Each user has an integer representation of a binary number (24
security on/off switches) stored in the database. Each link has a link
ID associated. This function takes the link ID, looks through a list
of restricted link ID / security pairs, and denies the user access to
a link if there is a match.

For instance, GetLinkSecurity (2, <user's security mask as integer>)
would pass the function a link ID of 2 and the user's security mask.
The function then looks through the list of link ID / security bit
pairs to see if the user should be denied access.

The problems started when I added a new security bit with an integer
value of 8388608 (24 bits). The function stopped working properly for
users with that security bit turned on. I don't know if it's some
overflow condition or something, and I can't find any reference to
capacity in the camel book.

I'm not a regular Perl programmer, so please forgive me if the
question is basic.

The function follows:

sub GetLinkSecurity {
&LogItem("--> GetLinkSecurity()");
my ($link, $knowrights) = @_;
my ($linknum, $badright);
my ($secflag) = 1;
open(LINKSEC, "linksec.txt");
while(<LINKSEC>){
chomp;
($linknum, $badright) = split;
if ($linknum == $link) {
if (($knowrights & $badright) == $badright) {
$secflag = 0;
}
}
}
close LINKSEC;
return($secflag);
}

Here is a sample of data in the linksec.txt file:

1 8
1 131072
1 524288
2 8
2 131072
2 524288
3 8
3 131072
3 524288
4 8
4 131072
4 524288
5 8
5 131072
5 524288
6 8
6 131072
6 524288

Thanks,

Corky
 
A

Anno Siegel

corky said:
I have a function that manages security for various hyperlinks on the
web. Each user has an integer representation of a binary number (24
security on/off switches) stored in the database. Each link has a link
ID associated. This function takes the link ID, looks through a list
of restricted link ID / security pairs, and denies the user access to
a link if there is a match.

For instance, GetLinkSecurity (2, <user's security mask as integer>)
would pass the function a link ID of 2 and the user's security mask.
The function then looks through the list of link ID / security bit
pairs to see if the user should be denied access.

The problems started when I added a new security bit with an integer
value of 8388608 (24 bits). The function stopped working properly for
users with that security bit turned on.

What does that mean? Explain what you expect to happen when it's
"working properly" and show what is happening instead.
I don't know if it's some
overflow condition or something, and I can't find any reference to
capacity in the camel book.

I'm not a regular Perl programmer, so please forgive me if the
question is basic.

The problem with the question is not that it's basic, but that we don't
know what the question is.

When I run your code below, it does exactly what I'd expect it to do.
The call

GetLinkSecurity( 2, 8388608)

returns 1 with the given data. That is okay, because none of the masks
has the corresponding bit set. Adding a line "2 838860" to the
data makes the same call return 0, okay again.

Do you see a different behavior? Or is this behavior not what you
expect? Please explain.

Anno

[code for reference]
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,764
Messages
2,569,564
Members
45,039
Latest member
CasimiraVa

Latest Threads

Top