]> git.tuebingen.mpg.de Git - paraslash.git/commitdiff
mp4: Add error checking for atom_read().
authorAndre Noll <maan@tuebingen.mpg.de>
Wed, 18 Aug 2021 14:59:35 +0000 (16:59 +0200)
committerAndre Noll <maan@tuebingen.mpg.de>
Mon, 30 May 2022 19:37:35 +0000 (21:37 +0200)
While the individual atom parsers all perform error checking and
return an error code, their caller, atom_read(), ignores errors.

Address this shortcoming, simplify the function by using a switch
instead of an if-else chain and move the descriptions of the atoms
to the enum where they belong.

mp4.c

diff --git a/mp4.c b/mp4.c
index 3e7a74a88d4c3c2c401fc77947c77296045c8d1f..7b5f4694d5afe93d2b5fb96efa46646d6f967420 100644 (file)
--- a/mp4.c
+++ b/mp4.c
@@ -180,16 +180,16 @@ enum atoms {
        ATOM_MVHD = 131,
        ATOM_TKHD = 132,
        ATOM_TREF = 133,
-       ATOM_MDHD = 134,
+       ATOM_MDHD = 134, /* track header */
        ATOM_VMHD = 135,
        ATOM_SMHD = 136,
        ATOM_HMHD = 137,
-       ATOM_STSD = 138,
-       ATOM_STTS = 139,
-       ATOM_STSZ = 140,
+       ATOM_STSD = 138, /* sample description box */
+       ATOM_STTS = 139, /* time to sample box */
+       ATOM_STSZ = 140, /* sample size box */
        ATOM_STZ2 = 141,
-       ATOM_STCO = 142,
-       ATOM_STSC = 143,
+       ATOM_STCO = 142, /* chunk offset box */
+       ATOM_STSC = 143, /* sample to chunk box */
        ATOM_MP4A = 144,
        ATOM_MP4V = 145,
        ATOM_MP4S = 146,
@@ -870,34 +870,22 @@ static int32_t read_meta(struct mp4 *f, uint64_t size)
        return 1;
 }
 
-static int32_t atom_read(struct mp4 *f, int32_t size, uint8_t atom_type)
+static int atom_read(struct mp4 *f, uint64_t size, uint8_t atom_type)
 {
        uint64_t dest_position = get_position(f) + size - 8;
-       if (atom_type == ATOM_STSZ) {
-               /* sample size box */
-               read_stsz(f);
-       } else if (atom_type == ATOM_STTS) {
-               /* time to sample box */
-               read_stts(f);
-       } else if (atom_type == ATOM_STSC) {
-               /* sample to chunk box */
-               read_stsc(f);
-       } else if (atom_type == ATOM_STCO) {
-               /* chunk offset box */
-               read_stco(f);
-       } else if (atom_type == ATOM_STSD) {
-               /* sample description box */
-               read_stsd(f);
-       } else if (atom_type == ATOM_MDHD) {
-               /* track header */
-               read_mdhd(f);
-       } else if (atom_type == ATOM_META) {
-               /* iTunes Metadata box */
-               read_meta(f, size);
-       }
+       int ret = 1; /* return success for atoms we don't care about */
 
+       switch (atom_type) {
+       case ATOM_STSZ: ret = read_stsz(f); break;
+       case ATOM_STTS: ret = read_stts(f); break;
+       case ATOM_STSC: ret = read_stsc(f); break;
+       case ATOM_STCO: ret = read_stco(f); break;
+       case ATOM_STSD: ret = read_stsd(f); break;
+       case ATOM_MDHD: ret = read_mdhd(f); break;
+       case ATOM_META: ret = read_meta(f, size); break;
+       }
        set_position(f, dest_position);
-       return 0;
+       return ret;
 }
 
 /* parse atoms that are sub atoms of other atoms */
@@ -927,7 +915,9 @@ static int parse_sub_atoms(struct mp4 *f, uint64_t total_size, int meta_only)
                } else if (atom_type < SUBATOMIC) {
                        parse_sub_atoms(f, size - header_size, meta_only);
                } else {
-                       atom_read(f, (uint32_t) size, atom_type);
+                       ret = atom_read(f, size, atom_type);
+                       if (ret <= 0)
+                               return ret;
                }
        }
        return 1;