ref: 83598791a77784364d65675275c2914ae6f99045
parent: 2244e8d05711ed7e6683d9a71b281109bd514ed9
author: lieff <[email protected]>
date: Tue Feb 25 18:36:35 EST 2020
test: improve coverage
--- a/minimp3_ex.h
+++ b/minimp3_ex.h
@@ -349,7 +349,10 @@
if ((allocated - info->samples*sizeof(mp3d_sample_t)) < MINIMP3_MAX_SAMPLES_PER_FRAME*sizeof(mp3d_sample_t))
{
allocated *= 2;
- info->buffer = (mp3d_sample_t*)realloc(info->buffer, allocated);
+ mp3d_sample_t *alloc_buf = (mp3d_sample_t*)realloc(info->buffer, allocated);
+ if (!alloc_buf)
+ return MP3D_E_MEMORY;
+ info->buffer = alloc_buf;
}
if (io)
{
@@ -412,7 +415,12 @@
info->samples = detected_samples; /* cut padding */
/* reallocate to normal buffer size */
if (allocated != info->samples*sizeof(mp3d_sample_t))
- info->buffer = (mp3d_sample_t*)realloc(info->buffer, info->samples*sizeof(mp3d_sample_t));
+ {
+ mp3d_sample_t *alloc_buf = (mp3d_sample_t*)realloc(info->buffer, info->samples*sizeof(mp3d_sample_t));
+ if (!alloc_buf && info->samples)
+ return MP3D_E_MEMORY;
+ info->buffer = alloc_buf;
+ }
if (frames)
info->avg_bitrate_kbps = avg_bitrate_kbps/frames;
return ret;
@@ -565,9 +573,10 @@
dec->index.capacity = 4096;
else
dec->index.capacity *= 2;
- dec->index.frames = (mp3dec_frame_t *)realloc((void*)dec->index.frames, sizeof(mp3dec_frame_t)*dec->index.capacity);
- if (!dec->index.frames)
+ mp3dec_frame_t *alloc_buf = (mp3dec_frame_t *)realloc((void*)dec->index.frames, sizeof(mp3dec_frame_t)*dec->index.capacity);
+ if (!alloc_buf)
return MP3D_E_MEMORY;
+ dec->index.frames = alloc_buf;
}
idx_frame = &dec->index.frames[dec->index.num_frames++];
idx_frame->offset = offset;
--- a/minimp3_test.c
+++ b/minimp3_test.c
@@ -1,9 +1,11 @@
+#ifdef MINIMP3_TEST
+static int malloc_num = 0, fail_malloc_num = -1;
#include <stdio.h>
#include <stdlib.h>
-static int malloc_num = 0, fail_malloc_num = -1;
+#include <sys/mman.h>
static void *local_malloc(size_t size)
{
- /*printf("%d malloc_num(%d)\n", malloc_num, (int)size);*/
+ /*printf("%d malloc(%d)\n", malloc_num, (int)size);*/
if (fail_malloc_num == malloc_num)
return 0;
malloc_num++;
@@ -10,6 +12,25 @@
return malloc(size);
}
#define malloc local_malloc
+void *local_realloc(void *ptr, size_t new_size)
+{
+ /*printf("%d realloc(%d)\n", malloc_num, (int)new_size);*/
+ if (fail_malloc_num == malloc_num)
+ return 0;
+ malloc_num++;
+ return realloc(ptr, new_size);
+}
+#define realloc local_realloc
+void *local_mmap(void *addr, size_t length, int prot, int flags, int fd, off_t offset)
+{
+ /*printf("%d mmap(%d)\n", malloc_num, (int)length);*/
+ if (fail_malloc_num == malloc_num)
+ return MAP_FAILED;
+ malloc_num++;
+ return mmap(addr, length, prot, flags, fd, offset);
+}
+#define mmap local_mmap
+#endif
/*#define MINIMP3_ONLY_MP3*/
/*#define MINIMP3_ONLY_SIMD*/
@@ -132,7 +153,10 @@
d->allocated = 1024*1024;
else
d->allocated *= 2;
- d->info->buffer = realloc(d->info->buffer, d->allocated);
+ mp3d_sample_t *alloc_buf = realloc(d->info->buffer, d->allocated);
+ if (!alloc_buf)
+ return MP3D_E_MEMORY;
+ d->info->buffer = alloc_buf;
}
int samples = mp3dec_decode_frame(d->mp3d, frame, frame_size, d->info->buffer + d->info->samples, info);
if (samples)
@@ -527,7 +551,9 @@
case 's': i++; if (i < argc) position = atoi(argv[i]); break;
case 'p': i++; if (i < argc) portion = atoi(argv[i]); break;
case 'e': i++; if (i < argc) fail_io_num = atoi(argv[i]); break;
+#ifdef MINIMP3_TEST
case 'f': i++; if (i < argc) fail_malloc_num = atoi(argv[i]); break;
+#endif
case 'b': seek_to_byte = 1; break;
case 't': do_self_test = 1; break;
default:
--- a/scripts/build.sh
+++ b/scripts/build.sh
@@ -73,9 +73,19 @@
[[ "$(./minimp3 -m 6 -s 633 -b vectors/l3-sin1k0db.bit -)" != "rate=44100 samples=723456 max_diff=0 PSNR=99.000000" ]] && echo fail && exit 1 || echo pass
[[ "$(./minimp3 -f 0 vectors/l3-sin1k0db.bit vectors/l3-sin1k0db.pcm)" != "error: not enough memory" ]] && echo fail && exit 1 || echo pass
-[[ "$(./minimp3 -f 1 vectors/l3-sin1k0db.bit vectors/l3-sin1k0db.pcm)" != "error: read function failed, code=-2" ]] && echo fail && exit 1 || echo pass
+[[ "$(./minimp3 -f 1 vectors/l3-sin1k0db.bit vectors/l3-sin1k0db.pcm)" != "error: read function failed, code=-3" ]] && echo fail && exit 1 || echo pass
+[[ "$(./minimp3 -f 2 vectors/l3-sin1k0db.bit vectors/l3-sin1k0db.pcm)" != "error: read function failed, code=-2" ]] && echo fail && exit 1 || echo pass
+[[ "$(./minimp3 -f 3 vectors/l3-sin1k0db.bit vectors/l3-sin1k0db.pcm)" != "error: read function failed, code=-2" ]] && echo fail && exit 1 || echo pass
+[[ "$(./minimp3 -m 2 -f 0 vectors/l3-sin1k0db.bit)" != "error: not enough memory" ]] && echo fail && exit 1 || echo pass
+[[ "$(./minimp3 -m 2 -f 1 vectors/l3-sin1k0db.bit)" != "error: read function failed, code=-2" ]] && echo fail && exit 1 || echo pass
+[[ "$(./minimp3 -m 2 -f 2 vectors/l3-sin1k0db.bit)" != "error: read function failed, code=-2" ]] && echo fail && exit 1 || echo pass
+[[ "$(./minimp3 -m 2 -f 3 vectors/l3-sin1k0db.bit)" != "error: read function failed, code=-2" ]] && echo fail && exit 1 || echo pass
+
+[[ "$(./minimp3 -m 6 -f 1 vectors/l3-sin1k0db.bit)" != "error: mp3dec_ex_open()=-2 failed" ]] && echo fail && exit 1 || echo pass
+
[[ "$(./minimp3 -m 8 -f 0 vectors/l3-sin1k0db.bit)" != "error: mp3dec_ex_open()=-2 failed" ]] && echo fail && exit 1 || echo pass
+[[ "$(./minimp3 -m 8 -f 1 vectors/l3-sin1k0db.bit)" != "error: mp3dec_ex_open()=-2 failed" ]] && echo fail && exit 1 || echo pass
set -e
./minimp3 -m 6 -s 215 -b vectors/l3-sin1k0db.bit vectors/l3-sin1k0db.pcm