Fix for potential bug with new GP Fill
authorJoshua Leung <aligorith@gmail.com>
Thu, 28 Apr 2016 15:25:48 +0000 (03:25 +1200)
committerJoshua Leung <aligorith@gmail.com>
Sun, 8 May 2016 12:53:49 +0000 (00:53 +1200)
While trying to track down why I still keep getting some random "extra" triangles
showing up when reloading files with fills, I noticed that there's an index mismatch
here that may cause problems in some rare cases:

There are "gps->totpoints" items in tmp_triangles, but there should only be
"gps->tot_triangles" triangles in the gps->triangles array. If for whatever reason
some of the triangles in tmp_triangles are invalid, this could have meant that
while a tmp_triangles candidate was skipped, a corresponding slot for a valid
triangle also got skipped.

source/blender/editors/gpencil/drawgpencil.c

index c885842e756ed7395f232d1f7dc20a730de5497d..2ba9a79eff3daa28dd4187b6da13e3270b605f83 100644 (file)
@@ -408,12 +408,15 @@ static void gp_triangulate_stroke_fill(bGPDstroke *gps)
                        gps->triangles = MEM_recallocN(gps->triangles, sizeof(bGPDtriangle) * gps->tot_triangles);
                }
                
-               for (int i = 0; i < gps->tot_triangles; i++) {
+               int triangle_index = 0;
+               int i;
+               
+               for (i = 0; (i < gps->totpoints) && (triangle_index < gps->tot_triangles); i++) {
                        if ((tmp_triangles[i][0] >= 0) && (tmp_triangles[i][0] < gps->totpoints) &&
                            (tmp_triangles[i][1] >= 0) && (tmp_triangles[i][1] < gps->totpoints) &&
                            (tmp_triangles[i][2] >= 0) && (tmp_triangles[i][2] < gps->totpoints))
                        {
-                               bGPDtriangle *stroke_triangle = &gps->triangles[i];
+                               bGPDtriangle *stroke_triangle = &gps->triangles[triangle_index++];
                                
                                stroke_triangle->v1 = tmp_triangles[i][0];
                                stroke_triangle->v2 = tmp_triangles[i][1];