From e7c96c9061addc2c41ce76973f040a24e07be5d9 Mon Sep 17 00:00:00 2001
From: Andreas Rheinhardt <andreas.rheinhardt@outlook.com>
Date: Sun, 20 Apr 2025 21:29:47 +0200
Subject: [PATCH 11/13] avcodec/webp: Avoid loop

Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@outlook.com>
---
 libavcodec/webp.c | 25 +++++++++----------------
 1 file changed, 9 insertions(+), 16 deletions(-)

diff --git a/libavcodec/webp.c b/libavcodec/webp.c
index e26bf01c6a..7d77d64524 100644
--- a/libavcodec/webp.c
+++ b/libavcodec/webp.c
@@ -267,31 +267,24 @@ static int huff_reader_build_canonical(HuffReader *r, const uint8_t *code_length
         len_counts[len] = nb_codes;
         nb_codes += cnt;
     }
+
+    for (int sym = 0; sym < alphabet_size; ++sym) {
+        if (code_lengths[sym]) {
+            unsigned idx = len_counts[code_lengths[sym]]++;
+            syms[idx] = sym;
+            lens[idx] = code_lengths[sym];
+        }
+    }
     if (nb_codes <= 1) {
         if (nb_codes == 1) {
             /* special-case 1 symbol since the vlc reader cannot handle it */
             r->nb_symbols = 1;
             r->simple = 1;
-            for (int sym = 0;; ++sym) {
-                av_assert1(sym < alphabet_size);
-                if (code_lengths[sym]) {
-                    r->simple_symbols[0] = sym;
-                    return 0;
-                }
-            }
+            r->simple_symbols[0] = syms[0];
         }
         // No symbols
         return AVERROR_INVALIDDATA;
     }
-
-    for (int sym = 0; sym < alphabet_size; ++sym) {
-        if (code_lengths[sym]) {
-            unsigned idx = len_counts[code_lengths[sym]]++;
-            syms[idx] = sym;
-            lens[idx] = code_lengths[sym];
-        }
-    }
-
     ret = ff_vlc_init_from_lengths(&r->vlc, 8, nb_codes, lens, 1,
                                    syms, 2, 2, 0, VLC_INIT_OUTPUT_LE, logctx);
     if (ret < 0)
-- 
2.45.2