Commits

Lenard Lindstrom  committed 91ac7a6

Fix some CPyChecker warnings for pixelarray (toward issue #171)

- _pxarray_get_dict in pixelarray.c: try to clear false memory leak warning for
self->dict. Did not succeed, but made code a little cleaner.
- _extract_color and _compare in pixelarray_methods.c: Fix memory leak involving
new_surface.
- _get_color_from_object in pixelarray_methods.c: Remove spurious warning about
uninitialized rgba.
- _get_single_pixel in pixelarray_methods.c: remove warning about uninitalized
pixel. This cleans up the switch statement.

  • Participants
  • Parent commits 9e222e1

Comments (0)

Files changed (2)

File src/pixelarray.c

 static PyObject *
 _pxarray_get_dict(PyPixelArray *self, void *closure)
 {
-    if (!self->dict) {
-        self->dict = PyDict_New();
-        if (!self->dict) {
+    PyObject *dict = self->dict;
+
+    if (!dict) {
+        dict = PyDict_New();
+        if (!dict) {
             return 0;
         }
+        self->dict = dict;
     }
-
-    Py_INCREF(self->dict);
-    return self->dict;
+    Py_INCREF(dict);
+    return dict;
 }
 
 /**

File src/pixelarray_methods.c

 static int
 _get_color_from_object(PyObject *val, SDL_PixelFormat *format, Uint32 *color)
 {
-    Uint8 rgba[4];
+    Uint8 rgba[] = {0, 0, 0, 0};
 
     if (!val) {
         return 0;
 
     /* Find the start of the pixel */
     switch (bpp) {
-        /* This switch statement is exhaustive over all surface pixel sizes */
 
     case 1:
         pixel = (Uint32)*pixel_p;
                  ((Uint32)pixel_p[0] << 16));
 #endif
         break;
-    case 4:
+    default:  /* 4 */
+        assert(bpp == 4);
         pixel = *((Uint32 *)pixel_p);
-        break;
-
-#ifndef NDEBUG
-        /* Assert that bpp is valid */
-    default:
-        /* Should not get here */
-        PyErr_Format(PyExc_SystemError,
-                     "Pygame internal error in _get_single_pixel: "
-                     "unexpected pixel size %i", bpp);
-        return 0;
-#endif
     }
 
     return PyInt_FromLong((long)pixel);
     }
 
     new_array = (PyPixelArray *)PyPixelArray_New(surface);
+    Py_DECREF(surface);
     if (!new_array) {
-        Py_DECREF(surface);
         return 0;
     }
 
     }
 
     new_array = (PyPixelArray *)PyPixelArray_New(new_surface);
+    Py_DECREF(new_surface);
     if (!new_array) {
-        Py_DECREF(new_surface);
         return 0;
     }