Skip to content

Commit 876504d

Browse files
committed
Coverity Scan fixes
1 parent 07f20e4 commit 876504d

File tree

10 files changed

+27
-23
lines changed

10 files changed

+27
-23
lines changed

frmts/grib/gribdataset.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2345,6 +2345,7 @@ GDALDataset *GRIBDataset::OpenMultiDim(GDALOpenInfo *poOpenInfo)
23452345
{
23462346
inventoryType *psInv = pInventories->get(i);
23472347
uInt4 bandNr = i + 1;
2348+
assert(bandNr <= 65536);
23482349

23492350
// GRIB messages can be preceded by "garbage". GRIB2Inventory()
23502351
// does not return the offset to the real start of the message
@@ -2411,7 +2412,6 @@ GDALDataset *GRIBDataset::OpenMultiDim(GDALOpenInfo *poOpenInfo)
24112412
// the first GRIB band.
24122413
poDS->SetGribMetaData(metaData);
24132414

2414-
// coverity[tainted_data]
24152415
GRIBRasterBand gribBand(poDS, bandNr, psInv);
24162416
if (psInv->GribVersion == 2)
24172417
gribBand.FindPDSTemplateGRIB2();

frmts/netcdf/netcdfmultidim.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3561,12 +3561,12 @@ bool netCDFVariable::IRead(const GUInt64 *arrayStartIdx, const size_t *count,
35613561
size_t array_idx[2] = {static_cast<size_t>(arrayStartIdx[0]), 0};
35623562
size_t array_count[2] = {1, m_nTextLength};
35633563
std::string osTmp(m_nTextLength, 0);
3564-
const char *pszTmp = osTmp.c_str();
3564+
char *pszTmp = &osTmp[0];
35653565
bool ret = true;
35663566
for (size_t i = 0; ret && i < count[0]; i++)
35673567
{
35683568
int ncErr =
3569-
nc_get_vara(m_gid, m_varid, array_idx, array_count, &osTmp[0]);
3569+
nc_get_vara(m_gid, m_varid, array_idx, array_count, pszTmp);
35703570
NCDF_ERR(ncErr);
35713571
ret = ncErr == NC_NOERR;
35723572
if (ret)

gcore/gdaldriver.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2912,7 +2912,6 @@ void GDALDriver::DeclareAlgorithm(const std::vector<std::string> &aosPath)
29122912
CPLString(osDriverName).tolower()};
29132913
if (!singleton.HasDeclaredSubAlgorithm(path))
29142914
{
2915-
// coverity[copy_constructor_call]
29162915
auto lambda = [osDriverName]() -> std::unique_ptr<GDALAlgorithm>
29172916
{
29182917
auto poDriver =
@@ -2936,7 +2935,6 @@ void GDALDriver::DeclareAlgorithm(const std::vector<std::string> &aosPath)
29362935

29372936
path.insert(path.end(), aosPath.begin(), aosPath.end());
29382937

2939-
// coverity[copy_constructor_call]
29402938
auto lambda = [osDriverName, aosPath]() -> std::unique_ptr<GDALAlgorithm>
29412939
{
29422940
auto poDriver =
@@ -2948,6 +2946,8 @@ void GDALDriver::DeclareAlgorithm(const std::vector<std::string> &aosPath)
29482946
};
29492947

29502948
singleton.DeclareAlgorithm(path, std::move(lambda));
2949+
2950+
CPL_IGNORE_RET_VAL(osDriverName);
29512951
}
29522952

29532953
//! @endcond

gcore/gdalmultidim.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1971,6 +1971,7 @@ bool GDALAbstractMDArray::CheckReadWriteParams(
19711971
if (bufferStride == nullptr)
19721972
{
19731973
GPtrDiff_t stride = 1;
1974+
assert(dims.empty() || count != nullptr);
19741975
// To compute strides we must proceed from the fastest varying dimension
19751976
// (the last one), and then reverse the result
19761977
for (size_t i = dims.size(); i != 0;)

gcore/gdalpython.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,8 @@ static bool LoadPythonAPI()
186186
}
187187
else
188188
{
189+
if (libHandle)
190+
dlclose(libHandle);
189191
libHandle = nullptr;
190192
}
191193

ogr/ogrsf_frmts/mitab/mitab_tabview.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -366,6 +366,7 @@ int TABView::OpenForWrite(const char *pszFname)
366366
m_papszTABFnames = CSLAppendPrintf(m_papszTABFnames, "%s%s%d.tab",
367367
pszPath, pszBasename, iFile + 1);
368368
#ifndef _WIN32
369+
assert(m_papszTABFnames);
369370
assert(m_papszTABFnames[iFile]);
370371
TABAdjustFilenameExtension(m_papszTABFnames[iFile]);
371372
#endif

ogr/ogrsf_frmts/sqlite/ogrsqlitelayer.cpp

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1412,7 +1412,7 @@ OGRErr OGRSQLiteLayer::createFromSpatialiteInternal(
14121412
if (NEED_SWAP_SPATIALITE())
14131413
CPL_SWAP32PTR(&nPointCount);
14141414

1415-
if (nPointCount < 0 || nPointCount - 2 > (INT_MAX - 16 * 2) / 8)
1415+
if (nPointCount < 0 || nPointCount > (INT_MAX - 16 * 2) / 8 + 2)
14161416
return OGRERR_CORRUPT_DATA;
14171417

14181418
compressedSize = 16 * 2; // first and last Points
@@ -1482,7 +1482,7 @@ OGRErr OGRSQLiteLayer::createFromSpatialiteInternal(
14821482
if (NEED_SWAP_SPATIALITE())
14831483
CPL_SWAP32PTR(&nPointCount);
14841484

1485-
if (nPointCount < 0 || nPointCount - 2 > (INT_MAX - 24 * 2) / 12)
1485+
if (nPointCount < 0 || nPointCount > (INT_MAX - 24 * 2) / 12 + 2)
14861486
return OGRERR_CORRUPT_DATA;
14871487

14881488
compressedSize = 24 * 2; // first and last Points
@@ -1556,7 +1556,7 @@ OGRErr OGRSQLiteLayer::createFromSpatialiteInternal(
15561556
if (NEED_SWAP_SPATIALITE())
15571557
CPL_SWAP32PTR(&nPointCount);
15581558

1559-
if (nPointCount < 0 || nPointCount - 2 > (INT_MAX - 24 * 2) / 16)
1559+
if (nPointCount < 0 || nPointCount > (INT_MAX - 24 * 2) / 16 + 2)
15601560
return OGRERR_CORRUPT_DATA;
15611561

15621562
compressedSize = 24 * 2; // first and last Points
@@ -1630,7 +1630,7 @@ OGRErr OGRSQLiteLayer::createFromSpatialiteInternal(
16301630
if (NEED_SWAP_SPATIALITE())
16311631
CPL_SWAP32PTR(&nPointCount);
16321632

1633-
if (nPointCount < 0 || nPointCount - 2 > (INT_MAX - 32 * 2) / 20)
1633+
if (nPointCount < 0 || nPointCount > (INT_MAX - 32 * 2) / 20 + 2)
16341634
return OGRERR_CORRUPT_DATA;
16351635

16361636
compressedSize = 32 * 2; // first and last Points
@@ -2055,7 +2055,7 @@ OGRErr OGRSQLiteLayer::createFromSpatialiteInternal(
20552055
if (NEED_SWAP_SPATIALITE())
20562056
CPL_SWAP32PTR(&nPointCount);
20572057

2058-
if (nPointCount < 0 || nPointCount - 2 > (INT_MAX - 16 * 2) / 8)
2058+
if (nPointCount < 0 || nPointCount > (INT_MAX - 16 * 2) / 8 + 2)
20592059
{
20602060
delete poPoly;
20612061
return OGRERR_CORRUPT_DATA;
@@ -2157,7 +2157,7 @@ OGRErr OGRSQLiteLayer::createFromSpatialiteInternal(
21572157
if (NEED_SWAP_SPATIALITE())
21582158
CPL_SWAP32PTR(&nPointCount);
21592159

2160-
if (nPointCount < 0 || nPointCount - 2 > (INT_MAX - 24 * 2) / 12)
2160+
if (nPointCount < 0 || nPointCount > (INT_MAX - 24 * 2) / 12 + 2)
21612161
{
21622162
delete poPoly;
21632163
return OGRERR_CORRUPT_DATA;
@@ -2263,7 +2263,7 @@ OGRErr OGRSQLiteLayer::createFromSpatialiteInternal(
22632263
if (NEED_SWAP_SPATIALITE())
22642264
CPL_SWAP32PTR(&nPointCount);
22652265

2266-
if (nPointCount < 0 || nPointCount - 2 > (INT_MAX - 24 * 2) / 16)
2266+
if (nPointCount < 0 || nPointCount > (INT_MAX - 24 * 2) / 16 + 2)
22672267
{
22682268
delete poPoly;
22692269
return OGRERR_CORRUPT_DATA;
@@ -2370,7 +2370,7 @@ OGRErr OGRSQLiteLayer::createFromSpatialiteInternal(
23702370
if (NEED_SWAP_SPATIALITE())
23712371
CPL_SWAP32PTR(&nPointCount);
23722372

2373-
if (nPointCount < 0 || nPointCount - 2 > (INT_MAX - 32 * 2) / 20)
2373+
if (nPointCount < 0 || nPointCount > (INT_MAX - 32 * 2) / 20 + 2)
23742374
{
23752375
delete poPoly;
23762376
return OGRERR_CORRUPT_DATA;

port/cpl_vsil.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2238,6 +2238,7 @@ bool VSIFilesystemHandler::Sync(const char *pszSource, const char *pszTarget,
22382238
}
22392239
}
22402240
int iFile = 0;
2241+
const int nDenom = std::max(1, nFileCount);
22412242
for (auto iter = papszSrcFiles; iter && *iter; ++iter, ++iFile)
22422243
{
22432244
if (strcmp(*iter, ".") == 0 || strcmp(*iter, "..") == 0)
@@ -2248,9 +2249,8 @@ bool VSIFilesystemHandler::Sync(const char *pszSource, const char *pszTarget,
22482249
osSourceWithoutSlash.c_str(), *iter, nullptr));
22492250
const std::string osSubTarget(
22502251
CPLFormFilenameSafe(osTargetDir.c_str(), *iter, nullptr));
2251-
// coverity[divide_by_zero]
22522252
void *pScaledProgress = GDALCreateScaledProgress(
2253-
double(iFile) / nFileCount, double(iFile + 1) / nFileCount,
2253+
double(iFile) / nDenom, double(iFile + 1) / nDenom,
22542254
pProgressFunc, pProgressData);
22552255
ret = Sync((osSubSource + SOURCE_SEP).c_str(),
22562256
osSubTarget.c_str(), aosChildOptions.List(),

port/cpl_vsil_curl.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3412,8 +3412,8 @@ void VSICurlHandle::AdviseRead(int nRanges, const vsi_l_offset *panOffsets,
34123412
static_cast<unsigned>(m_aoAdviseReadRanges.size()));
34133413
#endif
34143414

3415-
// coverity[uninit_member,copy_constructor_call]
3416-
const auto task = [this, aosHTTPOptions](const std::string &osURL)
3415+
const auto task = [this, aosHTTPOptions = std::move(aosHTTPOptions)](
3416+
const std::string &osURL)
34173417
{
34183418
if (!m_hCurlMultiHandleForAdviseRead)
34193419
m_hCurlMultiHandleForAdviseRead = VSICURLMultiInit();

port/cpl_vsil_s3.cpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4533,6 +4533,7 @@ bool IVSIS3LikeFSHandler::Sync(const char *pszSource, const char *pszTarget,
45334533
// Proceed to file copy
45344534
bool ret = true;
45354535
uint64_t nAccSize = 0;
4536+
const uint64_t nTotalSizeDenom = std::max<uint64_t>(1, nTotalSize);
45364537
for (const size_t iChunk : anIndexToCopy)
45374538
{
45384539
const auto &chunk = aoChunksToCopy[iChunk];
@@ -4542,11 +4543,10 @@ bool IVSIS3LikeFSHandler::Sync(const char *pszSource, const char *pszTarget,
45424543
chunk.osSrcFilename.c_str(), nullptr));
45434544
const std::string osSubTarget(CPLFormFilenameSafe(
45444545
osTargetDir.c_str(), chunk.osDstFilename.c_str(), nullptr));
4545-
// coverity[divide_by_zero]
45464546
void *pScaledProgress = GDALCreateScaledProgress(
4547-
double(nAccSize) / nTotalSize,
4548-
double(nAccSize + chunk.nSize) / nTotalSize, pProgressFunc,
4549-
pProgressData);
4547+
double(nAccSize) / nTotalSizeDenom,
4548+
double(nAccSize + chunk.nSize) / nTotalSizeDenom,
4549+
pProgressFunc, pProgressData);
45504550
ret =
45514551
CopyFile(osSubSource.c_str(), osSubTarget.c_str(), nullptr,
45524552
chunk.nSize, aosObjectCreationOptions.List(),
@@ -4959,14 +4959,14 @@ bool IVSIS3LikeFSHandler::Sync(const char *pszSource, const char *pszTarget,
49594959
}
49604960
if (pProgressFunc)
49614961
{
4962+
const uint64_t nTotalSizeDenom = std::max<uint64_t>(1, nTotalSize);
49624963
while (!sJobQueue.stop)
49634964
{
49644965
CPLSleep(0.1);
49654966
sJobQueue.sMutex.lock();
49664967
const auto nTotalCopied = sJobQueue.nTotalCopied;
49674968
sJobQueue.sMutex.unlock();
4968-
// coverity[divide_by_zero]
4969-
if (!pProgressFunc(double(nTotalCopied) / nTotalSize, "",
4969+
if (!pProgressFunc(double(nTotalCopied) / nTotalSizeDenom, "",
49704970
pProgressData))
49714971
{
49724972
sJobQueue.ret = false;

0 commit comments

Comments
 (0)