Date: Thursday, December 8, 2005 @ 09:04:57
  Author: zsolt
    Path: /cvsroot/carob/libmysequoia

Modified: include/CarobMySQL.hpp (1.5 -> 1.6) include/Utils.hpp (1.4 ->
          1.5) src/CarobMySQL.cpp (1.7 -> 1.8) src/Utils.cpp (1.4 -> 1.5)

Small memory problem fix when releasing and reallocating a character string.


------------------------+
 include/CarobMySQL.hpp |    3 ++-
 include/Utils.hpp      |   21 ++++++++++++++++++++-
 src/CarobMySQL.cpp     |   31 +++++++++++++++++--------------
 src/Utils.cpp          |   15 +++++++++++++++
 4 files changed, 54 insertions(+), 16 deletions(-)


Index: libmysequoia/include/CarobMySQL.hpp
diff -u libmysequoia/include/CarobMySQL.hpp:1.5 
libmysequoia/include/CarobMySQL.hpp:1.6
--- libmysequoia/include/CarobMySQL.hpp:1.5     Wed Dec  7 11:00:24 2005
+++ libmysequoia/include/CarobMySQL.hpp Thu Dec  8 09:04:57 2005
@@ -206,8 +206,9 @@
             
   /**
    * Delete and free the actual connection and it's associated resources
+   * @param free_mysql defines if the internal mysql structure is destroyed
    */
-  void delete_connection();
+  void delete_connection(bool free_mysql);
   
   /**
    * Set the current mysql error to the specified one. The mysql error message 
will be filled from the error list.
Index: libmysequoia/include/Utils.hpp
diff -u libmysequoia/include/Utils.hpp:1.4 libmysequoia/include/Utils.hpp:1.5
--- libmysequoia/include/Utils.hpp:1.4  Wed Dec  7 11:25:24 2005
+++ libmysequoia/include/Utils.hpp      Thu Dec  8 09:04:57 2005
@@ -34,7 +34,7 @@
 #define FREE_AND_NULL(p) if (p) {delete p; p=0;}
 
 /**
- * Creates a duplicate of the null terminated string and return the pointer to 
it.
+ * Creates a duplicate of a null terminated string and return the pointer to 
it.
  * @param src pointer to the source string
  * @return a pointer to the duplicate string
  */
@@ -49,6 +49,25 @@
 char *cstrdup(const char *src, int len);
 
 /**
+ * Creates a duplicate of a null terminated string only if the destination
+ * does not contain the same string and return the pointer to it.
+ * @param dest pointer to the destination string. Can contain null character.
+ * @param src pointer to the source string. Can contain null character.
+ * @return a pointer to the duplicate string
+ */
+char *cstrdupcond(char * &dest, const char *src);
+
+/**
+ * Creates a duplicate of the string of given length only if the destination
+ * does not contain the same string and return the pointer to it.
+ * @param dest pointer to the destination string. Can contain null character.
+ * @param src pointer to the source string. Can contain null character.
+ * @param len the length of the string
+ * @return a pointer to the duplicate string
+ */
+char *cstrdupcond(char * &dest, const char *src, int len);
+
+/**
  * Returns a client error message associated with the error code.
  * @param code MySQL client error code
  * @return a the detailed error message or null if the error code was not a 
client error
Index: libmysequoia/src/CarobMySQL.cpp
diff -u libmysequoia/src/CarobMySQL.cpp:1.7 libmysequoia/src/CarobMySQL.cpp:1.8
--- libmysequoia/src/CarobMySQL.cpp:1.7 Wed Dec  7 11:25:24 2005
+++ libmysequoia/src/CarobMySQL.cpp     Thu Dec  8 09:04:57 2005
@@ -52,7 +52,7 @@
 {
   if (mysqlPtr) delete mysqlPtr;
   
-  delete_connection();
+  delete_connection(true);
 }
 
 bool
@@ -82,7 +82,7 @@
    * The real connection will happen after the user call the command 
mysql_select_db or mysql_real_connect */
   if (!db || !*db)
   {
-    delete_connection();
+    delete_connection(false);
     set_connect_info(host, user, passwd, 0, port ? port : 25322);
     
     return true;
@@ -97,7 +97,7 @@
     if (newConnectionPtr)
     {
       //TODO handle not enough memory
-      delete_connection();
+      delete_connection(false);
       set_connect_info(host, user, passwd, db, port ? port : 25322);
 
       connectionPtr = newConnectionPtr;
@@ -215,26 +215,29 @@
 CarobMYSQL::set_connect_info(const char *host, const char *user,
             const char *passwd, const char *db, unsigned int port)
 {
-  mysqlPtr->my.host = cstrdup(host);
-  mysqlPtr->my.user = cstrdup(user);
-  mysqlPtr->my.passwd = cstrdup(passwd);
-  mysqlPtr->my.db = cstrdup(db);
+  cstrdupcond(mysqlPtr->my.host, host);
+  cstrdupcond(mysqlPtr->my.user, user);
+  cstrdupcond(mysqlPtr->my.passwd, passwd);
+  cstrdupcond(mysqlPtr->my.db, db);
   mysqlPtr->my.port = port;
 
   //Warning buffer overflow error ...
   char buf[101];
   sprintf(buf, "%s via TCP/IP", host);
-  mysqlPtr->my.host_info = cstrdup(buf);
+  cstrdupcond(mysqlPtr->my.host_info, buf);
 }
 
 void
-CarobMYSQL::delete_connection()
+CarobMYSQL::delete_connection(bool free_mysql)
 {
-  FREE_AND_NULL(mysqlPtr->my.host);
-  FREE_AND_NULL(mysqlPtr->my.user);
-  FREE_AND_NULL(mysqlPtr->my.passwd);
-  FREE_AND_NULL(mysqlPtr->my.db);
-  FREE_AND_NULL(mysqlPtr->my.host_info);
+  if (free_mysql)
+  {
+    FREE_AND_NULL(mysqlPtr->my.host);
+    FREE_AND_NULL(mysqlPtr->my.user);
+    FREE_AND_NULL(mysqlPtr->my.passwd);
+    FREE_AND_NULL(mysqlPtr->my.db);
+    FREE_AND_NULL(mysqlPtr->my.host_info);
+  }
   
   FREE_AND_NULL(connectionPtr);
   FREE_AND_NULL(stmtPtr);
Index: libmysequoia/src/Utils.cpp
diff -u libmysequoia/src/Utils.cpp:1.4 libmysequoia/src/Utils.cpp:1.5
--- libmysequoia/src/Utils.cpp:1.4      Wed Dec  7 11:25:24 2005
+++ libmysequoia/src/Utils.cpp  Thu Dec  8 09:04:57 2005
@@ -108,6 +108,21 @@
     return 0;
 }
 
+char *cstrdupcond(char * &dest, const char *src)
+{
+  return src ? cstrdupcond(dest, src, strlen(src)) : 0;
+}
+
+char *cstrdupcond(char * &dest, const char *src, int len)
+{
+  if (dest != src)
+  {
+    delete dest;
+    dest = cstrdup(src, len);
+  }
+  return dest;
+}
+
 const char *get_error_msg(const int code)
 {
   if (code >= CR_MIN_ERROR && code <= CR_MAX_ERROR)

_______________________________________________
Carob-commits mailing list
[email protected]
https://forge.continuent.org/mailman/listinfo/carob-commits

Reply via email to