Skip to content

Commit

Permalink
Fix Coverity warnings (mostly false positives)
Browse files Browse the repository at this point in the history
  • Loading branch information
rouault committed Sep 8, 2024
1 parent e4fa8f4 commit f396e32
Show file tree
Hide file tree
Showing 9 changed files with 40 additions and 24 deletions.
38 changes: 24 additions & 14 deletions alg/viewshed/cumulative.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -136,24 +136,34 @@ void Cumulative::runExecutor(const std::string &srcFilename, Progress &progress,
{
DatasetPtr srcDs(GDALDataset::Open(srcFilename.c_str(), GA_ReadOnly));
if (!srcDs)
{
err = true;

Location loc;
while (!err && m_observerQueue.pop(loc))
}
else
{
GDALDriver *memDriver = GetGDALDriverManager()->GetDriverByName("MEM");
DatasetPtr dstDs(memDriver->Create(
"", m_extent.xSize(), m_extent.ySize(), 1, GDT_Byte, nullptr));
err = (dstDs == nullptr);
if (!err)
Location loc;
while (!err && m_observerQueue.pop(loc))
{
ViewshedExecutor executor(*srcDs->GetRasterBand(1),
*dstDs->GetRasterBand(1), loc.x, loc.y,
m_extent, m_extent, m_opts, progress);
err = !executor.run();
GDALDriver *memDriver =
GetGDALDriverManager()->GetDriverByName("MEM");
DatasetPtr dstDs(memDriver ? memDriver->Create("", m_extent.xSize(),
m_extent.ySize(), 1,
GDT_Byte, nullptr)
: nullptr);
if (!dstDs)
{
err = true;
}
else
{
ViewshedExecutor executor(
*srcDs->GetRasterBand(1), *dstDs->GetRasterBand(1), loc.x,
loc.y, m_extent, m_extent, m_opts, progress);
err = !executor.run();
if (!err)
m_datasetQueue.push(std::move(dstDs));
}
}
if (!err)
m_datasetQueue.push(std::move(dstDs));
}

// Job done. Set the output queue state. If all the executor jobs have completed,
Expand Down
2 changes: 2 additions & 0 deletions alg/viewshed/viewshed_executor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,8 @@ ViewshedExecutor::ViewshedExecutor(GDALRasterBand &srcBand,
// calculate the height adjustment factor.
double ViewshedExecutor::calcHeightAdjFactor()
{
std::lock_guard g(iMutex);

const OGRSpatialReference *poDstSRS =
m_dstBand.GetDataset()->GetSpatialRef();

Expand Down
12 changes: 5 additions & 7 deletions apps/gdaldem_lib.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@
#include "commonutils.h"
#include "gdalargumentparser.h"

#include <cassert>
#include <cfloat>
#include <cmath>
#include <cstdio>
Expand Down Expand Up @@ -1666,13 +1667,10 @@ GDALColorReliefGetRGBA(const std::vector<ColorAssociation> &asColorAssociation,
if (eColorSelectionMode == COLOR_SELECTION_NEAREST_ENTRY &&
asColorAssociation[i - 1].dfVal != dfVal)
{
size_t index = i;
if (dfVal - asColorAssociation[i - 1].dfVal <
asColorAssociation[i].dfVal - dfVal)
{
--index;
}

const size_t index = (dfVal - asColorAssociation[i - 1].dfVal <
asColorAssociation[i].dfVal - dfVal)
? i - 1
: i;
*pnR = asColorAssociation[index].nR;
*pnG = asColorAssociation[index].nG;
*pnB = asColorAssociation[index].nB;
Expand Down
2 changes: 2 additions & 0 deletions autotest/cpp/test_alg.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,8 @@ TEST_F(test_alg, GDALWarpResolveWorkingDataType_padfSrcNoDataReal_with_band)
GDALDatasetUniquePtr poDS(GDALDriver::FromHandle(GDALGetDriverByName("MEM"))
->Create("", 1, 1, 1, GDT_Byte, nullptr));
GDALWarpOptions *psOptions = GDALCreateWarpOptions();
// False-positive: hSrcDS is no longer used after GDALDestroyWarpOptions()
// coverity[escape]
psOptions->hSrcDS = GDALDataset::ToHandle(poDS.get());
psOptions->nBandCount = 1;
psOptions->panSrcBands =
Expand Down
3 changes: 3 additions & 0 deletions frmts/kmlsuperoverlay/kmlsuperoverlaydataset.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2793,6 +2793,9 @@ KmlSuperOverlayReadDataset::Open(const char *pszFilename,
auto poOvrDS = std::make_unique<KmlSuperOverlayReadDataset>();

poOvrDS->bIsOvr = true;
// The life-time of objects is such that poOvrDS is destroyed when
// poDS is destroyed.
// coverity[escape]
poOvrDS->poParent = poDS.get();
poOvrDS->nFactor = nFactor;
poOvrDS->nRasterXSize = nFactor * poDSIcon->GetRasterXSize();
Expand Down
2 changes: 1 addition & 1 deletion frmts/vrt/gdaltileindexdataset.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1184,7 +1184,7 @@ bool GDALTileIndexDataset::Open(GDALOpenInfo *poOpenInfo)
? dfOutMaxY
: std::ceil(dfOutMaxY / dfResY) * dfResY);

m_oSRS = oSTACSRS;
m_oSRS = std::move(oSTACSRS);
pszResX = osResX.c_str();
pszResY = osResY.c_str();
pszMinX = osMinX.c_str();
Expand Down
1 change: 1 addition & 0 deletions ogr/ogrsf_frmts/sqlite/ogrsqlitedatasource.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,7 @@ void OGRSQLiteBaseDataSource::FinishSpatialite()
std::lock_guard oLock(oCleanupMutex);
pfn_spatialite_cleanup_ex(ctxt);
}
// coverity[thread1_overwrites_value_in_field]
hSpatialiteCtxt = nullptr;
}
}
Expand Down
2 changes: 1 addition & 1 deletion port/cpl_http.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -556,7 +556,7 @@ char **CPLHTTPGetOptionsFromEnv(const char *pszFilename)
{
CPLStringList aosOptions;
std::string osNonStreamingFilename;
if (STARTS_WITH(pszFilename, "/vsi"))
if (pszFilename && STARTS_WITH(pszFilename, "/vsi"))
{
VSIFilesystemHandler *poFSHandler =
VSIFileManager::GetHandler(pszFilename);
Expand Down
2 changes: 1 addition & 1 deletion port/cpl_worker_thread_pool.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -611,8 +611,8 @@ bool CPLJobQueue::SubmitJob(std::function<void()> task)
}

// cppcheck-suppress knownConditionTrueFalse
// coverity[uninit_member,copy_constructor_call]
return m_poPool->SubmitJob(
// coverity[uninit_member,copy_constructor_call]
[this, task]
{
task();
Expand Down

0 comments on commit f396e32

Please sign in to comment.