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>
This commit is contained in:
Andreas Rheinhardt 2021-03-21 06:29:13 +01:00
parent 48aa531984
commit 88b7d9fd36

View File

@ -595,6 +595,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;
@ -604,6 +609,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;
@ -639,14 +645,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 ||
@ -655,12 +661,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) {
@ -678,9 +683,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;
@ -738,9 +743,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)