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
5959 {
6060 const AVLumaCoefficients *coeffs;
6161
62− if (csp >= AVCOL_SPC_NB)
62+ if ((unsigned)csp >= AVCOL_SPC_NB)
6363 return NULL;
6464 coeffs = &luma_coefficients[csp];
6565 if (!coeffs->cr.num)
@@ -91,7 +91,7 @@ const AVColorPrimariesDesc *av_csp_primaries_desc_from_id(enum AVColorPrimaries · 25 unmodified lines
9191 {
9292 const AVColorPrimariesDesc *p;
9393
94− if (prm >= AVCOL_PRI_NB)
94+ if ((unsigned)prm >= AVCOL_PRI_NB)
9595 return NULL;
9696 p = &color_primaries[prm];
9797 if (!p->prim.r.x.num)
@@ -149,7 +149,7 @@ static const double approximate_gamma[AVCOL_TRC_NB] = { · 51 unmodified lines
149149 double av_csp_approximate_trc_gamma(enum AVColorTransferCharacteristic trc)
150150 {
151151 double gamma;
152− if (trc >= AVCOL_TRC_NB)
152+ if ((unsigned)trc >= AVCOL_TRC_NB)
153153 return 0.0;
154154 gamma = approximate_gamma[trc];
155155 if (gamma > 0)
@@ -399,7 +399,7 @@ static const av_csp_trc_function trc_funcs[AVCOL_TRC_NB] = { · 243 unmodified lines
399399
400400 av_csp_trc_function av_csp_trc_func_from_id(enum AVColorTransferCharacteristic trc)
401401 {
402− if (trc >= AVCOL_TRC_NB)
402+ if ((unsigned)trc >= AVCOL_TRC_NB)
403403 return NULL;
404404 return trc_funcs[trc];
405405 }
@@ -425,7 +425,7 @@ static const av_csp_trc_function trc_inv_funcs[AVCOL_TRC_NB] = { · 19 unmodified lines
425425
426426 av_csp_trc_function av_csp_trc_func_inv_from_id(enum AVColorTransferCharacteristic trc)
427427 {
428− if (trc >= AVCOL_TRC_NB)
428+ if ((unsigned)trc >= AVCOL_TRC_NB)
429429 return NULL;
430430 return trc_inv_funcs[trc];
431431 }
@@ -604,7 +604,7 @@ static const av_csp_eotf_function eotf_funcs[AVCOL_TRC_NB] = { · 172 unmodified lines
604604
605605 av_csp_eotf_function av_csp_itu_eotf(enum AVColorTransferCharacteristic trc)
606606 {
607− if (trc < 0 || trc >= AVCOL_TRC_NB)
607+ if ((unsigned)trc >= AVCOL_TRC_NB)
608608 return NULL;
609609 return eotf_funcs[trc];
610610 }
@@ -630,7 +630,7 @@ static const av_csp_eotf_function eotf_inv_funcs[AVCOL_TRC_NB] = { · 19 unmodified lines
630630
631631 av_csp_eotf_function av_csp_itu_eotf_inv(enum AVColorTransferCharacteristic trc)
632632 {
633− if (trc < 0 || trc >= AVCOL_TRC_NB)
633+ if ((unsigned)trc >= AVCOL_TRC_NB)
634634 return NULL;
635635 return eotf_inv_funcs[trc];
636636 }
libswscale/format.c+2−2
@@ -537,12 +537,12 @@ int sws_test_transfer(enum AVColorTransferCharacteristic trc, int output) · 536 unmodified lines
537537
538538 static int test_range(enum AVColorRange range)
539539 {
540− return range >= 0 && range < AVCOL_RANGE_NB;
540+ return (unsigned)range < AVCOL_RANGE_NB;
541541 }
542542
543543 static int test_loc(enum AVChromaLocation loc)
544544 {
545− return loc >= 0 && loc < AVCHROMA_LOC_NB;
545+ return (unsigned)loc < AVCHROMA_LOC_NB;
546546 }
547547
548548 int 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