Skip to content

Commit

Permalink
Add size check before calling stack_.at(dict_pos)
Browse files Browse the repository at this point in the history
  • Loading branch information
m4drat authored and pytorchmergebot committed May 2, 2023
1 parent 8556cf2 commit cbfc10f
Showing 1 changed file with 10 additions and 0 deletions.
10 changes: 10 additions & 0 deletions torch/csrc/jit/serialization/unpickler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -597,10 +597,20 @@ PickleOpCode Unpickler::readInstruction() {
// | Dict | -> (stack_size - 3)
// | Key | -> (stack_size - 2)
// | Value | -> (stack_size - 1)
TORCH_CHECK(
stack_.size() >= 3,
"Parsing error: stack doesn't have enough elements");

auto stack_size = stack_.size();
auto dict_pos = stack_size - 3;
auto key_pos = stack_size - 2;
auto val_pos = stack_size - 1;

TORCH_CHECK(
(dict_pos < stack_size) && (key_pos < stack_size) &&
(val_pos < stack_size),
"Parsing error: attempted out-of-bounds access while processing SETITEM opcode");

auto dict = stack_.at(dict_pos).toGenericDict();
dict.insert_or_assign(stack_.at(key_pos), stack_.at(val_pos));
stack_.erase(stack_.begin() + (key_pos), stack_.end());
Expand Down

0 comments on commit cbfc10f

Please sign in to comment.