hi,

I've found some leaks in the sqlite3 driver. As the code in question is
difficult to understand and easy to break (for me), I'd like to request
a review before I go on and commit it.

I've fixed two leaks:

 * find_result_field_types() leaks away the strduped statement_copy in
some cases.
 * in the same function, elements of the tables[] array are not freed. I
moved the getTables() function invocation to the block where its results
were actually used.

I was trying to run the testsuite this time, but without much success.
As it seems the "tests" subdirectory is not even referenced in the root
Makefile and neither from the configure script. So no Makefile is
generated for it.

Trying to hand compile it resulted in this compilation error:

cc -I/home/bazsi/zwa/install/syslog-ng-pe-3.0/include    test_dbi.c   -o 
test_dbi
test_dbi.c: In function ‘main’:
test_dbi.c:21: error: ‘DBI_DRIVER_DIR’ undeclared (first use in this function)
test_dbi.c:21: error: (Each undeclared identifier is reported only once
test_dbi.c:21: error: for each function it appears in.)
test_dbi.c:23: warning: incompatible implicit declaration of built-in function 
‘strncpy’
test_dbi.c:24: warning: incompatible implicit declaration of built-in function 
‘strlen’
test_dbi.c:26: warning: ‘dbi_initialize’ is deprecated (declared at 
/home/bazsi/zwa/install/syslog-ng-pe-3.0/include/dbi/dbi.h:164)
test_dbi.c:30: warning: ‘dbi_shutdown’ is deprecated (declared at 
/home/bazsi/zwa/install/syslog-ng-pe-3.0/include/dbi/dbi.h:166)
test_dbi.c:35: warning: ‘dbi_shutdown’ is deprecated (declared at 
/home/bazsi/zwa/install/syslog-ng-pe-3.0/include/dbi/dbi.h:166)
test_dbi.c:41: warning: ‘dbi_driver_list’ is deprecated (declared at 
/home/bazsi/zwa/install/syslog-ng-pe-3.0/include/dbi/dbi.h:172)
test_dbi.c:73: warning: ‘dbi_conn_new’ is deprecated (declared at 
/home/bazsi/zwa/install/syslog-ng-pe-3.0/include/dbi/dbi.h:193)
test_dbi.c:75: warning: ‘dbi_shutdown’ is deprecated (declared at 
/home/bazsi/zwa/install/syslog-ng-pe-3.0/include/dbi/dbi.h:166)
test_dbi.c:98: warning: ‘dbi_shutdown’ is deprecated (declared at 
/home/bazsi/zwa/install/syslog-ng-pe-3.0/include/dbi/dbi.h:166)
test_dbi.c:108: warning: ‘dbi_shutdown’ is deprecated (declared at 
/home/bazsi/zwa/install/syslog-ng-pe-3.0/include/dbi/dbi.h:166)
test_dbi.c:123: warning: ‘dbi_shutdown’ is deprecated (declared at 
/home/bazsi/zwa/install/syslog-ng-pe-3.0/include/dbi/dbi.h:166)

This macro is not defined in any of the dbi header files. This is where I gave 
up.


-- 
Bazsi
diff --git a/drivers/sqlite3/dbd_sqlite3.c b/drivers/sqlite3/dbd_sqlite3.c
index a7a940d..ffc9606 100644
--- a/drivers/sqlite3/dbd_sqlite3.c
+++ b/drivers/sqlite3/dbd_sqlite3.c
	@@ -718,22 +718,6 @@ int find_result_field_types(char* field, dbi_conn_t *conn, const char* statement
   }
   //printf("table = %s\ncolumn = %s\n",curr_table,curr_field);
 
-  /* If curr_table is empty, this means we have to get the
-     select tables from the statement (it is possible there is more than one),
-     otherwise we have the table for this field.
-     It would seem that even if the table is aliased in the statement,
-     we still have the original table name.
-     sqlite3_get_table returns the tablename and not the alias when returning table.column.
-     It probably isn't a good idea to rely on this, but we will. */
-
-  if ( strlen(curr_table) < 1 ) {
-    //printf("not curr_table\n");
-    table_count = getTables(tables,0,statement_copy);
-    //printf("*********TABLELIST************\n");
-    //		for ( counter = 0 ; counter < table_count ; counter++) {
-    //			printf("%s\n",tables[counter]);
-    //		}
-  }
 
   // resolve our curr_field to a real column
   char* token;
@@ -961,6 +945,7 @@ int find_result_field_types(char* field, dbi_conn_t *conn, const char* statement
     }
     token = strtok_r(NULL, " ,;", &saveptr);
   }
+  free(statement_copy);
   //printf("table = %s\ncolumn = %s\n",curr_table,curr_field);
 
   /* now we have to look for the field type in the curr_table
@@ -1008,6 +993,25 @@ int find_result_field_types(char* field, dbi_conn_t *conn, const char* statement
      * The reasoning here is that fields with the same name will
      * probably be the same type.  Obviously, this is a hole.
      */
+
+    /* If curr_table is empty, this means we have to get the
+       select tables from the statement (it is possible there is more than one),
+       otherwise we have the table for this field.
+       It would seem that even if the table is aliased in the statement,
+       we still have the original table name.
+       sqlite3_get_table returns the tablename and not the alias when returning table.column.
+       It probably isn't a good idea to rely on this, but we will. */
+
+    if ( strlen(curr_table) < 1 ) {
+      //printf("not curr_table\n");
+      table_count = getTables(tables,0,statement);
+      //printf("*********TABLELIST************\n");
+      //		for ( counter = 0 ; counter < table_count ; counter++) {
+      //			printf("%s\n",tables[counter]);
+      //		}
+    }
+     
+     
     if ( table_count > 0 ) {
 
       for ( counter = 0 ; counter < table_count ; counter++ ) {
@@ -1039,6 +1043,10 @@ int find_result_field_types(char* field, dbi_conn_t *conn, const char* statement
 	if ( curr_type )
 	  break;
       }
+      
+      for ( counter = 0 ; counter < table_count ; counter++ ) {
+        free(tables[counter]);
+      }
       if (!curr_type) {
 	/* the field was not found in any of the tables!
 	 * fallback to string
-------------------------------------------------------------------------
This SF.Net email is sponsored by the Moblin Your Move Developer's challenge
Build the coolest Linux based applications with Moblin SDK & win great prizes
Grand prize is a trip for two to an Open Source event anywhere in the world
http://moblin-contest.org/redirect.php?banner_id=100&url=/
_______________________________________________
Libdbi-drivers-devel mailing list
Libdbi-drivers-devel@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/libdbi-drivers-devel

Reply via email to