From 105676bb934a49d10f2a0dfcbb323ef32e9be6f7 Mon Sep 17 00:00:00 2001 From: Scott Schneider Date: Mon, 24 Mar 2025 17:44:29 -0700 Subject: [PATCH] Do nullptr check on result from av_get_sample_fmt_name() --- src/torchcodec/decoders/_core/VideoDecoder.cpp | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/src/torchcodec/decoders/_core/VideoDecoder.cpp b/src/torchcodec/decoders/_core/VideoDecoder.cpp index 80b6fa7c5..3d51b8a35 100644 --- a/src/torchcodec/decoders/_core/VideoDecoder.cpp +++ b/src/torchcodec/decoders/_core/VideoDecoder.cpp @@ -161,7 +161,15 @@ void VideoDecoder::initializeDecoder() { } else if (avStream->codecpar->codec_type == AVMEDIA_TYPE_AUDIO) { AVSampleFormat format = static_cast(avStream->codecpar->format); - streamMetadata.sampleFormat = av_get_sample_fmt_name(format); + + // If the AVSampleFormat is not recognized, we get back nullptr. We have + // to make sure we don't initialize a std::string with nullptr. There's + // nothing to do on the else branch because we're already using an + // optional; it'll just remain empty. + const char* rawSampleFormat = av_get_sample_fmt_name(format); + if (rawSampleFormat != nullptr) { + streamMetadata.sampleFormat = std::string(rawSampleFormat); + } containerMetadata_.numAudioStreams++; }