How libavif Decodes Corrupted and Truncated AVIFs

This article examines how libavif, the reference library for the AV1 Image File Format (AVIF), handles corrupted or truncated image bitstreams. It details the library’s multi-layered validation process, explaining how it parses the container structure, delegates payload decoding to AV1 codecs, returns specific error codes, and maintains security when encountering malformed data.

Container-Level Validation (ISOBMFF)

An AVIF file is structured using the ISO Base Media File Format (ISOBMFF). Before any image data is sent to the AV1 decoder, libavif parses this container structure.

If a file is truncated or corrupted at the container level, the library’s parser detects missing or malformed boxes (such as ftyp, meta, or iprp). If critical metadata or layout information is missing, libavif immediately halts execution and returns an error code, typically AVIF_RESULT_BMFF_PARSE_FAILED or AVIF_RESULT_TRUNCATED_DATA, preventing the allocation of unnecessary memory for incomplete images.

Decoder-Level Validation (AV1 Bitstream)

Once the container structure is validated, libavif extracts the raw AV1 bitstream from the Media Data (mdat) box and passes it to an underlying AV1 decoder, such as dav1d or libaom.

Error Reporting and Return Codes

libavif does not crash when encountering malformed files. Instead, it uses a robust error-reporting system utilizing the avifResult enum. Common results returned during failed decodes include:

Security and Memory Safety

Because AVIF files are widely used on the web, libavif is designed with strict security boundaries to prevent exploits like buffer overflows or out-of-bounds reads. The library is continuously fuzzed via projects like Google’s OSS-Fuzz. This testing ensures that even highly corrupted, malicious, or randomly mutated bitstreams are safely rejected without compromising the host system’s memory safety.