media: v4l2-tpg: Fix division by zero error in color_to_hsv

Message ID 20240301170838.57421-1-m.lobanov@rosalinux.ru
State New
Headers
Series media: v4l2-tpg: Fix division by zero error in color_to_hsv |

Commit Message

Mikhail Lobanov March 1, 2024, 5:08 p.m. UTC
  In the color_to_hsv function, division by zero is possible due to
attributes r,g,b are equal so diff_rgb = 0.

Found by Linux Verification Center (linuxtesting.org) with SVACE.

Fixes: 54fb15348385 ("[media] vivid: Add support for HSV formats")
Signed-off-by: Mikhail Lobanov <m.lobanov@rosalinux.ru>
---
 drivers/media/common/v4l2-tpg/v4l2-tpg-core.c | 5 +++++
 1 file changed, 5 insertions(+)
  

Patch

diff --git a/drivers/media/common/v4l2-tpg/v4l2-tpg-core.c b/drivers/media/common/v4l2-tpg/v4l2-tpg-core.c
index a366566f22c3..943aab3ad97c 100644
--- a/drivers/media/common/v4l2-tpg/v4l2-tpg-core.c
+++ b/drivers/media/common/v4l2-tpg/v4l2-tpg-core.c
@@ -597,6 +597,11 @@  static void color_to_hsv(struct tpg_data *tpg, int r, int g, int b,
 		third = third_size * 2;
 	}
 
+	if (!diff_rgb) {
+		*s = 0;
+		return;
+	}
+
 	aux *= third_size / 2;
 	aux += diff_rgb / 2;
 	aux /= diff_rgb;