History

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.

Andreas Rheinhardt committed 1 year ago

Commit dff498f1 parent 0ce405a2 files · +9 −9Browse files

Unified · Split

2 files
  1. libavutil/csp.c +7 −7
  2. libswscale/format.c +2 −2
libavutil/csp.c+7−7
@@ -59,7 +59,7 @@ const struct AVLumaCoefficients *av_csp_luma_coeffs_from_avcsp(enum AVColorSpace · 58 unmodified lines
59{59{
60 const AVLumaCoefficients *coeffs;60 const AVLumaCoefficients *coeffs;
61 61
62 if (csp >= AVCOL_SPC_NB)62 if ((unsigned)csp >= AVCOL_SPC_NB)
63 return NULL;63 return NULL;
64 coeffs = &luma_coefficients[csp];64 coeffs = &luma_coefficients[csp];
65 if (!coeffs->cr.num)65 if (!coeffs->cr.num)
@@ -91,7 +91,7 @@ const AVColorPrimariesDesc *av_csp_primaries_desc_from_id(enum AVColorPrimaries · 25 unmodified lines
91{91{
92 const AVColorPrimariesDesc *p;92 const AVColorPrimariesDesc *p;
93 93
94 if (prm >= AVCOL_PRI_NB)94 if ((unsigned)prm >= AVCOL_PRI_NB)
95 return NULL;95 return NULL;
96 p = &color_primaries[prm];96 p = &color_primaries[prm];
97 if (!p->prim.r.x.num)97 if (!p->prim.r.x.num)
@@ -149,7 +149,7 @@ static const double approximate_gamma[AVCOL_TRC_NB] = { · 51 unmodified lines
149double av_csp_approximate_trc_gamma(enum AVColorTransferCharacteristic trc)149double av_csp_approximate_trc_gamma(enum AVColorTransferCharacteristic trc)
150{150{
151 double gamma;151 double gamma;
152 if (trc >= AVCOL_TRC_NB)152 if ((unsigned)trc >= AVCOL_TRC_NB)
153 return 0.0;153 return 0.0;
154 gamma = approximate_gamma[trc];154 gamma = approximate_gamma[trc];
155 if (gamma > 0)155 if (gamma > 0)
@@ -399,7 +399,7 @@ static const av_csp_trc_function trc_funcs[AVCOL_TRC_NB] = { · 243 unmodified lines
399 399
400av_csp_trc_function av_csp_trc_func_from_id(enum AVColorTransferCharacteristic trc)400av_csp_trc_function av_csp_trc_func_from_id(enum AVColorTransferCharacteristic trc)
401{401{
402 if (trc >= AVCOL_TRC_NB)402 if ((unsigned)trc >= AVCOL_TRC_NB)
403 return NULL;403 return NULL;
404 return trc_funcs[trc];404 return trc_funcs[trc];
405}405}
@@ -425,7 +425,7 @@ static const av_csp_trc_function trc_inv_funcs[AVCOL_TRC_NB] = { · 19 unmodified lines
425 425
426av_csp_trc_function av_csp_trc_func_inv_from_id(enum AVColorTransferCharacteristic trc)426av_csp_trc_function av_csp_trc_func_inv_from_id(enum AVColorTransferCharacteristic trc)
427{427{
428 if (trc >= AVCOL_TRC_NB)428 if ((unsigned)trc >= AVCOL_TRC_NB)
429 return NULL;429 return NULL;
430 return trc_inv_funcs[trc];430 return trc_inv_funcs[trc];
431}431}
@@ -604,7 +604,7 @@ static const av_csp_eotf_function eotf_funcs[AVCOL_TRC_NB] = { · 172 unmodified lines
604 604
605av_csp_eotf_function av_csp_itu_eotf(enum AVColorTransferCharacteristic trc)605av_csp_eotf_function av_csp_itu_eotf(enum AVColorTransferCharacteristic trc)
606{606{
607 if (trc < 0 || trc >= AVCOL_TRC_NB)607 if ((unsigned)trc >= AVCOL_TRC_NB)
608 return NULL;608 return NULL;
609 return eotf_funcs[trc];609 return eotf_funcs[trc];
610}610}
@@ -630,7 +630,7 @@ static const av_csp_eotf_function eotf_inv_funcs[AVCOL_TRC_NB] = { · 19 unmodified lines
630 630
631av_csp_eotf_function av_csp_itu_eotf_inv(enum AVColorTransferCharacteristic trc)631av_csp_eotf_function av_csp_itu_eotf_inv(enum AVColorTransferCharacteristic trc)
632{632{
633 if (trc < 0 || trc >= AVCOL_TRC_NB)633 if ((unsigned)trc >= AVCOL_TRC_NB)
634 return NULL;634 return NULL;
635 return eotf_inv_funcs[trc];635 return eotf_inv_funcs[trc];
636}636}
libswscale/format.c+2−2
@@ -537,12 +537,12 @@ int sws_test_transfer(enum AVColorTransferCharacteristic trc, int output) · 536 unmodified lines
537 537
538static int test_range(enum AVColorRange range)538static int test_range(enum AVColorRange range)
539{539{
540 return range >= 0 && range < AVCOL_RANGE_NB;540 return (unsigned)range < AVCOL_RANGE_NB;
541}541}
542 542
543static int test_loc(enum AVChromaLocation loc)543static int test_loc(enum AVChromaLocation loc)
544{544{
545 return loc >= 0 && loc < AVCHROMA_LOC_NB;545 return (unsigned)loc < AVCHROMA_LOC_NB;
546}546}
547 547
548int ff_test_fmt(const SwsFormat *fmt, int output)548int ff_test_fmt(const SwsFormat *fmt, int output)

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t