Skip to content

Commit 6900a2b

Browse files
committed
Fix fuzzing condition to loop neighbors
1 parent 7e357bb commit 6900a2b

File tree

3 files changed

+17
-17
lines changed

3 files changed

+17
-17
lines changed

flatbush.h

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ class span {
6969
namespace flatbush {
7070

7171
constexpr auto gMaxHilbert = std::numeric_limits<uint16_t>::max();
72-
constexpr auto gMaxDistance = std::numeric_limits<double>::max();
72+
constexpr auto gMaxDistance = 1.34078e+154; // std::sqrt(std::numeric_limits<double>::max())
7373
constexpr auto gMaxResults = std::numeric_limits<size_t>::max();
7474
constexpr auto gInvalidArrayType = std::numeric_limits<uint8_t>::max();
7575
constexpr uint16_t gMinNodeSize = 2;
@@ -416,6 +416,7 @@ class Flatbush {
416416

417417
inline bool canDoNeighbors(const Point<ArrayType>& iPoint,
418418
size_t iMaxResults,
419+
double iMaxDistance,
419420
double iMaxDistSquared) const {
420421
#if defined(_WIN32) || defined(_WIN64)
421422
// On Windows, isnan throws on anything that is not float, double or long double
@@ -429,8 +430,8 @@ class Flatbush {
429430
const auto wDistY = axisDistance(iPoint.mY, mBounds.mMinY, mBounds.mMaxY);
430431
const auto wDistance = wDistX * wDistX + wDistY * wDistY;
431432

432-
return !wIsNanPoint && iMaxResults != 0UL && !std::isnan(iMaxDistSquared) &&
433-
iMaxDistSquared >= 0.0 && wDistance < iMaxDistSquared;
433+
return !wIsNanPoint && iMaxResults != 0UL && iMaxDistance > 0.0 && !std::isnan(wDistance) &&
434+
std::isnormal(iMaxDistSquared) && wDistance <= iMaxDistSquared;
434435
}
435436

436437
Flatbush(uint32_t iNumItems, uint16_t iNodeSize) noexcept;
@@ -758,7 +759,7 @@ std::vector<size_t> Flatbush<ArrayType>::neighbors(const Point<ArrayType>& iPoin
758759
double iMaxDistance,
759760
const FilterCb& iFilterFn) const noexcept {
760761
const auto wMaxDistSquared = iMaxDistance * iMaxDistance;
761-
const auto wCanLoop = canDoNeighbors(iPoint, iMaxResults, wMaxDistSquared);
762+
const auto wCanLoop = canDoNeighbors(iPoint, iMaxResults, iMaxDistance, wMaxDistSquared);
762763
const auto wNumItems = numItems();
763764
const auto wNodeSize = nodeSize();
764765
auto wNodeIndex = mBoxes.size() - 1UL;

fuzz_neighbors.cpp

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -35,22 +35,22 @@ flatbush::Flatbush<double> createIndex() {
3535
return wIndex;
3636
}
3737

38-
extern "C" int LLVMFuzzerTestOneInput(const uint8_t *iData, size_t iSize) {
38+
extern "C" int LLVMFuzzerTestOneInput(const uint8_t* iData, size_t iSize) {
3939
static auto sIndex = createIndex();
4040

4141
if (iSize == 32) {
42-
const auto wX = *flatbush::detail::bit_cast<const double *>(&iData[0]);
43-
const auto wY = *flatbush::detail::bit_cast<const double *>(&iData[8]);
44-
const flatbush::Point<double> wPoint{wX, wY};
45-
46-
const auto wMaxResults = *flatbush::detail::bit_cast<const size_t *>(&iData[16]);
47-
const auto wMaxDistance = *flatbush::detail::bit_cast<const double *>(&iData[24]);
48-
49-
auto wResult = sIndex.neighbors(wPoint, wMaxResults, wMaxDistance);
42+
const auto wX = *flatbush::detail::bit_cast<const double*>(&iData[0]);
43+
const auto wY = *flatbush::detail::bit_cast<const double*>(&iData[8]);
44+
const auto wMaxResults = *flatbush::detail::bit_cast<const size_t*>(&iData[16]);
45+
const auto wMaxDistance = *flatbush::detail::bit_cast<const double*>(&iData[24]);
46+
const auto wMaxDistSquared = wMaxDistance * wMaxDistance;
5047

48+
const flatbush::Point<double> wPoint{wX, wY};
49+
const auto wResult = sIndex.neighbors(wPoint, wMaxResults, wMaxDistance);
5150
const auto wDistance = std::pow(wX - 42, 2.0) + std::pow(wY, 2.0);
5251

53-
if (wMaxResults > 0 && wMaxDistance >= 0 && wDistance <= std::pow(wMaxDistance, 2.0)) {
52+
if (wMaxResults > 0 && wMaxDistance >= 0 && std::isnormal(wMaxDistSquared) &&
53+
wDistance <= wMaxDistSquared) {
5454
assert(wResult.size() == 1);
5555
} else {
5656
assert(wResult.size() == 0);

test.cpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -534,9 +534,8 @@ void quickSortImbalancedDataset() {
534534
std::cout << "quicksort should work with an imbalanced dataset" << std::endl;
535535

536536
static const auto linspace =
537-
[](double wStart, double wStop, uint32_t wNum, bool wEndpoint = true) {
538-
const auto wDiv = wEndpoint ? (wNum - 1) : wNum;
539-
const auto wStep = (wStop - wStart) / wDiv;
537+
[](double wStart, double wStop, uint32_t wNum) {
538+
const auto wStep = (wStop - wStart) / (wNum - 1);
540539
std::vector<double> wItems(wNum);
541540
for (uint32_t wIndex = 0; wIndex < wNum; ++wIndex) {
542541
wItems.at(wIndex) = wStart + wStep * static_cast<double>(wIndex);

0 commit comments

Comments
 (0)