Skip to content

Commit

Permalink
Coverity warnings fixes (for false positives or unlikely events)
Browse files Browse the repository at this point in the history
  • Loading branch information
rouault committed Nov 24, 2024
1 parent 181b6b9 commit 33fe7fe
Show file tree
Hide file tree
Showing 9 changed files with 59 additions and 51 deletions.
6 changes: 3 additions & 3 deletions alg/internal_libqhull/poly_r.c
Original file line number Diff line number Diff line change
Expand Up @@ -1180,9 +1180,9 @@ int qh_pointid(qhT *qh, pointT *point) {
/* coverity[divide_arg] */
id= offset / qh->hull_dim;
} else {
id = qh_setindex(qh->other_points, point);
if (id >= 0) {
id += qh->num_points;
const int idx = qh_setindex(qh->other_points, point);
if (idx >= 0) {
id = (ptr_intT)idx + qh->num_points;
} else {
return qh_IDunknown;
}
Expand Down
12 changes: 6 additions & 6 deletions autotest/ogr/ogr_geom.py
Original file line number Diff line number Diff line change
Expand Up @@ -931,21 +931,21 @@ def test_ogr_geom_segmentize_issue_1341():

geom = ogr.CreateGeometryFromWkt("LINESTRING(0 0,10 0)")
geom.Segmentize(0.399999999999)
assert geom.ExportToWkt() == expected_geom
ogrtest.check_feature_geometry(geom, expected_geom)
geom.Segmentize(0.399999999999)
assert geom.ExportToWkt() == expected_geom
ogrtest.check_feature_geometry(geom, expected_geom)

geom = ogr.CreateGeometryFromWkt("LINESTRING(0 0,10 0)")
geom.Segmentize(0.4)
assert geom.ExportToWkt() == expected_geom
ogrtest.check_feature_geometry(geom, expected_geom)
geom.Segmentize(0.4)
assert geom.ExportToWkt() == expected_geom
ogrtest.check_feature_geometry(geom, expected_geom)

geom = ogr.CreateGeometryFromWkt("LINESTRING(0 0,10 0)")
geom.Segmentize(0.400000000001)
assert geom.ExportToWkt() == expected_geom
ogrtest.check_feature_geometry(geom, expected_geom)
geom.Segmentize(0.400000000001)
assert geom.ExportToWkt() == expected_geom
ogrtest.check_feature_geometry(geom, expected_geom)


###############################################################################
Expand Down
38 changes: 18 additions & 20 deletions frmts/jpeg/jpgdataset.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3331,29 +3331,27 @@ void JPGDatasetCommon::CheckForMask()

GByte abyEOD[2] = {0, 0};

if (nImageSize < nFileSize / 2 || nImageSize > nFileSize - 4)
goto end;

// If that seems okay, seek back, and verify that just preceding
// the bitmask is an apparent end-of-jpeg-data marker.
VSIFSeekL(m_fpImage, nImageSize - 2, SEEK_SET);
VSIFReadL(abyEOD, 2, 1, m_fpImage);
if (abyEOD[0] != 0xff || abyEOD[1] != 0xd9)
goto end;
if (nImageSize >= 2 && nImageSize >= nFileSize / 2 &&
nImageSize <= nFileSize - 4)
{
// If that seems okay, seek back, and verify that just preceding
// the bitmask is an apparent end-of-jpeg-data marker.
VSIFSeekL(m_fpImage, nImageSize - 2, SEEK_SET);
VSIFReadL(abyEOD, 2, 1, m_fpImage);
if (abyEOD[0] == 0xff && abyEOD[1] == 0xd9)
{
// We seem to have a mask. Read it in.
nCMaskSize = static_cast<int>(nFileSize - nImageSize - 4);
pabyCMask = static_cast<GByte *>(VSI_MALLOC_VERBOSE(nCMaskSize));
if (pabyCMask)
{
VSIFReadL(pabyCMask, nCMaskSize, 1, m_fpImage);

// We seem to have a mask. Read it in.
nCMaskSize = static_cast<int>(nFileSize - nImageSize - 4);
pabyCMask = static_cast<GByte *>(VSI_MALLOC_VERBOSE(nCMaskSize));
if (pabyCMask == nullptr)
{
goto end;
CPLDebug("JPEG", "Got %d byte compressed bitmask.", nCMaskSize);
}
}
}
VSIFReadL(pabyCMask, nCMaskSize, 1, m_fpImage);

CPLDebug("JPEG", "Got %d byte compressed bitmask.", nCMaskSize);

// TODO(schwehr): Refactor to not use goto.
end:
VSIFSeekL(m_fpImage, nCurOffset, SEEK_SET);
}

Expand Down
10 changes: 6 additions & 4 deletions ogr/ogrlinestring.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2756,15 +2756,17 @@ bool OGRSimpleCurve::segmentize(double dfMaxLength)
sqrt(dfSquareDist / dfSquareMaxLength) - REL_EPSILON_ROUND);
const int nIntermediatePoints =
DoubleToIntClamp(dfIntermediatePoints);
const double dfRatioX =
dfX / (static_cast<double>(nIntermediatePoints) + 1);
const double dfRatioY =
dfY / (static_cast<double>(nIntermediatePoints) + 1);

for (int j = 1; j <= nIntermediatePoints; j++)
{
// coverity[overflow_const]
const int newI = nNewPointCount + j - 1;
paoNewPoints[newI].x =
paoPoints[i].x + j * dfX / (nIntermediatePoints + 1);
paoNewPoints[newI].y =
paoPoints[i].y + j * dfY / (nIntermediatePoints + 1);
paoNewPoints[newI].x = paoPoints[i].x + j * dfRatioX;
paoNewPoints[newI].y = paoPoints[i].y + j * dfRatioY;
if (padfZ != nullptr)
{
// No interpolation.
Expand Down
33 changes: 19 additions & 14 deletions ogr/ogrsf_frmts/vfk/vfkreadersqlite.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -442,24 +442,29 @@ int64_t VFKReaderSQLite::ReadDataRecords(IVFKDataBlock *poDataBlock)
StoreInfo2DB();

/* Insert VFK data records into DB */
nDataRecords += VFKReader::ReadDataRecords(poDataBlock);

/* update VFK_DB_TABLE table */
poDataBlockCurrent = nullptr;
for (int iDataBlock = 0; iDataBlock < GetDataBlockCount(); iDataBlock++)
const int64_t nExtraRecords = VFKReader::ReadDataRecords(poDataBlock);
if (nExtraRecords >= 0)
{
poDataBlockCurrent = GetDataBlock(iDataBlock);
nDataRecords += nExtraRecords;

if (poDataBlock && poDataBlock != poDataBlockCurrent)
continue;
/* update VFK_DB_TABLE table */
poDataBlockCurrent = nullptr;
for (int iDataBlock = 0; iDataBlock < GetDataBlockCount();
iDataBlock++)
{
poDataBlockCurrent = GetDataBlock(iDataBlock);

/* update number of records in metadata table */
osSQL.Printf("UPDATE %s SET num_records = %d WHERE "
"table_name = '%s'",
VFK_DB_TABLE, poDataBlockCurrent->GetRecordCount(),
poDataBlockCurrent->GetName());
if (poDataBlock && poDataBlock != poDataBlockCurrent)
continue;

ExecuteSQL(osSQL);
/* update number of records in metadata table */
osSQL.Printf("UPDATE %s SET num_records = %d WHERE "
"table_name = '%s'",
VFK_DB_TABLE, poDataBlockCurrent->GetRecordCount(),
poDataBlockCurrent->GetName());

ExecuteSQL(osSQL);
}
}

/* create indices if not exist */
Expand Down
5 changes: 3 additions & 2 deletions port/cpl_md5.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -85,8 +85,9 @@ void CPLMD5Update(struct CPLMD5Context *context, const void *buf, size_t len)
// Update bitcount
GUInt32 t = context->bits[0];
// coverity[overflow_const]
if ((context->bits[0] =
(t + (static_cast<GUInt32>(len) << 3)) & 0xffffffff) < t)
const GUInt32 lenShifted = static_cast<GUInt32>(len) << 3U;
context->bits[0] = (t + lenShifted) & 0xffffffff;
if (context->bits[0] < t)
context->bits[1]++; /* Carry from low to high */
context->bits[1] += static_cast<GUInt32>(len >> 29);

Expand Down
1 change: 1 addition & 0 deletions port/cpl_path.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -606,6 +606,7 @@ const char *CPLFormFilename(const char *pszPath, const char *pszBasename,
if (nLenPath >= static_cast<size_t>(CPL_PATH_BUF_SIZE))
return CPLStaticBufferTooSmall(pszStaticResult);

// coverity[overrun-buffer-arg]
memcpy(pszStaticResult, pszPath, nLenPath);
pszStaticResult[nLenPath] = 0;

Expand Down
4 changes: 2 additions & 2 deletions port/cpl_spawn.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -458,7 +458,7 @@ int CPLPipeRead(CPL_FILE_HANDLE fin, void *data, int length)
{
while (true)
{
const auto n = read(fin, pabyData, nRemain);
const ssize_t n = read(fin, pabyData, nRemain);
if (n < 0)
{
if (errno == EINTR)
Expand Down Expand Up @@ -499,7 +499,7 @@ int CPLPipeWrite(CPL_FILE_HANDLE fout, const void *data, int length)
{
while (true)
{
const auto n = write(fout, pabyData, nRemain);
const ssize_t n = write(fout, pabyData, nRemain);
if (n < 0)
{
if (errno == EINTR)
Expand Down
1 change: 1 addition & 0 deletions port/cpl_worker_thread_pool.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -503,6 +503,7 @@ CPLWorkerThreadPool::GetNextJob(CPLWorkerThread *psWorkerThread)
std::unique_lock<std::mutex> oGuardThisThread(psWorkerThread->m_mutex);
// coverity[uninit_use_in_call]
oGuard.unlock();
// coverity[wait_not_in_locked_loop]
psWorkerThread->m_cv.wait(oGuardThisThread);
}
}
Expand Down

0 comments on commit 33fe7fe

Please sign in to comment.