check for every malloc call if memory was allocated successfully
authorDiederick Niehorster <dcnieho@gmail.com>
Sat, 17 Mar 2012 02:21:19 +0000 (02:21 +0000)
committerDiederick Niehorster <dcnieho@gmail.com>
Sat, 17 Mar 2012 02:21:19 +0000 (02:21 +0000)
git-svn-id: svn+ssh://svn.code.sf.net/p/freeglut/code/trunk/freeglut/freeglut@1168 7f0cb862-5218-0410-a997-914c9d46530a

src/fg_geometry.c

index d12b3f6..6146ea4 100644 (file)
@@ -694,6 +694,12 @@ static void fghCube( GLdouble dSize, GLboolean useWireMode )
 
         /* Need to build new vertex list containing vertices for cube of different size */
         GLdouble *vertices = malloc(CUBE_VERT_ELEM_PER_OBJ * sizeof(GLdouble));
+        /* Bail out if memory allocation fails, fgError never returns */
+        if (!(*vertices))
+        {
+            free(*vertices);
+            fgError("Failed to allocate memory in fghCube");
+        }
         for (i=0; i<CUBE_VERT_ELEM_PER_OBJ; i++)
             vertices[i] = dSize*cube_verts[i];
 
@@ -721,6 +727,13 @@ static void fghSierpinskiSponge ( int numLevels, GLdouble offset[3], GLdouble sc
         /* Allocate memory */
         vertices = malloc(numVert*3 * sizeof(GLdouble));
         normals  = malloc(numVert*3 * sizeof(GLdouble));
+        /* Bail out if memory allocation fails, fgError never returns */
+        if (!(*vertices) || !(*normals))
+        {
+            free(*vertices);
+            free(*normals);
+            fgError("Failed to allocate memory in fghSierpinskiSponge");
+        }
 
         /* Generate elements */
         fghSierpinskiSpongeGenerate ( numLevels, offset, scale, vertices, normals );