Skip to content

Commit e37ffd0

Browse files
committed
Don't crash raster shader with nan or inf values (fix #15444)
(cherry-picked from 34ebe12)
1 parent 66da8cd commit e37ffd0

File tree

3 files changed

+41
-0
lines changed

3 files changed

+41
-0
lines changed

src/core/raster/qgscolorrampshader.cpp

+3
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,9 @@ bool QgsColorRampShader::shade( double theValue, int* theReturnRedValue, int* th
8888
{
8989
return false;
9090
}
91+
if ( qIsNaN( theValue ) || qIsInf( theValue ) )
92+
return false;
93+
9194
int colorRampItemListCount = mColorRampItemList.count();
9295
int idx;
9396
if ( !mLUTInitialized )

tests/src/python/CMakeLists.txt

+1
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ ADD_PYTHON_TEST(PyQgsPoint test_qgspoint.py)
6969
ADD_PYTHON_TEST(PyQgsRangeWidgets test_qgsrangewidgets.py)
7070
ADD_PYTHON_TEST(PyQgsRasterFileWriter test_qgsrasterfilewriter.py)
7171
ADD_PYTHON_TEST(PyQgsRasterLayer test_qgsrasterlayer.py)
72+
ADD_PYTHON_TEST(PyQgsRasterColorRampShader test_qgsrastercolorrampshader.py)
7273
ADD_PYTHON_TEST(PyQgsRectangle test_qgsrectangle.py)
7374
ADD_PYTHON_TEST(PyQgsRelation test_qgsrelation.py)
7475
ADD_PYTHON_TEST(PyQgsRelationManager test_qgsrelationmanager.py)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
# -*- coding: utf-8 -*-
2+
"""QGIS Unit tests for QgsColorRampShader.
3+
4+
.. note:: This program is free software; you can redistribute it and/or modify
5+
it under the terms of the GNU General Public License as published by
6+
the Free Software Foundation; either version 2 of the License, or
7+
(at your option) any later version.
8+
"""
9+
10+
__author__ = 'Nyall Dawson'
11+
__date__ = '17/08/2016'
12+
__copyright__ = 'Copyright 2016, The QGIS Project'
13+
# This will get replaced with a git SHA1 when you do a git archive
14+
__revision__ = '$Format:%H$'
15+
16+
import qgis # NOQA
17+
18+
19+
from qgis.PyQt.QtGui import QColor
20+
21+
from qgis.core import (QgsColorRampShader)
22+
from qgis.testing import unittest
23+
24+
25+
class TestQgsRasterColorRampShader(unittest.TestCase):
26+
27+
def testNan(self):
28+
shader = QgsColorRampShader()
29+
30+
item1 = QgsColorRampShader.ColorRampItem(1, QColor(0, 0, 0))
31+
item2 = QgsColorRampShader.ColorRampItem(2, QColor(255, 255, 255))
32+
shader.setColorRampItemList([item1, item2])
33+
self.assertFalse(shader.shade(float('NaN'))[0])
34+
self.assertFalse(shader.shade(float("inf"))[0])
35+
36+
if __name__ == '__main__':
37+
unittest.main()

0 commit comments

Comments
 (0)