1
0
mirror of https://github.com/FFmpeg/FFmpeg.git synced 2025-08-04 22:03:09 +02:00

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>
This commit is contained in:
Andreas Rheinhardt
2025-03-18 15:48:03 +01:00
parent 0ce405afb8
commit dff498fddf
2 changed files with 9 additions and 9 deletions

View File

@ -59,7 +59,7 @@ const struct AVLumaCoefficients *av_csp_luma_coeffs_from_avcsp(enum AVColorSpace
{ {
const AVLumaCoefficients *coeffs; const AVLumaCoefficients *coeffs;
if (csp >= AVCOL_SPC_NB) if ((unsigned)csp >= AVCOL_SPC_NB)
return NULL; return NULL;
coeffs = &luma_coefficients[csp]; coeffs = &luma_coefficients[csp];
if (!coeffs->cr.num) if (!coeffs->cr.num)
@ -91,7 +91,7 @@ const AVColorPrimariesDesc *av_csp_primaries_desc_from_id(enum AVColorPrimaries
{ {
const AVColorPrimariesDesc *p; const AVColorPrimariesDesc *p;
if (prm >= AVCOL_PRI_NB) if ((unsigned)prm >= AVCOL_PRI_NB)
return NULL; return NULL;
p = &color_primaries[prm]; p = &color_primaries[prm];
if (!p->prim.r.x.num) 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 av_csp_approximate_trc_gamma(enum AVColorTransferCharacteristic trc)
{ {
double gamma; double gamma;
if (trc >= AVCOL_TRC_NB) if ((unsigned)trc >= AVCOL_TRC_NB)
return 0.0; return 0.0;
gamma = approximate_gamma[trc]; gamma = approximate_gamma[trc];
if (gamma > 0) 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) 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 NULL;
return trc_funcs[trc]; 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) 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 NULL;
return trc_inv_funcs[trc]; 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) 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 NULL;
return eotf_funcs[trc]; 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) 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 NULL;
return eotf_inv_funcs[trc]; return eotf_inv_funcs[trc];
} }

View File

@ -537,12 +537,12 @@ int sws_test_transfer(enum AVColorTransferCharacteristic trc, int output)
static int test_range(enum AVColorRange range) 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) 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) int ff_test_fmt(const SwsFormat *fmt, int output)