Skip to content

A check for negativity may be incorrect #4393

@scuniff

Description

@scuniff

Regarding:

int n1 = DataTools.bytesToShort(b, 2, 2, in.isLittleEndian());
int n2 = DataTools.bytesToShort(b, 2, 2, !in.isLittleEndian());
n1 &= 0xffff;
n2 &= 0xffff;
if (n1 < 0 || n1 + in.getFilePointer() > in.length()) return n2;
if (n2 < 0 || n2 + in.getFilePointer() > in.length()) return n1;

I don’t think the check for negative is ever checked for correctly.

Variables n1 and n2 are ints but populated by shorts. Thus, if the shorts are negative they will be sign extended to negative for ints n1 and n2.
Via the code “&= 0xffff;”, the most significant 16 bits of the ints n1 and n2 are set to 0, thus losing the negativity of the number.
Thus, the checks for negativity (n1< 0 and n2<0) are never true.

Should n1 and n2 be changed to short and the two “&=” statement be removed?

    short n1 = DataTools.bytesToShort(b, 2, 2, in.isLittleEndian());
    short n2 = DataTools.bytesToShort(b, 2, 2, !in.isLittleEndian());
    if (n1 < 0 || n1 + in.getFilePointer() > in.length()) return n2;
    if (n2 < 0 || n2 + in.getFilePointer() > in.length()) return n1;

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions