瀏覽代碼

Fix color_ramp indexing negative elements

The 'pos' variable passed to get_color() and get_offset() can be
negative if the color ramp itself is empty. This causes a lookup in an
empty position in the color Vector which leads to a crash.

We add a check so we never do a lookup in the color Vector if the
gradient is empty.

This fixes #10501

(cherry picked from commit 67b9d6eef2df5253657c89725195c58fe01b39f0)
Hein-Pieter van Braam 8 年之前
父節點
當前提交
bec8fd1732
共有 1 個文件被更改,包括 2 次插入2 次删除
  1. 2 2
      scene/resources/color_ramp.cpp

+ 2 - 2
scene/resources/color_ramp.cpp

@@ -141,7 +141,7 @@ void ColorRamp::set_offset(int pos, const float offset) {
 }
 }
 
 
 float ColorRamp::get_offset(int pos) const {
 float ColorRamp::get_offset(int pos) const {
-	if (points.size() > pos)
+	if (points.size() && points.size() > pos)
 		return points[pos].offset;
 		return points[pos].offset;
 	return 0; //TODO: Maybe throw some error instead?
 	return 0; //TODO: Maybe throw some error instead?
 }
 }
@@ -155,7 +155,7 @@ void ColorRamp::set_color(int pos, const Color &color) {
 }
 }
 
 
 Color ColorRamp::get_color(int pos) const {
 Color ColorRamp::get_color(int pos) const {
-	if (points.size() > pos)
+	if (points.size() && points.size() > pos)
 		return points[pos].color;
 		return points[pos].color;
 	return Color(0, 0, 0, 1); //TODO: Maybe throw some error instead?
 	return Color(0, 0, 0, 1); //TODO: Maybe throw some error instead?
 }
 }