[FFmpeg-devel] [PATCH] lavu: make av_get_media_type_string() never return NULL

James Almer jamrial at gmail.com
Wed Feb 2 00:34:48 EET 2022


On 2/1/2022 7:30 PM, Scott Theisen wrote:
> printf %s with a null pointer is undefined behavior
> ---
>   libavutil/avutil.h | 3 +--
>   libavutil/utils.c  | 3 ++-
>   2 files changed, 3 insertions(+), 3 deletions(-)
> 
> diff --git a/libavutil/avutil.h b/libavutil/avutil.h
> index 4d633156d1..4bd468d72f 100644
> --- a/libavutil/avutil.h
> +++ b/libavutil/avutil.h
> @@ -207,8 +207,7 @@ enum AVMediaType {
>   };
>   
>   /**
> - * Return a string describing the media_type enum, NULL if media_type
> - * is unknown.
> + * Return a string describing the media_type enum, never NULL.

This is an API breakage, so it's not ok.

The doxy states it returns NULL if media_type is unknown, so you're 
expected to check the returned pointer before trying to use it.

>    */
>   const char *av_get_media_type_string(enum AVMediaType media_type);
>   
> diff --git a/libavutil/utils.c b/libavutil/utils.c
> index ea9b5097b8..c85d7abace 100644
> --- a/libavutil/utils.c
> +++ b/libavutil/utils.c
> @@ -71,12 +71,13 @@ const char *avutil_license(void)
>   const char *av_get_media_type_string(enum AVMediaType media_type)
>   {
>       switch (media_type) {
> +    case AVMEDIA_TYPE_UNKNOWN:    return "unknown";
>       case AVMEDIA_TYPE_VIDEO:      return "video";
>       case AVMEDIA_TYPE_AUDIO:      return "audio";
>       case AVMEDIA_TYPE_DATA:       return "data";
>       case AVMEDIA_TYPE_SUBTITLE:   return "subtitle";
>       case AVMEDIA_TYPE_ATTACHMENT: return "attachment";
> -    default:                      return NULL;
> +    default:                      return "invalid";
>       }
>   }
>   


More information about the ffmpeg-devel mailing list