From 0432740b52babfc307cc004fb008b41a44d0f388 Mon Sep 17 00:00:00 2001 From: Sebastian Parborg Date: Wed, 9 Sep 2020 16:18:01 +0200 Subject: Add nullptr checks in the depsgraph physics relation builder Without these check ASAN complains about null pointer member access. Reviewed By: Sergey Differential Revision: http://developer.blender.org/D8847 --- .../blender/depsgraph/intern/depsgraph_physics.cc | 44 +++++++++++++++++----- 1 file changed, 34 insertions(+), 10 deletions(-) (limited to 'source') diff --git a/source/blender/depsgraph/intern/depsgraph_physics.cc b/source/blender/depsgraph/intern/depsgraph_physics.cc index 76f3a2e8ff4..f51e707284b 100644 --- a/source/blender/depsgraph/intern/depsgraph_physics.cc +++ b/source/blender/depsgraph/intern/depsgraph_physics.cc @@ -62,17 +62,28 @@ static ePhysicsRelationType modifier_to_relation_type(unsigned int modifier_type BLI_assert(!"Unknown collision modifier type"); return DEG_PHYSICS_RELATIONS_NUM; } +/* Get ID from an ID type object, in a safe manner. This means that object can be nullptr, + * in which case the function returns nullptr. + */ +template static ID *object_id_safe(T *object) +{ + if (object == nullptr) { + return nullptr; + } + return &object->id; +} ListBase *DEG_get_effector_relations(const Depsgraph *graph, Collection *collection) { const deg::Depsgraph *deg_graph = reinterpret_cast(graph); - if (deg_graph->physics_relations[DEG_PHYSICS_EFFECTOR] == nullptr) { + blender::Map *hash = deg_graph->physics_relations[DEG_PHYSICS_EFFECTOR]; + if (hash == nullptr) { return nullptr; } - - ID *collection_orig = DEG_get_original_id(&collection->id); - return deg_graph->physics_relations[DEG_PHYSICS_EFFECTOR]->lookup_default(collection_orig, - nullptr); + /* Note: nullptr is a valid loolup key here as it means that the relation is not bound to a + * specific collection. */ + ID *collection_orig = DEG_get_original_id(object_id_safe(collection)); + return hash->lookup_default(collection_orig, nullptr); } ListBase *DEG_get_collision_relations(const Depsgraph *graph, @@ -81,11 +92,14 @@ ListBase *DEG_get_collision_relations(const Depsgraph *graph, { const deg::Depsgraph *deg_graph = reinterpret_cast(graph); const ePhysicsRelationType type = modifier_to_relation_type(modifier_type); - if (deg_graph->physics_relations[type] == nullptr) { + blender::Map *hash = deg_graph->physics_relations[type]; + if (hash == nullptr) { return nullptr; } - ID *collection_orig = DEG_get_original_id(&collection->id); - return deg_graph->physics_relations[type]->lookup_default(collection_orig, nullptr); + /* Note: nullptr is a valid loolup key here as it means that the relation is not bound to a + * specific collection. */ + ID *collection_orig = DEG_get_original_id(object_id_safe(collection)); + return hash->lookup_default(collection_orig, nullptr); } /********************** Depsgraph Building API ************************/ @@ -170,7 +184,12 @@ ListBase *build_effector_relations(Depsgraph *graph, Collection *collection) graph->physics_relations[DEG_PHYSICS_EFFECTOR] = new Map(); hash = graph->physics_relations[DEG_PHYSICS_EFFECTOR]; } - return hash->lookup_or_add_cb(&collection->id, [&]() { + /* If collection is nullptr still use it as a key. + * In this case the BKE_effector_relations_create() will create relates for all bases in the + * view layer. + */ + ID *collection_id = object_id_safe(collection); + return hash->lookup_or_add_cb(collection_id, [&]() { ::Depsgraph *depsgraph = reinterpret_cast<::Depsgraph *>(graph); return BKE_effector_relations_create(depsgraph, graph->view_layer, collection); }); @@ -186,7 +205,12 @@ ListBase *build_collision_relations(Depsgraph *graph, graph->physics_relations[type] = new Map(); hash = graph->physics_relations[type]; } - return hash->lookup_or_add_cb(&collection->id, [&]() { + /* If collection is nullptr still use it as a key. + * In this case the BKE_collision_relations_create() will create relates for all bases in the + * view layer. + */ + ID *collection_id = object_id_safe(collection); + return hash->lookup_or_add_cb(collection_id, [&]() { ::Depsgraph *depsgraph = reinterpret_cast<::Depsgraph *>(graph); return BKE_collision_relations_create(depsgraph, collection, modifier_type); }); -- cgit v1.2.3