[Bf-blender-cvs] [6899474615d] master: Fix: crash when adding curves in curves sculpt mode with interpolation

Jacques Lucke noreply at git.blender.org
Tue Jan 24 17:37:32 CET 2023


Commit: 6899474615dfb35b455fc05d91dadd066f60ae97
Author: Jacques Lucke
Date:   Tue Jan 24 17:30:24 2023 +0100
Branches: master
https://developer.blender.org/rB6899474615dfb35b455fc05d91dadd066f60ae97

Fix: crash when adding curves in curves sculpt mode with interpolation

There were two issues:
* The `new_point_counts_per_curve` was one too large, resulting in
  `interpolate_from_neighbors` reading invalid memory.
* Writing the counts into the existing offsets array didn't quite work
  because there can be a collision at the offset right between the
  last old curve and the first new point. There was a race condition
  where this value could be read and written at the same time.

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

M	source/blender/geometry/intern/add_curves_on_mesh.cc

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

diff --git a/source/blender/geometry/intern/add_curves_on_mesh.cc b/source/blender/geometry/intern/add_curves_on_mesh.cc
index 2ef51867e07..5f174558e29 100644
--- a/source/blender/geometry/intern/add_curves_on_mesh.cc
+++ b/source/blender/geometry/intern/add_curves_on_mesh.cc
@@ -286,19 +286,25 @@ AddCurvesOnMeshOutputs add_curves_on_mesh(CurvesGeometry &curves,
 
   /* Compute new curve offsets. */
   MutableSpan<int> curve_offsets = curves.offsets_for_write();
-  MutableSpan<int> new_point_counts_per_curve = curve_offsets.drop_front(old_curves_num);
-  if (inputs.interpolate_point_count) {
+  Array<int> new_point_counts_per_curve(added_curves_num);
+  if (inputs.interpolate_point_count && old_curves_num > 0) {
+    const OffsetIndices<int> old_points_by_curve{curve_offsets.take_front(old_curves_num + 1)};
     interpolate_from_neighbors<int>(
         neighbors_per_curve,
         inputs.fallback_point_count,
-        [&](const int curve_i) { return curve_offsets[curve_i + 1] - curve_offsets[curve_i]; },
+        [&](const int curve_i) { return old_points_by_curve.size(curve_i); },
         new_point_counts_per_curve);
   }
   else {
     new_point_counts_per_curve.fill(inputs.fallback_point_count);
   }
-  offset_indices::accumulate_counts_to_offsets(curve_offsets.drop_front(old_curves_num),
-                                               old_points_num);
+  curve_offsets[old_curves_num] = old_points_num;
+  int offset = old_points_num;
+  for (const int i : new_point_counts_per_curve.index_range()) {
+    const int point_count_in_curve = new_point_counts_per_curve[i];
+    curve_offsets[old_curves_num + i + 1] = offset + point_count_in_curve;
+    offset += point_count_in_curve;
+  }
 
   const int new_points_num = curves.offsets().last();
   curves.resize(new_points_num, new_curves_num);



More information about the Bf-blender-cvs mailing list