Skip to content
Permalink
Browse files

Fix a crash with terrain generation from DEM (fixes #17730)

A typical off by one error - reading data outside of heightmap array
  • Loading branch information
wonder-sk committed Jul 8, 2018
1 parent 031e15e commit d1e4c5e0ffd6c3d24b9390ca56429678f44c854b
Showing with 10 additions and 10 deletions.
  1. +10 −10 src/3d/terrain/qgsdemterraintilegeometry_p.cpp
@@ -101,20 +101,20 @@ static QByteArray createPlaneVertexData( int res, float skirtHeight, const QByte
return bufferBytes;
}

inline int ijToHeightMapIndex( int i, int j, int numVerticesX, int numVerticesZ )
inline int ijToHeightMapIndex( int i, int j, int resX, int resZ )
{
i = qBound( 1, i, numVerticesX - 1 ) - 1;
j = qBound( 1, j, numVerticesZ - 1 ) - 1;
return j * ( numVerticesX - 2 ) + i;
i = qBound( 1, i, resX ) - 1;
j = qBound( 1, j, resZ ) - 1;
return j * resX + i;
}


static bool hasNoData( int i, int j, const float *heightMap, int numVerticesX, int numVerticesZ )
static bool hasNoData( int i, int j, const float *heightMap, int resX, int resZ )
{
return std::isnan( heightMap[ ijToHeightMapIndex( i, j, numVerticesX, numVerticesZ ) ] ) ||
std::isnan( heightMap[ ijToHeightMapIndex( i + 1, j, numVerticesX, numVerticesZ ) ] ) ||
std::isnan( heightMap[ ijToHeightMapIndex( i, j + 1, numVerticesX, numVerticesZ ) ] ) ||
std::isnan( heightMap[ ijToHeightMapIndex( i + 1, j + 1, numVerticesX, numVerticesZ ) ] );
return std::isnan( heightMap[ ijToHeightMapIndex( i, j, resX, resZ ) ] ) ||
std::isnan( heightMap[ ijToHeightMapIndex( i + 1, j, resX, resZ ) ] ) ||
std::isnan( heightMap[ ijToHeightMapIndex( i, j + 1, resX, resZ ) ] ) ||
std::isnan( heightMap[ ijToHeightMapIndex( i + 1, j + 1, resX, resZ ) ] );
}

static QByteArray createPlaneIndexData( int res, const QByteArray &heightMap )
@@ -142,7 +142,7 @@ static QByteArray createPlaneIndexData( int res, const QByteArray &heightMap )
// Iterate over x
for ( int i = 0; i < numVerticesX - 1; ++i )
{
if ( hasNoData( i, j, heightMapFloat, numVerticesX, numVerticesZ ) )
if ( hasNoData( i, j, heightMapFloat, res, res ) )
{
// at least one corner of the quad has no-data value
// so let's make two invalid triangles

0 comments on commit d1e4c5e

Please sign in to comment.
You can’t perform that action at this time.