[ahub] fix ahub issue
authorhyeonseok lee <hs89.lee@samsung.com>
Thu, 16 Mar 2023 05:30:12 +0000 (14:30 +0900)
committerJijoong Moon <jijoong.moon@samsung.com>
Thu, 16 Mar 2023 08:43:48 +0000 (17:43 +0900)
 - cast unsigned value to signed value and check its value it overrides sign-bit.

Signed-off-by: hyeonseok lee <hs89.lee@samsung.com>
nntrainer/compiler/tflite_interpreter.cpp
nntrainer/models/neuralnet.cpp
nntrainer/tensor/tensor.cpp
nntrainer/utils/util_func.cpp

index 3140245..8e53e20 100644 (file)
@@ -57,7 +57,13 @@ void builder2file(const flatbuffers::FlatBufferBuilder &builder,
   NNTR_THROW_IF(!os.good(), std::invalid_argument)
     << FUNC_TAG
     << "failed to open, reason: " << strerror_r(errno, error_buf, error_buflen);
-  os.write((char *)builder.GetBufferPointer(), builder.GetSize());
+
+  std::streamsize sz = static_cast<std::streamsize>(builder.GetSize());
+  NNTR_THROW_IF(sz < 0, std::invalid_argument)
+    << FUNC_TAG << "builder size: " << builder.GetSize()
+    << " is too big. It cannot be represented by std::streamsize";
+
+  os.write((char *)builder.GetBufferPointer(), sz);
   os.close();
 }
 
index feb1284..a9148bf 100644 (file)
@@ -407,7 +407,7 @@ void NeuralNetwork::save(const std::string &file_path,
     }
     if (opt && istrequal(opt->getType(), "adam")) {
       std::string adam = "adam";
-      model_file.write(adam.c_str(), adam.size());
+      model_file.write(adam.c_str(), 4);
       for (auto iter = model_graph.cbegin(); iter != model_graph.cend();
            iter++) {
         (*iter)->save(model_file, true);
index 63f7e48..f76b13f 100644 (file)
@@ -1600,8 +1600,12 @@ void Tensor::save(std::ostream &file) {
   NNTR_THROW_IF(!contiguous, std::invalid_argument)
     << getName() << " is not contiguous, cannot save.";
 
-  checkedWrite(file, (char *)getData(), bytes(),
-               "[Tensor::save] operation failed");
+  std::streamsize sz = static_cast<std::streamsize>(bytes());
+  NNTR_THROW_IF(sz < 0, std::invalid_argument)
+    << "save size: " << bytes()
+    << " is too big. It cannot be represented by std::streamsize";
+
+  checkedWrite(file, (char *)getData(), sz, "[Tensor::save] operation failed");
   putData();
 }
 
@@ -1609,8 +1613,13 @@ void Tensor::read(std::ifstream &file) {
   NNTR_THROW_IF(!contiguous, std::invalid_argument)
     << getName() << " is not contiguous, cannot read.";
 
-  checkedRead(file, (char *)getData(), bytes(),
-              "[Tensor::read] operation failed");
+  std::streamsize sz = static_cast<std::streamsize>(bytes());
+
+  NNTR_THROW_IF(sz < 0, std::invalid_argument)
+    << "read size: " << bytes()
+    << " is too big. It cannot be represented by std::streamsize";
+
+  checkedRead(file, (char *)getData(), sz, "[Tensor::read] operation failed");
   putData();
 }
 
index 2deaadf..0c5b864 100644 (file)
@@ -98,8 +98,14 @@ std::string readString(std::ifstream &file, const char *error_msg) {
   size_t size;
 
   checkedRead(file, (char *)&size, sizeof(size), error_msg);
+
+  std::streamsize sz = static_cast<std::streamsize>(size);
+  NNTR_THROW_IF(sz < 0, std::invalid_argument)
+    << "read string size: " << sz
+    << " is too big. It cannot be represented by std::streamsize";
+
   str.resize(size);
-  checkedRead(file, (char *)&str[0], size, error_msg);
+  checkedRead(file, (char *)&str[0], sz, error_msg);
 
   return str;
 }
@@ -109,7 +115,13 @@ void writeString(std::ofstream &file, const std::string &str,
   size_t size = str.size();
 
   checkedWrite(file, (char *)&size, sizeof(size), error_msg);
-  checkedWrite(file, (char *)&str[0], size, error_msg);
+
+  std::streamsize sz = static_cast<std::streamsize>(size);
+  NNTR_THROW_IF(sz < 0, std::invalid_argument)
+    << "write string size: " << size
+    << " is too big. It cannot be represented by std::streamsize";
+
+  checkedWrite(file, (char *)&str[0], sz, error_msg);
 }
 
 bool endswith(const std::string &target, const std::string &suffix) {