Skip to content

Commit

Permalink
Add better overflow protection
Browse files Browse the repository at this point in the history
Signed-off-by: Ryan Friedman <[email protected]>
  • Loading branch information
Ryanf55 committed May 10, 2024
1 parent 424f71c commit 9d02aba
Showing 1 changed file with 2 additions and 1 deletion.
3 changes: 2 additions & 1 deletion geospatial/src/Dem.cc
Original file line number Diff line number Diff line change
Expand Up @@ -551,6 +551,7 @@ int Dem::LoadData()
std::vector<float> buffer;
// Convert to uint64_t for multiplication to avoid overflow.
// https://github.com/OSGeo/gdal/issues/9713
static_assert(std::numeric_limits<uint64_t>::max() >= std::numeric_limits<uint32_t>::max() * std::numeric_limits<uint32_t>::max());
buffer.resize(static_cast<uint64_t>(destWidth) * static_cast<uint64_t>(destHeight));
//! @todo Do not assume users only want to load from the origin of the dataset.
// Instead, add a configuration to change where in the dataset to read from.
Expand All @@ -564,7 +565,7 @@ int Dem::LoadData()
// Copy and align 'buffer' into the target vector. The destination vector is
// initialized to max() and later converted to the minimum elevation, so all
// the points not contained in 'buffer' will be extra padding
this->dataPtr->demData.resize(this->Width() * this->Height(),
this->dataPtr->demData.resize(static_cast<uint64_t>(this->Width()) * static_cast<uint64_t>(this->Height()),
this->dataPtr->bufferVal);
for (unsigned int y = 0; y < destHeight; ++y)
{
Expand Down

0 comments on commit 9d02aba

Please sign in to comment.