Cleanup.

Sat, 20 Dec 2008 23:53:07 +0000

author
nrich@ii.net
date
Sat, 20 Dec 2008 23:53:07 +0000
changeset 25
2cc3feba9277
parent 24
abb9499bef51
child 26
cf847efefdb5

Cleanup.

dbd/db2/dbd_db2.h 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/dbd_db2.h	Sat Dec 20 11:17:37 2008 +0000
+++ b/dbd/db2/dbd_db2.h	Sat Dec 20 23:53:07 2008 +0000
@@ -41,6 +41,7 @@
 typedef struct _statement {
     resultset_t * resultset;
     bindparams_t * bind;
+    unsigned char *buffer;
     SQLSMALLINT num_result_columns; /* variable for SQLNumResultCols */
 
     SQLHANDLE stmt;
--- a/dbd/db2/statement.c	Sat Dec 20 11:17:37 2008 +0000
+++ b/dbd/db2/statement.c	Sat Dec 20 23:53:07 2008 +0000
@@ -1,5 +1,7 @@
 #include "dbd_db2.h"
 
+#define BIND_BUFFER_SIZE    1024
+
 static lua_push_type_t db2_to_lua_push(unsigned int db2_type, int len) {
     lua_push_type_t lua_type;
 
@@ -80,7 +82,7 @@
     int errflag = 0;
     const char *errstr = NULL;
     SQLRETURN rc = SQL_SUCCESS;
-    unsigned char b[1024];
+    unsigned char b[BIND_BUFFER_SIZE];
     unsigned char *buffer = &b[0];
     int offset = 0;
     resultset_t *resultset = NULL; 
@@ -118,6 +120,10 @@
 	return 2;
     }
 
+    if (num_params > (BIND_BUFFER_SIZE/sizeof(double))) {
+        luaL_error(L, "Too many bind params: %d", num_params);
+    }
+
     for (p = 2; p <= n; p++) {
 	int i = p - 1;
 	int type = lua_type(L, p);
--- a/dbd/mysql/statement.c	Sat Dec 20 11:17:37 2008 +0000
+++ b/dbd/mysql/statement.c	Sat Dec 20 23:53:07 2008 +0000
@@ -70,8 +70,7 @@
     int num_bind_params = n - 1;
     int expected_params;
 
-    unsigned char b[1024];
-    unsigned char *buffer = &b[0];
+    unsigned char *buffer = NULL;
     int offset = 0;
     
     MYSQL_BIND *bind = NULL;
@@ -100,14 +99,16 @@
 	return 2;
     }
 
-    bind = malloc(sizeof(MYSQL_BIND) * num_bind_params);
-    
-    if (bind == NULL) {
-        luaL_error(L, "Could not alloc bind params\n");
+    if (num_bind_params) {
+        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));
+        memset(bind, 0, sizeof(MYSQL_BIND) * num_bind_params);
     }
 
-    memset(bind, 0, sizeof(MYSQL_BIND) * num_bind_params);
-
     for (p = 2; p <= n; p++) {
 	int type = lua_type(L, p);
 	int i = p - 2;
@@ -190,6 +191,10 @@
 	free(bind);
     }
 
+    if (buffer) {
+        free(buffer);
+    }
+
     if (error_message) {
 	lua_pushboolean(L, 0);
 	lua_pushfstring(L, error_message, errstr ? errstr : mysql_stmt_error(statement->stmt));

mercurial