Skip to content

Commit 9384239

Browse files
scottsfacebook-github-bot
authored andcommitted
Account for current pointer being outside of the buffer (#316)
Summary: D60461269 added `TORCH_CHECK` to catch negative values. This would catch the input argument `buf_size` being negative. However, it never caught the case of `bufferData->current` pointer being past `bufferData->size`. This is due to intricacies of C++ type rules. Both `current` and `size` are `size_t` (unsigned integers), which causes the subtraction to always be non-negative. [This example](https://www.programiz.com/online-compiler/3zokStBbD2TeP) demonstrates above well. If we only had `read` method the `current` would never be able to go past `size`, but we have `seek` method that can set `current` to arbitrary values. The diff here makes sure that we actually catch these negative values and fail with reasonable error instead of segfaulting trying to read memory outside of the buffer. Reviewed By: ahmadsharif1 Differential Revision: D65118896
1 parent 373d1c5 commit 9384239

File tree

1 file changed

+6
-0
lines changed

1 file changed

+6
-0
lines changed

src/torchcodec/decoders/_core/FFMPEGCommon.cpp

+6
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,12 @@ AVIOContext* AVIOBytesContext::getAVIO() {
6969
int AVIOBytesContext::read(void* opaque, uint8_t* buf, int buf_size) {
7070
struct AVIOBufferData* bufferData =
7171
static_cast<struct AVIOBufferData*>(opaque);
72+
TORCH_CHECK(
73+
bufferData->current <= bufferData->size,
74+
"Tried to read outside of the buffer: current=",
75+
bufferData->current,
76+
", size=",
77+
bufferData->size);
7278
buf_size = FFMIN(buf_size, bufferData->size - bufferData->current);
7379
TORCH_CHECK(
7480
buf_size >= 0,

0 commit comments

Comments
 (0)