[FFmpeg-devel] [PATCH] lavu/buffer: add release function

Michael Niedermayer michaelni at gmx.at
Mon Feb 24 02:18:33 CET 2014


On Sun, Feb 23, 2014 at 11:19:23PM +0100, Lukasz Marek wrote:
> new function allows to unref buffer and obtain its data.
> 
> Signed-off-by: Lukasz Marek <lukasz.m.luki at gmail.com>
> ---
>  libavutil/buffer.c | 26 ++++++++++++++++++++++++++
>  libavutil/buffer.h | 12 ++++++++++++
>  2 files changed, 38 insertions(+)
> 
> diff --git a/libavutil/buffer.c b/libavutil/buffer.c
> index e9bf54b..a68b0be 100644
> --- a/libavutil/buffer.c
> +++ b/libavutil/buffer.c
> @@ -117,6 +117,32 @@ void av_buffer_unref(AVBufferRef **buf)
>      }
>  }
>  
> +int av_buffer_release(AVBufferRef **buf, uint8_t **data)
> +{
> +    AVBuffer *b;
> +
> +    if (!buf || !*buf) {
> +        if (data)
> +            *data = NULL;
> +        return 0;
> +    }
> +    b = (*buf)->buffer;
> +    av_freep(buf);
> +
> +    if (!avpriv_atomic_int_add_and_fetch(&b->refcount, -1)) {
> +        if (data)
> +            *data = b->data;
> +        else
> +            b->free(b->opaque, b->data);
> +        av_freep(&b);

> +    } else if (data) {
> +        *data = av_memdup(b->data, b->size);
> +        if (!*data)
> +            return AVERROR(ENOMEM);

this is not safe

you decreased the ref count and afterwards copy
but between the 2 the memory could have been deallocated


[...]
-- 
Michael     GnuPG fingerprint: 9FF2128B147EF6730BADF133611EC787040B0FAB

I am the wisest man alive, for I know one thing, and that is that I know
nothing. -- Socrates
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 198 bytes
Desc: Digital signature
URL: <http://ffmpeg.org/pipermail/ffmpeg-devel/attachments/20140224/773b9ac6/attachment.asc>


More information about the ffmpeg-devel mailing list