ext/aubioproxy.c: improve sizes checks, cast to uint_t
authorPaul Brossier <piem@piem.org>
Tue, 10 Dec 2013 22:58:59 +0000 (17:58 -0500)
committerPaul Brossier <piem@piem.org>
Tue, 10 Dec 2013 22:58:59 +0000 (17:58 -0500)
Signed-off-by: Paul Brossier <piem@piem.org>
python/ext/aubioproxy.c

index 9b7f917..d3c0094 100644 (file)
@@ -35,7 +35,13 @@ PyAubio_ArrayToCFvec (PyObject *input) {
     // vec = new_fvec (vec->length);
     // no need to really allocate fvec, just its struct member
     vec = (fvec_t *)malloc(sizeof(fvec_t));
-    vec->length = PyArray_SIZE ((PyArrayObject *)array);
+    long length = PyArray_SIZE ((PyArrayObject *)array);
+    if (length > 0) {
+      vec->length = (uint_t)length;
+    } else {
+      PyErr_SetString (PyExc_ValueError, "input array size should be greater than 0");
+      goto fail;
+    }
     vec->data = (smpl_t *) PyArray_GETPTR1 ((PyArrayObject *)array, 0);
 
   } else if (PyObject_TypeCheck (input, &PyList_Type)) {
@@ -130,8 +136,20 @@ PyAubio_ArrayToCFmat (PyObject *input) {
 
     // no need to really allocate fvec, just its struct member
     mat = (fmat_t *)malloc(sizeof(fmat_t));
-    mat->length = PyArray_DIM ((PyArrayObject *)array, 1);
-    mat->height = PyArray_DIM ((PyArrayObject *)array, 0);
+    long length = PyArray_DIM ((PyArrayObject *)array, 1);
+    if (length > 0) {
+      mat->length = (uint_t)length;
+    } else {
+      PyErr_SetString (PyExc_ValueError, "input array dimension 1 should be greater than 0");
+      goto fail;
+    }
+    long height = PyArray_DIM ((PyArrayObject *)array, 0);
+    if (height > 0) {
+      mat->height = (uint_t)height;
+    } else {
+      PyErr_SetString (PyExc_ValueError, "input array dimension 0 should be greater than 0");
+      goto fail;
+    }
     mat->data = (smpl_t **)malloc(sizeof(smpl_t*) * mat->height);
     for (i=0; i< mat->height; i++) {
       mat->data[i] = (smpl_t*)PyArray_GETPTR1 ((PyArrayObject *)array, i);