Skip to content

Commit e43a20f

Browse files
ahmadsharif1facebook-github-bot
authored andcommitted
[torchcodec] Handle negative buffer sizes (#131)
Summary: Pull Request resolved: #131 Not sure if we will ever hit this case, but this may be a reason why TorchCodec is crashing on some videos. Maybe the user passes in a negative value for size and it trickles all the way to the read function. We should not be treating negative sizes as large sizes. Reviewed By: scotts Differential Revision: D60461269 fbshipit-source-id: 9172da109d14ddc505177988e85bc173b1fbf403
1 parent e78afdd commit e43a20f

File tree

2 files changed

+11
-0
lines changed

2 files changed

+11
-0
lines changed

src/torchcodec/decoders/_core/FFMPEGCommon.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66

77
#include "src/torchcodec/decoders/_core/FFMPEGCommon.h"
88

9+
#include <c10/util/Exception.h>
10+
911
namespace facebook::torchcodec {
1012

1113
std::string getFFMPEGErrorStringFromErrorCode(int errorCode) {
@@ -68,6 +70,14 @@ int AVIOBytesContext::read(void* opaque, uint8_t* buf, int buf_size) {
6870
struct AVIOBufferData* bufferData =
6971
static_cast<struct AVIOBufferData*>(opaque);
7072
buf_size = FFMIN(buf_size, bufferData->size - bufferData->current);
73+
TORCH_CHECK(
74+
buf_size >= 0,
75+
"Tried to read negative bytes: buf_size=",
76+
buf_size,
77+
", size=",
78+
bufferData->size,
79+
", current=",
80+
bufferData->current);
7181
if (!buf_size) {
7282
return AVERROR_EOF;
7383
}

src/torchcodec/decoders/_core/VideoDecoder.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -237,6 +237,7 @@ std::unique_ptr<VideoDecoder> VideoDecoder::createFromBuffer(
237237
const void* buffer,
238238
size_t length,
239239
const VideoDecoder::DecoderOptions& options) {
240+
TORCH_CHECK(buffer != nullptr, "Video buffer cannot be nullptr!");
240241
AVInput input = createAVFormatContextFromBuffer(buffer, length);
241242
std::unique_ptr<VideoDecoder> decoder(new VideoDecoder());
242243
decoder->formatContext_ = std::move(input.formatContext);

0 commit comments

Comments
 (0)