From cd9ce01657dcdf2a1cc866a0290ab089df799c81 Mon Sep 17 00:00:00 2001 From: Bastien Montagne Date: Tue, 17 Nov 2020 12:56:26 +0100 Subject: [PATCH] RNA define: check and report invalid usages of ID pointers properties. Some RNA structs, like operators or keymaps, are not allowed to have ID pointer properties. now this check will ignore those, and report an error message in the console. Related to T82597. Notes: While a bit more involved than rBf39fbb3e6046, this commit remains fairly localized and non-intrusive. It relies on some rather obscure and weird behaviors of our RNA code though, a cleaner solution could be e.g. to add a tye to `StructOrFunctionRNA`, so that we could properly 'rebuild' (re-cast) the pointer to either `StructRNA` or `FunctionRNA` when needed in internal code... --- source/blender/makesrna/RNA_define.h | 4 ++- source/blender/makesrna/intern/rna_define.c | 32 +++++++++++++++---- .../windowmanager/intern/wm_gesture_ops.c | 2 +- 3 files changed, 29 insertions(+), 9 deletions(-) diff --git a/source/blender/makesrna/RNA_define.h b/source/blender/makesrna/RNA_define.h index 272246d75d3..c12426ffcd0 100644 --- a/source/blender/makesrna/RNA_define.h +++ b/source/blender/makesrna/RNA_define.h @@ -372,7 +372,9 @@ void RNA_def_property_enum_items(PropertyRNA *prop, const EnumPropertyItem *item void RNA_def_property_enum_native_type(PropertyRNA *prop, const char *native_enum_type); void RNA_def_property_string_maxlength(PropertyRNA *prop, int maxlength); void RNA_def_property_struct_type(PropertyRNA *prop, const char *type); -void RNA_def_property_struct_runtime(PropertyRNA *prop, StructRNA *type); +void RNA_def_property_struct_runtime(StructOrFunctionRNA *cont, + PropertyRNA *prop, + StructRNA *type); void RNA_def_property_boolean_default(PropertyRNA *prop, bool value); void RNA_def_property_boolean_array_default(PropertyRNA *prop, const bool *array); diff --git a/source/blender/makesrna/intern/rna_define.c b/source/blender/makesrna/intern/rna_define.c index 1b0a2fca0ce..8cc4f545ca9 100644 --- a/source/blender/makesrna/intern/rna_define.c +++ b/source/blender/makesrna/intern/rna_define.c @@ -1812,8 +1812,9 @@ void RNA_def_property_struct_type(PropertyRNA *prop, const char *type) } } -void RNA_def_property_struct_runtime(PropertyRNA *prop, StructRNA *type) +void RNA_def_property_struct_runtime(StructOrFunctionRNA *cont, PropertyRNA *prop, StructRNA *type) { + /* Never valid when defined from python. */ StructRNA *srna = DefRNA.laststruct; if (DefRNA.preprocess) { @@ -1821,11 +1822,26 @@ void RNA_def_property_struct_runtime(PropertyRNA *prop, StructRNA *type) return; } + const bool is_id_type = (type->flag & STRUCT_ID) != 0; + switch (prop->type) { case PROP_POINTER: { PointerPropertyRNA *pprop = (PointerPropertyRNA *)prop; pprop->type = type; + /* Check between `cont` and `srna` is mandatory, since when defined from python + * `DefRNA.laststruct` is not valid. + * This is not an issue as bpy code already checks for this case on its own. */ + if (cont == srna && (srna->flag & STRUCT_NO_DATABLOCK_IDPROPERTIES) != 0 && is_id_type) { + CLOG_ERROR(&LOG, + "\"%s.%s\", this struct type (probably an Operator, Keymap or UserPreference) " + "does not accept ID pointer properties.", + CONTAINER_RNA_ID(cont), + prop->identifier); + DefRNA.error = true; + return; + } + if (type && (type->flag & STRUCT_ID_REFCOUNT)) { prop->flag |= PROP_ID_REFCOUNT; } @@ -1838,13 +1854,15 @@ void RNA_def_property_struct_runtime(PropertyRNA *prop, StructRNA *type) break; } default: - CLOG_ERROR( - &LOG, "\"%s.%s\", invalid type for struct type.", srna->identifier, prop->identifier); + CLOG_ERROR(&LOG, + "\"%s.%s\", invalid type for struct type.", + CONTAINER_RNA_ID(cont), + prop->identifier); DefRNA.error = true; - break; + return; } - if ((type->flag & STRUCT_ID) != 0) { + if (is_id_type) { prop->flag |= PROP_PTR_NO_OWNERSHIP; } } @@ -4144,7 +4162,7 @@ PropertyRNA *RNA_def_pointer_runtime(StructOrFunctionRNA *cont_, PropertyRNA *prop; prop = RNA_def_property(cont, identifier, PROP_POINTER, PROP_NONE); - RNA_def_property_struct_runtime(prop, type); + RNA_def_property_struct_runtime(cont, prop, type); if ((type->flag & STRUCT_ID) != 0) { prop->flag |= PROP_EDITABLE; } @@ -4179,7 +4197,7 @@ PropertyRNA *RNA_def_collection_runtime(StructOrFunctionRNA *cont_, PropertyRNA *prop; prop = RNA_def_property(cont, identifier, PROP_COLLECTION, PROP_NONE); - RNA_def_property_struct_runtime(prop, type); + RNA_def_property_struct_runtime(cont, prop, type); RNA_def_property_ui_text(prop, ui_name, ui_description); return prop; diff --git a/source/blender/windowmanager/intern/wm_gesture_ops.c b/source/blender/windowmanager/intern/wm_gesture_ops.c index edf9a3bc693..07d68293714 100644 --- a/source/blender/windowmanager/intern/wm_gesture_ops.c +++ b/source/blender/windowmanager/intern/wm_gesture_ops.c @@ -818,7 +818,7 @@ void WM_OT_lasso_gesture(wmOperatorType *ot) ot->poll = WM_operator_winactive; prop = RNA_def_property(ot->srna, "path", PROP_COLLECTION, PROP_NONE); - RNA_def_property_struct_runtime(prop, &RNA_OperatorMousePath); + RNA_def_property_struct_runtime(ot->srna, prop, &RNA_OperatorMousePath); } #endif