On Sat, 30 Nov 2013, Diego Biurrun wrote:

On Thu, Nov 28, 2013 at 06:08:07PM +0100, Alexandra Khirnova wrote:
--- a/libavcodec/bitstream.c
+++ b/libavcodec/bitstream.c
@@ -106,12 +106,16 @@ static int alloc_table(VLC *vlc, int size, int use_static)
     vlc->table_size += size;
     if (vlc->table_size > vlc->table_allocated) {
+        int err;
         if (use_static)
             return AVERROR_BUG;
         vlc->table_allocated += (1 << vlc->bits);
-        vlc->table = av_realloc(vlc->table, sizeof(VLC_TYPE) * 2 * 
vlc->table_allocated);
-        if (!vlc->table)
-            return AVERROR(ENOMEM);
+        if ((err = av_reallocp(&vlc->table,
+                               sizeof(VLC_TYPE) * 2 *
+                               vlc->table_allocated)) < 0) {
+            vlc->table_allocated = 0;
+            return err;
+        }
     }

It's not clear to me what the advantage of av_reallocp is here...

The advantage is that the existing code leaks memory if the realloc fails. Remember, if (av_)realloc fails, the old allocation is still valid. Whenever you do "ptr = realloc(ptr, ..)" you will have a leak on errors.

There's nothing that says that every single realloc needs to be changed into av_reallocp though. If handled properly, a normal av_realloc is just as good. But we tend to have faults in the handling of av_reallocs, and in some cases, using av_reallocp simplifies the code for handling it properly.

// Martin
_______________________________________________
libav-devel mailing list
[email protected]
https://lists.libav.org/mailman/listinfo/libav-devel

Reply via email to