Fix: crash printing integers in IDP_reprN

Regression in [0] caused printing integers to crash.
While this function isn't widely used it could crash logging
key-map items.

Also add tests for IDP_reprN.

Ref !148109

[0]: 92cf9dd2f2
This commit is contained in:
Campbell Barton
2025-10-15 07:46:58 +00:00
parent 68c175b5c1
commit be43077b1b
2 changed files with 44 additions and 3 deletions

View File

@@ -93,4 +93,42 @@ TEST(idproperties, SyncGroupValues)
IDP_FreeProperty(group2); IDP_FreeProperty(group2);
} }
TEST(idproperties, ReprGroup)
{
auto repr_fn = [](IDProperty *prop) -> std::string {
uint result_len;
char *c_str = IDP_reprN(prop, &result_len);
std::string result = std::string(c_str, result_len);
MEM_freeN(c_str);
return result;
};
IDProperty *group = idprop::create_group("test").release();
EXPECT_EQ(repr_fn(group), "{}");
IDP_AddToGroup(group, idprop::create("a", 1).release());
IDP_AddToGroup(group, idprop::create("b", 0.5f).release());
IDP_AddToGroup(group, idprop::create_bool("c", true).release());
IDP_AddToGroup(group, idprop::create_bool("d", false).release());
IDP_AddToGroup(group, idprop::create("e", "ABC (escape \" \\)").release());
IDP_AddToGroup(group, idprop::create("f", Span<int32_t>({-1, 0, 1})).release());
IDP_AddToGroup(group, idprop::create("g", Span<float>({-0.5f, 0.0f, 0.5f})).release());
IDP_AddToGroup(group, idprop::create_group("h").release());
EXPECT_EQ(repr_fn(group),
"{"
"\"a\": 1, "
"\"b\": 0.5, "
"\"c\": True, "
"\"d\": False, "
"\"e\": \"ABC (escape \\\" \\\\)\", "
"\"f\": [-1, 0, 1], "
"\"g\": [-0.5, 0, 0.5], "
"\"h\": {}"
"}");
IDP_FreeProperty(group);
}
} // namespace blender::bke::tests } // namespace blender::bke::tests

View File

@@ -100,10 +100,13 @@ static void idp_repr_fn_recursive(ReprState *state, const IDProperty *prop)
break; break;
} }
case IDP_INT: { case IDP_INT: {
if (const IDPropertyUIDataEnumItem *item = IDP_EnumItemFind(prop)) { if (const IDPropertyUIDataEnumItem *item = prop->ui_data ? IDP_EnumItemFind(prop) : nullptr)
STR_APPEND_FMT("%s", item->name); {
STR_APPEND_STR_QUOTE(item->name);
}
else {
STR_APPEND_FMT("%d", IDP_int_get(prop));
} }
STR_APPEND_FMT("%d", IDP_int_get(prop));
break; break;
} }
case IDP_FLOAT: { case IDP_FLOAT: {