diff mbox series

[FFmpeg-devel,6/7] avutil/channel_layout: fix some (un)initialization issues in av_channel_layout_from_string()

Message ID 20240309215414.26699-6-cus@passwd.hu
State Accepted
Commit a688fbfb88779b3b06ef91c196b70996c7fad5f4
Headers show
Series [FFmpeg-devel,1/7] avutil/channel_layout: add AV_CHANNEL_LAYOUT_RETYPE_FLAG_CANONICAL | 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 March 9, 2024, 9:54 p.m. UTC
Also make initialization/uninitialization behaviour more explicit in the docs,
and make sure we do not leak a channel map on error.

Signed-off-by: Marton Balint <cus@passwd.hu>
---
 libavutil/channel_layout.c | 5 +++++
 libavutil/channel_layout.h | 8 ++++++--
 2 files changed, 11 insertions(+), 2 deletions(-)
diff mbox series

Patch

diff --git a/libavutil/channel_layout.c b/libavutil/channel_layout.c
index 5db4cc9df0..9e8a1c12e6 100644
--- a/libavutil/channel_layout.c
+++ b/libavutil/channel_layout.c
@@ -302,6 +302,10 @@  int av_channel_layout_from_string(AVChannelLayout *channel_layout,
         }
     }
 
+    /* This function is a channel layout initializer, so we have to
+     * zero-initialize before we start setting fields individually. */
+    memset(channel_layout, 0, sizeof(*channel_layout));
+
     /* ambisonic */
     if (!strncmp(str, "ambisonic ", 10)) {
         const char *p = str + 10;
@@ -343,6 +347,7 @@  int av_channel_layout_from_string(AVChannelLayout *channel_layout,
                 for (i = 0; i < extra.nb_channels; i++) {
                     enum AVChannel ch = av_channel_layout_channel_from_index(&extra, i);
                     if (CHAN_IS_AMBI(ch)) {
+                        av_channel_layout_uninit(channel_layout);
                         av_channel_layout_uninit(&extra);
                         return AVERROR(EINVAL);
                     }
diff --git a/libavutil/channel_layout.h b/libavutil/channel_layout.h
index a1e9b08064..8a078d1601 100644
--- a/libavutil/channel_layout.h
+++ b/libavutil/channel_layout.h
@@ -512,10 +512,14 @@  int av_channel_layout_from_mask(AVChannelLayout *channel_layout, uint64_t mask);
  *  - the number of unordered channels (eg. "4C" or "4 channels")
  *  - the ambisonic order followed by optional non-diegetic channels (eg.
  *    "ambisonic 2+stereo")
+ * On error, the channel layout will remain uninitialized, but not necessarily
+ * untouched.
  *
- * @param channel_layout input channel layout
+ * @param channel_layout uninitialized channel layout for the result
  * @param str string describing the channel layout
- * @return 0 channel layout was detected, AVERROR_INVALIDATATA otherwise
+ * @return 0 on success parsing the channel layout
+ *         AVERROR(EINVAL) if an invalid channel layout string was provided
+ *         AVERROR(ENOMEM) if there was not enough memory
  */
 int av_channel_layout_from_string(AVChannelLayout *channel_layout,
                                   const char *str);