Bugfix - fix allocation bugs

Mon, 22 Dec 2008 07:36:54 +0000

author
nrich@ii.net
date
Mon, 22 Dec 2008 07:36:54 +0000
changeset 26
cf847efefdb5
parent 25
2cc3feba9277
child 27
1d2369ebee21

Bugfix - fix allocation bugs

dbd/db2/connection.c file | annotate | diff | comparison | revisions
dbd/db2/statement.c file | annotate | diff | comparison | revisions
dbd/mysql/statement.c file | annotate | diff | comparison | revisions
--- a/dbd/db2/connection.c	Sat Dec 20 23:53:07 2008 +0000
+++ b/dbd/db2/connection.c	Mon Dec 22 07:36:54 2008 +0000
@@ -129,10 +129,9 @@
 static int connection_close(lua_State *L) {
     connection_t *conn = (connection_t *)luaL_checkudata(L, 1, DBD_DB2_CONNECTION);
     int disconnect = 0;   
+    SQLRETURN rc = SQL_SUCCESS;
 
     if (conn->db2) {
-	SQLRETURN rc = SQL_SUCCESS;
-
 	rollback(conn);
 
 	/* disconnect from the database */
@@ -141,10 +140,12 @@
 	/* free connection handle */
 	rc = SQLFreeHandle(SQL_HANDLE_DBC, conn->db2);
 
+	conn->db2 = 0;
+    }
+
+    if (conn->env) {
 	/* free environment handle */
 	rc = SQLFreeHandle(SQL_HANDLE_ENV, conn->env);
-
-	conn->db2 = 0;
     }
 
     lua_pushboolean(L, disconnect);
--- a/dbd/db2/statement.c	Sat Dec 20 23:53:07 2008 +0000
+++ b/dbd/db2/statement.c	Mon Dec 22 07:36:54 2008 +0000
@@ -75,15 +75,14 @@
  * success = statement:execute(...)
  */
 static int statement_execute(lua_State *L) {
+    statement_t *statement = (statement_t *)luaL_checkudata(L, 1, DBD_DB2_STATEMENT);
     int n = lua_gettop(L);
-    statement_t *statement = (statement_t *)luaL_checkudata(L, 1, DBD_DB2_STATEMENT);
     int p;
     int i;
     int errflag = 0;
     const char *errstr = NULL;
     SQLRETURN rc = SQL_SUCCESS;
-    unsigned char b[BIND_BUFFER_SIZE];
-    unsigned char *buffer = &b[0];
+    unsigned char *buffer = NULL;
     int offset = 0;
     resultset_t *resultset = NULL; 
     bindparams_t *bind; /* variable to read the results */
@@ -120,8 +119,8 @@
 	return 2;
     }
 
-    if (num_params > (BIND_BUFFER_SIZE/sizeof(double))) {
-        luaL_error(L, "Too many bind params: %d", num_params);
+    if (num_params > 0) {
+        buffer = (unsigned char *)malloc(sizeof(double) * num_params);
     }
 
     for (p = 2; p <= n; p++) {
@@ -140,7 +139,7 @@
 	    errflag = rc != SQL_SUCCESS && rc != SQL_SUCCESS_WITH_INFO;
 	    break;
 	case LUA_TNUMBER:
-	    num = (double *)buffer + offset;
+	    num = (double *)(buffer + offset);
 	    *num = lua_tonumber(L, p);
 	    offset += sizeof(double);
 	    rc = SQLBindParameter(statement->stmt, i, SQL_PARAM_INPUT, SQL_C_DOUBLE, SQL_DECIMAL, 10, 0, (SQLPOINTER)num, 0, NULL);
@@ -152,7 +151,7 @@
 	    errflag = rc != SQL_SUCCESS && rc != SQL_SUCCESS_WITH_INFO;
 	    break;
 	case LUA_TBOOLEAN:
-	    boolean = (int *)buffer + offset;
+	    boolean = (int *)(buffer + offset);
 	    *boolean = lua_toboolean(L, p);
 	    offset += sizeof(int);
 	    rc = SQLBindParameter(statement->stmt, i, SQL_PARAM_INPUT, SQL_C_LONG, SQL_INTEGER, 0, 0, (SQLPOINTER)boolean, len, NULL);
@@ -172,6 +171,9 @@
     }
 
     if (errflag) {
+        if (buffer) 
+            free(buffer);
+
 	lua_pushboolean(L, 0);
 
 	if (errstr) {
@@ -187,6 +189,9 @@
 
     rc = SQLExecute(statement->stmt);
     if (rc != SQL_SUCCESS) {
+        if (buffer) 
+            free(buffer);
+
 	SQLGetDiagRec(SQL_HANDLE_STMT, statement->stmt, 1, sqlstate, &sqlcode, message, SQL_MAX_MESSAGE_LENGTH + 1, &length);
 
 	lua_pushnil(L);
@@ -221,6 +226,9 @@
                         NULL);
 
 	    if (rc != SQL_SUCCESS) {
+                if (buffer) 
+                    free(buffer);
+
 		SQLGetDiagRec(SQL_HANDLE_STMT, statement->stmt, 1, sqlstate, &sqlcode, message, SQL_MAX_MESSAGE_LENGTH + 1, &length);
 
 		lua_pushnil(L);
@@ -243,6 +251,9 @@
                        &bind[i].len);
 
 	    if (rc != SQL_SUCCESS) {
+                if (buffer) 
+                    free(buffer);
+
 		SQLGetDiagRec(SQL_HANDLE_STMT, statement->stmt, 1, sqlstate, &sqlcode, message, SQL_MAX_MESSAGE_LENGTH + 1, &length);
 
 		lua_pushnil(L);
@@ -255,6 +266,9 @@
 	statement->bind = bind;
     }
 
+    if (buffer) 
+        free(buffer);
+
     lua_pushboolean(L, 1);
     return 1;
 }
--- a/dbd/mysql/statement.c	Sat Dec 20 23:53:07 2008 +0000
+++ b/dbd/mysql/statement.c	Mon Dec 22 07:36:54 2008 +0000
@@ -99,13 +99,13 @@
 	return 2;
     }
 
-    if (num_bind_params) {
+    if (num_bind_params > 0) {
         bind = malloc(sizeof(MYSQL_BIND) * num_bind_params);
         if (bind == NULL) {
             luaL_error(L, "Could not alloc bind params\n");
         }
 
-        buffer = malloc(num_bind_params * sizeof(size_t));
+        buffer = (unsigned char *)malloc(num_bind_params * sizeof(double));
         memset(bind, 0, sizeof(MYSQL_BIND) * num_bind_params);
     }
 
@@ -126,7 +126,7 @@
 		break;
 
 	    case LUA_TBOOLEAN:
-		boolean = (int *)buffer + offset;
+		boolean = (int *)(buffer + offset);
 		offset += sizeof(int);
 		*boolean = lua_toboolean(L, p);
 
@@ -141,7 +141,7 @@
 		 * num needs to be it's own 
 		 * memory here
                  */
-		num = (double *)buffer + offset;
+		num = (double *)(buffer + offset);
 		offset += sizeof(double);
 		*num = lua_tonumber(L, p);
 
@@ -152,7 +152,7 @@
 		break;
 
 	    case LUA_TSTRING:
-		str_len = (size_t *)buffer + offset;
+		str_len = (size_t *)(buffer + offset);
 		offset += sizeof(size_t);
 		str = lua_tolstring(L, p, str_len);
 

mercurial