Fix for [#19847] Joined meshes fail to render
authorMatt Ebb <matt@mke3.net>
Tue, 10 Nov 2009 06:29:10 +0000 (06:29 +0000)
committerMatt Ebb <matt@mke3.net>
Tue, 10 Nov 2009 06:29:10 +0000 (06:29 +0000)
Join function was accessing invalid memory for material indices when no materials were originally present on the joining objects

source/blender/editors/mesh/meshtools.c

index 8bd8629a5952430ef72d191398e2ddebabf88cfc..18125207eca4b21018ed04a326be3218ec96b58b 100644 (file)
@@ -123,7 +123,7 @@ int join_mesh_exec(bContext *C, wmOperator *op)
        KeyBlock *kb, *okb, *kbn;
        float imat[4][4], cmat[4][4], *fp1, *fp2, curpos;
        int a, b, totcol, totmat=0, totedge=0, totvert=0, totface=0, ok=0;
-       int vertofs, *matmap;
+       int vertofs, *matmap=NULL;
        int     i, j, index, haskey=0, edgeofs, faceofs;
        bDeformGroup *dg, *odg;
        MDeformVert *dvert;
@@ -168,7 +168,7 @@ int join_mesh_exec(bContext *C, wmOperator *op)
        
        /* new material indices and material array */
        matar= MEM_callocN(sizeof(void*)*totmat, "join_mesh matar");
-       matmap= MEM_callocN(sizeof(int)*totmat, "join_mesh matmap");
+       if (totmat) matmap= MEM_callocN(sizeof(int)*totmat, "join_mesh matmap");
        totcol= ob->totcol;
        
        /* obact materials in new main array, is nicer start! */
@@ -435,7 +435,10 @@ int join_mesh_exec(bContext *C, wmOperator *op)
                                        mface->v3+= vertofs;
                                        if(mface->v4) mface->v4+= vertofs;
                                        
-                                       mface->mat_nr= matmap[(int)mface->mat_nr];
+                                       if (matmap)
+                                               mface->mat_nr= matmap[(int)mface->mat_nr];
+                                       else 
+                                               mface->mat_nr= 0;
                                }
                                
                                faceofs += me->totface;
@@ -508,7 +511,7 @@ int join_mesh_exec(bContext *C, wmOperator *op)
        ob->totcol= me->totcol= totcol;
        ob->colbits= 0;
 
-       MEM_freeN(matmap);
+       if (matmap) MEM_freeN(matmap);
        
        /* other mesh users */
        test_object_materials((ID *)me);