Skip to content

Commit f250a12

Browse files
committed
Fix potential crash in random color ramp
Badly choosen values (eg max < min) could cause a crash. Now, values are sanitised prior to generating the ramp colors.
1 parent 269b626 commit f250a12

File tree

1 file changed

+11
-3
lines changed

1 file changed

+11
-3
lines changed

src/core/symbology-ng/qgsvectorcolorrampv2.cpp

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -318,6 +318,14 @@ QList<QColor> QgsVectorRandomColorRampV2::randomColors( int count,
318318
int h, s, v;
319319
QList<QColor> colors;
320320

321+
//normalize values
322+
int safeHueMax = qMax( hueMin, hueMax );
323+
int safeHueMin = qMin( hueMin, hueMax );
324+
int safeSatMax = qMax( satMin, satMax );
325+
int safeSatMin = qMin( satMin, satMax );
326+
int safeValMax = qMax( valMin, valMax );
327+
int safeValMin = qMin( valMin, valMax );
328+
321329
//start hue at random angle
322330
double currentHueAngle = 360.0 * ( double )rand() / RAND_MAX;
323331

@@ -328,9 +336,9 @@ QList<QColor> QgsVectorRandomColorRampV2::randomColors( int count,
328336
//see http://basecase.org/env/on-rainbows for more details
329337
currentHueAngle += 137.50776;
330338
//scale hue to between hueMax and hueMin
331-
h = ( fmod( currentHueAngle, 360.0 ) / 360.0 ) * ( hueMax - hueMin ) + hueMin;
332-
s = ( rand() % ( satMax - satMin + 1 ) ) + satMin;
333-
v = ( rand() % ( valMax - valMin + 1 ) ) + valMin;
339+
h = qBound( 0, qRound(( fmod( currentHueAngle, 360.0 ) / 360.0 ) * ( safeHueMax - safeHueMin ) + safeHueMin ), 359 );
340+
s = qBound( 0, ( rand() % ( safeSatMax - safeSatMin + 1 ) ) + safeValMax, 255 );
341+
v = qBound( 0, ( rand() % ( safeValMax - safeValMin + 1 ) ) + safeValMin, 255 );
334342
colors.append( QColor::fromHsv( h, s, v ) );
335343
}
336344
return colors;

0 commit comments

Comments
 (0)