- cast unsigned value to signed value and check its value it overrides sign-bit.
Signed-off-by: hyeonseok lee <hs89.lee@samsung.com>
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();
}
}
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);
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();
}
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();
}
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;
}
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) {