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 88b7d9fd36)
This commit is contained in:
Andreas Rheinhardt
2021-03-21 06:29:13 +01:00
committed by Andreas Rheinhardt
parent a57ba45eb4
commit 0bbf1f4785

View File

@ -644,6 +644,11 @@ FF_ENABLE_DEPRECATION_WARNINGS
return 0; 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) void avcodec_string(char *buf, int buf_size, AVCodecContext *enc, int encode)
{ {
const char *codec_type; const char *codec_type;
@ -653,6 +658,7 @@ void avcodec_string(char *buf, int buf_size, AVCodecContext *enc, int encode)
int new_line = 0; int new_line = 0;
AVRational display_aspect_ratio; AVRational display_aspect_ratio;
const char *separator = enc->dump_separator ? (const char *)enc->dump_separator : ", "; const char *separator = enc->dump_separator ? (const char *)enc->dump_separator : ", ";
const char *str;
if (!buf || buf_size <= 0) if (!buf || buf_size <= 0)
return; return;
@ -688,14 +694,14 @@ void avcodec_string(char *buf, int buf_size, AVCodecContext *enc, int encode)
av_strlcat(buf, separator, buf_size); av_strlcat(buf, separator, buf_size);
snprintf(buf + strlen(buf), buf_size - strlen(buf), snprintf(buf + strlen(buf), buf_size - strlen(buf),
"%s", enc->pix_fmt == AV_PIX_FMT_NONE ? "none" : "%s", enc->pix_fmt == AV_PIX_FMT_NONE ? "none" :
av_get_pix_fmt_name(enc->pix_fmt)); unknown_if_null(av_get_pix_fmt_name(enc->pix_fmt)));
if (enc->bits_per_raw_sample && enc->pix_fmt != AV_PIX_FMT_NONE && 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) 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); av_strlcatf(detail, sizeof(detail), "%d bpc, ", enc->bits_per_raw_sample);
if (enc->color_range != AVCOL_RANGE_UNSPECIFIED) if (enc->color_range != AVCOL_RANGE_UNSPECIFIED &&
av_strlcatf(detail, sizeof(detail), "%s, ", (str = av_color_range_name(enc->color_range)))
av_color_range_name(enc->color_range)); av_strlcatf(detail, sizeof(detail), "%s, ", str);
if (enc->colorspace != AVCOL_SPC_UNSPECIFIED || if (enc->colorspace != AVCOL_SPC_UNSPECIFIED ||
enc->color_primaries != AVCOL_PRI_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) { enc->colorspace != (int)enc->color_trc) {
new_line = 1; new_line = 1;
av_strlcatf(detail, sizeof(detail), "%s/%s/%s, ", av_strlcatf(detail, sizeof(detail), "%s/%s/%s, ",
av_color_space_name(enc->colorspace), unknown_if_null(av_color_space_name(enc->colorspace)),
av_color_primaries_name(enc->color_primaries), unknown_if_null(av_color_primaries_name(enc->color_primaries)),
av_color_transfer_name(enc->color_trc)); unknown_if_null(av_color_transfer_name(enc->color_trc)));
} else } else if (str = av_get_colorspace_name(enc->colorspace))
av_strlcatf(detail, sizeof(detail), "%s, ", av_strlcatf(detail, sizeof(detail), "%s, ", str);
av_get_colorspace_name(enc->colorspace));
} }
if (enc->field_order != AV_FIELD_UNKNOWN) { 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 && if (av_log_get_level() >= AV_LOG_VERBOSE &&
enc->chroma_sample_location != AVCHROMA_LOC_UNSPECIFIED) enc->chroma_sample_location != AVCHROMA_LOC_UNSPECIFIED &&
av_strlcatf(detail, sizeof(detail), "%s, ", (str = av_chroma_location_name(enc->chroma_sample_location)))
av_chroma_location_name(enc->chroma_sample_location)); av_strlcatf(detail, sizeof(detail), "%s, ", str);
if (strlen(detail) > 1) { if (strlen(detail) > 1) {
detail[strlen(detail) - 2] = 0; 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); "%d Hz, ", enc->sample_rate);
} }
av_get_channel_layout_string(buf + strlen(buf), buf_size - strlen(buf), enc->channels, enc->channel_layout); 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), 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 if ( enc->bits_per_raw_sample > 0
&& enc->bits_per_raw_sample != av_get_bytes_per_sample(enc->sample_fmt) * 8) && enc->bits_per_raw_sample != av_get_bytes_per_sample(enc->sample_fmt) * 8)