Handling malloc failure

Heikki Linnakangas <[email protected]> Mon, 24 Mar 2014 21:37:06 +0200
Newsgroups gmane.comp.db.unixodbc.devel
Organization VMware
Message-ID <[email protected]>
This is a multi-part message in MIME format.
--------------080701020404030904070002
Content-Type: text/plain; charset=ISO-8859-1; format=flowed
Content-Transfer-Encoding: 7bit

Hi,

Many of the functions in __handles.c don't handle a NULL result from 
malloc/calloc properly. There are if-checks for it, but then they go 
ahead and reference the NULL pointer anyway. See attached patch.

- Heikki

--------------080701020404030904070002
Content-Type: text/x-diff;
 name="fix-allochandle-oom-handling-1.patch"
Content-Transfer-Encoding: 7bit
Content-Disposition: attachment;
	filename="fix-allochandle-oom-handling-1.patch"

Index: DriverManager/__handles.c
===================================================================
--- DriverManager/__handles.c	(revision 96)
+++ DriverManager/__handles.c	(working copy)
@@ -461,11 +461,11 @@
                     LOG_INFO,
                     LOG_INFO, environment -> msg );
         }
+
+        setup_error_head( &environment -> error, environment,
+                SQL_HANDLE_ENV );
     }
 
-    setup_error_head( &environment -> error, environment,
-            SQL_HANDLE_ENV );
-
     mutex_exit( &mutex_lists );
 
     return environment;
@@ -599,27 +599,27 @@
         connection -> next_class_list = connection_root;
         connection_root = connection;
         connection -> type = HDBC_MAGIC;
-    }
 
-    setup_error_head( &connection -> error, connection,
-            SQL_HANDLE_DBC );
+        setup_error_head( &connection -> error, connection,
+                SQL_HANDLE_DBC );
 
 #ifdef HAVE_LIBPTH
-    pth_mutex_init( &connection -> mutex );
-    /*
-     * for the moment protect on a environment level
-     */
-    connection -> protection_level = TS_LEVEL3;
+        pth_mutex_init( &connection -> mutex );
+        /*
+         * for the moment protect on a environment level
+         */
+        connection -> protection_level = TS_LEVEL3;
 #elif HAVE_LIBPTHREAD
-    pthread_mutex_init( &connection -> mutex, NULL );
-    /*
-     * for the moment protect on a environment level
-     */
-    connection -> protection_level = TS_LEVEL3;
+        pthread_mutex_init( &connection -> mutex, NULL );
+        /*
+         * for the moment protect on a environment level
+         */
+        connection -> protection_level = TS_LEVEL3;
 #elif HAVE_LIBTHREAD
-    mutex_init( &connection -> mutex, USYNC_THREAD, NULL );
-    connection -> protection_level = TS_LEVEL3;
+        mutex_init( &connection -> mutex, USYNC_THREAD, NULL );
+        connection -> protection_level = TS_LEVEL3;
 #endif
+    }
 
     mutex_exit( &mutex_lists );
 
@@ -785,18 +785,18 @@
 #endif    
         statement_root = statement;
         statement -> type = HSTMT_MAGIC;
-    }
 
-    setup_error_head( &statement -> error, statement,
-            SQL_HANDLE_STMT );
+        setup_error_head( &statement -> error, statement,
+                SQL_HANDLE_STMT );
 
 #ifdef HAVE_LIBPTH
-    pth_mutex_init( &statement -> mutex );
+        pth_mutex_init( &statement -> mutex );
 #elif HAVE_LIBPTHREAD
-    pthread_mutex_init( &statement -> mutex, NULL );
+        pthread_mutex_init( &statement -> mutex, NULL );
 #elif HAVE_LIBTHREAD
-    mutex_init( &statement -> mutex, USYNC_THREAD, NULL );
+        mutex_init( &statement -> mutex, USYNC_THREAD, NULL );
 #endif
+    }
 
     mutex_exit( &mutex_lists );
 

--------------080701020404030904070002
Content-Type: text/plain; charset="us-ascii"
MIME-Version: 1.0
Content-Transfer-Encoding: 7bit
Content-Disposition: inline

_______________________________________________
unixODBC-dev mailing list
[email protected]
http://mailman.unixodbc.org/mailman/listinfo/unixodbc-dev

--------------080701020404030904070002--