[Bf-blender-cvs] [0432740b52b] master: Add nullptr checks in the depsgraph physics relation builder

Sebastian Parborg noreply at git.blender.org
Wed Sep 9 16:19:57 CEST 2020


Commit: 0432740b52babfc307cc004fb008b41a44d0f388
Author: Sebastian Parborg
Date:   Wed Sep 9 16:18:01 2020 +0200
Branches: master
https://developer.blender.org/rB0432740b52babfc307cc004fb008b41a44d0f388

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

===================================================================

M	source/blender/depsgraph/intern/depsgraph_physics.cc

===================================================================

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<class T> 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<const deg::Depsgraph *>(graph);
-  if (deg_graph->physics_relations[DEG_PHYSICS_EFFECTOR] == nullptr) {
+  blender::Map<const ID *, ListBase *> *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<const deg::Depsgraph *>(graph);
   const ePhysicsRelationType type = modifier_to_relation_type(modifier_type);
-  if (deg_graph->physics_relations[type] == nullptr) {
+  blender::Map<const ID *, ListBase *> *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<const ID *, ListBase *>();
     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<const ID *, ListBase *>();
     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);
   });



More information about the Bf-blender-cvs mailing list