Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix bugs found with fuzzing #156

Merged
merged 3 commits into from
Nov 3, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 18 additions & 32 deletions CDT/include/Triangulation.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -566,8 +566,6 @@ void Triangulation<T, TNearPointLocator>::insertEdgeIteration(
polyR.push_back(iVR);
outerTrisR.push_back(edgeNeighbor(t, iA, iVR));
VertInd iV = iA;
IndexSizeType nChainedHangingEdgesL = 0;
IndexSizeType nChainedHangingEdgesR = 0;

while(!t.containsVertex(iB))
{
Expand Down Expand Up @@ -596,42 +594,14 @@ void Triangulation<T, TNearPointLocator>::insertEdgeIteration(
locatePointLine(vertices[iVopo], a, b, distanceTolerance);
if(loc == PtLineLocation::Left)
{
// hanging edge check
// previous entry of the vertex in poly if edge is hanging
const IndexSizeType prev =
(polyL.size() - 2) - 2 * nChainedHangingEdgesL;
if(iVopo == polyL[prev])
{ // hanging edge
++nChainedHangingEdgesL;
outerTrisL[prev] = noNeighbor;
outerTrisL.push_back(noNeighbor);
}
else
{ // normal case
nChainedHangingEdgesL = 0;
outerTrisL.push_back(edgeNeighbor(tOpo, polyL.back(), iVopo));
}
outerTrisL.push_back(edgeNeighbor(tOpo, polyL.back(), iVopo));
polyL.push_back(iVopo);
iV = iVL;
iVL = iVopo;
}
else if(loc == PtLineLocation::Right)
{
// hanging edge check
// previous entry of the vertex in poly if edge is hanging
const IndexSizeType prev =
(polyR.size() - 2) - 2 * nChainedHangingEdgesR;
if(iVopo == polyR[prev])
{ // hanging edge
++nChainedHangingEdgesR;
outerTrisR[prev] = noNeighbor;
outerTrisR.push_back(noNeighbor);
}
else
{ // normal case
nChainedHangingEdgesR = 0;
outerTrisR.push_back(edgeNeighbor(tOpo, polyR.back(), iVopo));
}
outerTrisR.push_back(edgeNeighbor(tOpo, polyR.back(), iVopo));
polyR.push_back(iVopo);
iV = iVR;
iVR = iVopo;
Expand All @@ -655,6 +625,15 @@ void Triangulation<T, TNearPointLocator>::insertEdgeIteration(
pivotVertexTriangleCW(iA);
if(m_vertTris[iB] == intersected.back())
pivotVertexTriangleCW(iB);
// Handle outer triangles for the cases of hanging edges
typedef std::vector<TriInd>::iterator TriIndIt;
std::sort(intersected.begin(), intersected.end());
for(TriIndIt it = outerTrisR.begin(); it != outerTrisR.end(); ++it)
if(std::binary_search(intersected.begin(), intersected.end(), *it))
*it = noNeighbor;
for(TriIndIt it = outerTrisL.begin(); it != outerTrisL.end(); ++it)
if(std::binary_search(intersected.begin(), intersected.end(), *it))
*it = noNeighbor;
// Remove intersected triangles
typedef std::vector<TriInd>::const_iterator TriIndCit;
for(TriIndCit it = intersected.begin(); it != intersected.end(); ++it)
Expand Down Expand Up @@ -969,6 +948,13 @@ void Triangulation<T, TNearPointLocator>::addSuperTriangle(const Box2d<T>& box)
const T h = box.max.y - box.min.y;
T r = std::sqrt(w * w + h * h) / T(2); // incircle radius
r *= T(1.1);

// Note: for very large floating point numbers rounding can lead to wrong
// super-triangle coordinates. This is a very rare corner-case so the
// handling is very primitive.
while(center.y <= center.y - r) // '<=' means '==' but avoids the warning
r *= T(2);

const T R = T(2) * r; // excircle radius
const T shiftX = R * std::sqrt(T(3)) / T(2); // R * cos(30 deg)
const V2d<T> posV1 = {center.x - shiftX, center.y - r};
Expand Down
79 changes: 79 additions & 0 deletions CDT/tests/cdt.test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -862,3 +862,82 @@ TEST_CASE("Regression test", "")
cdt.insertEdges(ee);
REQUIRE(CDT::verifyTopology(cdt));
}

TEST_CASE("Regression test issue #154 (1)", "")
{
// Very large coordinate values lead to wrong super-triangle coordinates due
// to the floating-point rounding
auto cdt = Triangulation<double>{};
cdt.insertVertices({
{0.0, 1e38},
{1.0, 1e38},
});
REQUIRE(CDT::verifyTopology(cdt));
const auto outFile = "expected/154_1.txt";
if(updateFiles)
topologyToFile(outFile, cdt);
else
{
REQUIRE(topologyString(cdt) == topologyString(outFile));
}
}

TEST_CASE("Regression test issue #154 (2)", "")
{
// Explanation: There was an incorrect assumptions that there are no 'loops'
// in the pseudo-polygons, only 'hanging' edges. The loops are possible as
// shown by this test case.
auto cdt = Triangulation<double>{};
cdt.insertVertices({
{2.0, -2.18933983E-5},
{-0.0896810815, -2.18407786E-5},
{-2.19008489E-5, -7.64692231E-6},
{8.73939061E-5, 0.00568488613},
{-0.00142463227, -0.00142461748},
{-7.67273832E-6, 8.7602064E-5},
{0.00569847599, -0.00142463227},
{-2.18156383E-5, -7.6295637E-6},
});
REQUIRE(CDT::verifyTopology(cdt));
cdt.insertEdges({
{0, 1},
});
REQUIRE(CDT::verifyTopology(cdt));
const auto outFile = "expected/154_2.txt";
if(updateFiles)
topologyToFile(outFile, cdt);
else
{
REQUIRE(topologyString(cdt) == topologyString(outFile));
}
}

TEST_CASE("Regression test issue #154 (3)", "")
{
// Explanation: There was an incorrect assumptions that there are no 'loops'
// in the pseudo-polygons, only 'hanging' edges. The loops are possible as
// shown by this test case.
auto cdt = Triangulation<double>{};
cdt.insertVertices({
{-2.0, 1.47656155},
{-6.40527344, -40.4999084},
{0.0, -7.96960115},
{-2.00152564, 1.46877956},
{-2.70361328, -7.99999619},
{-2.70465064, -7.99901962},
{-7.97778273, -19.3754253},
{7.96885204, -5.37488127},
{-7.97180128, -39.7499695},
});
cdt.insertEdges({
{0, 8},
});
REQUIRE(CDT::verifyTopology(cdt));
const auto outFile = "expected/154_3.txt";
if(updateFiles)
topologyToFile(outFile, cdt);
else
{
REQUIRE(topologyString(cdt) == topologyString(outFile));
}
}
12 changes: 12 additions & 0 deletions CDT/tests/expected/154_1.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
5
0 1 3 4294967295 3 1
0 3 2 0 4 4294967295
1 2 4 4294967295 4 3
1 4 3 2 4 0
2 3 4 1 3 2

0

0

0
25 changes: 25 additions & 0 deletions CDT/tests/expected/154_2.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
17
0 1 4 4294967295 4 1
0 4 2 0 6 4294967295
1 2 3 4294967295 7 3
1 3 9 2 8 5
1 7 4 5 14 0
1 9 7 3 14 4
2 4 6 1 13 7
2 6 3 6 10 2
3 4 9 9 14 3
3 5 4 12 13 8
3 6 8 7 15 11
3 8 10 10 16 12
3 10 5 11 16 9
4 5 6 9 15 6
4 7 9 4 5 8
5 8 6 16 10 13
5 10 8 12 11 15

1
3 4

0

0
27 changes: 27 additions & 0 deletions CDT/tests/expected/154_3.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
19
0 1 4 4294967295 6 2
0 3 2 3 7 4294967295
0 4 11 0 13 4
0 9 3 4 11 1
0 11 9 2 17 3
1 2 10 4294967295 7 6
1 10 4 5 14 0
2 3 10 1 8 5
3 5 10 10 14 7
3 6 11 11 17 12
3 8 5 12 16 8
3 9 6 3 17 9
3 11 8 9 18 10
4 5 11 14 15 2
4 10 5 6 8 13
5 7 11 16 18 13
5 8 7 10 18 15
6 9 11 11 4 9
7 8 11 16 12 15

1
3 11

0

0
1 change: 1 addition & 0 deletions docs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -261,6 +261,7 @@ For work-in-progress on Python bindings check-out [PythonCDT](https://github.com
- [alkonior](https://github.com/alkonior): finding, reproducing, and reporting a bug ([#142](https://github.com/artem-ogre/CDT/issues/142))
- [ldcMasa](https://github.com/ldcMasa): finding compilation issue ([#144](https://github.com/artem-ogre/CDT/issues/144))
- [egladil86](https://github.com/egladil86): finding, reproducing, and reporting a bug ([#148](https://github.com/artem-ogre/CDT/issues/148))
- [Som1Lse](https://github.com/Som1Lse): fuzzing CDT: finding, reproducing, and reporting bugs ([#154](https://github.com/artem-ogre/CDT/issues/148))

<a name="contributing"></a>

Expand Down