From d96d324975baf4b119232abeace6a3b60c9996bf Mon Sep 17 00:00:00 2001 From: Syoyo Fujita Date: Sun, 4 Jun 2023 05:45:24 +0900 Subject: [PATCH] Prevent duplicated key generation when serializing lights + RapidJSON backend. Fixes #420 --- README.md | 12 ++++++++++++ tiny_gltf.h | 11 ++++++++++- 2 files changed, 22 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 557734a..4245c86 100644 --- a/README.md +++ b/README.md @@ -197,6 +197,18 @@ if (!ret) { * `TINYGLTF_USE_RAPIDJSON` : Use RapidJSON as a JSON parser/serializer. RapidJSON files are not included in TinyGLTF repo. Please set an include path to RapidJSON if you enable this feature. * `TINYGLTF_USE_CPP14` : Use C++14 feature(requires C++14 compiler). This may give better performance than C++11. +### Wuffs image loader option(faster and secure JPEG/PNG deocoding) + +You can use `wuffs` image loader to load JPEG and PNG in fast and securely. +(`stb_image` has some security issues, whereas `wuffs` is well fuzz tested) + +Not that some uncommon JPEG format is unsupported in `wuffs` `std/jpeg` decoder. + +* `TINYGLTF_USE_WUFFS_IMAGE_LOADER` : Use `wuffs` to load images. `stb_image` related stuff will be disabled. + * `TINYGLTF_NO_STB_IMAGE` supercedes `wuffs` macros. i.e. when `TINYGLTF_NO_STB_IMAGE` is defined, both `stb_image` and `wuffs` are disabled. +* `TINYGLTF_NO_WUFFS_IMPLEMENTATION` : Do not define `WUFFS_IMPLEMENTATION` inside `tiny_gltf.h`. Define this macro if you use `wuffs` in another C/C++ file. + + ## CMake options You can add tinygltf using `add_subdirectory` feature. diff --git a/tiny_gltf.h b/tiny_gltf.h index 0f69ad2..67ebe97 100644 --- a/tiny_gltf.h +++ b/tiny_gltf.h @@ -6767,7 +6767,16 @@ void JsonAddMember(detail::json &o, const char *key, detail::json &&value) { if (!o.IsObject()) { o.SetObject(); } - o.AddMember(detail::json(key, detail::GetAllocator()), std::move(value), detail::GetAllocator()); + + // Issue 420. + // AddMember may create duplicated key, so use [] API when a key already exists. + // https://github.com/Tencent/rapidjson/issues/771#issuecomment-254386863 + detail::json_const_iterator it; + if (detail::FindMember(o, key, it)) { + o[key] = std::move(value); // replace + } else { + o.AddMember(detail::json(key, detail::GetAllocator()), std::move(value), detail::GetAllocator()); + } #else o[key] = std::move(value); #endif