bgl.Buffer()
authorCampbell Barton <ideasman42@gmail.com>
Tue, 18 Jan 2011 03:49:28 +0000 (03:49 +0000)
committerCampbell Barton <ideasman42@gmail.com>
Tue, 18 Jan 2011 03:49:28 +0000 (03:49 +0000)
 - invalid dimension type could be passed without raising an error.
 - negative dimensions could crash blender, now they raise errors.
 - zero length dimension arg was not detected.
 - floating point lengths were accepted, now only allow ints.

 also comment unused vars.

source/blender/editors/gpencil/gpencil_paint.c
source/blender/python/generic/bgl.c

index 86f3e3c0b9105bb0a19b39dcd35f453496d4edc2..baa6f7d6a25907d94a4615bc7125a9d527c03976 100644 (file)
@@ -899,8 +899,8 @@ static tGPsdata *gp_session_initpaint (bContext *C)
                /* supported views first */
                case SPACE_VIEW3D:
                {
-                       View3D *v3d= curarea->spacedata.first;
-                       RegionView3D *rv3d= ar->regiondata;
+                       // View3D *v3d= curarea->spacedata.first;
+                       // RegionView3D *rv3d= ar->regiondata;
                        
                        /* set current area 
                         *      - must verify that region data is 3D-view (and not something else)
index 3d525b08ecc11e8c4a09fe7852c9f18714a71eec..474259fe0e39850386a7648020e6a66b301d1202 100644 (file)
@@ -187,43 +187,60 @@ Buffer *BGL_MakeBuffer(int type, int ndimensions, int *dimensions, void *initbuf
 #define MAX_DIMENSIONS 256
 static PyObject *Method_Buffer (PyObject *UNUSED(self), PyObject *args)
 {
-       PyObject *length_ob= NULL, *template= NULL;
+       PyObject *length_ob= NULL, *init= NULL;
        Buffer *buffer;
        int dimensions[MAX_DIMENSIONS];
        
        int i, type;
        int ndimensions = 0;
        
-       if (!PyArg_ParseTuple(args, "iO|O", &type, &length_ob, &template)) {
+       if (!PyArg_ParseTuple(args, "iO|O", &type, &length_ob, &init)) {
                PyErr_SetString(PyExc_AttributeError, "expected an int and one or two PyObjects");
                return NULL;
        }
-       if (type!=GL_BYTE && type!=GL_SHORT && type!=GL_INT && type!=GL_FLOAT && type!=GL_DOUBLE) {
+       if (!ELEM5(type, GL_BYTE, GL_SHORT, GL_INT, GL_FLOAT, GL_DOUBLE)) {
                PyErr_SetString(PyExc_AttributeError, "invalid first argument type, should be one of GL_BYTE, GL_SHORT, GL_INT, GL_FLOAT or GL_DOUBLE");
                return NULL;
        }
 
-       if (PyNumber_Check(length_ob)) {
+       if (PyLong_Check(length_ob)) {
                ndimensions= 1;
-               dimensions[0]= PyLong_AsLong(length_ob);
-       } else if (PySequence_Check(length_ob)) {
+               if(((dimensions[0]= PyLong_AsLong(length_ob)) < 1)) {
+                       PyErr_SetString(PyExc_AttributeError, "dimensions must be between 1 and "STRINGIFY(MAX_DIMENSIONS));
+                       return NULL;
+               }
+       }
+       else if (PySequence_Check(length_ob)) {
                ndimensions= PySequence_Size(length_ob);
                if (ndimensions > MAX_DIMENSIONS) {
-                       PyErr_SetString(PyExc_AttributeError, "too many dimensions, max is 256");
+                       PyErr_SetString(PyExc_AttributeError, "too many dimensions, max is "STRINGIFY(MAX_DIMENSIONS));
+                       return NULL;
+               }
+               else if (ndimensions < 1) {
+                       PyErr_SetString(PyExc_AttributeError, "sequence must have at least one dimension");
                        return NULL;
                }
                for (i=0; i<ndimensions; i++) {
                        PyObject *ob= PySequence_GetItem(length_ob, i);
 
-                       if (!PyNumber_Check(ob)) dimensions[i]= 1;
+                       if (!PyLong_Check(ob)) dimensions[i]= 1;
                        else dimensions[i]= PyLong_AsLong(ob);
                        Py_DECREF(ob);
+
+                       if(dimensions[i] < 1) {
+                               PyErr_SetString(PyExc_AttributeError, "dimensions must be between 1 and "STRINGIFY(MAX_DIMENSIONS));
+                               return NULL;
+                       }
                }
        }
+       else {
+               PyErr_Format(PyExc_TypeError, "invalid second argument argument expected a sequence or an int, not a %.200s", Py_TYPE(length_ob)->tp_name);
+               return NULL;
+       }
        
        buffer= BGL_MakeBuffer(type, ndimensions, dimensions, NULL);
-       if (template && ndimensions) {
-               if (Buffer_ass_slice((PyObject *) buffer, 0, dimensions[0], template)) {
+       if (init && ndimensions) {
+               if (Buffer_ass_slice((PyObject *) buffer, 0, dimensions[0], init)) {
                        Py_DECREF(buffer);
                        return NULL;
                }