From 3a37c17379f82af90caa6990d402b1fc4930e9cd Mon Sep 17 00:00:00 2001 From: Andre Noll Date: Thu, 26 Aug 2021 19:20:49 +0200 Subject: [PATCH] mp4: Provide proper error codes for all errors. This changes the few remaining places where we return -1 to indicate failure by proper error codes which can be turned into a meaningful error message. --- error.h | 3 ++- mp4.c | 16 +++++++--------- 2 files changed, 9 insertions(+), 10 deletions(-) diff --git a/error.h b/error.h index 843c4ee8..a4fe78a5 100644 --- a/error.h +++ b/error.h @@ -137,11 +137,12 @@ PARA_ERROR(MP3DEC_CORRUPT, "too many corrupt frames"), \ PARA_ERROR(MP3DEC_EOF, "mp3dec: end of file"), \ PARA_ERROR(MP3_INFO, "could not read mp3 info"), \ + PARA_ERROR(MP4_READ, "mp4: read error or unexpected end of file"), \ + PARA_ERROR(MP4_CORRUPT, "invalid/corrupt mp4 file"), \ PARA_ERROR(MP4_BAD_CHANNEL_COUNT, "mp4: invalid number of channels"), \ PARA_ERROR(MP4_BAD_SAMPLE, "mp4: invalid sample number"), \ PARA_ERROR(MP4_BAD_SAMPLERATE, "mp4: invalid sample rate"), \ PARA_ERROR(MP4_BAD_SAMPLE_COUNT, "mp4: invalid number of samples"), \ - PARA_ERROR(MP4_OPEN, "mp4: open failed"), \ PARA_ERROR(MP4_TRACK, "mp4: no audio track"), \ PARA_ERROR(MP4_MISSING_ATOM, "mp4: essential atom not found"), \ PARA_ERROR(MPI_SCAN, "could not scan multi-precision integer"), \ diff --git a/mp4.c b/mp4.c index dbe2cf1c..757c4295 100644 --- a/mp4.c +++ b/mp4.c @@ -75,8 +75,8 @@ struct mp4 { }; /* - * Returns -1, 0, or 1 on errors/EOF/success. Partial reads followed by EOF or - * read errors are treated as errors. + * Returns -E_MP4_READ, 0, or 1 on errors/EOF/success. Partial reads followed + * by EOF or read errors are treated as errors. */ static int read_data(struct mp4 *f, void *data, size_t size) { @@ -86,7 +86,7 @@ static int read_data(struct mp4 *f, void *data, size_t size) continue; /* regard EAGAIN as an error as reads should be blocking. */ if (ret <= 0) - return ret < 0? -1 : 0; + return ret < 0? -E_MP4_READ : 0; size -= ret; } return 1; @@ -380,7 +380,7 @@ static int parse_tag(struct mp4 *f, uint8_t parent, int32_t size) if (atom_type != ATOM_DATA) continue; skip_bytes(f, 8); /* version (1), flags (3), reserved (4) */ - ret = -ERRNO_TO_PARA_ERROR(EINVAL); + ret = -E_MP4_CORRUPT; if (subsize < header_size + 8 || subsize > UINT_MAX) goto fail; len = subsize - (header_size + 8); @@ -392,7 +392,7 @@ static int parse_tag(struct mp4 *f, uint8_t parent, int32_t size) value[len] = '\0'; } if (!value) - return -ERRNO_TO_PARA_ERROR(EINVAL); + return -E_MP4_CORRUPT; f->meta.tags = para_realloc(f->meta.tags, (f->meta.count + 1) * sizeof(struct mp4_tag)); tag = f->meta.tags + f->meta.count; @@ -538,7 +538,7 @@ static int parse_sub_atoms(struct mp4 *f, uint64_t total_size, bool meta_only) if (ret <= 0) return ret; if (size == 0) - return -1; + return -E_MP4_CORRUPT; dest = get_position(f) + size - header_size; if (atom_type == ATOM_TRAK && f->track.state == ATS_SEEN_MP4A) { f->track.state = ATS_TRACK_CHANGE; @@ -591,10 +591,8 @@ static int open_file(const struct mp4_callback *cb, bool meta_only, struct mp4 * if (ret <= 0) break; } - if (ret < 0) { - ret = -E_MP4_OPEN; + if (ret < 0) goto fail; - } ret = -E_MP4_TRACK; if (f->track.channel_count == 0) goto fail; -- 2.39.2