Merge pull request #63 from dimhotepus/pvs_studio_fixes_tinyusdz

Run PVS-Studio over tinyusdz library and fix found issues
This commit is contained in:
Syoyo Fujita
2023-01-20 20:17:28 +09:00
committed by GitHub
12 changed files with 23 additions and 34 deletions

View File

@@ -87,7 +87,7 @@ class CrateReader::Impl
//
// --
//
CrateReader::CrateReader(StreamReader *sr, const CrateReaderConfig &config) : _sr(sr) {
CrateReader::CrateReader(StreamReader *sr, const CrateReaderConfig &config) : _sr(sr), _impl(nullptr) {
_config = config;
if (_config.numThreads == -1) {
#if defined(__wasi__)
@@ -267,7 +267,7 @@ bool CrateReader::ReadString(std::string *s) {
nonstd::optional<std::string> CrateReader::GetSpecString(
crate::Index index) const {
if (index.value <= _specs.size()) {
if (index.value < _specs.size()) {
// ok
} else {
return nonstd::nullopt;

View File

@@ -126,7 +126,7 @@ bool DecodeImageSTB(const uint8_t *bytes, const size_t size,
image->channels = req_comp;
image->bpp = bits;
image->format = Image::PixelFormat::UInt;
image->data.resize(static_cast<size_t>(w * h * req_comp) * size_t(bits / 8));
image->data.resize(size_t(w) * size_t(h) * size_t(req_comp) * size_t(bits / 8));
std::copy(data, data + w * h * req_comp * (bits / 8), image->data.begin());
stbi_image_free(data);
@@ -187,7 +187,7 @@ bool DecodeImageEXR(const uint8_t *bytes, const size_t size,
image->channels = 4; // RGBA
image->bpp = 32; // fp32
image->format = Image::PixelFormat::Float;
image->data.resize(static_cast<size_t>(width * height * 4) * sizeof(float));
image->data.resize(size_t(width) * size_t(height) * 4 * sizeof(float));
memcpy(image->data.data(), rgba, sizeof(float) * size_t(width) * size_t(height) * 4);
free(rgba);

View File

@@ -434,7 +434,7 @@ bool IsAbsPath(const std::string &filename) {
// UNC path?
if (filename.size() > 2) {
if ((filename[1] == '\\') && (filename[1] == '\\')) {
if ((filename[0] == '\\') && (filename[1] == '\\')) {
return true;
}
}

View File

@@ -1700,8 +1700,6 @@ std::string to_string(tinyusdz::ListEditQual v) {
return "append";
} else if (v == tinyusdz::ListEditQual::Add) {
return "add";
} else if (v == tinyusdz::ListEditQual::Append) {
return "append";
} else if (v == tinyusdz::ListEditQual::Delete) {
return "delete";
} else if (v == tinyusdz::ListEditQual::Prepend) {

View File

@@ -227,11 +227,7 @@ static ParseResult ParseTypedAttribute(std::set<std::string> &table, /* inout */
ret.code = ParseResult::ResultCode::InternalError;
ret.err = "Internal error. Invalid Property with Attribute connection.";
}
if (auto pv = prop.get_relationTarget()) {
return ret;
} else {
return ret;
}
return ret;
}

View File

@@ -436,10 +436,8 @@ class Path {
}
bool is_absolute_path() const {
if (_prim_part.size()) {
if ((_prim_part.size() > 0) && (_prim_part[0] == '/')) {
return true;
}
if (_prim_part.size() && _prim_part[0] == '/') {
return true;
}
return false;

View File

@@ -261,10 +261,10 @@ std::string escapeBackslash(const std::string &str, const bool triple_quoted_str
for (size_t i = 0; i < str.size(); i++) {
if (str[i] == '\\') {
if (i + 3 < str.size()) {
if ((str[i+1] == '\'') && (str[i+1] == '\'') && (str[i+2] == '\'')) {
if ((str[i+1] == '\'') && (str[i+2] == '\'') && (str[i+3] == '\'')) {
s += "\\'''";
i += 3;
} else if ((str[i+1] == '"') && (str[i+1] == '"') && (str[i+2] == '"')) {
} else if ((str[i+1] == '"') && (str[i+2] == '"') && (str[i+3] == '"')) {
s += "\\\"\"\"";
i += 3;
} else {

View File

@@ -163,7 +163,7 @@ inline std::vector<std::string> split(
while ((s = str.find_first_not_of(sep, e)) != std::string::npos) {
e = str.find(sep, s);
result.push_back(str.substr(s, e - s));
if (count > kMaxItems) {
if (++count > kMaxItems) {
break;
}
}

View File

@@ -194,7 +194,7 @@ bool LoadUSDCFromFile(const std::string &_filename, Stage *stage,
std::string filepath = io::ExpandFilePath(_filename, /* userdata */ nullptr);
std::vector<uint8_t> data;
size_t max_bytes = size_t(1024 * 1024 * options.max_memory_limit_in_mb);
size_t max_bytes = 1024 * 1024 * size_t(options.max_memory_limit_in_mb);
if (!io::ReadWholeFile(&data, err, filepath, max_bytes,
/* userdata */ nullptr)) {
if (err) {
@@ -591,7 +591,7 @@ bool LoadUSDZFromFile(const std::string &_filename, Stage *stage,
std::string filepath = io::ExpandFilePath(_filename, /* userdata */ nullptr);
std::vector<uint8_t> data;
size_t max_bytes = size_t(1024 * 1024 * options.max_memory_limit_in_mb);
size_t max_bytes = 1024 * 1024 * size_t(options.max_memory_limit_in_mb);
if (!io::ReadWholeFile(&data, err, filepath, max_bytes,
/* userdata */ nullptr)) {
return false;
@@ -682,7 +682,7 @@ bool LoadUSDAFromFile(const std::string &_filename, Stage *stage,
std::string base_dir = io::GetBaseDir(_filename);
std::vector<uint8_t> data;
size_t max_bytes = size_t(1024 * 1024 * options.max_memory_limit_in_mb);
size_t max_bytes = 1024 * 1024 * size_t(options.max_memory_limit_in_mb);
if (!io::ReadWholeFile(&data, err, filepath, max_bytes,
/* userdata */ nullptr)) {
if (err) {
@@ -703,7 +703,7 @@ bool LoadUSDFromFile(const std::string &_filename, Stage *stage,
std::string base_dir = io::GetBaseDir(_filename);
std::vector<uint8_t> data;
size_t max_bytes = size_t(1024 * 1024 * options.max_memory_limit_in_mb);
size_t max_bytes = 1024 * 1024 * size_t(options.max_memory_limit_in_mb);
if (!io::ReadWholeFile(&data, err, filepath, max_bytes,
/* userdata */ nullptr)) {
return false;

View File

@@ -344,7 +344,7 @@ class USDCReader::Impl {
size_t memory_used{0}; // in bytes.
nonstd::optional<Path> GetPath(crate::Index index) const {
if (index.value <= _paths.size()) {
if (index.value < _paths.size()) {
return _paths[index.value];
}
@@ -352,7 +352,7 @@ class USDCReader::Impl {
}
nonstd::optional<Path> GetElemPath(crate::Index index) const {
if (index.value <= _elemPaths.size()) {
if (index.value < _elemPaths.size()) {
return _elemPaths[index.value];
}

View File

@@ -289,7 +289,7 @@ class Writer {
warn_ += s;
}
bool WriteHeader() {
bool WriteHeader(uint64_t toc_offset) {
char magic[8];
magic[0] = 'P';
magic[1] = 'X';
@@ -305,10 +305,6 @@ class Writer {
version[1] = 8;
version[2] = 0;
// TOC offset(8bytes)
// Must be 89 or greater.
uint64_t toc_offset;
std::array<uint8_t, 88> header;
memset(&header, 0, 88);
@@ -434,14 +430,15 @@ class Writer {
// return false;
//}
const uint64_t toc_offset = static_cast<uint64_t>(oss_.tellp());
if (!WriteTOC()) {
PUSH_ERROR("Failed to write TOC.");
return false;
}
// write heder
// write header
oss_.seekp(0, std::ios::beg);
if (!WriteHeader()) {
if (!WriteHeader(toc_offset)) {
PUSH_ERROR("Failed to write Header.");
return false;
}

View File

@@ -1947,7 +1947,7 @@ struct TimeSamples {
}
nonstd::optional<double> get_time(size_t idx) const {
if (idx > _samples.size()) {
if (idx >= _samples.size()) {
return nonstd::nullopt;
}
@@ -1959,7 +1959,7 @@ struct TimeSamples {
}
nonstd::optional<value::Value> get_value(size_t idx) const {
if (idx > _samples.size()) {
if (idx >= _samples.size()) {
return nonstd::nullopt;
}