Fix T86983: `bl_description = None` crash on RNA class registration

This commit is contained in:
Campbell Barton 2021-03-29 21:47:16 +11:00
parent 1ea5157051
commit e0ce76f1c5
Notes: blender-bot 2023-02-14 06:46:23 +01:00
Referenced by issue #86983, `bl_description = None` triggers segfault on register.
1 changed files with 57 additions and 46 deletions

View File

@ -1781,41 +1781,40 @@ static void rna_Operator_bl_label_set(PointerRNA *ptr, const char *value)
}
}
static void rna_Operator_bl_translation_context_set(PointerRNA *ptr, const char *value)
{
wmOperator *data = (wmOperator *)(ptr->data);
char *str = (char *)data->type->translation_context;
if (!str[0]) {
BLI_strncpy(str, value, RNA_DYN_DESCR_MAX); /* utf8 already ensured */
}
else {
BLI_assert(!"setting the bl_translation_context on a non-builtin operator");
}
}
/**
* Use callbacks that check for NULL instead of clearing #PROP_NEVER_NULL on the string property,
* so the internal value may be NULL, without allowing Python to assign `None` which doesn't
* make any sense in this case.
*/
# define OPERATOR_STR_MAYBE_NULL_GETSET(attr, len) \
static void rna_Operator_bl_##attr##_set(PointerRNA *ptr, const char *value) \
{ \
wmOperator *data = (wmOperator *)(ptr->data); \
char *str = (char *)data->type->attr; \
if (str && !str[0]) { \
BLI_strncpy(str, value, len); /* utf8 already ensured */ \
} \
else { \
BLI_assert( \
!"setting the bl_" STRINGIFY(translation_context) " on a non-builtin operator"); \
} \
} \
static void rna_Operator_bl_##attr##_get(PointerRNA *ptr, char *value) \
{ \
const wmOperator *data = (wmOperator *)(ptr->data); \
const char *str = data->type->attr; \
BLI_strncpy(value, str ? str : "", len); \
} \
static int rna_Operator_bl_##attr##_length(PointerRNA *ptr) \
{ \
const wmOperator *data = (wmOperator *)(ptr->data); \
const char *str = data->type->attr; \
return BLI_strnlen(str ? str : "", len); \
}
static void rna_Operator_bl_description_set(PointerRNA *ptr, const char *value)
{
wmOperator *data = (wmOperator *)(ptr->data);
char *str = (char *)data->type->description;
if (!str[0]) {
BLI_strncpy(str, value, RNA_DYN_DESCR_MAX); /* utf8 already ensured */
}
else {
BLI_assert(!"setting the bl_description on a non-builtin operator");
}
}
static void rna_Operator_bl_undo_group_set(PointerRNA *ptr, const char *value)
{
wmOperator *data = (wmOperator *)(ptr->data);
char *str = (char *)data->type->undo_group;
if (!str[0]) {
BLI_strncpy(str, value, OP_MAX_TYPENAME); /* utf8 already ensured */
}
else {
BLI_assert(!"setting the bl_undo_group on a non-builtin operator");
}
}
OPERATOR_STR_MAYBE_NULL_GETSET(translation_context, RNA_DYN_DESCR_MAX)
OPERATOR_STR_MAYBE_NULL_GETSET(description, RNA_DYN_DESCR_MAX)
OPERATOR_STR_MAYBE_NULL_GETSET(undo_group, OP_MAX_TYPENAME)
static void rna_KeyMapItem_update(Main *UNUSED(bmain), Scene *UNUSED(scene), PointerRNA *ptr)
{
@ -1930,26 +1929,32 @@ static void rna_def_operator(BlenderRNA *brna)
prop = RNA_def_property(srna, "bl_translation_context", PROP_STRING, PROP_NONE);
RNA_def_property_string_sdna(prop, NULL, "type->translation_context");
RNA_def_property_string_maxlength(prop, RNA_DYN_DESCR_MAX); /* else it uses the pointer size! */
RNA_def_property_string_funcs(prop, NULL, NULL, "rna_Operator_bl_translation_context_set");
RNA_def_property_string_funcs(prop,
"rna_Operator_bl_translation_context_get",
"rna_Operator_bl_translation_context_length",
"rna_Operator_bl_translation_context_set");
RNA_def_property_string_default(prop, BLT_I18NCONTEXT_OPERATOR_DEFAULT);
RNA_def_property_flag(prop, PROP_REGISTER_OPTIONAL);
RNA_def_property_clear_flag(prop, PROP_NEVER_NULL); /* check for NULL */
prop = RNA_def_property(srna, "bl_description", PROP_STRING, PROP_NONE);
RNA_def_property_string_sdna(prop, NULL, "type->description");
RNA_def_property_string_maxlength(prop, RNA_DYN_DESCR_MAX); /* else it uses the pointer size! */
RNA_def_property_string_funcs(prop, NULL, NULL, "rna_Operator_bl_description_set");
RNA_def_property_string_funcs(prop,
"rna_Operator_bl_description_get",
"rna_Operator_bl_description_length",
"rna_Operator_bl_description_set");
/* RNA_def_property_clear_flag(prop, PROP_EDITABLE); */
RNA_def_property_flag(prop, PROP_REGISTER_OPTIONAL);
RNA_def_property_clear_flag(prop, PROP_NEVER_NULL); /* check for NULL */
prop = RNA_def_property(srna, "bl_undo_group", PROP_STRING, PROP_NONE);
RNA_def_property_string_sdna(prop, NULL, "type->undo_group");
RNA_def_property_string_maxlength(prop, OP_MAX_TYPENAME); /* else it uses the pointer size! */
RNA_def_property_string_funcs(prop, NULL, NULL, "rna_Operator_bl_undo_group_set");
RNA_def_property_string_funcs(prop,
"rna_Operator_bl_undo_group_get",
"rna_Operator_bl_undo_group_length",
"rna_Operator_bl_undo_group_set");
/* RNA_def_property_clear_flag(prop, PROP_EDITABLE); */
RNA_def_property_flag(prop, PROP_REGISTER_OPTIONAL);
RNA_def_property_clear_flag(prop, PROP_NEVER_NULL); /* check for NULL */
prop = RNA_def_property(srna, "bl_options", PROP_ENUM, PROP_NONE);
RNA_def_property_enum_sdna(prop, NULL, "type->flag");
@ -2022,26 +2027,32 @@ static void rna_def_macro_operator(BlenderRNA *brna)
prop = RNA_def_property(srna, "bl_translation_context", PROP_STRING, PROP_NONE);
RNA_def_property_string_sdna(prop, NULL, "type->translation_context");
RNA_def_property_string_maxlength(prop, RNA_DYN_DESCR_MAX); /* else it uses the pointer size! */
RNA_def_property_string_funcs(prop, NULL, NULL, "rna_Operator_bl_translation_context_set");
RNA_def_property_string_funcs(prop,
"rna_Operator_bl_translation_context_get",
"rna_Operator_bl_translation_context_length",
"rna_Operator_bl_translation_context_set");
RNA_def_property_string_default(prop, BLT_I18NCONTEXT_OPERATOR_DEFAULT);
RNA_def_property_flag(prop, PROP_REGISTER_OPTIONAL);
RNA_def_property_clear_flag(prop, PROP_NEVER_NULL); /* check for NULL */
prop = RNA_def_property(srna, "bl_description", PROP_STRING, PROP_NONE);
RNA_def_property_string_sdna(prop, NULL, "type->description");
RNA_def_property_string_maxlength(prop, RNA_DYN_DESCR_MAX); /* else it uses the pointer size! */
RNA_def_property_string_funcs(prop, NULL, NULL, "rna_Operator_bl_description_set");
RNA_def_property_string_funcs(prop,
"rna_Operator_bl_description_get",
"rna_Operator_bl_description_length",
"rna_Operator_bl_description_set");
/* RNA_def_property_clear_flag(prop, PROP_EDITABLE); */
RNA_def_property_flag(prop, PROP_REGISTER_OPTIONAL);
RNA_def_property_clear_flag(prop, PROP_NEVER_NULL); /* check for NULL */
prop = RNA_def_property(srna, "bl_undo_group", PROP_STRING, PROP_NONE);
RNA_def_property_string_sdna(prop, NULL, "type->undo_group");
RNA_def_property_string_maxlength(prop, OP_MAX_TYPENAME); /* else it uses the pointer size! */
RNA_def_property_string_funcs(prop, NULL, NULL, "rna_Operator_bl_undo_group_set");
RNA_def_property_string_funcs(prop,
"rna_Operator_bl_undo_group_get",
"rna_Operator_bl_undo_group_length",
"rna_Operator_bl_undo_group_set");
/* RNA_def_property_clear_flag(prop, PROP_EDITABLE); */
RNA_def_property_flag(prop, PROP_REGISTER_OPTIONAL);
RNA_def_property_clear_flag(prop, PROP_NEVER_NULL); /* check for NULL */
prop = RNA_def_property(srna, "bl_options", PROP_ENUM, PROP_NONE);
RNA_def_property_enum_sdna(prop, NULL, "type->flag");