History

avformat/webpenc: Fix memleak when using invalid packets

The WebP muxer sometimes caches a packet it receives to write it later; yet if a cached packet is too small (so small as to be invalid), it is cached, but not written and not unreferenced. Such a packet leaks, either by being overwritten by the next packet or because it is never unreferenced at all. Fix this by not caching unusable packets at all; and error out on invalid packets.

Andreas Rheinhardt committed 5 years ago

Commit f9043de1 parent 484a3221 file · +11 −10Browse files

Unified · Split

libavformat/webpenc.c+11−10
@@ -53,24 +53,22 @@ static int webp_init(AVFormatContext *s) · 52 unmodified lines
5353
5454 static int is_animated_webp_packet(AVPacket *pkt)
5555 {
56− if (pkt->size) {
5756 int skip = 0;
5857 unsigned flags = 0;
5958
6059 if (pkt->size < 4)
61− return 0;
60+ return AVERROR_INVALIDDATA;
6261 if (AV_RL32(pkt->data) == AV_RL32("RIFF"))
6362 skip = 12;
64−
63+ // Safe to do this as a valid WebP bitstream is >=30 bytes.
6564 if (pkt->size < skip + 4)
66− return 0;
65+ return AVERROR_INVALIDDATA;
6766 if (AV_RL32(pkt->data + skip) == AV_RL32("VP8X")) {
6867 flags |= pkt->data[skip + 4 + 4];
6968 }
7069
7170 if (flags & 2) // ANIMATION_FLAG is on
7271 return 1;
73− }
7472 return 0;
7573 }
7674
@@ -84,13 +82,9 @@ static int flush(AVFormatContext *s, int trailer, int64_t pts) · 7 unmodified lines
8482 unsigned flags = 0;
8583 int vp8x = 0;
8684
87− if (w->last_pkt.size < 4)
88− return 0;
8985 if (AV_RL32(w->last_pkt.data) == AV_RL32("RIFF"))
9086 skip = 12;
9187
92− if (w->last_pkt.size < skip + 4)
93− return 0; // Safe to do this as a valid WebP bitstream is >=30 bytes.
9488 if (AV_RL32(w->last_pkt.data + skip) == AV_RL32("VP8X")) {
9589 flags |= w->last_pkt.data[skip + 4 + 4];
9690 vp8x = 1;
@@ -149,7 +143,14 @@ static int flush(AVFormatContext *s, int trailer, int64_t pts) · 52 unmodified lines
149143 static int webp_write_packet(AVFormatContext *s, AVPacket *pkt)
150144 {
151145 WebpContext *w = s->priv_data;
152− w->using_webp_anim_encoder |= is_animated_webp_packet(pkt);
146+ int ret;
147+
148+ if (!pkt->size)
149+ return 0;
150+ ret = is_animated_webp_packet(pkt);
151+ if (ret < 0)
152+ return ret;
153+ w->using_webp_anim_encoder |= ret;
153154
154155 if (w->using_webp_anim_encoder) {
155156 avio_write(s->pb, pkt->data, pkt->size);

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t