diff --git a/alg/gdalchecksum.cpp b/alg/gdalchecksum.cpp index 64f6158433b8..e1f5e38f1b1d 100644 --- a/alg/gdalchecksum.cpp +++ b/alg/gdalchecksum.cpp @@ -368,5 +368,6 @@ int CPL_STDCALL GDALChecksumImage(GDALRasterBandH hBand, int nXOff, int nYOff, CPLFree(panLineData); } + // coverity[return_overflow] return nChecksum; } diff --git a/gcore/gdalmultidim_gltorthorectification.cpp b/gcore/gdalmultidim_gltorthorectification.cpp index 4baf11e31863..dfa7c38826d2 100644 --- a/gcore/gdalmultidim_gltorthorectification.cpp +++ b/gcore/gdalmultidim_gltorthorectification.cpp @@ -129,14 +129,14 @@ class GLTOrthoRectifiedArray final : public GDALPamMDArray {/*latIdx = */ 1, /* lonIdx = */ 2}); if (CPLTestBool(CSLFetchNameValueDef(papszOptions, "USE_GOOD_WAVELENGTHS", "YES")) && - poParent->GetDimensionCount() == 3) + newAr->m_poParent->GetDimensionCount() == 3) { const auto poGoodWaveLengths = poRootGroup->OpenMDArrayFromFullname( "/sensor_band_parameters/good_wavelengths"); if (poGoodWaveLengths && poGoodWaveLengths->GetDimensionCount() == 1 && poGoodWaveLengths->GetDimensions()[0]->GetSize() == - poParent->GetDimensions()[2]->GetSize() && + newAr->m_poParent->GetDimensions()[2]->GetSize() && poGoodWaveLengths->GetDimensions()[0]->GetSize() < 1000 * 1000 && poGoodWaveLengths->GetDataType().GetClass() == GEDTC_NUMERIC) diff --git a/ogr/ogrsf_frmts/arrow_common/ograrrowlayer.hpp b/ogr/ogrsf_frmts/arrow_common/ograrrowlayer.hpp index b364925ea9a9..51b71cf4b332 100644 --- a/ogr/ogrsf_frmts/arrow_common/ograrrowlayer.hpp +++ b/ogr/ogrsf_frmts/arrow_common/ograrrowlayer.hpp @@ -2385,8 +2385,9 @@ inline OGRFeature *OGRArrowLayer::ReadFeature( arrow::LargeBinaryArray::offset_type out_length = 0; const uint8_t *data = castArray->GetValue(nIdxInBatch, &out_length); - if (out_length <= INT_MAX - 1) + if (out_length >= 0 && out_length <= INT_MAX - 1) { + // coverity[overflow_sink] poFeature->SetField(i, static_cast(out_length), data); } else diff --git a/ogr/ogrsf_frmts/gpkg/ogrgeopackagetablelayer.cpp b/ogr/ogrsf_frmts/gpkg/ogrgeopackagetablelayer.cpp index be706dbebb4a..074dcad5d3cd 100644 --- a/ogr/ogrsf_frmts/gpkg/ogrgeopackagetablelayer.cpp +++ b/ogr/ogrsf_frmts/gpkg/ogrgeopackagetablelayer.cpp @@ -7771,33 +7771,36 @@ void OGR_GPKG_FillArrowArray_Step(sqlite3_context *pContext, int /*argc*/, auto psFillArrowArray = static_cast( sqlite3_user_data(pContext)); - if (psFillArrowArray->nCountRows >= - psFillArrowArray->psHelper->m_nMaxBatchSize) { - if (psFillArrowArray->bAsynchronousMode) + std::unique_lock oLock(psFillArrowArray->oMutex); + if (psFillArrowArray->nCountRows >= + psFillArrowArray->psHelper->m_nMaxBatchSize) { - std::unique_lock oLock(psFillArrowArray->oMutex); - psFillArrowArray->psHelper->Shrink(psFillArrowArray->nCountRows); - psFillArrowArray->oCV.notify_one(); - while (psFillArrowArray->nCountRows > 0) + if (psFillArrowArray->bAsynchronousMode) { - psFillArrowArray->oCV.wait(oLock); + psFillArrowArray->psHelper->Shrink( + psFillArrowArray->nCountRows); + psFillArrowArray->oCV.notify_one(); + while (psFillArrowArray->nCountRows > 0) + { + psFillArrowArray->oCV.wait(oLock); + } + // Note that psFillArrowArray->psHelper.get() will generally now be + // different from before the wait() + } + else + { + // should not happen ! + psFillArrowArray->osErrorMsg = "OGR_GPKG_FillArrowArray_Step() " + "got more rows than expected!"; + sqlite3_interrupt(psFillArrowArray->hDB); + psFillArrowArray->bErrorOccurred = true; + return; } - // Note that psFillArrowArray->psHelper.get() will generally now be - // different from before the wait() } - else - { - // should not happen ! - psFillArrowArray->osErrorMsg = - "OGR_GPKG_FillArrowArray_Step() got more rows than expected!"; - sqlite3_interrupt(psFillArrowArray->hDB); - psFillArrowArray->bErrorOccurred = true; + if (psFillArrowArray->nCountRows < 0) return; - } } - if (psFillArrowArray->nCountRows < 0) - return; if (psFillArrowArray->nMemLimit == 0) psFillArrowArray->nMemLimit = OGRArrowArrayHelper::GetMemLimit(); @@ -7943,7 +7946,7 @@ void OGR_GPKG_FillArrowArray_Step(sqlite3_context *pContext, int /*argc*/, } } - if (psFillArrowArray->nCountRows > 0) + if (iFeat > 0) { auto panOffsets = static_cast( const_cast(psArray->buffers[1])); @@ -7956,7 +7959,7 @@ void OGR_GPKG_FillArrowArray_Step(sqlite3_context *pContext, int /*argc*/, "OGR_GPKG_FillArrowArray_Step(): premature " "notification of %d features to consumer due " "to too big array", - psFillArrowArray->nCountRows); + iFeat); psFillArrowArray->bMemoryLimitReached = true; if (psFillArrowArray->bAsynchronousMode) { @@ -8081,7 +8084,7 @@ void OGR_GPKG_FillArrowArray_Step(sqlite3_context *pContext, int /*argc*/, const void *pabyData = sqlite3_value_blob(argv[iCol]); if (pabyData != nullptr || nBytes == 0) { - if (psFillArrowArray->nCountRows > 0) + if (iFeat > 0) { auto panOffsets = static_cast( const_cast(psArray->buffers[1])); @@ -8094,7 +8097,7 @@ void OGR_GPKG_FillArrowArray_Step(sqlite3_context *pContext, int /*argc*/, "OGR_GPKG_FillArrowArray_Step(): " "premature notification of %d features to " "consumer due to too big array", - psFillArrowArray->nCountRows); + iFeat); psFillArrowArray->bMemoryLimitReached = true; if (psFillArrowArray->bAsynchronousMode) { @@ -8171,7 +8174,7 @@ void OGR_GPKG_FillArrowArray_Step(sqlite3_context *pContext, int /*argc*/, if (pszTxt != nullptr) { const size_t nBytes = strlen(pszTxt); - if (psFillArrowArray->nCountRows > 0) + if (iFeat > 0) { auto panOffsets = static_cast( const_cast(psArray->buffers[1])); @@ -8184,7 +8187,7 @@ void OGR_GPKG_FillArrowArray_Step(sqlite3_context *pContext, int /*argc*/, "OGR_GPKG_FillArrowArray_Step(): " "premature notification of %d features to " "consumer due to too big array", - psFillArrowArray->nCountRows); + iFeat); psFillArrowArray->bMemoryLimitReached = true; if (psFillArrowArray->bAsynchronousMode) { diff --git a/ogr/ogrsf_frmts/shape/ogrshapedatasource.cpp b/ogr/ogrsf_frmts/shape/ogrshapedatasource.cpp index 69e45b7d4b50..eb102e8c3b8a 100644 --- a/ogr/ogrsf_frmts/shape/ogrshapedatasource.cpp +++ b/ogr/ogrsf_frmts/shape/ogrshapedatasource.cpp @@ -1500,8 +1500,10 @@ bool OGRShapeDataSource::UncompressIfNeeded() return false; } m_psLockFile = f; + CPLAcquireMutex(m_poRefreshLockFileMutex, 1000); m_bExitRefreshLockFileThread = false; m_bRefreshLockFileThreadStarted = false; + CPLReleaseMutex(m_poRefreshLockFileMutex); // Config option mostly for testing purposes // coverity[tainted_data] m_dfRefreshLockDelay = CPLAtof(CPLGetConfigOption( diff --git a/port/cpl_vsi_mem.cpp b/port/cpl_vsi_mem.cpp index 17f57ef9fa88..a4ccaedd9266 100644 --- a/port/cpl_vsi_mem.cpp +++ b/port/cpl_vsi_mem.cpp @@ -421,25 +421,36 @@ size_t VSIMemHandle::Read(void *pBuffer, size_t nSize, size_t nCount) return 0; } + bool bEOFTmp = bEOF; + // Do not access/modify bEOF under the lock to avoid confusing Coverity + // Scan since we access it in other methods outside of the lock. + const auto DoUnderLock = + [this, nOffset, pBuffer, nSize, &nBytesToRead, &nCount, &bEOFTmp] { CPL_SHARED_LOCK oLock(poFile->m_oMutex); if (poFile->nLength <= nOffset || nBytesToRead + nOffset < nBytesToRead) { - bEOF = true; - return 0; + bEOFTmp = true; + return false; } if (nBytesToRead + nOffset > poFile->nLength) { nBytesToRead = static_cast(poFile->nLength - nOffset); nCount = nBytesToRead / nSize; - bEOF = true; + bEOFTmp = true; } if (nBytesToRead) memcpy(pBuffer, poFile->pabyData + nOffset, static_cast(nBytesToRead)); - } + return true; + }; + + bool bRet = DoUnderLock(); + bEOF = bEOFTmp; + if (!bRet) + return 0; m_nOffset += nBytesToRead;