Fix uninitialized value in pickler (#18678)
authorDavid Riazati <davidriazati@fb.com>
Tue, 2 Apr 2019 00:31:53 +0000 (17:31 -0700)
committerFacebook Github Bot <facebook-github-bot@users.noreply.github.com>
Tue, 2 Apr 2019 00:34:36 +0000 (17:34 -0700)
Summary:
Fixes #18671
Pull Request resolved: https://github.com/pytorch/pytorch/pull/18678

Differential Revision: D14708969

Pulled By: driazati

fbshipit-source-id: d372c6e3a2a3d3fc48d8afc1fa6807f2ce0e5c6e

torch/csrc/jit/pickler.cpp
torch/csrc/jit/pickler.h

index df95bc4..3da6b7e 100644 (file)
@@ -338,8 +338,15 @@ OpCode Unpickler::readInstruction() {
       // Look back to see if the last opcode was an IntList class
       if (last_opcode_ == OpCode::NEWOBJ) {
         // It's a list specialization, the enum ID of which is on the stack
-        PicklerClass cls =
-            static_cast<PicklerClass>(uint8_t(stack_.back().toInt()));
+        AT_CHECK(
+            stack_.size() > 0,
+            "Unpickler found an empty stack when it expected a value");
+        auto value = stack_.back().toInt();
+        AT_CHECK(
+            value >= 0 && value <= std::numeric_limits<uint8_t>::max(),
+            "Unpickler could not decode PicklerClass for ",
+            value);
+        PicklerClass cls = static_cast<PicklerClass>(uint8_t(value));
         if (cls == PicklerClass::INTLIST) {
           stack_.emplace_back(std::vector<int64_t>());
         }
index ab35a70..22061c9 100644 (file)
@@ -150,7 +150,8 @@ class Unpickler {
       const std::vector<at::Tensor>* tensor_table)
       : bytes_(static_cast<const uint8_t*>(data)),
         end_ptr_(bytes_ + size),
-        tensor_table_(tensor_table) {}
+        tensor_table_(tensor_table),
+        last_opcode_(OpCode::STOP) {}
 
   std::vector<IValue> parse_ivalue_list();