Bitwise comparison failing

Discussion in 'Perl Misc' started by corky, Jul 10, 2004.

  1. corky

    corky Guest

    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
     
    corky, Jul 10, 2004
    #1
    1. Advertising

  2. corky

    Anno Siegel Guest

    corky <> 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, 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]

    > 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
     
    Anno Siegel, Jul 10, 2004
    #2
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Mark Rae

    Bitwise comparison in RowFilters

    Mark Rae, Oct 7, 2006, in forum: ASP .Net
    Replies:
    2
    Views:
    767
    Mark Rae
    Oct 7, 2006
  2. D. Susman
    Replies:
    19
    Views:
    659
    Jerry Coffin
    Mar 19, 2008
  3. tschmittldk
    Replies:
    8
    Views:
    397
    Jorgen Grahn
    Dec 29, 2010
  4. sqlvs
    Replies:
    3
    Views:
    175
    sqlvs
    Aug 6, 2003
  5. Deepu
    Replies:
    1
    Views:
    266
    ccc31807
    Feb 7, 2011
Loading...

Share This Page