From 61b6da718b6ff8d37700a2fe89f186b133c444e5 Mon Sep 17 00:00:00 2001 From: semenov_gv Date: Fri, 22 Oct 2021 18:16:44 +0300 Subject: [PATCH 1/2] Code refactor --- loader_example.cc | 4 ++-- tiny_gltf.h | 40 +++++++++++++++++++--------------------- 2 files changed, 21 insertions(+), 23 deletions(-) diff --git a/loader_example.cc b/loader_example.cc index dbe10add..4191a7fa 100644 --- a/loader_example.cc +++ b/loader_example.cc @@ -11,8 +11,8 @@ #include static std::string GetFilePathExtension(const std::string &FileName) { - if (FileName.find_last_of(".") != std::string::npos) - return FileName.substr(FileName.find_last_of(".") + 1); + if (FileName.find_last_of('.') != std::string::npos) + return FileName.substr(FileName.find_last_of('.') + 1); return ""; } diff --git a/tiny_gltf.h b/tiny_gltf.h index 82a2e6ae..a22cccf4 100644 --- a/tiny_gltf.h +++ b/tiny_gltf.h @@ -275,9 +275,8 @@ class Value { real_value_ = i; } explicit Value(double n) : type_(REAL_TYPE) { real_value_ = n; } - explicit Value(const std::string &s) : type_(STRING_TYPE) { - string_value_ = s; - } + explicit Value(const std::string &s) + : type_(STRING_TYPE), string_value_(s) {} explicit Value(std::string &&s) : type_(STRING_TYPE), string_value_(std::move(s)) {} explicit Value(const unsigned char *p, size_t n) : type_(BINARY_TYPE) { @@ -287,13 +286,17 @@ class Value { explicit Value(std::vector &&v) noexcept : type_(BINARY_TYPE), binary_value_(std::move(v)) {} - explicit Value(const Array &a) : type_(ARRAY_TYPE) { array_value_ = a; } - explicit Value(Array &&a) noexcept : type_(ARRAY_TYPE), - array_value_(std::move(a)) {} - - explicit Value(const Object &o) : type_(OBJECT_TYPE) { object_value_ = o; } - explicit Value(Object &&o) noexcept : type_(OBJECT_TYPE), - object_value_(std::move(o)) {} + explicit Value(const Array &a) + : type_(ARRAY_TYPE), array_value_(a) {} + explicit Value(Array &&a) noexcept + : type_(ARRAY_TYPE), + array_value_(std::move(a)) {} + + explicit Value(const Object &o) + : type_(OBJECT_TYPE), object_value_(o) {} + explicit Value(Object &&o) noexcept + : type_(OBJECT_TYPE), + object_value_(std::move(o)) {} DEFAULT_METHODS(Value) @@ -966,7 +969,7 @@ struct Camera { PerspectiveCamera perspective; OrthographicCamera orthographic; - Camera() {} + Camera() = default; DEFAULT_METHODS(Camera) bool operator==(const Camera &) const; @@ -1288,7 +1291,7 @@ class TinyGLTF { #pragma clang diagnostic pop #endif - ~TinyGLTF() {} + ~TinyGLTF() = default; /// /// Loads glTF ASCII asset from a file. @@ -2028,8 +2031,8 @@ static std::string FindFile(const std::vector &paths, } static std::string GetFilePathExtension(const std::string &FileName) { - if (FileName.find_last_of(".") != std::string::npos) - return FileName.substr(FileName.find_last_of(".") + 1); + if (FileName.find_last_of('.') != std::string::npos) + return FileName.substr(FileName.find_last_of('.') + 1); return ""; } @@ -2811,7 +2814,6 @@ static void UpdateImageObject(Image &image, std::string &baseDir, int index, // If callback is set, modify image data object if (*WriteImageData != nullptr && !filename.empty()) { - std::string uri; (*WriteImageData)(&baseDir, &filename, &image, embedImages, user_data); } } @@ -3795,7 +3797,7 @@ static bool ParseImage(Image *image, const int image_idx, std::string *err, // Just only save some information here. Loading actual image data from // bufferView is done after this `ParseImage` function. image->bufferView = bufferView; - image->mimeType = mime_type; + image->mimeType = std::move(mime_type); image->width = width; image->height = height; @@ -4744,7 +4746,7 @@ static bool ParsePbrMetallicRoughness( } return false; } - pbr->baseColorFactor = baseColorFactor; + pbr->baseColorFactor = std::move(baseColorFactor); } { @@ -6233,10 +6235,6 @@ bool TinyGLTF::LoadBinaryFromMemory(Model *model, std::string *err, return false; } - // Extract JSON string. - std::string jsonString(reinterpret_cast(&bytes[20]), - model_length); - is_binary_ = true; bin_data_ = bytes + 20 + model_length + 8; // 4 bytes (buffer_length) + 4 bytes(buffer_format) From cc689a7ca33eb60847f37e592beae481cc0e20cf Mon Sep 17 00:00:00 2001 From: semenov_gv Date: Wed, 27 Oct 2021 15:48:29 +0300 Subject: [PATCH 2/2] Fixed possible overflow CWE-190 --- tiny_gltf.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tiny_gltf.h b/tiny_gltf.h index a22cccf4..7f91318b 100644 --- a/tiny_gltf.h +++ b/tiny_gltf.h @@ -2445,7 +2445,7 @@ bool LoadImageData(Image *image, const int image_idx, std::string *err, image->component = comp; image->bits = bits; image->pixel_type = pixel_type; - image->image.resize(static_cast(w * h * comp) * size_t(bits / 8)); + image->image.resize((static_cast(w) * h * comp) * static_cast(bits / 8)); std::copy(data, data + w * h * comp * (bits / 8), image->image.begin()); stbi_image_free(data);