From e38c3aebf110a9f5c9de73d0b9677c5b8a64000d Mon Sep 17 00:00:00 2001
From: Andreas Rheinhardt <andreas.rheinhardt@outlook.com>
Date: Tue, 18 Mar 2025 15:48:03 +0100
Subject: [PATCH] avutil/csp: Improve enum range comparisons

The underlying integer type of an enumeration is
implementation-defined (see C11, 6.7.2.2 (4)); GCC defaults
to unsigned if there are no negative values like for all enums
from pixfmt.h except enum AVPixelFormat.

This means that tests like "if (csp >= AVCOL_SPC_NB)" for
invalid colorspaces need not work as expected (namely if
enum AVColorSpace is signed). It also means that testing
for such an enum variable to be >= 0 may be tautologically
true. Clang emits a -Wtautological-unsigned-enum-zero-compare
warning for this.

Fix both of these issues by casting to unsigned.
Also do the same in libswscale/format.c.

Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@outlook.com>
---
 libavutil/csp.c     | 14 +++++++-------
 libswscale/format.c |  4 ++--
 2 files changed, 9 insertions(+), 9 deletions(-)

diff --git a/libavutil/csp.c b/libavutil/csp.c
index d69599f513..75ac871616 100644
--- a/libavutil/csp.c
+++ b/libavutil/csp.c
@@ -59,7 +59,7 @@ const struct AVLumaCoefficients *av_csp_luma_coeffs_from_avcsp(enum AVColorSpace
 {
     const AVLumaCoefficients *coeffs;
 
-    if (csp >= AVCOL_SPC_NB)
+    if ((unsigned)csp >= AVCOL_SPC_NB)
         return NULL;
     coeffs = &luma_coefficients[csp];
     if (!coeffs->cr.num)
@@ -91,7 +91,7 @@ const AVColorPrimariesDesc *av_csp_primaries_desc_from_id(enum AVColorPrimaries
 {
     const AVColorPrimariesDesc *p;
 
-    if (prm >= AVCOL_PRI_NB)
+    if ((unsigned)prm >= AVCOL_PRI_NB)
         return NULL;
     p = &color_primaries[prm];
     if (!p->prim.r.x.num)
@@ -149,7 +149,7 @@ static const double approximate_gamma[AVCOL_TRC_NB] = {
 double av_csp_approximate_trc_gamma(enum AVColorTransferCharacteristic trc)
 {
     double gamma;
-    if (trc >= AVCOL_TRC_NB)
+    if ((unsigned)trc >= AVCOL_TRC_NB)
         return 0.0;
     gamma = approximate_gamma[trc];
     if (gamma > 0)
@@ -399,7 +399,7 @@ static const av_csp_trc_function trc_funcs[AVCOL_TRC_NB] = {
 
 av_csp_trc_function av_csp_trc_func_from_id(enum AVColorTransferCharacteristic trc)
 {
-    if (trc >= AVCOL_TRC_NB)
+    if ((unsigned)trc >= AVCOL_TRC_NB)
         return NULL;
     return trc_funcs[trc];
 }
@@ -425,7 +425,7 @@ static const av_csp_trc_function trc_inv_funcs[AVCOL_TRC_NB] = {
 
 av_csp_trc_function av_csp_trc_func_inv_from_id(enum AVColorTransferCharacteristic trc)
 {
-    if (trc >= AVCOL_TRC_NB)
+    if ((unsigned)trc >= AVCOL_TRC_NB)
         return NULL;
     return trc_inv_funcs[trc];
 }
@@ -604,7 +604,7 @@ static const av_csp_eotf_function eotf_funcs[AVCOL_TRC_NB] = {
 
 av_csp_eotf_function av_csp_itu_eotf(enum AVColorTransferCharacteristic trc)
 {
-    if (trc < 0 || trc >= AVCOL_TRC_NB)
+    if ((unsigned)trc >= AVCOL_TRC_NB)
         return NULL;
     return eotf_funcs[trc];
 }
@@ -630,7 +630,7 @@ static const av_csp_eotf_function eotf_inv_funcs[AVCOL_TRC_NB] = {
 
 av_csp_eotf_function av_csp_itu_eotf_inv(enum AVColorTransferCharacteristic trc)
 {
-    if (trc < 0 || trc >= AVCOL_TRC_NB)
+    if ((unsigned)trc >= AVCOL_TRC_NB)
         return NULL;
     return eotf_inv_funcs[trc];
 }
diff --git a/libswscale/format.c b/libswscale/format.c
index c3ef499438..eeb9761396 100644
--- a/libswscale/format.c
+++ b/libswscale/format.c
@@ -535,12 +535,12 @@ int sws_test_transfer(enum AVColorTransferCharacteristic trc, int output)
 
 static int test_range(enum AVColorRange range)
 {
-    return range >= 0 && range < AVCOL_RANGE_NB;
+    return (unsigned)range < AVCOL_RANGE_NB;
 }
 
 static int test_loc(enum AVChromaLocation loc)
 {
-    return loc >= 0 && loc < AVCHROMA_LOC_NB;
+    return (unsigned)loc < AVCHROMA_LOC_NB;
 }
 
 int ff_test_fmt(const SwsFormat *fmt, int output)
-- 
2.45.2