From: Andreas Rheinhardt <andreas.rheinhardt@outlook.com> To: FFmpeg development discussions and patches <ffmpeg-devel@ffmpeg.org> Subject: [FFmpeg-devel] [PATCH] avutil/csp: Improve enum range comparisons Date: Tue, 18 Mar 2025 16:07:35 +0100 Message-ID: <AS8P250MB0744B836F1E7A547DDE3881D8FDE2@AS8P250MB0744.EURP250.PROD.OUTLOOK.COM> (raw) [-- 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".
next reply other threads:[~2025-03-18 15:07 UTC|newest] Thread overview: 2+ messages / expand[flat|nested] mbox.gz Atom feed top 2025-03-18 15:07 Andreas Rheinhardt [this message] 2025-03-20 8:07 ` Andreas Rheinhardt
Reply instructions: You may reply publicly to this message via plain-text email using any one of the following methods: * Save the following mbox file, import it into your mail client, and reply-to-all from there: mbox Avoid top-posting and favor interleaved quoting: https://en.wikipedia.org/wiki/Posting_style#Interleaved_style * Reply using the --to, --cc, and --in-reply-to switches of git-send-email(1): git send-email \ --in-reply-to=AS8P250MB0744B836F1E7A547DDE3881D8FDE2@AS8P250MB0744.EURP250.PROD.OUTLOOK.COM \ --to=andreas.rheinhardt@outlook.com \ --cc=ffmpeg-devel@ffmpeg.org \ /path/to/YOUR_REPLY https://kernel.org/pub/software/scm/git/docs/git-send-email.html * If your mail client supports setting the In-Reply-To header via mailto: links, try the mailto: link
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