diff mbox series

[FFmpeg-devel,1/4] avformat/mov_chan: check channel count at compile time by using a nonconst expression

Message ID 20240401185621.15297-1-cus@passwd.hu
State New
Headers show
Series [FFmpeg-devel,1/4] avformat/mov_chan: check channel count at compile time by using a nonconst expression | expand

Checks

Context Check Description
yinshiyou/make_loongarch64 success Make finished
yinshiyou/make_fate_loongarch64 success Make fate finished
andriy/make_x86 success Make finished
andriy/make_fate_x86 success Make fate finished

Commit Message

Marton Balint April 1, 2024, 6:56 p.m. UTC
If the channel tag does not match the number of channels, the constant
expression evaluation should fail.

Signed-off-by: Marton Balint <cus@passwd.hu>
---
 libavformat/mov_chan.c | 48 +++++++++++++++++++-----------------------
 1 file changed, 22 insertions(+), 26 deletions(-)

Comments

Andreas Rheinhardt April 1, 2024, 8:41 p.m. UTC | #1
Marton Balint:
> If the channel tag does not match the number of channels, the constant
> expression evaluation should fail.
> 
> Signed-off-by: Marton Balint <cus@passwd.hu>
> ---
>  libavformat/mov_chan.c | 48 +++++++++++++++++++-----------------------
>  1 file changed, 22 insertions(+), 26 deletions(-)
> 
> diff --git a/libavformat/mov_chan.c b/libavformat/mov_chan.c
> index e7d181d71f..6ccc26b3f1 100644
> --- a/libavformat/mov_chan.c
> +++ b/libavformat/mov_chan.c
> @@ -81,24 +81,28 @@ struct MovChannelLayoutMap {
>      };
>  };
>  
> -#define TAG(_0)                                          {.tag = _0}
> -#define ID(_0)                                           {.id = c_##_0}
> -#define CHLIST(_0, ...)                                  TAG(_0), __VA_ARGS__
> -#define CHLIST01(_0, _1)                                 CHLIST(_0, ID(_1))
> -#define CHLIST02(_0, _1, _2)                             CHLIST(_0, ID(_1), ID(_2))
> -#define CHLIST03(_0, _1, _2, _3)                         CHLIST(_0, ID(_1), ID(_2), ID(_3))
> -#define CHLIST04(_0, _1, _2, _3, _4)                     CHLIST(_0, ID(_1), ID(_2), ID(_3), ID(_4))
> -#define CHLIST05(_0, _1, _2, _3, _4, _5)                 CHLIST(_0, ID(_1), ID(_2), ID(_3), ID(_4), ID(_5))
> -#define CHLIST06(_0, _1, _2, _3, _4, _5, _6)             CHLIST(_0, ID(_1), ID(_2), ID(_3), ID(_4), ID(_5), ID(_6))
> -#define CHLIST07(_0, _1, _2, _3, _4, _5, _6, _7)         CHLIST(_0, ID(_1), ID(_2), ID(_3), ID(_4), ID(_5), ID(_6), ID(_7))
> -#define CHLIST08(_0, _1, _2, _3, _4, _5, _6, _7, _8)     CHLIST(_0, ID(_1), ID(_2), ID(_3), ID(_4), ID(_5), ID(_6), ID(_7), ID(_8))
> -#define CHLIST09(_0, _1, _2, _3, _4, _5, _6, _7, _8, _9) CHLIST(_0, ID(_1), ID(_2), ID(_3), ID(_4), ID(_5), ID(_6), ID(_7), ID(_8), ID(_9))
> -#define CHLIST16(_0, _1, _2, _3, _4, _5, _6, _7, _8, _9, _10, _11, _12, _13, _14, _15, _16) \
> -    CHLIST(_0, ID(_1),  ID(_2),  ID(_3),  ID(_4),  ID(_5),  ID(_6), ID(_7), ID(_8), ID(_9), ID(_10), \
> -               ID(_11), ID(_12), ID(_13), ID(_14), ID(_15), ID(_16))
> -#define CHLIST21(_0, _1, _2, _3, _4, _5, _6, _7, _8, _9, _10, _11, _12, _13, _14, _15, _16, _17, _18, _19, _20, _21) \
> -    CHLIST(_0, ID(_1),  ID(_2),  ID(_3),  ID(_4),  ID(_5),  ID(_6),  ID(_7),  ID(_8),  ID(_9),  ID(_10), \
> -               ID(_11), ID(_12), ID(_13), ID(_14), ID(_15), ID(_16), ID(_17), ID(_18), ID(_19), ID(_20), ID(_21))
> +static int nonconst_expr(void) {
> +    return 0;
> +}
> +
> +#define TAG(_tag, _cnt)                                    {.tag = (_tag & 0xffff) == _cnt ? _tag : nonconst_expr()}
> +#define ID(_0)                                             {.id = c_##_0}
> +#define CHLIST(_tag, _cnt, ...)                            TAG(_tag, _cnt), __VA_ARGS__
> +#define CHLIST01(_tag, _1)                                 CHLIST(_tag, 1, ID(_1))
> +#define CHLIST02(_tag, _1, _2)                             CHLIST(_tag, 2, ID(_1), ID(_2))
> +#define CHLIST03(_tag, _1, _2, _3)                         CHLIST(_tag, 3, ID(_1), ID(_2), ID(_3))
> +#define CHLIST04(_tag, _1, _2, _3, _4)                     CHLIST(_tag, 4, ID(_1), ID(_2), ID(_3), ID(_4))
> +#define CHLIST05(_tag, _1, _2, _3, _4, _5)                 CHLIST(_tag, 5, ID(_1), ID(_2), ID(_3), ID(_4), ID(_5))
> +#define CHLIST06(_tag, _1, _2, _3, _4, _5, _6)             CHLIST(_tag, 6, ID(_1), ID(_2), ID(_3), ID(_4), ID(_5), ID(_6))
> +#define CHLIST07(_tag, _1, _2, _3, _4, _5, _6, _7)         CHLIST(_tag, 7, ID(_1), ID(_2), ID(_3), ID(_4), ID(_5), ID(_6), ID(_7))
> +#define CHLIST08(_tag, _1, _2, _3, _4, _5, _6, _7, _8)     CHLIST(_tag, 8, ID(_1), ID(_2), ID(_3), ID(_4), ID(_5), ID(_6), ID(_7), ID(_8))
> +#define CHLIST09(_tag, _1, _2, _3, _4, _5, _6, _7, _8, _9) CHLIST(_tag, 9, ID(_1), ID(_2), ID(_3), ID(_4), ID(_5), ID(_6), ID(_7), ID(_8), ID(_9))
> +#define CHLIST16(_tag, _1, _2, _3, _4, _5, _6, _7, _8, _9, _10, _11, _12, _13, _14, _15, _16) \
> +    CHLIST(_tag, 16, ID(_1),  ID(_2),  ID(_3),  ID(_4),  ID(_5),  ID(_6), ID(_7), ID(_8), ID(_9), ID(_10), \
> +                     ID(_11), ID(_12), ID(_13), ID(_14), ID(_15), ID(_16))
> +#define CHLIST21(_tag, _1, _2, _3, _4, _5, _6, _7, _8, _9, _10, _11, _12, _13, _14, _15, _16, _17, _18, _19, _20, _21) \
> +    CHLIST(_tag, 21, ID(_1),  ID(_2),  ID(_3),  ID(_4),  ID(_5),  ID(_6),  ID(_7),  ID(_8),  ID(_9),  ID(_10), \
> +                     ID(_11), ID(_12), ID(_13), ID(_14), ID(_15), ID(_16), ID(_17), ID(_18), ID(_19), ID(_20), ID(_21))
>  
>  static const struct MovChannelLayoutMap mov_ch_layout_map[] = {
>      CHLIST01( MOV_CH_LAYOUT_MONO,                 C ),
> @@ -281,14 +285,6 @@ static const struct {
>  
>  static const struct MovChannelLayoutMap* find_layout_map(uint32_t tag)
>  {
> -#if defined(ASSERT_LEVEL) && ASSERT_LEVEL > 1
> -    {
> -        int i;
> -        for (i = 0; i < FF_ARRAY_ELEMS(mov_ch_layout_map); i += 1 + (mov_ch_layout_map[i].tag & 0xffff))
> -            av_assert2(mov_ch_layout_map[i].tag & 0xffff0000);
> -        av_assert2(i == FF_ARRAY_ELEMS(mov_ch_layout_map));
> -    }
> -#endif
>      for (int i = 0; i < FF_ARRAY_ELEMS(mov_ch_layout_map); i += 1 + (mov_ch_layout_map[i].tag & 0xffff))
>          if (mov_ch_layout_map[i].tag == tag)
>              return &mov_ch_layout_map[i + 1];

When you want to check something at compile-time, wouldn't a
static_assert be the more appropriate tool? It allows to get nicer error
messages.
I see two ways how to use static_assert here (a) is preferred):
a) Turn the table used to initialize mov_ch_layout_map into an X-macro
and make TAG evaluate to static_assert((_tag & 0xffff) == _cnt, "Channel
count of " #_tag " wrong");} in one pass.
b) Shoehorn it into the TAG macro, e.g. like so:

#define CHECK_CONSISTENCY(_tag, _cnt)                            \
    sizeof(struct {int a; static_assert((_tag & 0xffff) == _cnt, \
           "Channel count of " #_tag " wrong");})
#define TAG(_tag, _cnt)                                    {.tag =
CHECK_CONSISTENCY(_tag, (_cnt)) ? _tag : 0}

static_assert may only be used where a declaration can be used; we use
sizeof with a new struct type to circumvent this. This is legal C, but
unfortunately MSVC warns about it (although it accepts it).

- Andreas
diff mbox series

Patch

diff --git a/libavformat/mov_chan.c b/libavformat/mov_chan.c
index e7d181d71f..6ccc26b3f1 100644
--- a/libavformat/mov_chan.c
+++ b/libavformat/mov_chan.c
@@ -81,24 +81,28 @@  struct MovChannelLayoutMap {
     };
 };
 
-#define TAG(_0)                                          {.tag = _0}
-#define ID(_0)                                           {.id = c_##_0}
-#define CHLIST(_0, ...)                                  TAG(_0), __VA_ARGS__
-#define CHLIST01(_0, _1)                                 CHLIST(_0, ID(_1))
-#define CHLIST02(_0, _1, _2)                             CHLIST(_0, ID(_1), ID(_2))
-#define CHLIST03(_0, _1, _2, _3)                         CHLIST(_0, ID(_1), ID(_2), ID(_3))
-#define CHLIST04(_0, _1, _2, _3, _4)                     CHLIST(_0, ID(_1), ID(_2), ID(_3), ID(_4))
-#define CHLIST05(_0, _1, _2, _3, _4, _5)                 CHLIST(_0, ID(_1), ID(_2), ID(_3), ID(_4), ID(_5))
-#define CHLIST06(_0, _1, _2, _3, _4, _5, _6)             CHLIST(_0, ID(_1), ID(_2), ID(_3), ID(_4), ID(_5), ID(_6))
-#define CHLIST07(_0, _1, _2, _3, _4, _5, _6, _7)         CHLIST(_0, ID(_1), ID(_2), ID(_3), ID(_4), ID(_5), ID(_6), ID(_7))
-#define CHLIST08(_0, _1, _2, _3, _4, _5, _6, _7, _8)     CHLIST(_0, ID(_1), ID(_2), ID(_3), ID(_4), ID(_5), ID(_6), ID(_7), ID(_8))
-#define CHLIST09(_0, _1, _2, _3, _4, _5, _6, _7, _8, _9) CHLIST(_0, ID(_1), ID(_2), ID(_3), ID(_4), ID(_5), ID(_6), ID(_7), ID(_8), ID(_9))
-#define CHLIST16(_0, _1, _2, _3, _4, _5, _6, _7, _8, _9, _10, _11, _12, _13, _14, _15, _16) \
-    CHLIST(_0, ID(_1),  ID(_2),  ID(_3),  ID(_4),  ID(_5),  ID(_6), ID(_7), ID(_8), ID(_9), ID(_10), \
-               ID(_11), ID(_12), ID(_13), ID(_14), ID(_15), ID(_16))
-#define CHLIST21(_0, _1, _2, _3, _4, _5, _6, _7, _8, _9, _10, _11, _12, _13, _14, _15, _16, _17, _18, _19, _20, _21) \
-    CHLIST(_0, ID(_1),  ID(_2),  ID(_3),  ID(_4),  ID(_5),  ID(_6),  ID(_7),  ID(_8),  ID(_9),  ID(_10), \
-               ID(_11), ID(_12), ID(_13), ID(_14), ID(_15), ID(_16), ID(_17), ID(_18), ID(_19), ID(_20), ID(_21))
+static int nonconst_expr(void) {
+    return 0;
+}
+
+#define TAG(_tag, _cnt)                                    {.tag = (_tag & 0xffff) == _cnt ? _tag : nonconst_expr()}
+#define ID(_0)                                             {.id = c_##_0}
+#define CHLIST(_tag, _cnt, ...)                            TAG(_tag, _cnt), __VA_ARGS__
+#define CHLIST01(_tag, _1)                                 CHLIST(_tag, 1, ID(_1))
+#define CHLIST02(_tag, _1, _2)                             CHLIST(_tag, 2, ID(_1), ID(_2))
+#define CHLIST03(_tag, _1, _2, _3)                         CHLIST(_tag, 3, ID(_1), ID(_2), ID(_3))
+#define CHLIST04(_tag, _1, _2, _3, _4)                     CHLIST(_tag, 4, ID(_1), ID(_2), ID(_3), ID(_4))
+#define CHLIST05(_tag, _1, _2, _3, _4, _5)                 CHLIST(_tag, 5, ID(_1), ID(_2), ID(_3), ID(_4), ID(_5))
+#define CHLIST06(_tag, _1, _2, _3, _4, _5, _6)             CHLIST(_tag, 6, ID(_1), ID(_2), ID(_3), ID(_4), ID(_5), ID(_6))
+#define CHLIST07(_tag, _1, _2, _3, _4, _5, _6, _7)         CHLIST(_tag, 7, ID(_1), ID(_2), ID(_3), ID(_4), ID(_5), ID(_6), ID(_7))
+#define CHLIST08(_tag, _1, _2, _3, _4, _5, _6, _7, _8)     CHLIST(_tag, 8, ID(_1), ID(_2), ID(_3), ID(_4), ID(_5), ID(_6), ID(_7), ID(_8))
+#define CHLIST09(_tag, _1, _2, _3, _4, _5, _6, _7, _8, _9) CHLIST(_tag, 9, ID(_1), ID(_2), ID(_3), ID(_4), ID(_5), ID(_6), ID(_7), ID(_8), ID(_9))
+#define CHLIST16(_tag, _1, _2, _3, _4, _5, _6, _7, _8, _9, _10, _11, _12, _13, _14, _15, _16) \
+    CHLIST(_tag, 16, ID(_1),  ID(_2),  ID(_3),  ID(_4),  ID(_5),  ID(_6), ID(_7), ID(_8), ID(_9), ID(_10), \
+                     ID(_11), ID(_12), ID(_13), ID(_14), ID(_15), ID(_16))
+#define CHLIST21(_tag, _1, _2, _3, _4, _5, _6, _7, _8, _9, _10, _11, _12, _13, _14, _15, _16, _17, _18, _19, _20, _21) \
+    CHLIST(_tag, 21, ID(_1),  ID(_2),  ID(_3),  ID(_4),  ID(_5),  ID(_6),  ID(_7),  ID(_8),  ID(_9),  ID(_10), \
+                     ID(_11), ID(_12), ID(_13), ID(_14), ID(_15), ID(_16), ID(_17), ID(_18), ID(_19), ID(_20), ID(_21))
 
 static const struct MovChannelLayoutMap mov_ch_layout_map[] = {
     CHLIST01( MOV_CH_LAYOUT_MONO,                 C ),
@@ -281,14 +285,6 @@  static const struct {
 
 static const struct MovChannelLayoutMap* find_layout_map(uint32_t tag)
 {
-#if defined(ASSERT_LEVEL) && ASSERT_LEVEL > 1
-    {
-        int i;
-        for (i = 0; i < FF_ARRAY_ELEMS(mov_ch_layout_map); i += 1 + (mov_ch_layout_map[i].tag & 0xffff))
-            av_assert2(mov_ch_layout_map[i].tag & 0xffff0000);
-        av_assert2(i == FF_ARRAY_ELEMS(mov_ch_layout_map));
-    }
-#endif
     for (int i = 0; i < FF_ARRAY_ELEMS(mov_ch_layout_map); i += 1 + (mov_ch_layout_map[i].tag & 0xffff))
         if (mov_ch_layout_map[i].tag == tag)
             return &mov_ch_layout_map[i + 1];