Skip to content

Operator Precedence bug in dns.c #7

@nigeltao

Description

@nigeltao

In https://github.com/jbangert/nail/blob/master/examples/dns/dns.c there is this snippet:

pos = highbyte & 63 << 8 | current->data[pos];

or

pos = highbyte & 63 << 8 | etc;

In C, the operator precedence is <<, then &, then |, so that the equivalent parenthesized expression is:

pos = (highbyte & (63 << 8)) | etc;

Folding the 63<<8 constant gives:

pos = (highbyte & 0x3F00) | etc;

highbyte is a uint8_t, obviously in the range [0x00, 0xFF], so and'ing it with 0x3F00 will always be zero:

pos = 0 | etc;

which simplifies to:

pos = etc;

which is:

pos = current->data[pos];

which is clearly an error, as it ignores the highbyte value. Presumably the original statement should have been:

pos = (highbyte & 63) << 8 | current->data[pos];

although it's not obvious if that is still semantically correct, for compressed DNS records.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions