From 8a6a1fc2df5022607835ccdef32783f0d02562b0 Mon Sep 17 00:00:00 2001 From: RichardTea <31507749+RichardTea@users.noreply.github.com> Date: Tue, 1 Mar 2022 12:12:07 +0000 Subject: [PATCH 1/3] Update AI_TEXTURE_TYPE_MAX Must be equal to the largest aiTextureType_XXX enum --- include/assimp/material.h | 2 +- test/unit/utMaterialSystem.cpp | 60 ++++++++++++++++++++++++++++++++++ 2 files changed, 61 insertions(+), 1 deletion(-) diff --git a/include/assimp/material.h b/include/assimp/material.h index 416dbc4a8..2ccd83763 100644 --- a/include/assimp/material.h +++ b/include/assimp/material.h @@ -331,7 +331,7 @@ enum aiTextureType { #endif }; -#define AI_TEXTURE_TYPE_MAX aiTextureType_UNKNOWN +#define AI_TEXTURE_TYPE_MAX aiTextureType_TRANSMISSION // ------------------------------------------------------------------------------- // Get a string for a given aiTextureType diff --git a/test/unit/utMaterialSystem.cpp b/test/unit/utMaterialSystem.cpp index 7b4560443..3016f8881 100644 --- a/test/unit/utMaterialSystem.cpp +++ b/test/unit/utMaterialSystem.cpp @@ -135,3 +135,63 @@ TEST_F(MaterialSystemTest, testMaterialNameAccess) { delete mat; } + +// ------------------------------------------------------------------------------------------------ +#if defined(_MSC_VER) +// Refuse to compile on Windows if any enum values are not explicitly handled in the switch +// TODO: Move this into assimp/Compiler as a macro and add clang/gcc versions so other code can use it +__pragma(warning(push)); +__pragma(warning(error : 4061)); // enumerator 'identifier' in switch of enum 'enumeration' is not explicitly handled by a case label +__pragma(warning(error : 4062)); // enumerator 'identifier' in switch of enum 'enumeration' is not handled +#endif + +TEST_F(MaterialSystemTest, testMaterialTextureTypeEnum) { + // Verify that AI_TEXTURE_TYPE_MAX equals the largest 'real' value in the enum + + int32_t maxTextureType = 0; + static constexpr int32_t bigNumber = 255; + EXPECT_GT(bigNumber, AI_TEXTURE_TYPE_MAX) << "AI_TEXTURE_TYPE_MAX too large for valid enum test, increase bigNumber"; + + // Loop until a value larger than any enum + for (int32_t i = 0; i < bigNumber; ++i) { + aiTextureType texType = static_cast(i); + switch (texType) { + default: break; +#ifndef SWIG + case _aiTextureType_Force32Bit: break; +#endif + // All the real values + case aiTextureType_NONE: + case aiTextureType_DIFFUSE: + case aiTextureType_SPECULAR: + case aiTextureType_AMBIENT: + case aiTextureType_EMISSIVE: + case aiTextureType_HEIGHT: + case aiTextureType_NORMALS: + case aiTextureType_SHININESS: + case aiTextureType_OPACITY: + case aiTextureType_DISPLACEMENT: + case aiTextureType_LIGHTMAP: + case aiTextureType_REFLECTION: + case aiTextureType_BASE_COLOR: + case aiTextureType_NORMAL_CAMERA: + case aiTextureType_EMISSION_COLOR: + case aiTextureType_METALNESS: + case aiTextureType_DIFFUSE_ROUGHNESS: + case aiTextureType_AMBIENT_OCCLUSION: + case aiTextureType_SHEEN: + case aiTextureType_CLEARCOAT: + case aiTextureType_TRANSMISSION: + case aiTextureType_UNKNOWN: + if (i > maxTextureType) + maxTextureType = i; + break; + } + } + + EXPECT_EQ(maxTextureType, AI_TEXTURE_TYPE_MAX) << "AI_TEXTURE_TYPE_MAX macro must be equal to the largest valid aiTextureType_XXX"; +} + +#if defined(_MSC_VER) +__pragma (warning(pop)) +#endif From c14eccefaf0ba0286d2e9f3ae7b8dd1f3e055a27 Mon Sep 17 00:00:00 2001 From: Jonas Karlsson Date: Wed, 9 Mar 2022 22:19:10 +0100 Subject: [PATCH 2/3] Fix 'i >= 0' always true bug If 'disk_filename' does not contain a dot (.) then 'i' would overflow. Making 'i' an int makes sure the for loop works as intended. --- code/Common/ZipArchiveIOSystem.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/code/Common/ZipArchiveIOSystem.cpp b/code/Common/ZipArchiveIOSystem.cpp index c322b140f..ba90ae9b3 100644 --- a/code/Common/ZipArchiveIOSystem.cpp +++ b/code/Common/ZipArchiveIOSystem.cpp @@ -122,7 +122,7 @@ voidpf IOSystem2Unzip::open(voidpf opaque, const char *filename, int mode) { voidpf IOSystem2Unzip::opendisk(voidpf opaque, voidpf stream, uint32_t number_disk, int mode) { ZipFile *io_stream = (ZipFile *)stream; voidpf ret = NULL; - size_t i; + int i; char *disk_filename = (char*)malloc(io_stream->m_Filename.length() + 1); strncpy(disk_filename, io_stream->m_Filename.c_str(), io_stream->m_Filename.length() + 1); @@ -130,7 +130,7 @@ voidpf IOSystem2Unzip::opendisk(voidpf opaque, voidpf stream, uint32_t number_di { if (disk_filename[i] != '.') continue; - snprintf(&disk_filename[i], io_stream->m_Filename.length() - i, ".z%02u", number_disk + 1); + snprintf(&disk_filename[i], io_stream->m_Filename.length() - size_t(i), ".z%02u", number_disk + 1); break; } From 47f004517fca061e89896f2244be32fe6a506eb9 Mon Sep 17 00:00:00 2001 From: Kim Kulling Date: Thu, 10 Mar 2022 10:33:29 +0100 Subject: [PATCH 3/3] Add missing cast. --- code/Common/ZipArchiveIOSystem.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/code/Common/ZipArchiveIOSystem.cpp b/code/Common/ZipArchiveIOSystem.cpp index ba90ae9b3..e0c9883d2 100644 --- a/code/Common/ZipArchiveIOSystem.cpp +++ b/code/Common/ZipArchiveIOSystem.cpp @@ -126,7 +126,7 @@ voidpf IOSystem2Unzip::opendisk(voidpf opaque, voidpf stream, uint32_t number_di char *disk_filename = (char*)malloc(io_stream->m_Filename.length() + 1); strncpy(disk_filename, io_stream->m_Filename.c_str(), io_stream->m_Filename.length() + 1); - for (i = io_stream->m_Filename.length() - 1; i >= 0; i -= 1) + for (i = (int)io_stream->m_Filename.length() - 1; i >= 0; i -= 1) { if (disk_filename[i] != '.') continue;