Git Inbox Mirror of the ffmpeg-devel mailing list - see https://ffmpeg.org/mailman/listinfo/ffmpeg-devel
 help / color / mirror / Atom feed
* [FFmpeg-devel] [PATCH] avutil/csp: Improve enum range comparisons
@ 2025-03-18 15:07 Andreas Rheinhardt
  2025-03-20  8:07 ` Andreas Rheinhardt
  0 siblings, 1 reply; 2+ messages in thread
From: Andreas Rheinhardt @ 2025-03-18 15:07 UTC (permalink / raw)
  To: FFmpeg development discussions and patches

[-- Attachment #1: Type: text/plain, Size: 27 bytes --]

Patch attached.

- Andreas

[-- Attachment #2: 0001-avutil-csp-Improve-enum-range-comparisons.patch --]
[-- Type: text/x-patch, Size: 3897 bytes --]

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


[-- Attachment #3: Type: text/plain, Size: 251 bytes --]

_______________________________________________
ffmpeg-devel mailing list
ffmpeg-devel@ffmpeg.org
https://ffmpeg.org/mailman/listinfo/ffmpeg-devel

To unsubscribe, visit link above, or email
ffmpeg-devel-request@ffmpeg.org with subject "unsubscribe".

^ permalink raw reply	[flat|nested] 2+ messages in thread

* Re: [FFmpeg-devel] [PATCH] avutil/csp: Improve enum range comparisons
  2025-03-18 15:07 [FFmpeg-devel] [PATCH] avutil/csp: Improve enum range comparisons Andreas Rheinhardt
@ 2025-03-20  8:07 ` Andreas Rheinhardt
  0 siblings, 0 replies; 2+ messages in thread
From: Andreas Rheinhardt @ 2025-03-20  8:07 UTC (permalink / raw)
  To: ffmpeg-devel

Andreas Rheinhardt:
> Patch attached.
> 
> - Andreas
> 
Will apply this patch tomorrow unless there are objections.

- Andreas

_______________________________________________
ffmpeg-devel mailing list
ffmpeg-devel@ffmpeg.org
https://ffmpeg.org/mailman/listinfo/ffmpeg-devel

To unsubscribe, visit link above, or email
ffmpeg-devel-request@ffmpeg.org with subject "unsubscribe".

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2025-03-20  8:08 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2025-03-18 15:07 [FFmpeg-devel] [PATCH] avutil/csp: Improve enum range comparisons Andreas Rheinhardt
2025-03-20  8:07 ` Andreas Rheinhardt

Git Inbox Mirror of the ffmpeg-devel mailing list - see https://ffmpeg.org/mailman/listinfo/ffmpeg-devel

This inbox may be cloned and mirrored by anyone:

	git clone --mirror https://master.gitmailbox.com/ffmpegdev/0 ffmpegdev/git/0.git

	# If you have public-inbox 1.1+ installed, you may
	# initialize and index your mirror using the following commands:
	public-inbox-init -V2 ffmpegdev ffmpegdev/ https://master.gitmailbox.com/ffmpegdev \
		ffmpegdev@gitmailbox.com
	public-inbox-index ffmpegdev

Example config snippet for mirrors.


AGPL code for this site: git clone https://public-inbox.org/public-inbox.git