diff --git a/apps/gdal_footprint_lib.cpp b/apps/gdal_footprint_lib.cpp index 128065a490c7..a81c185abf8b 100644 --- a/apps/gdal_footprint_lib.cpp +++ b/apps/gdal_footprint_lib.cpp @@ -560,10 +560,10 @@ class GeoTransformCoordinateTransformation final return nullptr; } - int Transform(int nCount, double *x, double *y, double * /* z */, + int Transform(size_t nCount, double *x, double *y, double * /* z */, double * /* t */, int *pabSuccess) override { - for (int i = 0; i < nCount; ++i) + for (size_t i = 0; i < nCount; ++i) { const double X = m_gt[0] + x[i] * m_gt[1] + y[i] * m_gt[2]; const double Y = m_gt[3] + x[i] * m_gt[4] + y[i] * m_gt[5]; diff --git a/apps/gdalwarp_lib.cpp b/apps/gdalwarp_lib.cpp index 164a1a8e28d9..6c5969cfe7dd 100644 --- a/apps/gdalwarp_lib.cpp +++ b/apps/gdalwarp_lib.cpp @@ -4700,11 +4700,14 @@ class CutlineTransformer : public OGRCoordinateTransformation GDALDestroyTransformer(hSrcImageTransformer); } - virtual int Transform(int nCount, double *x, double *y, double *z, + virtual int Transform(size_t nCount, double *x, double *y, double *z, double * /* t */, int *pabSuccess) override { - return GDALGenImgProjTransform(hSrcImageTransformer, TRUE, nCount, x, y, - z, pabSuccess); + CPLAssert(nCount <= + static_cast(std::numeric_limits::max())); + return GDALGenImgProjTransform(hSrcImageTransformer, TRUE, + static_cast(nCount), x, y, z, + pabSuccess); } virtual OGRCoordinateTransformation *Clone() const override diff --git a/apps/ogr2ogr_lib.cpp b/apps/ogr2ogr_lib.cpp index edacb43c5eec..3acb6d596b77 100644 --- a/apps/ogr2ogr_lib.cpp +++ b/apps/ogr2ogr_lib.cpp @@ -39,6 +39,7 @@ #include #include +#include #include #include #include @@ -1127,14 +1128,18 @@ class GCPCoordTransformation : public OGRCoordinateTransformation return poSRS; } - virtual int Transform(int nCount, double *x, double *y, double *z, + virtual int Transform(size_t nCount, double *x, double *y, double *z, double * /* t */, int *pabSuccess) override { + CPLAssert(nCount <= + static_cast(std::numeric_limits::max())); if (bUseTPS) - return GDALTPSTransform(hTransformArg, FALSE, nCount, x, y, z, + return GDALTPSTransform(hTransformArg, FALSE, + static_cast(nCount), x, y, z, pabSuccess); else - return GDALGCPTransform(hTransformArg, FALSE, nCount, x, y, z, + return GDALGCPTransform(hTransformArg, FALSE, + static_cast(nCount), x, y, z, pabSuccess); } @@ -1214,7 +1219,7 @@ class CompositeCT : public OGRCoordinateTransformation poCT2->SetEmitErrors(bEmitErrors); } - virtual int Transform(int nCount, double *x, double *y, double *z, + virtual int Transform(size_t nCount, double *x, double *y, double *z, double *t, int *pabSuccess) override { int nResult = TRUE; @@ -1280,10 +1285,10 @@ class AxisMappingCoordinateTransformation : public OGRCoordinateTransformation return nullptr; } - virtual int Transform(int nCount, double *x, double *y, double * /*z*/, + virtual int Transform(size_t nCount, double *x, double *y, double * /*z*/, double * /*t*/, int *pabSuccess) override { - for (int i = 0; i < nCount; i++) + for (size_t i = 0; i < nCount; i++) { if (pabSuccess) pabSuccess[i] = true; diff --git a/frmts/vrt/vrtwarped.cpp b/frmts/vrt/vrtwarped.cpp index d1dfe87a25a4..cc5a93e5bd8d 100644 --- a/frmts/vrt/vrtwarped.cpp +++ b/frmts/vrt/vrtwarped.cpp @@ -651,10 +651,10 @@ class GDALWarpCoordRescaler : public OGRCoordinateTransformation return nullptr; } - virtual int Transform(int nCount, double *x, double *y, double * /*z*/, + virtual int Transform(size_t nCount, double *x, double *y, double * /*z*/, double * /*t*/, int *pabSuccess) override { - for (int i = 0; i < nCount; i++) + for (size_t i = 0; i < nCount; i++) { x[i] *= m_dfRatioX; y[i] *= m_dfRatioY; diff --git a/ogr/ogr_spatialref.h b/ogr/ogr_spatialref.h index 2d819993a50b..c5f56bafb4fb 100644 --- a/ogr/ogr_spatialref.h +++ b/ogr/ogr_spatialref.h @@ -802,7 +802,8 @@ class CPL_DLL OGRCoordinateTransformation * * This method is the same as the C function OCTTransformEx(). * - * @param nCount number of points to transform. + * @param nCount number of points to transform (`size_t` type since 3.9, + * `int` in previous versions). * @param x array of nCount X vertices, modified in place. Should not be * NULL. * @param y array of nCount Y vertices, modified in place. Should not be @@ -814,7 +815,7 @@ class CPL_DLL OGRCoordinateTransformation * @return TRUE if some or all points transform successfully, or FALSE if * if none transform. */ - int Transform(int nCount, double *x, double *y, double *z = nullptr, + int Transform(size_t nCount, double *x, double *y, double *z = nullptr, int *pabSuccess = nullptr); /** @@ -822,7 +823,8 @@ class CPL_DLL OGRCoordinateTransformation * * This method is the same as the C function OCTTransform4D(). * - * @param nCount number of points to transform. + * @param nCount number of points to transform (`size_t` type since 3.9, + * `int` in previous versions). * @param x array of nCount X vertices, modified in place. Should not be * NULL. * @param y array of nCount Y vertices, modified in place. Should not be @@ -835,7 +837,7 @@ class CPL_DLL OGRCoordinateTransformation * @return TRUE if some or all points transform successfully, or FALSE if * if none transform. */ - virtual int Transform(int nCount, double *x, double *y, double *z, + virtual int Transform(size_t nCount, double *x, double *y, double *z, double *t, int *pabSuccess) = 0; /** @@ -843,7 +845,8 @@ class CPL_DLL OGRCoordinateTransformation * * This method is the same as the C function OCTTransform4DWithErrorCodes(). * - * @param nCount number of points to transform. + * @param nCount number of points to transform (`size_t` type since 3.9, + * `int` in previous versions). * @param x array of nCount X vertices, modified in place. Should not be * NULL. * @param y array of nCount Y vertices, modified in place. Should not be @@ -857,7 +860,7 @@ class CPL_DLL OGRCoordinateTransformation * if none transform. * @since GDAL 3.3, and PROJ 8 to be able to use PROJ public error codes */ - virtual int TransformWithErrorCodes(int nCount, double *x, double *y, + virtual int TransformWithErrorCodes(size_t nCount, double *x, double *y, double *z, double *t, int *panErrorCodes); diff --git a/ogr/ogrct.cpp b/ogr/ogrct.cpp index 180ea9ba477b..782fa6943f25 100644 --- a/ogr/ogrct.cpp +++ b/ogr/ogrct.cpp @@ -740,7 +740,7 @@ class OGRProjCT : public OGRCoordinateTransformation bool bWebMercatorToWGS84LongLat = false; - int nErrorCount = 0; + size_t nErrorCount = 0; double dfThreshold = 0.0; @@ -815,10 +815,10 @@ class OGRProjCT : public OGRCoordinateTransformation const OGRSpatialReference *GetSourceCS() const override; const OGRSpatialReference *GetTargetCS() const override; - int Transform(int nCount, double *x, double *y, double *z, double *t, + int Transform(size_t nCount, double *x, double *y, double *z, double *t, int *pabSuccess) override; - int TransformWithErrorCodes(int nCount, double *x, double *y, double *z, + int TransformWithErrorCodes(size_t nCount, double *x, double *y, double *z, double *t, int *panErrorCodes) override; int TransformBounds(const double xmin, const double ymin, const double xmax, @@ -2170,18 +2170,21 @@ const OGRSpatialReference *OGRProjCT::GetTargetCS() const /* Transform() */ /************************************************************************/ -int OGRCoordinateTransformation::Transform(int nCount, double *x, double *y, +int OGRCoordinateTransformation::Transform(size_t nCount, double *x, double *y, double *z, int *pabSuccessIn) { - int *pabSuccess = pabSuccessIn - ? pabSuccessIn - : static_cast(CPLMalloc(sizeof(int) * nCount)); + int *pabSuccess = + pabSuccessIn + ? pabSuccessIn + : static_cast(VSI_MALLOC2_VERBOSE(sizeof(int), nCount)); + if (!pabSuccess) + return FALSE; bool bOverallSuccess = CPL_TO_BOOL(Transform(nCount, x, y, z, nullptr, pabSuccess)); - for (int i = 0; i < nCount; i++) + for (size_t i = 0; i < nCount; i++) { if (!pabSuccess[i]) { @@ -2200,20 +2203,30 @@ int OGRCoordinateTransformation::Transform(int nCount, double *x, double *y, /* TransformWithErrorCodes() */ /************************************************************************/ -int OGRCoordinateTransformation::TransformWithErrorCodes(int nCount, double *x, - double *y, double *z, - double *t, +int OGRCoordinateTransformation::TransformWithErrorCodes(size_t nCount, + double *x, double *y, + double *z, double *t, int *panErrorCodes) { - std::vector abSuccess(nCount + 1); + std::vector abSuccess; + try + { + abSuccess.resize(nCount); + } + catch (const std::bad_alloc &) + { + CPLError(CE_Failure, CPLE_OutOfMemory, + "Cannot allocate abSuccess[] temporary array"); + return FALSE; + } - bool bOverallSuccess = - CPL_TO_BOOL(Transform(nCount, x, y, z, t, &abSuccess[0])); + const bool bOverallSuccess = + CPL_TO_BOOL(Transform(nCount, x, y, z, t, abSuccess.data())); if (panErrorCodes) { - for (int i = 0; i < nCount; i++) + for (size_t i = 0; i < nCount; i++) { panErrorCodes[i] = abSuccess[i] ? 0 : -1; } @@ -2226,8 +2239,8 @@ int OGRCoordinateTransformation::TransformWithErrorCodes(int nCount, double *x, /* Transform() */ /************************************************************************/ -int OGRProjCT::Transform(int nCount, double *x, double *y, double *z, double *t, - int *pabSuccess) +int OGRProjCT::Transform(size_t nCount, double *x, double *y, double *z, + double *t, int *pabSuccess) { bool bOverallSuccess = @@ -2235,7 +2248,7 @@ int OGRProjCT::Transform(int nCount, double *x, double *y, double *z, double *t, if (pabSuccess) { - for (int i = 0; i < nCount; i++) + for (size_t i = 0; i < nCount; i++) { pabSuccess[i] = (pabSuccess[i] == 0); } @@ -2254,7 +2267,7 @@ int OGRProjCT::Transform(int nCount, double *x, double *y, double *z, double *t, #define PROJ_ERR_COORD_TRANSFM_NO_OPERATION 2051 #endif -int OGRProjCT::TransformWithErrorCodes(int nCount, double *x, double *y, +int OGRProjCT::TransformWithErrorCodes(size_t nCount, double *x, double *y, double *z, double *t, int *panErrorCodes) { @@ -2266,7 +2279,7 @@ int OGRProjCT::TransformWithErrorCodes(int nCount, double *x, double *y, { if (panErrorCodes) { - for (int i = 0; i < nCount; i++) + for (size_t i = 0; i < nCount; i++) { panErrorCodes[i] = 0; } @@ -2279,10 +2292,10 @@ int OGRProjCT::TransformWithErrorCodes(int nCount, double *x, double *y, if (bDebugCT) { CPLDebug("OGRCT", "count = %d", nCount); - for (int i = 0; i < nCount; ++i) + for (size_t i = 0; i < nCount; ++i) { - CPLDebug("OGRCT", " x[%d] = %.16g y[%d] = %.16g", i, x[i], i, - y[i]); + CPLDebug("OGRCT", " x[%d] = %.16g y[%d] = %.16g", int(i), x[i], + int(i), y[i]); } } #endif @@ -2300,7 +2313,7 @@ int OGRProjCT::TransformWithErrorCodes(int nCount, double *x, double *y, const auto &mapping = poSRSSource->GetDataAxisToSRSAxisMapping(); if (mapping.size() >= 2 && (mapping[0] != 1 || mapping[1] != 2)) { - for (int i = 0; i < nCount; i++) + for (size_t i = 0; i < nCount; i++) { double newX = (mapping[0] == 1) ? x[i] : (mapping[0] == -1) ? -x[i] @@ -2325,7 +2338,7 @@ int OGRProjCT::TransformWithErrorCodes(int nCount, double *x, double *y, { if (m_eSourceFirstAxisOrient == OAO_East) { - for (int i = 0; i < nCount; i++) + for (size_t i = 0; i < nCount; i++) { if (x[i] != HUGE_VAL && y[i] != HUGE_VAL) { @@ -2338,7 +2351,7 @@ int OGRProjCT::TransformWithErrorCodes(int nCount, double *x, double *y, } else { - for (int i = 0; i < nCount; i++) + for (size_t i = 0; i < nCount; i++) { if (x[i] != HUGE_VAL && y[i] != HUGE_VAL) { @@ -2361,14 +2374,14 @@ int OGRProjCT::TransformWithErrorCodes(int nCount, double *x, double *y, if (m_eSourceFirstAxisOrient != OAO_East) { - for (int i = 0; i < nCount; i++) + for (size_t i = 0; i < nCount; i++) { std::swap(x[i], y[i]); } } double y0 = y[0]; - for (int i = 0; i < nCount; i++) + for (size_t i = 0; i < nCount; i++) { if (x[i] != HUGE_VAL) { @@ -2433,7 +2446,7 @@ int OGRProjCT::TransformWithErrorCodes(int nCount, double *x, double *y, if (panErrorCodes) { - for (int i = 0; i < nCount; i++) + for (size_t i = 0; i < nCount; i++) { if (x[i] != HUGE_VAL) panErrorCodes[i] = 0; @@ -2445,7 +2458,7 @@ int OGRProjCT::TransformWithErrorCodes(int nCount, double *x, double *y, if (m_eTargetFirstAxisOrient != OAO_East) { - for (int i = 0; i < nCount; i++) + for (size_t i = 0; i < nCount; i++) { std::swap(x[i], y[i]); } @@ -2490,8 +2503,8 @@ int OGRProjCT::TransformWithErrorCodes(int nCount, double *x, double *y, { double avgX = 0.0; double avgY = 0.0; - int nCountValid = 0; - for (int i = 0; i < nCount; i++) + size_t nCountValid = 0; + for (size_t i = 0; i < nCount; i++) { if (x[i] != HUGE_VAL && y[i] != HUGE_VAL) { @@ -2502,8 +2515,8 @@ int OGRProjCT::TransformWithErrorCodes(int nCount, double *x, double *y, } if (nCountValid != 0) { - avgX /= nCountValid; - avgY /= nCountValid; + avgX /= static_cast(nCountValid); + avgY /= static_cast(nCountValid); } constexpr int N_MAX_RETRY = 2; @@ -2615,7 +2628,7 @@ int OGRProjCT::TransformWithErrorCodes(int nCount, double *x, double *y, "suppressed on the transform object."); } - for (int i = 0; i < nCount; i++) + for (size_t i = 0; i < nCount; i++) { x[i] = HUGE_VAL; y[i] = HUGE_VAL; @@ -2638,7 +2651,7 @@ int OGRProjCT::TransformWithErrorCodes(int nCount, double *x, double *y, { const auto nLastErrorCounter = CPLGetErrorCounter(); - for (int i = 0; i < nCount; i++) + for (size_t i = 0; i < nCount; i++) { PJ_COORD coord; const double xIn = x[i]; @@ -2785,7 +2798,7 @@ int OGRProjCT::TransformWithErrorCodes(int nCount, double *x, double *y, { if (m_eTargetFirstAxisOrient == OAO_East) { - for (int i = 0; i < nCount; i++) + for (size_t i = 0; i < nCount; i++) { if (x[i] != HUGE_VAL && y[i] != HUGE_VAL) { @@ -2798,7 +2811,7 @@ int OGRProjCT::TransformWithErrorCodes(int nCount, double *x, double *y, } else { - for (int i = 0; i < nCount; i++) + for (size_t i = 0; i < nCount; i++) { if (x[i] != HUGE_VAL && y[i] != HUGE_VAL) { @@ -2819,7 +2832,7 @@ int OGRProjCT::TransformWithErrorCodes(int nCount, double *x, double *y, const auto &mapping = poSRSTarget->GetDataAxisToSRSAxisMapping(); if (mapping.size() >= 2 && (mapping[0] != 1 || mapping[1] != 2)) { - for (int i = 0; i < nCount; i++) + for (size_t i = 0; i < nCount; i++) { double newX = (mapping[0] == 1) ? x[i] : (mapping[0] == -1) ? -x[i] @@ -2841,10 +2854,10 @@ int OGRProjCT::TransformWithErrorCodes(int nCount, double *x, double *y, if (bDebugCT) { CPLDebug("OGRCT", "Out:"); - for (int i = 0; i < nCount; ++i) + for (size_t i = 0; i < nCount; ++i) { - CPLDebug("OGRCT", " x[%d] = %.16g y[%d] = %.16g", i, x[i], i, - y[i]); + CPLDebug("OGRCT", " x[%d] = %.16g y[%d] = %.16g", int(i), x[i], + int(i), y[i]); } } #endif diff --git a/ogr/ogrsf_frmts/dwg/ogrdwglayer.cpp b/ogr/ogrsf_frmts/dwg/ogrdwglayer.cpp index 17271a10d618..0e0572cf3e00 100644 --- a/ogr/ogrsf_frmts/dwg/ogrdwglayer.cpp +++ b/ogr/ogrsf_frmts/dwg/ogrdwglayer.cpp @@ -1104,11 +1104,10 @@ class GeometryInsertTransformer : public OGRCoordinateTransformation return nullptr; } - int Transform(int nCount, double *x, double *y, double *z = nullptr, + int Transform(size_t nCount, double *x, double *y, double *z = nullptr, double * /*t*/ = nullptr, int *pabSuccess = nullptr) override { - int i; - for (i = 0; i < nCount; i++) + for (size_t i = 0; i < nCount; i++) { double dfXNew, dfYNew; diff --git a/ogr/ogrsf_frmts/dxf/ogr_dxf.h b/ogr/ogrsf_frmts/dxf/ogr_dxf.h index f85cedf7a9ce..ac9b3693fa3a 100644 --- a/ogr/ogrsf_frmts/dxf/ogr_dxf.h +++ b/ogr/ogrsf_frmts/dxf/ogr_dxf.h @@ -179,10 +179,10 @@ class OGRDXFInsertTransformer final : public OGRCoordinateTransformation return nullptr; } - int Transform(int nCount, double *x, double *y, double *z, double * /* t */, - int *pabSuccess) override + int Transform(size_t nCount, double *x, double *y, double *z, + double * /* t */, int *pabSuccess) override { - for (int i = 0; i < nCount; i++) + for (size_t i = 0; i < nCount; i++) { x[i] *= dfXScale; y[i] *= dfYScale; @@ -296,10 +296,11 @@ class OGRDXFOCSTransformer final : public OGRCoordinateTransformation return nullptr; } - int Transform(int nCount, double *adfX, double *adfY, double *adfZ, + int Transform(size_t nCount, double *adfX, double *adfY, double *adfZ, double *adfT, int *pabSuccess) override; - int InverseTransform(int nCount, double *adfX, double *adfY, double *adfZ); + int InverseTransform(size_t nCount, double *adfX, double *adfY, + double *adfZ); void ComposeOnto(OGRDXFAffineTransform &poCT) const; diff --git a/ogr/ogrsf_frmts/dxf/ogrdxf_ocstransformer.cpp b/ogr/ogrsf_frmts/dxf/ogrdxf_ocstransformer.cpp index b29b3d31a5e5..e8fe22cdce17 100644 --- a/ogr/ogrsf_frmts/dxf/ogrdxf_ocstransformer.cpp +++ b/ogr/ogrsf_frmts/dxf/ogrdxf_ocstransformer.cpp @@ -125,11 +125,11 @@ OGRDXFOCSTransformer::OGRDXFOCSTransformer(double adfNIn[3], /* Transform() */ /************************************************************************/ -int OGRDXFOCSTransformer::Transform(int nCount, double *adfX, double *adfY, +int OGRDXFOCSTransformer::Transform(size_t nCount, double *adfX, double *adfY, double *adfZ, double * /* adfT */, int *pabSuccess /* = nullptr */) { - for (int i = 0; i < nCount; i++) + for (size_t i = 0; i < nCount; i++) { const double x = adfX[i]; const double y = adfY[i]; @@ -149,13 +149,13 @@ int OGRDXFOCSTransformer::Transform(int nCount, double *adfX, double *adfY, /* InverseTransform() */ /************************************************************************/ -int OGRDXFOCSTransformer::InverseTransform(int nCount, double *adfX, +int OGRDXFOCSTransformer::InverseTransform(size_t nCount, double *adfX, double *adfY, double *adfZ) { if (dfDeterminant == 0.0) return FALSE; - for (int i = 0; i < nCount; i++) + for (size_t i = 0; i < nCount; i++) { const double x = adfX[i]; const double y = adfY[i];