[Bf-blender-cvs] SVN commit: /data/svn/bf-blender [42873] trunk/blender/source: fix for a bug in mathutils when a vector was accessing a matrix and the matrix size increased , future access too the vector would write past the allocated bounds.

Campbell Barton ideasman42 at gmail.com
Mon Dec 26 01:05:48 CET 2011


Revision: 42873
          http://projects.blender.org/scm/viewvc.php?view=rev&root=bf-blender&revision=42873
Author:   campbellbarton
Date:     2011-12-26 00:05:41 +0000 (Mon, 26 Dec 2011)
Log Message:
-----------
fix for a bug in mathutils when a vector was accessing a matrix and the matrix size increased, future access too the vector would write past the allocated bounds. now raise an exception.

Modified Paths:
--------------
    trunk/blender/source/blender/python/mathutils/mathutils.c
    trunk/blender/source/blender/python/mathutils/mathutils_Matrix.c
    trunk/blender/source/blender/python/mathutils/mathutils_Matrix.h
    trunk/blender/source/creator/creator.c

Modified: trunk/blender/source/blender/python/mathutils/mathutils.c
===================================================================
--- trunk/blender/source/blender/python/mathutils/mathutils.c	2011-12-25 21:31:19 UTC (rev 42872)
+++ trunk/blender/source/blender/python/mathutils/mathutils.c	2011-12-26 00:05:41 UTC (rev 42873)
@@ -284,7 +284,7 @@
 /* Mathutils Callbacks */
 
 /* for mathutils internal use only, eventually should re-alloc but to start with we only have a few users */
-static Mathutils_Callback *mathutils_callbacks[8] = {NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL};
+static Mathutils_Callback *mathutils_callbacks[8] = {NULL};
 
 int Mathutils_RegisterCallback(Mathutils_Callback *cb)
 {
@@ -459,6 +459,7 @@
 
 	mathutils_matrix_row_cb_index = Mathutils_RegisterCallback(&mathutils_matrix_row_cb);
 	mathutils_matrix_col_cb_index = Mathutils_RegisterCallback(&mathutils_matrix_col_cb);
+	mathutils_matrix_translation_cb_index = Mathutils_RegisterCallback(&mathutils_matrix_translation_cb);
 
 	return submodule;
 }

Modified: trunk/blender/source/blender/python/mathutils/mathutils_Matrix.c
===================================================================
--- trunk/blender/source/blender/python/mathutils/mathutils_Matrix.c	2011-12-25 21:31:19 UTC (rev 42872)
+++ trunk/blender/source/blender/python/mathutils/mathutils_Matrix.c	2011-12-26 00:05:41 UTC (rev 42873)
@@ -48,22 +48,53 @@
 static PyObject *matrix__apply_to_copy(PyNoArgsFunction matrix_func, MatrixObject *self);
 static PyObject *MatrixAccess_CreatePyObject(MatrixObject *matrix, const eMatrixAccess_t type);
 
-/* matrix row callbacks */
+static int matrix_row_vector_check(MatrixObject *mat, VectorObject *vec, int row)
+{
+	if ((vec->size != mat->num_col) || (row >= mat->num_row)) {
+		PyErr_SetString(PyExc_AttributeError,
+		                "Matrix(): "
+		                "owner matrix has been resized since this row vector was created");
+		return 0;
+	}
+	else {
+		return 1;
+	}
+}
+
+static int matrix_col_vector_check(MatrixObject *mat, VectorObject *vec, int col)
+{
+	if ((vec->size != mat->num_row) || (col >= mat->num_col)) {
+		PyErr_SetString(PyExc_AttributeError,
+		                "Matrix(): "
+		                "owner matrix has been resized since this column vector was created");
+		return 0;
+	}
+	else {
+		return 1;
+	}
+}
+
+/* ----------------------------------------------------------------------------
+ * matrix row callbacks
+ * this is so you can do matrix[i][j] = val OR matrix.row[i][j] = val */
+
 int mathutils_matrix_row_cb_index = -1;
 
-static int mathutils_matrix_vector_check(BaseMathObject *bmo)
+static int mathutils_matrix_row_check(BaseMathObject *bmo)
 {
 	MatrixObject *self = (MatrixObject *)bmo->cb_user;
 	return BaseMath_ReadCallback(self);
 }
 
-static int mathutils_matrix_vector_get(BaseMathObject *bmo, int row)
+static int mathutils_matrix_row_get(BaseMathObject *bmo, int row)
 {
 	MatrixObject *self = (MatrixObject *)bmo->cb_user;
 	int col;
 
 	if (BaseMath_ReadCallback(self) == -1)
 		return -1;
+	if (!matrix_row_vector_check(self, (VectorObject *)bmo, row))
+		return -1;
 
 	for (col = 0; col < self->num_col; col++) {
 		bmo->data[col] = MATRIX_ITEM(self, row, col);
@@ -72,13 +103,15 @@
 	return 0;
 }
 
-static int mathutils_matrix_vector_set(BaseMathObject *bmo, int row)
+static int mathutils_matrix_row_set(BaseMathObject *bmo, int row)
 {
 	MatrixObject *self = (MatrixObject *)bmo->cb_user;
 	int col;
 
 	if (BaseMath_ReadCallback(self) == -1)
 		return -1;
+	if (!matrix_row_vector_check(self, (VectorObject *)bmo, row))
+		return -1;
 
 	for (col = 0; col < self->num_col; col++) {
 		MATRIX_ITEM(self, row, col) = bmo->data[col];
@@ -88,23 +121,27 @@
 	return 0;
 }
 
-static int mathutils_matrix_vector_get_index(BaseMathObject *bmo, int row, int col)
+static int mathutils_matrix_row_get_index(BaseMathObject *bmo, int row, int col)
 {
 	MatrixObject *self = (MatrixObject *)bmo->cb_user;
 
 	if (BaseMath_ReadCallback(self) == -1)
 		return -1;
+	if (!matrix_row_vector_check(self, (VectorObject *)bmo, row))
+		return -1;
 
 	bmo->data[col] = MATRIX_ITEM(self, row, col);
 	return 0;
 }
 
-static int mathutils_matrix_vector_set_index(BaseMathObject *bmo, int row, int col)
+static int mathutils_matrix_row_set_index(BaseMathObject *bmo, int row, int col)
 {
 	MatrixObject *self = (MatrixObject *)bmo->cb_user;
 
 	if (BaseMath_ReadCallback(self) == -1)
 		return -1;
+	if (!matrix_row_vector_check(self, (VectorObject *)bmo, row))
+		return -1;
 
 	MATRIX_ITEM(self, row, col) = bmo->data[col];
 
@@ -113,24 +150,27 @@
 }
 
 Mathutils_Callback mathutils_matrix_row_cb = {
-	mathutils_matrix_vector_check,
-	mathutils_matrix_vector_get,
-	mathutils_matrix_vector_set,
-	mathutils_matrix_vector_get_index,
-	mathutils_matrix_vector_set_index
+	mathutils_matrix_row_check,
+	mathutils_matrix_row_get,
+	mathutils_matrix_row_set,
+	mathutils_matrix_row_get_index,
+	mathutils_matrix_row_set_index
 };
-/* matrix vector callbacks, this is so you can do matrix[i][j] = val  */
 
-/* matrix row callbacks */
+
+/* ----------------------------------------------------------------------------
+ * matrix row callbacks
+ * this is so you can do matrix.col[i][j] = val */
+
 int mathutils_matrix_col_cb_index = -1;
 
-static int mathutils_matrix_column_check(BaseMathObject *bmo)
+static int mathutils_matrix_col_check(BaseMathObject *bmo)
 {
 	MatrixObject *self = (MatrixObject *)bmo->cb_user;
 	return BaseMath_ReadCallback(self);
 }
 
-static int mathutils_matrix_column_get(BaseMathObject *bmo, int col)
+static int mathutils_matrix_col_get(BaseMathObject *bmo, int col)
 {
 	MatrixObject *self = (MatrixObject *)bmo->cb_user;
 	int num_row;
@@ -138,6 +178,8 @@
 
 	if (BaseMath_ReadCallback(self) == -1)
 		return -1;
+	if (!matrix_col_vector_check(self, (VectorObject *)bmo, col))
+		return -1;
 
 	/* for 'translation' size will always be '3' even on 4x4 vec */
 	num_row = MIN2(self->num_row, ((VectorObject *)bmo)->size);
@@ -149,7 +191,7 @@
 	return 0;
 }
 
-static int mathutils_matrix_column_set(BaseMathObject *bmo, int col)
+static int mathutils_matrix_col_set(BaseMathObject *bmo, int col)
 {
 	MatrixObject *self = (MatrixObject *)bmo->cb_user;
 	int num_row;
@@ -157,6 +199,8 @@
 
 	if (BaseMath_ReadCallback(self) == -1)
 		return -1;
+	if (!matrix_col_vector_check(self, (VectorObject *)bmo, col))
+		return -1;
 
 	/* for 'translation' size will always be '3' even on 4x4 vec */
 	num_row = MIN2(self->num_row, ((VectorObject *)bmo)->size);
@@ -169,23 +213,27 @@
 	return 0;
 }
 
-static int mathutils_matrix_column_get_index(BaseMathObject *bmo, int col, int row)
+static int mathutils_matrix_col_get_index(BaseMathObject *bmo, int col, int row)
 {
 	MatrixObject *self = (MatrixObject *)bmo->cb_user;
 
 	if (BaseMath_ReadCallback(self) == -1)
 		return -1;
+	if (!matrix_col_vector_check(self, (VectorObject *)bmo, col))
+		return -1;
 
 	bmo->data[row] = MATRIX_ITEM(self, row, col);
 	return 0;
 }
 
-static int mathutils_matrix_column_set_index(BaseMathObject *bmo, int col, int row)
+static int mathutils_matrix_col_set_index(BaseMathObject *bmo, int col, int row)
 {
 	MatrixObject *self = (MatrixObject *)bmo->cb_user;
 
 	if (BaseMath_ReadCallback(self) == -1)
 		return -1;
+	if (!matrix_col_vector_check(self, (VectorObject *)bmo, col))
+		return -1;
 
 	MATRIX_ITEM(self, row, col) = bmo->data[row];
 
@@ -194,12 +242,91 @@
 }
 
 Mathutils_Callback mathutils_matrix_col_cb = {
-	mathutils_matrix_column_check,
-	mathutils_matrix_column_get,
-	mathutils_matrix_column_set,
-	mathutils_matrix_column_get_index,
-	mathutils_matrix_column_set_index
+	mathutils_matrix_col_check,
+	mathutils_matrix_col_get,
+	mathutils_matrix_col_set,
+	mathutils_matrix_col_get_index,
+	mathutils_matrix_col_set_index
 };
+
+
+/* ----------------------------------------------------------------------------
+ * matrix row callbacks
+ * this is so you can do matrix.translation = val
+ * note, this is _exactly like matrix.col except the 4th component is always omitted */
+
+int mathutils_matrix_translation_cb_index = -1;
+
+static int mathutils_matrix_translation_check(BaseMathObject *bmo)
+{
+	MatrixObject *self = (MatrixObject *)bmo->cb_user;
+	return BaseMath_ReadCallback(self);
+}
+
+static int mathutils_matrix_translation_get(BaseMathObject *bmo, int col)
+{
+	MatrixObject *self = (MatrixObject *)bmo->cb_user;
+	int row;
+
+	if (BaseMath_ReadCallback(self) == -1)
+		return -1;
+
+	for (row = 0; row < 3; row++) {
+		bmo->data[row] = MATRIX_ITEM(self, row, col);
+	}
+
+	return 0;
+}
+
+static int mathutils_matrix_translation_set(BaseMathObject *bmo, int col)
+{
+	MatrixObject *self = (MatrixObject *)bmo->cb_user;
+	int row;
+
+	if (BaseMath_ReadCallback(self) == -1)
+		return -1;
+
+	for (row = 0; row < 3; row++) {
+		MATRIX_ITEM(self, row, col) = bmo->data[row];
+	}
+
+	(void)BaseMath_WriteCallback(self);
+	return 0;
+}
+
+static int mathutils_matrix_translation_get_index(BaseMathObject *bmo, int col, int row)
+{
+	MatrixObject *self = (MatrixObject *)bmo->cb_user;
+
+	if (BaseMath_ReadCallback(self) == -1)
+		return -1;
+
+	bmo->data[row] = MATRIX_ITEM(self, row, col);
+	return 0;
+}
+
+static int mathutils_matrix_translation_set_index(BaseMathObject *bmo, int col, int row)
+{
+	MatrixObject *self = (MatrixObject *)bmo->cb_user;
+
+	if (BaseMath_ReadCallback(self) == -1)
+		return -1;
+
+	MATRIX_ITEM(self, row, col) = bmo->data[row];
+
+	(void)BaseMath_WriteCallback(self);
+	return 0;
+}
+
+Mathutils_Callback mathutils_matrix_translation_cb = {
+	mathutils_matrix_translation_check,
+	mathutils_matrix_translation_get,
+	mathutils_matrix_translation_set,
+	mathutils_matrix_translation_get_index,
+	mathutils_matrix_translation_set_index
+};
+
+
 /* matrix column callbacks, this is so you can do matrix.translation = Vector()  */
 
 //----------------------------------mathutils.Matrix() -----------------
@@ -1972,7 +2099,7 @@
 		return NULL;
 	}
 
-	ret = (PyObject *)Vector_CreatePyObject_cb((PyObject *)self, 3, mathutils_matrix_col_cb_index, 3);
+	ret = (PyObject *)Vector_CreatePyObject_cb((PyObject *)self, 3, mathutils_matrix_translation_cb_index, 3);
 
 	return ret;
 }

Modified: trunk/blender/source/blender/python/mathutils/mathutils_Matrix.h
===================================================================
--- trunk/blender/source/blender/python/mathutils/mathutils_Matrix.h	2011-12-25 21:31:19 UTC (rev 42872)
+++ trunk/blender/source/blender/python/mathutils/mathutils_Matrix.h	2011-12-26 00:05:41 UTC (rev 42873)
@@ -75,8 +75,11 @@
 

@@ Diff output truncated at 10240 characters. @@



More information about the Bf-blender-cvs mailing list