[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]

[pygame] Patch to compile with MSVC7.1, bug fix to set_blocked(), adds event range-checking



Hi,

just did a check-out and had trouble compiling with the Microsoft Visual
C++ Toolkit 2003 compiler (version 7.1).

Mainly variables not declared at the top of a block, and a couple of
casts in 'transform.c'.

Also found a minor bug in pygame.event.set_blocked() when type is None,
in 'event.c'.

Whilst I was in there (and looking at the SDL code) I noticed that there
is no range checking on the event values in Pygame or SDL. To prevent
inadvertent trampling of memory, I've added some range checking code
which raises a ValueError().

Patch against latest SVN attached.

Hope it's useful,

regards,
John.
PS the reformatted code looks much better :-)

Index: src/transform.c
===================================================================
--- src/transform.c	(revision 1052)
+++ src/transform.c	(working copy)
@@ -987,13 +987,13 @@
             }
             else
             {
+                int xfrac = 0x10000 - xcounter;
                 /* write out a destination pixel */
                 *dstpix++ = (Uint8) (((accumulate[0] + ((srcpix[0] * xcounter) >> 16)) * xrecip) >> 16);
                 *dstpix++ = (Uint8) (((accumulate[1] + ((srcpix[1] * xcounter) >> 16)) * xrecip) >> 16);
                 *dstpix++ = (Uint8) (((accumulate[2] + ((srcpix[2] * xcounter) >> 16)) * xrecip) >> 16);
                 *dstpix++ = (Uint8) (((accumulate[3] + ((srcpix[3] * xcounter) >> 16)) * xrecip) >> 16);
                 /* reload the accumulator with the remainder of this pixel */
-                int xfrac = 0x10000 - xcounter;
                 accumulate[0] = (Uint16) ((*srcpix++ * xfrac) >> 16);
                 accumulate[1] = (Uint16) ((*srcpix++ * xfrac) >> 16);
                 accumulate[2] = (Uint16) ((*srcpix++ * xfrac) >> 16);
@@ -1180,15 +1180,15 @@
     int srcdiff = srcpitch - (width * 4);
     int dstdiff = dstpitch - (width * 4);
     int x, y;
+    int yspace = 0x10000 * srcheight / dstheight; /* must be > 1 */
+    int yrecip = (int) ((long long) 0x100000000 / yspace);
+    int ycounter = yspace;
 
     /* allocate and clear a memory area for storing the accumulator line */
     templine = (Uint16 *) malloc(dstpitch * 2);
     if (templine == NULL) return;
     memset(templine, 0, dstpitch * 2);
 
-    int yspace = 0x10000 * srcheight / dstheight; /* must be > 1 */
-    int yrecip = (int) ((long long) 0x100000000 / yspace);
-    int ycounter = yspace;
     for (y = 0; y < srcheight; y++)
     {
         Uint16 *accumulate = templine;
@@ -1205,6 +1205,7 @@
         }
         else
         {
+            int yfrac = 0x10000 - ycounter;
             /* write out a destination line */
             for (x = 0; x < width; x++)
             {
@@ -1217,7 +1218,6 @@
             /* reload the accumulator with the remainder of this line */
             accumulate = templine;
             srcpix -= 4 * width;
-            int yfrac = 0x10000 - ycounter;
             for (x = 0; x < width; x++)
             {
                 *accumulate++ = (Uint16) ((*srcpix++ * yfrac) >> 16);
@@ -1241,15 +1241,15 @@
     int srcdiff = srcpitch - (width * 4);
     int dstdiff = dstpitch - (width * 4);
     int x, y;
+    int yspace = 0x4000 * srcheight / dstheight; /* must be > 1 */
+    int yrecip = (int) ((long long) 0x040000000 / yspace);
+    long long One64 = 0x4000400040004000ULL;
 
     /* allocate and clear a memory area for storing the accumulator line */
     templine = (Uint16 *) malloc(dstpitch * 2);
     if (templine == NULL) return;
     memset(templine, 0, dstpitch * 2);
 
-    int yspace = 0x4000 * srcheight / dstheight; /* must be > 1 */
-    int yrecip = (int) ((long long) 0x040000000 / yspace);
-    long long One64 = 0x4000400040004000ULL;
 #if defined(__GNUC__) && defined(__x86_64__)
     long long srcdiff64 = srcdiff;
     long long dstdiff64 = dstdiff;
@@ -1439,11 +1439,11 @@
     int dstdiff = dstpitch - (dstwidth * 4);
     int *xidx0, *xmult0, *xmult1;
     int x, y;
+    int factorwidth = 4;
 
     /* Allocate memory for factors */
     xidx0 = malloc(dstwidth * 4);
     if (xidx0 == NULL) return;
-    int factorwidth = 4;
     xmult0 = (int *) malloc(dstwidth * factorwidth);
     xmult1 = (int *) malloc(dstwidth * factorwidth);
     if (xmult0 == NULL || xmult1 == NULL)
@@ -1490,11 +1490,11 @@
     int dstdiff = dstpitch - (dstwidth * 4);
     int *xidx0, *xmult0, *xmult1;
     int x, y;
+    int factorwidth = 8;
 
     /* Allocate memory for factors */
     xidx0 = malloc(dstwidth * 4);
     if (xidx0 == NULL) return;
-    int factorwidth = 8;
     xmult0 = (int *) malloc(dstwidth * factorwidth);
     xmult1 = (int *) malloc(dstwidth * factorwidth);
     if (xmult0 == NULL || xmult1 == NULL)
@@ -1507,9 +1507,9 @@
     /* Create multiplier factors and starting indices and put them in arrays */
     for (x = 0; x < dstwidth; x++)
     {
-        xidx0[x] = x * (srcwidth - 1) / dstwidth;
         int xm1 = 0x100 * ((x * (srcwidth - 1)) % dstwidth) / dstwidth;
         int xm0 = 0x100 - xm1;
+        xidx0[x] = x * (srcwidth - 1) / dstwidth;
         xmult1[x*2]   = xm1 | (xm1 << 16);
         xmult1[x*2+1] = xm1 | (xm1 << 16);
         xmult0[x*2]   = xm0 | (xm0 << 16);
@@ -1751,6 +1751,9 @@
 
     int bpp = src->format->BytesPerPixel;
 
+    Uint8 *temppix = NULL;
+    int tempwidth=0, temppitch=0, tempheight=0;
+
     /* convert to 32-bit if necessary */
     if (bpp == 3)
     {
@@ -1772,8 +1775,6 @@
     }
 
     /* Create a temporary processing buffer if we will be scaling both X and Y */
-    Uint8 *temppix = NULL;
-    int tempwidth=0, temppitch=0, tempheight=0;
     if (srcwidth != dstwidth && srcheight != dstheight)
     {
         tempwidth = dstwidth;
@@ -1926,8 +1927,8 @@
         if (surf->w == width && surf->h == height) {
             int y;
             for (y = 0; y < height; y++) {
-                memcpy(newsurf->pixels + y * newsurf->pitch, 
-                       surf->pixels + y * surf->pitch, width * bpp);
+                memcpy((Uint8*)newsurf->pixels + y * newsurf->pitch, 
+                       (Uint8*)surf->pixels + y * surf->pitch, width * bpp);
             }
         }
         else {
Index: src/event.c
===================================================================
--- src/event.c	(revision 1052)
+++ src/event.c	(working copy)
@@ -784,6 +784,12 @@
     Py_RETURN_NONE;
 }
 
+static int
+CheckEventInRange(int evt)
+{
+    return evt >= 0 && evt < SDL_NUMEVENTS;
+}
+
 static PyObject*
 set_allowed (PyObject* self, PyObject* args)
 {
@@ -805,13 +811,19 @@
             if (!IntFromObjIndex (type, loop, &val))
                 return RAISE (PyExc_TypeError,
                               "type sequence must contain valid event types");
+            if(!CheckEventInRange(val))
+                return RAISE (PyExc_ValueError, "Invalid event in sequence");
             SDL_EventState ((Uint8)val, SDL_ENABLE);
         }
     }
     else if (type == Py_None)
         SDL_EventState ((Uint8)0xFF, SDL_IGNORE);
     else if (IntFromObj (type, &val))
+    {
+        if(!CheckEventInRange (val))
+            return RAISE (PyExc_ValueError, "Invalid event");
         SDL_EventState ((Uint8)val, SDL_ENABLE);
+    }
     else
         return RAISE (PyExc_TypeError, "type must be numeric or a sequence");
 
@@ -839,13 +851,19 @@
             if (!IntFromObjIndex (type, loop, &val))
                 return RAISE (PyExc_TypeError,
                               "type sequence must contain valid event types");
+            if(!CheckEventInRange(val))
+                return RAISE (PyExc_ValueError, "Invalid event in sequence");
             SDL_EventState ((Uint8)val, SDL_IGNORE);
         }
     }
     else if (type == Py_None)
-        SDL_EventState ((Uint8)0, SDL_IGNORE);
+        SDL_EventState ((Uint8)0xFF, SDL_IGNORE);
     else if (IntFromObj (type, &val))
+    {
+        if(!CheckEventInRange (val))
+            return RAISE (PyExc_ValueError, "Invalid event");
         SDL_EventState ((Uint8)val, SDL_IGNORE);
+    }
     else
         return RAISE (PyExc_TypeError, "type must be numeric or a sequence");
 
@@ -874,11 +892,17 @@
             if (!IntFromObjIndex (type, loop, &val))
                 return RAISE (PyExc_TypeError,
                               "type sequence must contain valid event types");
+            if(!CheckEventInRange(val))
+                return RAISE (PyExc_ValueError, "Invalid event in sequence");
             isblocked |= SDL_EventState ((Uint8)val, SDL_QUERY) == SDL_IGNORE;
         }
     }
     else if (IntFromObj (type, &val))
+    {
+        if(!CheckEventInRange (val))
+            return RAISE (PyExc_ValueError, "Invalid event");
         isblocked = SDL_EventState ((Uint8)val, SDL_QUERY) == SDL_IGNORE;
+    }
     else
         return RAISE (PyExc_TypeError, "type must be numeric or a sequence");