Skip to content

Commit e7fbc86

Browse files
haasnsfan5
authored andcommitted
af_lavcac3enc: fix memory leak on no-op
Simply returning out of this function leaks avpkt, need to always "goto done". Rewrite the logic a bit to make it more clear what's going on (IMO). Fixes #9593
1 parent 85a4557 commit e7fbc86

File tree

1 file changed

+19
-16
lines changed

1 file changed

+19
-16
lines changed

audio/filter/af_lavcac3enc.c

Lines changed: 19 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -163,54 +163,55 @@ static void process(struct mp_filter *f)
163163
break;
164164
if (lavc_ret < 0 && lavc_ret != AVERROR(EAGAIN)) {
165165
MP_FATAL(f, "Encode failed (receive).\n");
166-
goto done;
166+
goto error;
167167
}
168168
}
169169
AVFrame *frame = NULL;
170170
struct mp_frame input = mp_pin_out_read(s->in_pin);
171171
// The following code assumes no sample data buffering in the encoder.
172-
if (input.type == MP_FRAME_EOF) {
172+
switch (input.type) {
173+
case MP_FRAME_NONE:
174+
goto done; // no data yet
175+
case MP_FRAME_EOF:
173176
mp_pin_in_write(f->ppins[1], input);
174-
return;
175-
} else if (input.type == MP_FRAME_AUDIO) {
177+
goto done;
178+
case MP_FRAME_AUDIO:
176179
TA_FREEP(&s->in_frame);
177180
s->in_frame = input.data;
178181
frame = mp_frame_to_av(input, NULL);
179182
if (!frame)
180-
goto done;
183+
goto error;
181184
if (mp_aframe_get_channels(s->in_frame) < s->opts->min_channel_num) {
182185
// Just pass it through.
183186
s->in_frame = NULL;
184187
mp_pin_in_write(f->ppins[1], input);
185-
return;
188+
goto done;
186189
}
187190
if (!mp_aframe_config_equals(s->in_frame, s->cur_format)) {
188191
if (!reinit(f))
189-
goto done;
192+
goto error;
190193
}
191-
} else if (input.type) {
192-
goto done;
193-
} else {
194-
return; // no data yet
194+
break;
195+
default: goto error; // unexpected packet type
195196
}
196197
int lavc_ret = avcodec_send_frame(s->lavc_actx, frame);
197198
av_frame_free(&frame);
198199
if (lavc_ret < 0 && lavc_ret != AVERROR(EAGAIN)) {
199200
MP_FATAL(f, "Encode failed (send).\n");
200-
goto done;
201+
goto error;
201202
}
202203
}
203204

204205
if (!s->in_frame)
205-
goto done;
206+
goto error;
206207

207208
out = mp_aframe_create();
208209
mp_aframe_set_format(out, AF_FORMAT_S_AC3);
209210
mp_aframe_set_chmap(out, &(struct mp_chmap)MP_CHMAP_INIT_STEREO);
210211
mp_aframe_set_rate(out, 48000);
211212

212213
if (mp_aframe_pool_allocate(s->out_pool, out, s->out_samples) < 0)
213-
goto done;
214+
goto error;
214215

215216
int sstride = mp_aframe_get_sstride(out);
216217

@@ -239,7 +240,7 @@ static void process(struct mp_filter *f)
239240

240241
uint8_t **planes = mp_aframe_get_data_rw(out);
241242
if (!planes)
242-
goto done;
243+
goto error;
243244
char *buf = planes[0];
244245
memcpy(buf, hdr, header_len);
245246
memcpy(buf + header_len, pkt.data, pkt.size);
@@ -250,8 +251,10 @@ static void process(struct mp_filter *f)
250251
mp_pin_in_write(f->ppins[1], MAKE_FRAME(MP_FRAME_AUDIO, out));
251252
out = NULL;
252253

253-
err = 0;
254254
done:
255+
err = false;
256+
// fall through
257+
error:
255258
av_packet_unref(&pkt);
256259
talloc_free(out);
257260
if (err)

0 commit comments

Comments
 (0)