Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > Bitwise comparison failing

Reply
Thread Tools

Bitwise comparison failing

 
 
corky
Guest
Posts: n/a
 
      07-10-2004
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
 
Reply With Quote
 
 
 
 
Anno Siegel
Guest
Posts: n/a
 
      07-10-2004
corky <(E-Mail Removed)> wrote in comp.lang.perl.misc:
> 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, 838860

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]

> 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



 
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
wtf is happening here @ bitwise comparison tschmittldk C++ 8 12-29-2010 10:25 PM
Bitwise Object Comparison - Is it possible & safe? D. Susman C++ 19 03-19-2008 02:05 PM
Price Comparison Service. Best Deal. Special Coupon atBest-Price-Comparison.com rapee Digital Photography 0 03-14-2008 06:46 AM
Bitwise comparison in RowFilters Mark Rae ASP .Net 2 10-07-2006 05:13 PM
Double type equality comparison failing? sqlvs ASP General 3 08-06-2003 12:55 AM



Advertisments