@@ -1556,6 +1556,17 @@ struct sPolyExtended
15561556 pBounds->maxx = poPolyEx->sEnvelope .MaxX ;
15571557 pBounds->maxy = poPolyEx->sEnvelope .MaxY ;
15581558 }
1559+
1560+ // recent libc++ std::sort() involve unsigned integer overflow in some
1561+ // situation
1562+ CPL_NOSANITIZE_UNSIGNED_INT_OVERFLOW
1563+ static void SortByIncreasingEra (const sPolyExtended **pStart,
1564+ const sPolyExtended **pEnd)
1565+ {
1566+ std::sort (pStart, pEnd,
1567+ [](const sPolyExtended *psPoly1, const sPolyExtended *psPoly2)
1568+ { return psPoly1->dfArea < psPoly2->dfArea ; });
1569+ }
15591570};
15601571
15611572static bool OGRGeometryFactoryCompareAreaDescending (const sPolyExtended &sPoly1 ,
@@ -2026,24 +2037,21 @@ OGRGeometry *OGRGeometryFactory::organizePolygons(OGRGeometry **papoPolygons,
20262037 aoi.maxx = thisPoly.sEnvelope .MaxX ;
20272038 aoi.maxy = thisPoly.sEnvelope .MaxY ;
20282039 int nCandidates = 0 ;
2029- std::unique_ptr<void *, decltype (&CPLFree)> aphCandidateShells (
2030- CPLQuadTreeSearch (poQuadTree.get (), &aoi, &nCandidates), CPLFree);
2040+ std::unique_ptr<const sPolyExtended *, decltype (&CPLFree)>
2041+ aphCandidateShells (
2042+ const_cast <const sPolyExtended **>(
2043+ reinterpret_cast <sPolyExtended **>(CPLQuadTreeSearch (
2044+ poQuadTree.get (), &aoi, &nCandidates))),
2045+ CPLFree);
20312046
20322047 // Sort candidate outer rings by increasing area
2033- std::sort (
2034- aphCandidateShells.get (), aphCandidateShells.get () + nCandidates,
2035- [](void *hFeature1, void *hFeature2)
2036- {
2037- const auto &sPoly1 = *(static_cast <sPolyExtended *>(hFeature1));
2038- const auto &sPoly2 = *(static_cast <sPolyExtended *>(hFeature2));
2039- return sPoly1 .dfArea < sPoly2 .dfArea ;
2040- });
2048+ sPolyExtended::SortByIncreasingEra (
2049+ aphCandidateShells.get (), aphCandidateShells.get () + nCandidates);
20412050
20422051 int j = 0 ;
20432052 for (; bValidTopology && j < nCandidates; j++)
20442053 {
2045- const auto &otherPoly = *static_cast <const sPolyExtended *>(
2046- (aphCandidateShells.get ())[j]);
2054+ const auto &otherPoly = *(aphCandidateShells.get ()[j]);
20472055
20482056 if (method == METHOD_ONLY_CCW && otherPoly.bIsCW == false )
20492057 {
0 commit comments