Fix #126160: Incorrect string returned from usd::make_safe_name

When a multi-byte character needed to be turned into a single "_", the
final returned string would be too large in size.

Resize it to the exact number of bytes required and add test coverage.

Pull Request: https://projects.blender.org/blender/blender/pulls/126164
This commit is contained in:
Jesse Yurkovich
2024-08-10 05:59:05 +02:00
committed by Jesse Yurkovich
parent 7e80bfaa62
commit ef2e1f8423
2 changed files with 30 additions and 0 deletions

View File

@@ -43,6 +43,8 @@ std::string make_safe_name(const std::string &name, [[maybe_unused]] bool allow_
first = false;
}
/* Ensure the returned string is sized exactly to the number of required bytes. */
buf.resize(offset);
return buf;
#else
return pxr::TfMakeValidIdentifier(name);

View File

@@ -37,6 +37,7 @@
#include "WM_api.hh"
#include "usd.hh"
#include "usd_utils.hh"
#include "usd_writer_material.hh"
namespace blender::io::usd {
@@ -311,4 +312,31 @@ TEST_F(UsdExportTest, usd_export_material)
compare_blender_image_to_usd_image_shader(image_node, image_prim);
}
TEST(utilities, make_safe_name)
{
ASSERT_EQ(make_safe_name("", false), std::string("_"));
ASSERT_EQ(make_safe_name("1", false), std::string("_"));
ASSERT_EQ(make_safe_name("1Test", false), std::string("_Test"));
ASSERT_EQ(make_safe_name("Test", false), std::string("Test"));
ASSERT_EQ(make_safe_name("Test|$bézier @ world", false), std::string("Test__b__zier___world"));
ASSERT_EQ(make_safe_name("Test|ハローワールド", false),
std::string("Test______________________"));
ASSERT_EQ(make_safe_name("Test|Γεια σου κόσμε", false),
std::string("Test___________________________"));
ASSERT_EQ(make_safe_name("Test|∧hello ○ wórld", false), std::string("Test____hello_____w__rld"));
#if PXR_VERSION >= 2403
ASSERT_EQ(make_safe_name("", true), std::string("_"));
ASSERT_EQ(make_safe_name("1", true), std::string("_"));
ASSERT_EQ(make_safe_name("1Test", true), std::string("_Test"));
ASSERT_EQ(make_safe_name("Test", true), std::string("Test"));
ASSERT_EQ(make_safe_name("Test|$bézier @ world", true), std::string("Test__bézier___world"));
ASSERT_EQ(make_safe_name("Test|ハローワールド", true), std::string("Test_ハローワールド"));
ASSERT_EQ(make_safe_name("Test|Γεια σου κόσμε", true), std::string("Test_Γεια_σου_κόσμε"));
ASSERT_EQ(make_safe_name("Test|∧hello ○ wórld", true), std::string("Test__hello___wórld"));
#endif
}
} // namespace blender::io::usd