avpacket: use av_reallocp() in av_packet_add_side_data()

Message ID 20161119185918.5832-1-jamrial@gmail.com
State New
Headers show

Commit Message

James Almer Nov. 19, 2016, 6:59 p.m.
Prevents leaking the previously allocated buffer, honoring the expected behavior
of keeping the packet unchanged in case of error.

Signed-off-by: James Almer <jamrial@gmail.com>
---
Now using av_reallocp() as suggested. Should still be backported to release/12.

 libavcodec/avpacket.c | 9 ++++-----
 1 file changed, 4 insertions(+), 5 deletions(-)

Comments

James Almer Nov. 19, 2016, 11:20 p.m. | #1
On 11/19/2016 3:59 PM, James Almer wrote:
> Prevents leaking the previously allocated buffer, honoring the expected behavior
> of keeping the packet unchanged in case of error.
> 
> Signed-off-by: James Almer <jamrial@gmail.com>
> ---
> Now using av_reallocp() as suggested.

Actually no, don't use this one. I didn't carefully read the doxy for
av_reallocp and just noticed it frees the original buffer if relocation
fails, which is not what should be done for av_packet_add_side_data().

This patch is dropped in favor of the original. Sorry for the noise.

Patch

diff --git a/libavcodec/avpacket.c b/libavcodec/avpacket.c
index f2b0a29..863659c 100644
--- a/libavcodec/avpacket.c
+++ b/libavcodec/avpacket.c
@@ -240,15 +240,14 @@  FF_ENABLE_DEPRECATION_WARNINGS
 int av_packet_add_side_data(AVPacket *pkt, enum AVPacketSideDataType type,
                             uint8_t *data, size_t size)
 {
-    int elems = pkt->side_data_elems;
+    int ret, elems = pkt->side_data_elems;
 
     if ((unsigned)elems + 1 > INT_MAX / sizeof(*pkt->side_data))
         return AVERROR(ERANGE);
 
-    pkt->side_data = av_realloc(pkt->side_data,
-                                (elems + 1) * sizeof(*pkt->side_data));
-    if (!pkt->side_data)
-        return AVERROR(ENOMEM);
+    ret = av_reallocp(&pkt->side_data, (elems + 1) * sizeof(*pkt->side_data));
+    if (ret < 0)
+        return ret;
 
     pkt->side_data[elems].data = data;
     pkt->side_data[elems].size = size;