shithub: mp3dec

Download patch

ref: 2244e8d05711ed7e6683d9a71b281109bd514ed9
parent: b2f252b9301b93c6fba095140db6edf26d463f2c
author: lieff <[email protected]>
date: Tue Feb 25 17:23:53 EST 2020

test: improve coverage

--- a/minimp3_test.c
+++ b/minimp3_test.c
@@ -1,3 +1,16 @@
+#include <stdio.h>
+#include <stdlib.h>
+static int malloc_num = 0, fail_malloc_num = -1;
+static void *local_malloc(size_t size)
+{
+    /*printf("%d malloc_num(%d)\n", malloc_num, (int)size);*/
+    if (fail_malloc_num == malloc_num)
+        return 0;
+    malloc_num++;
+    return malloc(size);
+}
+#define malloc local_malloc
+
 /*#define MINIMP3_ONLY_MP3*/
 /*#define MINIMP3_ONLY_SIMD*/
 /*#define MINIMP3_NONSTANDARD_BUT_LOGICAL*/
@@ -63,8 +76,13 @@
     if (fseek(file, 0, SEEK_SET))
         return 0;
     data = (unsigned char*)malloc(*data_size);
-    if (!data)
-        return 0;
+#define FAIL_MEM(data) \
+    if (!(data)) \
+    { \
+        printf("error: not enough memory\n"); \
+        exit(1); \
+    }
+    FAIL_MEM(data);
     if ((int)fread(data, 1, *data_size, file) != *data_size)
         exit(1);
     return data;
@@ -159,6 +177,7 @@
     } else if (MODE_LOAD_CB == mode)
     {
         uint8_t *io_buf = malloc(MINIMP3_IO_SIZE);
+        FAIL_MEM(io_buf);
         FILE *file = fopen(input_file_name, "rb");
         io.read_data = io.seek_data = file;
         res = file ? mp3dec_load_cb(&mp3d, &io, io_buf, MINIMP3_IO_SIZE, &info, 0, 0) : MP3D_E_IOERROR;
@@ -182,6 +201,7 @@
     } else if (MODE_ITERATE_CB == mode)
     {
         uint8_t *io_buf = malloc(MINIMP3_IO_SIZE);
+        FAIL_MEM(io_buf);
         FILE *file = fopen(input_file_name, "rb");
         io.read_data = io.seek_data = file;
         frames_iterate_data d = { &mp3d, &info, 0 };
@@ -217,6 +237,7 @@
         }
         info.samples = dec.samples;
         info.buffer  = malloc(dec.samples*sizeof(mp3d_sample_t));
+        FAIL_MEM(info.buffer);
         info.hz      = dec.info.hz;
         info.layer   = dec.info.layer;
         info.channels = dec.info.channels;
@@ -299,6 +320,7 @@
     }
 #ifdef MINIMP3_FLOAT_OUTPUT
     int16_t *buffer = malloc(info.samples*sizeof(int16_t));
+    FAIL_MEM(buffer);
     mp3dec_f32_to_s16(info.buffer, buffer, info.samples);
     free(info.buffer);
 #else
@@ -505,6 +527,7 @@
         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;
+        case 'f': i++; if (i < argc) fail_malloc_num = atoi(argv[i]); break;
         case 'b': seek_to_byte = 1; break;
         case 't': do_self_test = 1; break;
         default:
--- a/scripts/build.sh
+++ b/scripts/build.sh
@@ -71,6 +71,11 @@
 
 [[ "$(./minimp3 -m 6 -s 215 -b vectors/l3-sin1k0db.bit -)" != "rate=44100 samples=725760 max_diff=0 PSNR=99.000000" ]] && echo fail && exit 1 || echo pass
 [[ "$(./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 -m 8 -f 0 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