shithub: mp3dec

Download patch

ref: cd9f1b5737c4869ddb655095304bb40861517f20
parent: 459b8319e5839fffa6ba421b6c0efffe357f742c
author: lieff <lieff@users.noreply.github.com>
date: Tue Feb 25 20:54:29 EST 2020

mp3dec_ex: fix mp3dec_ex_read io error handling + test

--- a/minimp3_ex.h
+++ b/minimp3_ex.h
@@ -810,7 +810,10 @@
                 dec->input_consumed = 0;
                 size_t readed = dec->io->read((uint8_t*)dec->file.buffer + dec->input_filled, dec->file.size - dec->input_filled, dec->io->read_data);
                 if (readed > (dec->file.size - dec->input_filled))
+                {
                     dec->last_error = MP3D_E_IOERROR;
+                    readed = 0;
+                }
                 if (readed != (dec->file.size - dec->input_filled))
                     eof = 1;
                 dec->input_filled += readed;
--- a/minimp3_test.c
+++ b/minimp3_test.c
@@ -115,9 +115,8 @@
 static size_t read_cb(void *buf, size_t size, void *user_data)
 {
     /*printf("%d read_cb(%d)\n", io_num, (int)size);*/
-    if (fail_io_num == io_num)
+    if (fail_io_num == io_num++)
         return -1;
-    io_num++;
     return fread(buf, 1, size, (FILE*)user_data);
 }
 
@@ -124,9 +123,8 @@
 static int seek_cb(uint64_t position, void *user_data)
 {
     /*printf("%d seek_cb(%d)\n", io_num, (int)position);*/
-    if (fail_io_num == io_num)
+    if (fail_io_num == io_num++)
         return -1;
-    io_num++;
     return fseek((FILE*)user_data, position, SEEK_SET);
 }
 
@@ -503,7 +501,9 @@
     memset(&mp3d, 0xff, sizeof(mp3d));
     memset(&finfo, 0xff, sizeof(finfo));
     ret = mp3dec_load(&mp3d, input_file_name, &finfo, progress_cb, 0);
-    ASSERT(MP3D_E_USER == ret && 2304 == finfo.samples);
+    ASSERT(MP3D_E_USER == ret && 2304 == finfo.samples && 44100 == finfo.hz && 2 == finfo.channels && 3 == finfo.layer);
+    ASSERT(NULL != finfo.buffer);
+    free(finfo.buffer);
 
     ret = mp3dec_iterate(0, frames_iterate_cb, 0);
     ASSERT(MP3D_E_PARAM == ret);
@@ -520,6 +520,30 @@
     ASSERT(MP3D_E_PARAM == ret);
     ret = mp3dec_ex_open(&dec, "not_foud", MP3D_SEEK_TO_SAMPLE);
     ASSERT(MP3D_E_IOERROR == ret);
+
+    file = fopen(input_file_name, "rb");
+    io.read = read_cb;
+    io.seek = seek_cb;
+    io.read_data = io.seek_data = file;
+
+    ret = mp3dec_ex_open_cb(&dec, &io, MP3D_SEEK_TO_SAMPLE);
+    ASSERT(0 == ret);
+    ASSERT(5 == io_num);
+    fail_io_num = 5;
+    mp3d_sample_t sample;
+    size_t readed = mp3dec_ex_read(&dec, &sample, 1);
+    ASSERT(0 == readed);
+    ASSERT(MP3D_E_IOERROR == dec.last_error);
+    readed = mp3dec_ex_read(&dec, &sample, 1);
+    ASSERT(0 == readed);
+    ASSERT(MP3D_E_IOERROR == dec.last_error); /* stays in error state */
+    ret = mp3dec_ex_seek(&dec, 0);
+    ASSERT(0 == ret);
+    ASSERT(0 == dec.last_error); /* error state reset */
+    readed = mp3dec_ex_read(&dec, &sample, 1);
+    ASSERT(1 == readed);
+    mp3dec_ex_close(&dec);
+    fclose((FILE*)io.read_data);
 
     printf("passed\n");
     return 0;
--- a/scripts/build.sh
+++ b/scripts/build.sh
@@ -54,7 +54,7 @@
 [[ "$(./minimp3 -m 8 -e 2 vectors/l3-sin1k0db.bit)" != "error: mp3dec_ex_open()=-3 failed" ]] && echo fail && exit 1 || echo pass
 [[ "$(./minimp3 -m 8 -e 3 vectors/l3-sin1k0db.bit)" != "error: mp3dec_ex_open()=-3 failed" ]] && echo fail && exit 1 || echo pass
 [[ "$(./minimp3 -m 8 -e 4 vectors/l3-sin1k0db.bit)" != "error: mp3dec_ex_open()=-3 failed" ]] && echo fail && exit 1 || echo pass
-[[ "$(./minimp3 -m 8 -e 5 vectors/l3-sin1k0db.bit)" != "error: mp3dec_ex_read() readed less than expected, last_error=-5" ]] && echo fail && exit 1 || echo pass
+[[ "$(./minimp3 -m 8 -e 5 vectors/l3-sin1k0db.bit)" != "error: mp3dec_ex_read() readed less than expected, last_error=-3" ]] && echo fail && exit 1 || echo pass
 
 [[ "$(./minimp3 -m 8 -s 2304 -e 5 vectors/l3-sin1k0db.bit)" != "error: mp3dec_ex_seek()=-3 failed" ]] && echo fail && exit 1 || echo pass
 [[ "$(./minimp3 -m 8 -s 2304 -e 6 vectors/l3-sin1k0db.bit)" != "error: mp3dec_ex_seek()=-3 failed" ]] && echo fail && exit 1 || echo pass