Skip to content

Commit 9f85107

Browse files
wma: fix crash caused by retry goto jump without properly re-initiaizing buffer state
1 parent bbd2d90 commit 9f85107

File tree

1 file changed

+15
-14
lines changed

1 file changed

+15
-14
lines changed

plugins/wma/wma_plugin.c

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -207,23 +207,24 @@ wmaplug_read (DB_fileinfo_t *_info, char *bytes, int size) {
207207

208208
#if !USE_FFMPEG
209209
while (size > 0) {
210-
if (info->remaining == 0) {
210+
int sould_read_next_packet = 1;
211+
while (sould_read_next_packet && info->remaining == 0) {
212+
sould_read_next_packet = 0;
211213
int errcount = 0;
212214
int res = 0;
213-
uint8_t audiobuf_mem[info->wfx.packet_size]; // FIXME: it's not a good idea to allocate that much on stack
214-
uint8_t* audiobuf = audiobuf_mem;
215+
uint8_t *audiobuf_mem = calloc(1, info->wfx.packet_size);
216+
if (audiobuf_mem == NULL) {
217+
trace("wma: could not allocate memory for packet size: %d\n", (int)info->wfx.packet_size);
218+
break;
219+
}
220+
uint8_t *audiobuf = audiobuf_mem;
215221
int audiobufsize = 0;
216222
int packetlength = 0;
217223
new_packet:
218-
{
219-
int pos = deadbeef->ftell (info->info.file);
220-
res = asf_read_packet(&audiobuf, &audiobufsize, &packetlength, &info->wfx, info->info.file);
221-
int endpos = deadbeef->ftell (info->info.file);
222-
// trace ("[1] packet pos: %d, packet size: %d (%d), data size: %d, blockalign: %d, audiobufsize: %d\n", pos, endpos-pos, info->wfx.packet_size, packetlength, info->wfx.blockalign, audiobufsize);
223-
}
224+
res = asf_read_packet(&audiobuf, &audiobufsize, &packetlength, &info->wfx, info->info.file);
224225
if (res > 0) {
225226
int nb = audiobufsize / info->wfx.blockalign;
226-
for (int b = 0; b < nb; b++) {
227+
for (int b = 0; !sould_read_next_packet && b < nb; b++) {
227228
wma_decode_superframe_init(&info->wmadec, audiobuf + b * info->wfx.blockalign, info->wfx.blockalign);
228229

229230
int n = 0;
@@ -237,11 +238,10 @@ wmaplug_read (DB_fileinfo_t *_info, char *bytes, int size) {
237238
/* Do the above, but for errors in decode. */
238239
errcount++;
239240
trace ("WMA decode error %d, errcount %d\n",wmares, errcount);
240-
if (errcount > 5) {
241-
break;
242-
} else {
243-
goto new_packet;
241+
if (errcount <= 5) {
242+
sould_read_next_packet = 1;
244243
}
244+
break;
245245
} else if (wmares > 0) {
246246
if (wmares * info->wfx.channels * info->wfx.bitspersample / 8 > sizeof (info->buffer) - info->remaining) {
247247
fprintf (stderr, "WMA: decoding buffer is too small\n");
@@ -260,6 +260,7 @@ wmaplug_read (DB_fileinfo_t *_info, char *bytes, int size) {
260260
}
261261
}
262262
}
263+
free (audiobuf_mem);
263264
}
264265

265266
if (info->remaining == 0) {

0 commit comments

Comments
 (0)