[FFmpeg-devel] [PATCH] examples: Replace with proper error handling to avoid potential memory errors

Nicolas George george at nsup.org
Wed Aug 6 12:14:51 EEST 2025


Alexander Strasser via ffmpeg-devel (HE12025-08-06):
> I'm a bit undecided about this; especially because these are example files.
> 
> Best would be to have them correct, small, and beautiful while still
> promoting good C practices.

Good C practices are verbose, and verbosity makes for poor examples.

> I agree that the patch as proposed is not an improvement over all.
> 
> I would say it would be best to shorten it to only the first change:
> 
>     --- a/doc/examples/decode_audio.c
>     +++ b/doc/examples/decode_audio.c
>     @@ -128,41 +128,48 @@ int main(int argc, char **argv)
>          outfilename = argv[2];
>     
>          pkt = av_packet_alloc();
>     +    if (!pkt)
>     +        exit(1);

Yes, the first change is necessary, it is not just good practice. It
would be better like the next ones, with an error message, though. This,
IMHO, is the sweet spot between verbosity and API didactics:

    codec = avcodec_find_decoder(AV_CODEC_ID_MP2);
    if (!codec) {
        fprintf(stderr, "Codec not found\n");
        exit(1);
    }

If we really want to bring attention to good practices, we can add:

	exit(1); /* or proper cleanup and returning */

And in any case, the commit message should inaccurately pretend to fix
possible leaks.

Regards,

-- 
  Nicolas George


More information about the ffmpeg-devel mailing list