andrey                                   Mon, 03 May 2010 13:45:58 +0000

Revision: http://svn.php.net/viewvc?view=revision&revision=298901

Log:
Better handing of OOM in mysqlnd::set_client_option

Changed paths:
    U   php/php-src/branches/PHP_5_3/ext/mysqlnd/mysqlnd.c
    U   php/php-src/trunk/ext/mysqlnd/mysqlnd.c

Modified: php/php-src/branches/PHP_5_3/ext/mysqlnd/mysqlnd.c
===================================================================
--- php/php-src/branches/PHP_5_3/ext/mysqlnd/mysqlnd.c  2010-05-03 13:22:06 UTC 
(rev 298900)
+++ php/php-src/branches/PHP_5_3/ext/mysqlnd/mysqlnd.c  2010-05-03 13:45:58 UTC 
(rev 298901)
@@ -1122,6 +1122,7 @@
                                                                           
FALSE, TRUE TSRMLS_CC)) {
                DBG_RETURN(NULL);
        }
+
        /*
           Prepare for the worst case.
           MyISAM goes to 2500 BIT columns, double it for safety.
@@ -1981,20 +1982,26 @@
                        }
                        break;
                case MYSQL_INIT_COMMAND:
+               {
+                       char ** new_init_commands;
+                       char * new_command;
                        DBG_INF("MYSQL_INIT_COMMAND");
                        DBG_INF_FMT("command=%s", value);
                        /* when num_commands is 0, then realloc will be 
effectively a malloc call, internally */
-                       conn->options.init_commands = 
mnd_perealloc(conn->options.init_commands, sizeof(char *) * 
(conn->options.num_commands + 1),
-                                                                               
                                conn->persistent);
-                       if (!conn->options.init_commands) {
+                       /* Don't assign to conn->options.init_commands because 
in case of OOM we will lose the pointer and leak */
+                       new_init_commands = 
mnd_perealloc(conn->options.init_commands, sizeof(char *) * 
(conn->options.num_commands + 1), conn->persistent);
+                       if (!new_init_commands) {
                                goto oom;
                        }
-                       conn->options.init_commands[conn->options.num_commands] 
= mnd_pestrdup(value, conn->persistent);
-                       if 
(!conn->options.init_commands[conn->options.num_commands]) {
+                       conn->options.init_commands = new_init_commands;
+                       new_command = mnd_pestrdup(value, conn->persistent);
+                       if (!new_command) {
                                goto oom;
                        }
+                       conn->options.init_commands[conn->options.num_commands] 
= new_command;
                        ++conn->options.num_commands;
                        break;
+               }
                case MYSQL_READ_DEFAULT_FILE:
                case MYSQL_READ_DEFAULT_GROUP:
 #ifdef WHEN_SUPPORTED_BY_MYSQLI
@@ -2004,17 +2011,19 @@
                        /* currently not supported. Todo!! */
                        break;
                case MYSQL_SET_CHARSET_NAME:
+               {
+                       char * new_charset_name = mnd_pestrdup(value, 
conn->persistent);
                        DBG_INF("MYSQL_SET_CHARSET_NAME");
+                       if (!new_charset_name) {
+                               goto oom;
+                       }
                        if (conn->options.charset_name) {
                                mnd_pefree(conn->options.charset_name, 
conn->persistent);
-                               conn->options.charset_name = NULL;
                        }
-                       conn->options.charset_name = mnd_pestrdup(value, 
conn->persistent);
-                       if (!conn->options.charset_name) {
-                               goto oom;
-                       }
+                       conn->options.charset_name = new_charset_name;
                        DBG_INF_FMT("charset=%s", conn->options.charset_name);
                        break;
+               }
 #ifdef WHEN_SUPPORTED_BY_MYSQLI
                case MYSQL_SET_CHARSET_DIR:
                case MYSQL_OPT_RECONNECT:

Modified: php/php-src/trunk/ext/mysqlnd/mysqlnd.c
===================================================================
--- php/php-src/trunk/ext/mysqlnd/mysqlnd.c     2010-05-03 13:22:06 UTC (rev 
298900)
+++ php/php-src/trunk/ext/mysqlnd/mysqlnd.c     2010-05-03 13:45:58 UTC (rev 
298901)
@@ -1122,6 +1122,7 @@
                                                                           
FALSE, TRUE TSRMLS_CC)) {
                DBG_RETURN(NULL);
        }
+
        /*
           Prepare for the worst case.
           MyISAM goes to 2500 BIT columns, double it for safety.
@@ -1981,20 +1982,26 @@
                        }
                        break;
                case MYSQL_INIT_COMMAND:
+               {
+                       char ** new_init_commands;
+                       char * new_command;
                        DBG_INF("MYSQL_INIT_COMMAND");
                        DBG_INF_FMT("command=%s", value);
                        /* when num_commands is 0, then realloc will be 
effectively a malloc call, internally */
-                       conn->options.init_commands = 
mnd_perealloc(conn->options.init_commands, sizeof(char *) * 
(conn->options.num_commands + 1),
-                                                                               
                                conn->persistent);
-                       if (!conn->options.init_commands) {
+                       /* Don't assign to conn->options.init_commands because 
in case of OOM we will lose the pointer and leak */
+                       new_init_commands = 
mnd_perealloc(conn->options.init_commands, sizeof(char *) * 
(conn->options.num_commands + 1), conn->persistent);
+                       if (!new_init_commands) {
                                goto oom;
                        }
-                       conn->options.init_commands[conn->options.num_commands] 
= mnd_pestrdup(value, conn->persistent);
-                       if 
(!conn->options.init_commands[conn->options.num_commands]) {
+                       conn->options.init_commands = new_init_commands;
+                       new_command = mnd_pestrdup(value, conn->persistent);
+                       if (!new_command) {
                                goto oom;
                        }
+                       conn->options.init_commands[conn->options.num_commands] 
= new_command;
                        ++conn->options.num_commands;
                        break;
+               }
                case MYSQL_READ_DEFAULT_FILE:
                case MYSQL_READ_DEFAULT_GROUP:
 #ifdef WHEN_SUPPORTED_BY_MYSQLI
@@ -2004,17 +2011,19 @@
                        /* currently not supported. Todo!! */
                        break;
                case MYSQL_SET_CHARSET_NAME:
+               {
+                       char * new_charset_name = mnd_pestrdup(value, 
conn->persistent);
                        DBG_INF("MYSQL_SET_CHARSET_NAME");
+                       if (!new_charset_name) {
+                               goto oom;
+                       }
                        if (conn->options.charset_name) {
                                mnd_pefree(conn->options.charset_name, 
conn->persistent);
-                               conn->options.charset_name = NULL;
                        }
-                       conn->options.charset_name = mnd_pestrdup(value, 
conn->persistent);
-                       if (!conn->options.charset_name) {
-                               goto oom;
-                       }
+                       conn->options.charset_name = new_charset_name;
                        DBG_INF_FMT("charset=%s", conn->options.charset_name);
                        break;
+               }
 #ifdef WHEN_SUPPORTED_BY_MYSQLI
                case MYSQL_SET_CHARSET_DIR:
                case MYSQL_OPT_RECONNECT:

-- 
PHP CVS Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Reply via email to