Skip to content

Commit

Permalink
Don't crash raster shader with nan or inf values (fix #15444)
Browse files Browse the repository at this point in the history
(cherry-picked from 34ebe12)
  • Loading branch information
nyalldawson committed Aug 17, 2016
1 parent 0feca4c commit a1f8a59
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 0 deletions.
3 changes: 3 additions & 0 deletions src/core/raster/qgscolorrampshader.cpp
Expand Up @@ -88,6 +88,9 @@ bool QgsColorRampShader::shade( double theValue, int* theReturnRedValue, int* th
{
return false;
}
if ( qIsNaN( theValue ) || qIsInf( theValue ) )
return false;

int colorRampItemListCount = mColorRampItemList.count();
int idx;
if ( !mLUTInitialized )
Expand Down
1 change: 1 addition & 0 deletions tests/src/python/CMakeLists.txt
Expand Up @@ -69,6 +69,7 @@ ADD_PYTHON_TEST(PyQgsPoint test_qgspoint.py)
ADD_PYTHON_TEST(PyQgsRangeWidgets test_qgsrangewidgets.py)
ADD_PYTHON_TEST(PyQgsRasterFileWriter test_qgsrasterfilewriter.py)
ADD_PYTHON_TEST(PyQgsRasterLayer test_qgsrasterlayer.py)
ADD_PYTHON_TEST(PyQgsRasterColorRampShader test_qgsrastercolorrampshader.py)
ADD_PYTHON_TEST(PyQgsRectangle test_qgsrectangle.py)
ADD_PYTHON_TEST(PyQgsRelation test_qgsrelation.py)
ADD_PYTHON_TEST(PyQgsRelationManager test_qgsrelationmanager.py)
Expand Down
37 changes: 37 additions & 0 deletions tests/src/python/test_qgsrastercolorrampshader.py
@@ -0,0 +1,37 @@
# -*- coding: utf-8 -*-
"""QGIS Unit tests for QgsColorRampShader.
.. note:: This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
"""

__author__ = 'Nyall Dawson'
__date__ = '17/08/2016'
__copyright__ = 'Copyright 2016, The QGIS Project'
# This will get replaced with a git SHA1 when you do a git archive
__revision__ = '$Format:%H$'

import qgis # NOQA


from qgis.PyQt.QtGui import QColor

from qgis.core import (QgsColorRampShader)
from qgis.testing import unittest


class TestQgsRasterColorRampShader(unittest.TestCase):

def testNan(self):
shader = QgsColorRampShader()

item1 = QgsColorRampShader.ColorRampItem(1, QColor(0, 0, 0))
item2 = QgsColorRampShader.ColorRampItem(2, QColor(255, 255, 255))
shader.setColorRampItemList([item1, item2])
self.assertFalse(shader.shade(float('NaN'))[0])
self.assertFalse(shader.shade(float("inf"))[0])

if __name__ == '__main__':
unittest.main()

0 comments on commit a1f8a59

Please sign in to comment.