PR #24557 opened by emmaworley
URL: https://code.ffmpeg.org/FFmpeg/FFmpeg/pulls/24557
Patch URL: https://code.ffmpeg.org/FFmpeg/FFmpeg/pulls/24557.patch

There is a bug where `ff_hashtable_delete` does not decrement a hashtable's 
occupancy when no elements following a deleted entry need to be moved. There is 
also a bug where `ff_hashtable_clear` does not reset the occupancy to zero, 
This change fixes both issues.

Signed-off-by: Emma Worley <[email protected]>


>From 7b5591172ebb410bae9b521b943864603d489110 Mon Sep 17 00:00:00 2001
From: Emma Worley <[email protected]>
Date: Fri, 18 Sep 2026 00:28:24 -0700
Subject: [PATCH] lavc/hashtable: fix occupancy tracking across delete/clears
 There is a bug where `ff_hashtable_delete` does not decrement a hashtable's
 occupancy when no elements following a deleted entry need to be moved. There
 is also a bug where `ff_hashtable_clear` does not reset the occupancy to
 zero, This change fixes both issues.

Signed-off-by: Emma Worley <[email protected]>
---
 libavcodec/hashtable.c | 9 +++++----
 1 file changed, 5 insertions(+), 4 deletions(-)

diff --git a/libavcodec/hashtable.c b/libavcodec/hashtable.c
index d18e872f4f..a49c488e40 100644
--- a/libavcodec/hashtable.c
+++ b/libavcodec/hashtable.c
@@ -184,15 +184,15 @@ int ff_hashtable_delete(struct FFHashtableContext *ctx, 
const void *key)
                 if (++wrapped_index == ctx->max_entries)
                     wrapped_index = 0;
                 next_entry = ctx->table + wrapped_index * ctx->entry_size;
-                if (ENTRY_PSL_VAL(next_entry) <= 1) {
-                    ctx->nb_entries--;
-                    return 1;
-                }
+                if (ENTRY_PSL_VAL(next_entry) <= 1)
+                    break;
                 memcpy(entry, next_entry, ctx->entry_size);
                 ENTRY_PSL_VAL(entry)--;
                 ENTRY_PSL_VAL(next_entry) = 0;
                 entry = next_entry;
             }
+            ctx->nb_entries--;
+            return 1;
         }
     }
     return 0;
@@ -201,6 +201,7 @@ int ff_hashtable_delete(struct FFHashtableContext *ctx, 
const void *key)
 void ff_hashtable_clear(struct FFHashtableContext *ctx)
 {
     memset(ctx->table, 0, ctx->entry_size * ctx->max_entries);
+    ctx->nb_entries = 0;
 }
 
 av_cold void ff_hashtable_freep(FFHashtableContext **ctx)
-- 
2.52.0

_______________________________________________
ffmpeg-devel mailing list -- [email protected]
To unsubscribe send an email to [email protected]

Reply via email to