diff options
author | Andreas Rheinhardt <andreas.rheinhardt@gmail.com> | 2021-03-21 06:29:13 +0100 |
---|---|---|
committer | Andreas Rheinhardt <andreas.rheinhardt@outlook.com> | 2021-04-02 21:35:55 +0200 |
commit | 0bbf1f4785f6efcb252aacbf5c7391f45286e313 (patch) | |
tree | 8d36875aac5ad89f1c5c47892ee300d68e388734 | |
parent | a57ba45eb4f5a0beefc833012420605ad6c992ee (diff) | |
download | ffmpeg-0bbf1f4785f6efcb252aacbf5c7391f45286e313.tar.gz |
avcodec/avcodec: Don't use NULL for %s printf specifier
Our "get name" functions can return NULL for invalid/unknown
arguments. So check for this.
Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@gmail.com>
(cherry picked from commit 88b7d9fd367c16302c9cc5dfbd045a7cc684eca4)
-rw-r--r-- | libavcodec/avcodec.c | 38 |
1 files changed, 22 insertions, 16 deletions
diff --git a/libavcodec/avcodec.c b/libavcodec/avcodec.c index 50d910b06a..c6b8afe1c7 100644 --- a/libavcodec/avcodec.c +++ b/libavcodec/avcodec.c @@ -644,6 +644,11 @@ FF_ENABLE_DEPRECATION_WARNINGS return 0; } +static const char *unknown_if_null(const char *str) +{ + return str ? str : "unknown"; +} + void avcodec_string(char *buf, int buf_size, AVCodecContext *enc, int encode) { const char *codec_type; @@ -653,6 +658,7 @@ void avcodec_string(char *buf, int buf_size, AVCodecContext *enc, int encode) int new_line = 0; AVRational display_aspect_ratio; const char *separator = enc->dump_separator ? (const char *)enc->dump_separator : ", "; + const char *str; if (!buf || buf_size <= 0) return; @@ -688,14 +694,14 @@ void avcodec_string(char *buf, int buf_size, AVCodecContext *enc, int encode) av_strlcat(buf, separator, buf_size); snprintf(buf + strlen(buf), buf_size - strlen(buf), - "%s", enc->pix_fmt == AV_PIX_FMT_NONE ? "none" : - av_get_pix_fmt_name(enc->pix_fmt)); + "%s", enc->pix_fmt == AV_PIX_FMT_NONE ? "none" : + unknown_if_null(av_get_pix_fmt_name(enc->pix_fmt))); if (enc->bits_per_raw_sample && enc->pix_fmt != AV_PIX_FMT_NONE && enc->bits_per_raw_sample < av_pix_fmt_desc_get(enc->pix_fmt)->comp[0].depth) av_strlcatf(detail, sizeof(detail), "%d bpc, ", enc->bits_per_raw_sample); - if (enc->color_range != AVCOL_RANGE_UNSPECIFIED) - av_strlcatf(detail, sizeof(detail), "%s, ", - av_color_range_name(enc->color_range)); + if (enc->color_range != AVCOL_RANGE_UNSPECIFIED && + (str = av_color_range_name(enc->color_range))) + av_strlcatf(detail, sizeof(detail), "%s, ", str); if (enc->colorspace != AVCOL_SPC_UNSPECIFIED || enc->color_primaries != AVCOL_PRI_UNSPECIFIED || @@ -704,12 +710,11 @@ void avcodec_string(char *buf, int buf_size, AVCodecContext *enc, int encode) enc->colorspace != (int)enc->color_trc) { new_line = 1; av_strlcatf(detail, sizeof(detail), "%s/%s/%s, ", - av_color_space_name(enc->colorspace), - av_color_primaries_name(enc->color_primaries), - av_color_transfer_name(enc->color_trc)); - } else - av_strlcatf(detail, sizeof(detail), "%s, ", - av_get_colorspace_name(enc->colorspace)); + unknown_if_null(av_color_space_name(enc->colorspace)), + unknown_if_null(av_color_primaries_name(enc->color_primaries)), + unknown_if_null(av_color_transfer_name(enc->color_trc))); + } else if (str = av_get_colorspace_name(enc->colorspace)) + av_strlcatf(detail, sizeof(detail), "%s, ", str); } if (enc->field_order != AV_FIELD_UNKNOWN) { @@ -727,9 +732,9 @@ void avcodec_string(char *buf, int buf_size, AVCodecContext *enc, int encode) } if (av_log_get_level() >= AV_LOG_VERBOSE && - enc->chroma_sample_location != AVCHROMA_LOC_UNSPECIFIED) - av_strlcatf(detail, sizeof(detail), "%s, ", - av_chroma_location_name(enc->chroma_sample_location)); + enc->chroma_sample_location != AVCHROMA_LOC_UNSPECIFIED && + (str = av_chroma_location_name(enc->chroma_sample_location))) + av_strlcatf(detail, sizeof(detail), "%s, ", str); if (strlen(detail) > 1) { detail[strlen(detail) - 2] = 0; @@ -787,9 +792,10 @@ void avcodec_string(char *buf, int buf_size, AVCodecContext *enc, int encode) "%d Hz, ", enc->sample_rate); } av_get_channel_layout_string(buf + strlen(buf), buf_size - strlen(buf), enc->channels, enc->channel_layout); - if (enc->sample_fmt != AV_SAMPLE_FMT_NONE) { + if (enc->sample_fmt != AV_SAMPLE_FMT_NONE && + (str = av_get_sample_fmt_name(enc->sample_fmt))) { snprintf(buf + strlen(buf), buf_size - strlen(buf), - ", %s", av_get_sample_fmt_name(enc->sample_fmt)); + ", %s", str); } if ( enc->bits_per_raw_sample > 0 && enc->bits_per_raw_sample != av_get_bytes_per_sample(enc->sample_fmt) * 8) |