robbiehanson / CocoaHTTPServer

A small, lightweight, embeddable HTTP server for Mac OS X or iOS applications
Other
5.59k stars 1.31k forks source link

DDRange fails around UINT64_MAX #135

Open kenthumphries opened 9 years ago

kenthumphries commented 9 years ago

Busy writing simple tests as part of the CocoaPods Test Jam!

Created the following test:

- (void)testLocationInRangeMax
{
    UInt64 midLocation = UINT64_MAX / 2;
    UInt64 maxLength = UINT64_MAX;
    DDRange maxRange = DDMakeRange(midLocation, maxLength);

    XCTAssertFalse(DDLocationInRange(midLocation + maxLength + 1, maxRange));
    XCTAssertTrue(DDLocationInRange(midLocation, maxRange));
    XCTAssertFalse(DDLocationInRange(midLocation - 1, maxRange));
    XCTAssertFalse(DDLocationInRange(midLocation - 2, maxRange));
}

The first and last assertions will fail.

The third assertion passes only by chance.

 => DDLocationInRange(midLocation - 1, maxRange)
 => loc - range.location < range.length
 => midLocation -1 - midLocation < maxLength
 => (UINT64_MAX / 2) - 1 - (UINT64_MAX / 2) < UINT64_MAX
 => -1 < range.length
 => UINT64_MAX < UINT64_MAX // Since we're dealing with unsigned ints, -1 wraps back round to UNIT64_MAX
 => Return False

I won't commit this test as it will fail until DDRange is adjusted to take care of UINT64_MAX border cases. :)