shithub: mp3dec

Download patch

ref: ca285b2238643bedfc47107d7b5373a79d8b5026
parent: a0b3bfd8c6bd68d2a3492e24d3d79eae5d7064d8
author: lieff <lieff@users.noreply.github.com>
date: Sun Feb 23 15:38:03 EST 2020

test: check MP3D_E_PARAM cases

--- a/minimp3_ex.h
+++ b/minimp3_ex.h
@@ -220,6 +220,8 @@
 
 int mp3dec_load_cb(mp3dec_t *dec, mp3dec_io_t *io, uint8_t *buf, size_t buf_size, mp3dec_file_info_t *info, MP3D_PROGRESS_CB progress_cb, void *user_data)
 {
+    if (!dec || !buf || !info || (size_t)-1 == buf_size)
+        return MP3D_E_PARAM;
     uint64_t detected_samples = 0;
     size_t orig_buf_size = buf_size;
     int to_skip = 0;
@@ -226,8 +228,6 @@
     mp3dec_frame_info_t frame_info;
     memset(info, 0, sizeof(*info));
     memset(&frame_info, 0, sizeof(frame_info));
-    if (!dec || !buf || !info || (size_t)-1 == buf_size)
-        return MP3D_E_PARAM;
 
     /* skip id3 */
     size_t filled = 0, consumed = 0;
@@ -421,7 +421,7 @@
 int mp3dec_iterate_buf(const uint8_t *buf, size_t buf_size, MP3D_ITERATE_CB callback, void *user_data)
 {
     const uint8_t *orig_buf = buf;
-    if (!buf || (size_t)-1 == buf_size)
+    if (!buf || (size_t)-1 == buf_size || !callback)
         return MP3D_E_PARAM;
     /* skip id3 */
     mp3dec_skip_id3(&buf, &buf_size);
@@ -459,7 +459,7 @@
 
 int mp3dec_iterate_cb(mp3dec_io_t *io, uint8_t *buf, size_t buf_size, MP3D_ITERATE_CB callback, void *user_data)
 {
-    if (!io || !buf || (size_t)-1 == buf_size || buf_size < MINIMP3_BUF_SIZE)
+    if (!io || !buf || (size_t)-1 == buf_size || buf_size < MINIMP3_BUF_SIZE || !callback)
         return MP3D_E_PARAM;
     size_t filled = io->read(buf, MINIMP3_ID3_DETECT_SIZE, io->read_data), consumed = 0;
     uint64_t readed = 0;
@@ -584,7 +584,7 @@
 
 int mp3dec_ex_open_buf(mp3dec_ex_t *dec, const uint8_t *buf, size_t buf_size, int seek_method)
 {
-    if (!dec || !buf || (size_t)-1 == buf_size)
+    if (!dec || !buf || (size_t)-1 == buf_size || !(MP3D_SEEK_TO_BYTE == seek_method || MP3D_SEEK_TO_SAMPLE == seek_method))
         return MP3D_E_PARAM;
     memset(dec, 0, sizeof(*dec));
     dec->file.buffer = buf;
@@ -1149,6 +1149,8 @@
 int mp3dec_ex_open(mp3dec_ex_t *dec, const char *file_name, int seek_method)
 {
     int ret;
+    if (!dec)
+        return MP3D_E_PARAM;
     if ((ret = mp3dec_open_file(file_name, &dec->file)))
         return ret;
     return mp3dec_ex_open_mapinfo(dec, seek_method);
@@ -1156,7 +1158,7 @@
 
 int mp3dec_ex_open_cb(mp3dec_ex_t *dec, mp3dec_io_t *io, int seek_method)
 {
-    if (!dec || !io)
+    if (!dec || !io || !(MP3D_SEEK_TO_BYTE == seek_method || MP3D_SEEK_TO_SAMPLE == seek_method))
         return MP3D_E_PARAM;
     memset(dec, 0, sizeof(*dec));
 #ifdef MINIMP3_HAVE_RING
--- a/minimp3_test.c
+++ b/minimp3_test.c
@@ -348,6 +348,103 @@
 #endif
 }
 
+static int self_test(const char *input_file_name)
+{
+    int ret, size = 0;
+    mp3dec_t mp3d;
+    mp3dec_ex_t dec;
+    mp3dec_frame_info_t frame_info;
+    mp3dec_file_info_t finfo;
+    mp3dec_io_t io;
+    FILE *file = fopen(input_file_name, "rb");
+    uint8_t *buf = preload(file, &size);
+    fclose(file);
+    int samples = mp3dec_decode_frame(&mp3d, buf, size, 0, &frame_info);
+    free(buf);
+#define ASSERT(c) if (!(c)) { printf("failed, line=%d\n", __LINE__); exit(1); }
+    ASSERT(1152 == samples);
+
+    ret = mp3dec_load_buf(0, buf, size, &finfo, 0, 0);
+    ASSERT(MP3D_E_PARAM == ret);
+    ret = mp3dec_load_buf(&mp3d, 0, size, &finfo, 0, 0);
+    ASSERT(MP3D_E_PARAM == ret);
+    ret = mp3dec_load_buf(&mp3d, buf, (size_t)-1, &finfo, 0, 0);
+    ASSERT(MP3D_E_PARAM == ret);
+    ret = mp3dec_load_buf(&mp3d, buf, size, 0, 0, 0);
+    ASSERT(MP3D_E_PARAM == ret);
+
+    ret = mp3dec_load_cb(0, &io, buf, size, &finfo, 0, 0);
+    ASSERT(MP3D_E_PARAM == ret);
+    ret = mp3dec_load_cb(&mp3d, &io, 0, size, &finfo, 0, 0);
+    ASSERT(MP3D_E_PARAM == ret);
+    ret = mp3dec_load_cb(&mp3d, &io, buf, (size_t)-1, &finfo, 0, 0);
+    ASSERT(MP3D_E_PARAM == ret);
+    ret = mp3dec_load_cb(&mp3d, &io, buf, size, 0, 0, 0);
+    ASSERT(MP3D_E_PARAM == ret);
+
+    ret = mp3dec_iterate_buf(0, size, frames_iterate_cb, 0);
+    ASSERT(MP3D_E_PARAM == ret);
+    ret = mp3dec_iterate_buf(buf, (size_t)-1, frames_iterate_cb, 0);
+    ASSERT(MP3D_E_PARAM == ret);
+    ret = mp3dec_iterate_buf(buf, size, 0, 0);
+    ASSERT(MP3D_E_PARAM == ret);
+
+    ret = mp3dec_iterate_cb(0, buf, size, frames_iterate_cb, 0);
+    ASSERT(MP3D_E_PARAM == ret);
+    ret = mp3dec_iterate_cb(&io, 0, size, frames_iterate_cb, 0);
+    ASSERT(MP3D_E_PARAM == ret);
+    ret = mp3dec_iterate_cb(&io, buf, (size_t)-1, frames_iterate_cb, 0);
+    ASSERT(MP3D_E_PARAM == ret);
+    ret = mp3dec_iterate_cb(&io, buf, size, 0, 0);
+    ASSERT(MP3D_E_PARAM == ret);
+
+    ret = mp3dec_ex_open_buf(0, buf, size, MP3D_SEEK_TO_SAMPLE);
+    ASSERT(MP3D_E_PARAM == ret);
+    ret = mp3dec_ex_open_buf(&dec, 0, size, MP3D_SEEK_TO_SAMPLE);
+    ASSERT(MP3D_E_PARAM == ret);
+    ret = mp3dec_ex_open_buf(&dec, buf, (size_t)-1, MP3D_SEEK_TO_SAMPLE);
+    ASSERT(MP3D_E_PARAM == ret);
+    ret = mp3dec_ex_open_buf(&dec, buf, size, MP3D_SEEK_TO_SAMPLE + 1);
+    ASSERT(MP3D_E_PARAM == ret);
+
+    ret = mp3dec_ex_open_cb(0, &io, MP3D_SEEK_TO_SAMPLE);
+    ASSERT(MP3D_E_PARAM == ret);
+    ret = mp3dec_ex_open_cb(&dec, 0, MP3D_SEEK_TO_SAMPLE);
+    ASSERT(MP3D_E_PARAM == ret);
+    ret = mp3dec_ex_open_cb(&dec, &io, MP3D_SEEK_TO_SAMPLE + 1);
+    ASSERT(MP3D_E_PARAM == ret);
+
+    ret = mp3dec_ex_seek(0, 0);
+    ASSERT(MP3D_E_PARAM == ret);
+
+    ret = mp3dec_ex_read(0, (mp3d_sample_t*)buf, 10);
+    ASSERT(MP3D_E_PARAM == ret);
+    ret = mp3dec_ex_read(&dec, 0, 10);
+    ASSERT(MP3D_E_PARAM == ret);
+
+    ret = mp3dec_load(0, input_file_name, &finfo, 0, 0);
+    ASSERT(MP3D_E_PARAM == ret);
+    ret = mp3dec_load(&mp3d, 0, &finfo, 0, 0);
+    ASSERT(MP3D_E_PARAM == ret);
+    ret = mp3dec_load(&mp3d, input_file_name, 0, 0, 0);
+    ASSERT(MP3D_E_PARAM == ret);
+
+    ret = mp3dec_iterate(0, frames_iterate_cb, 0);
+    ASSERT(MP3D_E_PARAM == ret);
+    ret = mp3dec_iterate(input_file_name, 0, 0);
+    ASSERT(MP3D_E_PARAM == ret);
+
+    ret = mp3dec_ex_open(0, input_file_name, MP3D_SEEK_TO_SAMPLE);
+    ASSERT(MP3D_E_PARAM == ret);
+    ret = mp3dec_ex_open(&dec, 0, MP3D_SEEK_TO_SAMPLE);
+    ASSERT(MP3D_E_PARAM == ret);
+    ret = mp3dec_ex_open(&dec, input_file_name, MP3D_SEEK_TO_SAMPLE + 1);
+    ASSERT(MP3D_E_PARAM == ret);
+
+    printf("passed\n");
+    return 0;
+}
+
 #ifdef LIBFUZZER
 int LLVMFuzzerTestOneInput(const uint8_t *Data, size_t Size)
 {
@@ -363,7 +460,7 @@
 int main(int argc, char *argv[])
 #endif
 {
-    int i, ref_size;
+    int i, ref_size, do_self_test = 0;
     for(i = 1; i < argc; i++)
     {
         if (argv[i][0] != '-')
@@ -375,6 +472,7 @@
         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 'b': seek_to_byte = 1; break;
+        case 't': do_self_test = 1; break;
         default:
             printf("error: unrecognized option\n");
             return 1;
@@ -406,6 +504,8 @@
         printf("error: no file names given\n");
         return 1;
     }
+    if (do_self_test)
+        return self_test(input_file_name);
     decode_file(input_file_name, buf_ref, ref_size, file_out);
 #ifdef __AFL_HAVE_MANUAL_CONTROL
     }
--- a/scripts/build.sh
+++ b/scripts/build.sh
@@ -69,6 +69,7 @@
 ./minimp3 -m 6 -s 633 -b vectors/l3-sin1k0db.bit vectors/l3-sin1k0db_ofs633.pcm
 ./minimp3 -m 8 -s 215 -b vectors/l3-sin1k0db.bit vectors/l3-sin1k0db.pcm
 ./minimp3 -m 8 -s 633 -b vectors/l3-sin1k0db.bit vectors/l3-sin1k0db_ofs633.pcm
+./minimp3 -t vectors/l3-sin1k0db.bit
 
 gcov minimp3_test.c