[Bf-blender-cvs] SVN commit: /data/svn/bf-blender [34020] trunk/blender/source: - add in asserts for unlikely cases of invalid ID types being assigned to key- >from.

Campbell Barton ideasman42 at gmail.com
Mon Jan 3 05:59:57 CET 2011


Revision: 34020
          http://projects.blender.org/plugins/scmsvn/viewcvs.php?view=rev&root=bf-blender&revision=34020
Author:   campbellbarton
Date:     2011-01-03 05:59:57 +0100 (Mon, 03 Jan 2011)

Log Message:
-----------
- add in asserts for unlikely cases of invalid ID types being assigned to key->from.
- mode duplicate pointer/offset code into a static function.

Modified Paths:
--------------
    trunk/blender/source/blender/blenkernel/BKE_key.h
    trunk/blender/source/blender/blenkernel/intern/key.c
    trunk/blender/source/gameengine/Converter/BL_ShapeDeformer.cpp

Modified: trunk/blender/source/blender/blenkernel/BKE_key.h
===================================================================
--- trunk/blender/source/blender/blenkernel/BKE_key.h	2011-01-03 04:46:37 UTC (rev 34019)
+++ trunk/blender/source/blender/blenkernel/BKE_key.h	2011-01-03 04:59:57 UTC (rev 34020)
@@ -66,7 +66,7 @@
 struct KeyBlock *key_get_named_keyblock(struct Key *key, const char name[]);
 char *key_get_curValue_rnaPath(struct Key *key, struct KeyBlock *kb);
 // needed for the GE
-void do_rel_key(int start, int end, int tot, char *basispoin, struct Key *key, struct KeyBlock *actkb, int mode);
+void do_rel_key(int start, int end, const int tot, char *basispoin, struct Key *key, struct KeyBlock *actkb, const int mode);
 
 /* conversion functions */
 void key_to_mesh(struct KeyBlock *kb, struct Mesh *me);

Modified: trunk/blender/source/blender/blenkernel/intern/key.c
===================================================================
--- trunk/blender/source/blender/blenkernel/intern/key.c	2011-01-03 04:46:37 UTC (rev 34019)
+++ trunk/blender/source/blender/blenkernel/intern/key.c	2011-01-03 04:59:57 UTC (rev 34020)
@@ -61,10 +61,10 @@
 
 #include "RNA_access.h"
 
+#define KEY_MODE_DUMMY		0 /* use where mode isn't checked for */
+#define KEY_MODE_BPOINT		1
+#define KEY_MODE_BEZTRIPLE	2
 
-#define KEY_BPOINT		1
-#define KEY_BEZTRIPLE	2
-
 	// old defines from DNA_ipo_types.h for data-type
 #define IPO_FLOAT		4
 #define IPO_BEZTRIPLE	100
@@ -523,37 +523,54 @@
 	return kb->data;
 }
 
-static void cp_key(int start, int end, int tot, char *poin, Key *key, KeyBlock *actkb, KeyBlock *kb, float *weights, int mode)
+
+/* currently only the first value of 'ofs' may be set. */
+static short key_pointer_size(const Key *key, const int mode, int *poinsize, int *ofs)
 {
+	if(key->from==NULL) {
+		return FALSE;
+	}
+
+	switch(GS(key->from->name)) {
+	case ID_ME:
+		*ofs= sizeof(float)*3;
+		*poinsize= *ofs;
+		break;
+	case ID_LT:
+		*ofs= sizeof(float)*3;
+		*poinsize= *ofs;
+		break;
+	case ID_CU:
+		if(mode == KEY_MODE_BPOINT) {
+			*ofs= sizeof(float)*4;
+			*poinsize= *ofs;
+		} else {
+			ofs[0]= sizeof(float)*12;
+			*poinsize= (*ofs) / 3;
+		}
+
+		break;
+	default:
+		BKE_assert(!"invalid 'key->from' ID type");
+		return FALSE;
+	}
+
+	return TRUE;
+}
+
+static void cp_key(const int start, int end, const int tot, char *poin, Key *key, KeyBlock *actkb, KeyBlock *kb, float *weights, const int mode)
+{
 	float ktot = 0.0, kd = 0.0;
 	int elemsize, poinsize = 0, a, *ofsp, ofs[32], flagflo=0;
 	char *k1, *kref, *freek1, *freekref;
 	char *cp, elemstr[8];
 
-	if(key->from==NULL) return;
+	/* currently always 0, in future key_pointer_size may assign */
+	ofs[1]= 0;
 
-	if( GS(key->from->name)==ID_ME ) {
-		ofs[0]= sizeof(float)*3;
-		ofs[1]= 0;
-		poinsize= ofs[0];
-	}
-	else if( GS(key->from->name)==ID_LT ) {
-		ofs[0]= sizeof(float)*3;
-		ofs[1]= 0;
-		poinsize= ofs[0];
-	}
-	else if( GS(key->from->name)==ID_CU ) {
-		if(mode==KEY_BPOINT) {
-			ofs[0]= sizeof(float)*4;
-			poinsize= ofs[0];
-		}else {
-			ofs[0]= sizeof(float)*12;
-			poinsize= ofs[0]/3;
-		}
+	if(!key_pointer_size(key, mode, &poinsize, &ofs[0]))
+		return;
 
-		ofs[1]= 0;
-	}
-
 	if(end>tot) end= tot;
 	
 	if(tot != kb->totelem) {
@@ -584,7 +601,7 @@
 		else k1+= start*key->elemsize;
 	}	
 	
-	if(mode==KEY_BEZTRIPLE) {
+	if(mode == KEY_MODE_BEZTRIPLE) {
 		elemstr[0]= 1;
 		elemstr[1]= IPO_BEZTRIPLE;
 		elemstr[2]= 0;
@@ -592,11 +609,11 @@
 	
 	/* just do it here, not above! */
 	elemsize= key->elemsize;
-	if(mode==KEY_BEZTRIPLE) elemsize*= 3;
+	if(mode == KEY_MODE_BEZTRIPLE) elemsize*= 3;
 
 	for(a=start; a<end; a++) {
 		cp= key->elemstr;
-		if(mode==KEY_BEZTRIPLE) cp= elemstr;
+		if(mode == KEY_MODE_BEZTRIPLE) cp= elemstr;
 
 		ofsp= ofs;
 		
@@ -619,8 +636,14 @@
 			case IPO_BEZTRIPLE:
 				memcpy(poin, k1, sizeof(float)*12);
 				break;
+			default:
+				/* should never happen */
+				if(freek1) MEM_freeN(freek1);
+				if(freekref) MEM_freeN(freekref);
+				BKE_assert(!"invalid 'cp[1]'");
+				return;
 			}
-			
+
 			poin+= ofsp[0];	
 			cp+= 2; ofsp++;
 		}
@@ -639,14 +662,14 @@
 			kref+= elemsize;
 		}
 		
-		if(mode==KEY_BEZTRIPLE) a+=2;
+		if(mode == KEY_MODE_BEZTRIPLE) a+=2;
 	}
 
 	if(freek1) MEM_freeN(freek1);
 	if(freekref) MEM_freeN(freekref);
 }
 
-static void cp_cu_key(Curve *cu, Key *key, KeyBlock *actkb, KeyBlock *kb, int start, int end, char *out, int tot)
+static void cp_cu_key(Curve *cu, Key *key, KeyBlock *actkb, KeyBlock *kb, const int start, int end, char *out, const int tot)
 {
 	Nurb *nu;
 	int a, step, a1, a2;
@@ -658,7 +681,7 @@
 			a1= MAX2(a, start);
 			a2= MIN2(a+step, end);
 
-			if(a1<a2) cp_key(a1, a2, tot, out, key, actkb, kb, NULL, KEY_BPOINT);
+			if(a1<a2) cp_key(a1, a2, tot, out, key, actkb, kb, NULL, KEY_MODE_BPOINT);
 		}
 		else if(nu->bezt) {
 			step= 3*nu->pntsu;
@@ -667,46 +690,27 @@
 			a1= MAX2(a, start);
 			a2= MIN2(a+step, end);
 
-			if(a1<a2) cp_key(a1, a2, tot, out, key, actkb, kb, NULL, KEY_BEZTRIPLE);
+			if(a1<a2) cp_key(a1, a2, tot, out, key, actkb, kb, NULL, KEY_MODE_BEZTRIPLE);
 		}
 		else
 			step= 0;
 	}
 }
 
-
-void do_rel_key(int start, int end, int tot, char *basispoin, Key *key, KeyBlock *actkb, int mode)
+void do_rel_key(const int start, int end, const int tot, char *basispoin, Key *key, KeyBlock *actkb, const int mode)
 {
 	KeyBlock *kb;
 	int *ofsp, ofs[3], elemsize, b;
 	char *cp, *poin, *reffrom, *from, elemstr[8];
 	char *freefrom, *freereffrom;
-	int poinsize= 0;
+	int poinsize;
 
-	if(key->from==NULL) return;
+	/* currently always 0, in future key_pointer_size may assign */
+	ofs[1]= 0;
 
-	if( GS(key->from->name)==ID_ME ) {
-		ofs[0]= sizeof(float)*3;
-		ofs[1]= 0;
-		poinsize= ofs[0];
-	}
-	else if( GS(key->from->name)==ID_LT ) {
-		ofs[0]= sizeof(float)*3;
-		ofs[1]= 0;
-		poinsize= ofs[0];
-	}
-	else if( GS(key->from->name)==ID_CU ) {
-		if(mode==KEY_BPOINT) {
-			ofs[0]= sizeof(float)*4;
-			poinsize= ofs[0];
-		} else {
-			ofs[0]= sizeof(float)*12;
-			poinsize= ofs[0] / 3;
-		}
+	if(!key_pointer_size(key, mode, &poinsize, &ofs[0]))
+		return;
 
-		ofs[1]= 0;
-	}
-
 	if(end>tot) end= tot;
 
 	/* in case of beztriple */
@@ -716,7 +720,7 @@
 
 	/* just here, not above! */
 	elemsize= key->elemsize;
-	if(mode==KEY_BEZTRIPLE) elemsize*= 3;
+	if(mode == KEY_MODE_BEZTRIPLE) elemsize*= 3;
 
 	/* step 1 init */
 	cp_key(start, end, tot, basispoin, key, actkb, key->refkey, NULL, mode);
@@ -752,7 +756,7 @@
 						weight= icuval;
 					
 					cp= key->elemstr;	
-					if(mode==KEY_BEZTRIPLE) cp= elemstr;
+					if(mode == KEY_MODE_BEZTRIPLE) cp= elemstr;
 					
 					ofsp= ofs;
 					
@@ -768,8 +772,14 @@
 						case IPO_BEZTRIPLE:
 							rel_flerp(12, (float *)poin, (float *)reffrom, (float *)from, weight);
 							break;
+						default:
+							/* should never happen */
+							if(freefrom) MEM_freeN(freefrom);
+							if(freereffrom) MEM_freeN(freereffrom);
+							BKE_assert(!"invalid 'cp[1]'");
+							return;
 						}
-						
+
 						poin+= ofsp[0];				
 						
 						cp+= 2;
@@ -779,7 +789,7 @@
 					reffrom+= elemsize;
 					from+= elemsize;
 					
-					if(mode==KEY_BEZTRIPLE) b+= 2;
+					if(mode == KEY_MODE_BEZTRIPLE) b+= 2;
 					if(weights) weights++;
 				}
 
@@ -791,7 +801,7 @@
 }
 
 
-static void do_key(int start, int end, int tot, char *poin, Key *key, KeyBlock *actkb, KeyBlock **k, float *t, int mode)
+static void do_key(const int start, int end, const int tot, char *poin, Key *key, KeyBlock *actkb, KeyBlock **k, float *t, const int mode)
 {
 	float k1tot = 0.0, k2tot = 0.0, k3tot = 0.0, k4tot = 0.0;
 	float k1d = 0.0, k2d = 0.0, k3d = 0.0, k4d = 0.0;
@@ -800,29 +810,11 @@
 	char *k1, *k2, *k3, *k4, *freek1, *freek2, *freek3, *freek4;
 	char *cp, elemstr[8];;
 
-	if(key->from==0) return;
+	/* currently always 0, in future key_pointer_size may assign */
+	ofs[1]= 0;
 
-	if( GS(key->from->name)==ID_ME ) {
-		ofs[0]= sizeof(float)*3;
-		ofs[1]= 0;
-		poinsize= ofs[0];
-	}
-	else if( GS(key->from->name)==ID_LT ) {
-		ofs[0]= sizeof(float)*3;
-		ofs[1]= 0;
-		poinsize= ofs[0];
-	}
-	else if( GS(key->from->name)==ID_CU ) {
-		if(mode==KEY_BPOINT) {
-			ofs[0]= sizeof(float)*4;
-			poinsize= ofs[0];
-		} else {
-			ofs[0]= sizeof(float)*12;
-			poinsize= ofs[0] / 3;
-		}
-
-		ofs[1]= 0;
-	}
+	if(!key_pointer_size(key, mode, &poinsize, &ofs[0]))
+		return;
 	
 	if(end>tot) end= tot;
 
@@ -924,12 +916,12 @@
 
 	/* only here, not above! */
 	elemsize= key->elemsize;
-	if(mode==KEY_BEZTRIPLE) elemsize*= 3;
+	if(mode == KEY_MODE_BEZTRIPLE) elemsize*= 3;
 
 	for(a=start; a<end; a++) {
 	
 		cp= key->elemstr;	
-		if(mode==KEY_BEZTRIPLE) cp= elemstr;
+		if(mode == KEY_MODE_BEZTRIPLE) cp= elemstr;
 		
 		ofsp= ofs;
 		
@@ -945,6 +937,14 @@
 			case IPO_BEZTRIPLE:
 				flerp(12, (void *)poin, (void *)k1, (void *)k2, (void *)k3, (void *)k4, t);
 				break;
+			default:
+				/* should never happen */
+				if(freek1) MEM_freeN(freek1);
+				if(freek2) MEM_freeN(freek2);
+				if(freek3) MEM_freeN(freek3);
+				if(freek4) MEM_freeN(freek4);
+				BKE_assert(!"invalid 'cp[1]'");
+				return;
 			}
 			
 			poin+= ofsp[0];				
@@ -993,7 +993,7 @@
 			else k4+= elemsize;
 		}
 		
-		if(mode==KEY_BEZTRIPLE) a+= 2;
+		if(mode == KEY_MODE_BEZTRIPLE) a+= 2;
 	}
 
 	if(freek1) MEM_freeN(freek1);
@@ -1067,7 +1067,7 @@
 	return NULL;
 }
 
-static void do_mesh_key(Scene *scene, Object *ob, Key *key, char *out, int tot)
+static void do_mesh_key(Scene *scene, Object *ob, Key *key, char *out, const int tot)
 {
 	KeyBlock *k[4], *actkb= ob_get_keyblock(ob);
 	float cfra, ctime, t[4], delta;
@@ -1102,9 +1102,9 @@
 			flag= setkeys(ctime, &key->block, k, t, 0);
 
 			if(flag==0)
-				do_key(a, a+step, tot, (char *)out, key, actkb, k, t, 0);
+				do_key(a, a+step, tot, (char *)out, key, actkb, k, t, KEY_MODE_DUMMY);
 			else
-				cp_key(a, a+step, tot, (char *)out, key, actkb, k[2], NULL, 0);
+				cp_key(a, a+step, tot, (char *)out, key, actkb, k[2], NULL, KEY_MODE_DUMMY);
 		}
 	}
 	else {
@@ -1114,7 +1114,7 @@
 			for(kb= key->block.first; kb; kb= kb->next)
 				kb->weights= get_weights_array(ob, kb->vgroup);
 
-			do_rel_key(0, tot, tot, (char *)out, key, actkb, 0);
+			do_rel_key(0, tot, tot, (char *)out, key, actkb, KEY_MODE_DUMMY);
 			
 			for(kb= key->block.first; kb; kb= kb->next) {
 				if(kb->weights) MEM_freeN(kb->weights);
@@ -1137,14 +1137,14 @@
 			flag= setkeys(ctime, &key->block, k, t, 0);
 
 			if(flag==0)
-				do_key(0, tot, tot, (char *)out, key, actkb, k, t, 0);
+				do_key(0, tot, tot, (char *)out, key, actkb, k, t, KEY_MODE_DUMMY);
 			else

@@ Diff output truncated at 10240 characters. @@




More information about the Bf-blender-cvs mailing list