andrey                                   Fri, 18 Mar 2011 14:56:20 +0000

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

Log:
add a proxy for sppintf and vspprintf

Changed paths:
    U   php/php-src/trunk/ext/mysqlnd/mysqlnd.c
    U   php/php-src/trunk/ext/mysqlnd/mysqlnd_alloc.c
    U   php/php-src/trunk/ext/mysqlnd/mysqlnd_alloc.h
    U   php/php-src/trunk/ext/mysqlnd/mysqlnd_debug.c
    U   php/php-src/trunk/ext/mysqlnd/mysqlnd_net.c
    U   php/php-src/trunk/ext/mysqlnd/mysqlnd_ps.c
    U   php/php-src/trunk/ext/mysqlnd/mysqlnd_ps_codec.c

Modified: php/php-src/trunk/ext/mysqlnd/mysqlnd.c
===================================================================
--- php/php-src/trunk/ext/mysqlnd/mysqlnd.c	2011-03-18 14:54:02 UTC (rev 309387)
+++ php/php-src/trunk/ext/mysqlnd/mysqlnd.c	2011-03-18 14:56:20 UTC (rev 309388)
@@ -524,11 +524,11 @@
 			{
 				char * plugin_name = NULL;

-				spprintf(&plugin_name, 0, "auth_plugin_%s", requested_protocol);
+				mnd_sprintf(&plugin_name, 0, "auth_plugin_%s", requested_protocol);

 				DBG_INF_FMT("looking for %s auth plugin", plugin_name);
 				auth_plugin = mysqlnd_plugin_find(plugin_name);
-				efree(plugin_name);
+				mnd_sprintf_free(plugin_name);

 				if (!auth_plugin) {
 					php_error_docref(NULL TSRMLS_CC, E_WARNING, "The server requested authentication method unknown to the client [%s]", requested_protocol);
@@ -695,7 +695,7 @@
 			if (!socket_or_pipe) {
 				socket_or_pipe = "/tmp/mysql.sock";
 			}
-			transport_len = spprintf(&transport, 0, "unix://%s", socket_or_pipe);
+			transport_len = mnd_sprintf(&transport, 0, "unix://%s", socket_or_pipe);
 			unix_socket = TRUE;
 #else
 		if (host_len == sizeof(".") - 1 && host[0] == '.') {
@@ -703,14 +703,14 @@
 			if (!socket_or_pipe) {
 				socket_or_pipe = "\\\\.\\pipe\\MySQL";
 			}
-			transport_len = spprintf(&transport, 0, "pipe://%s", socket_or_pipe);
+			transport_len = mnd_sprintf(&transport, 0, "pipe://%s", socket_or_pipe);
 			named_pipe = TRUE;
 #endif
 		} else {
 			if (!port) {
 				port = 3306;
 			}
-			transport_len = spprintf(&transport, 0, "tcp://%s:%u", host, port);
+			transport_len = mnd_sprintf(&transport, 0, "tcp://%s:%u", host, port);
 		}
 		if (!transport) {
 			SET_OOM_ERROR(conn->error_info);
@@ -719,7 +719,7 @@
 		DBG_INF_FMT("transport=%s", transport);
 		conn->scheme = mnd_pestrndup(transport, transport_len, conn->persistent);
 		conn->scheme_len = transport_len;
-		efree(transport); /* allocated by spprintf */
+		mnd_sprintf_free(transport);
 		transport = NULL;
 		if (!conn->scheme) {
 			goto err; /* OOM */
@@ -833,13 +833,13 @@
 			conn->host_len = strlen(conn->host);
 			{
 				char *p;
-				spprintf(&p, 0, "%s via TCP/IP", conn->host);
+				mnd_sprintf(&p, 0, "%s via TCP/IP", conn->host);
 				if (!p) {
 					SET_OOM_ERROR(conn->error_info);
 					goto err; /* OOM */
 				}
 				conn->host_info =  mnd_pestrdup(p, conn->persistent);
-				efree(p); /* allocated by spprintf */
+				mnd_sprintf_free(p);
 				if (!conn->host_info) {
 					SET_OOM_ERROR(conn->error_info);
 					goto err; /* OOM */
@@ -851,13 +851,13 @@
 				conn->host_info		= mnd_pestrdup("Localhost via UNIX socket", conn->persistent);
 			} else if (named_pipe) {
 				char *p;
-				spprintf(&p, 0, "%s via named pipe", conn->unix_socket);
+				mnd_sprintf(&p, 0, "%s via named pipe", conn->unix_socket);
 				if (!p) {
 					SET_OOM_ERROR(conn->error_info);
 					goto err; /* OOM */
 				}
 				conn->host_info =  mnd_pestrdup(p, conn->persistent);
-				efree(p); /* allocated by spprintf */
+				mnd_sprintf_free(p);
 				if (!conn->host_info) {
 					SET_OOM_ERROR(conn->error_info);
 					goto err; /* OOM */
@@ -1317,13 +1317,13 @@

 	if (par1) {
 		if (achtung_wild) {
-			show_query_len = spprintf(&show_query, 0, query, par1, achtung_wild);
+			show_query_len = mnd_sprintf(&show_query, 0, query, par1, achtung_wild);
 		} else {
-			show_query_len = spprintf(&show_query, 0, query, par1);
+			show_query_len = mnd_sprintf(&show_query, 0, query, par1);
 		}
 	} else {
 		if (achtung_wild) {
-			show_query_len = spprintf(&show_query, 0, query, achtung_wild);
+			show_query_len = mnd_sprintf(&show_query, 0, query, achtung_wild);
 		} else {
 			show_query_len = strlen(show_query = (char *)query);
 		}
@@ -1333,7 +1333,7 @@
 		result = conn->m->store_result(conn TSRMLS_CC);
 	}
 	if (show_query != query) {
-		efree(show_query); /* allocated by spprintf */
+		mnd_sprintf_free(show_query);
 	}
 	DBG_RETURN(result);
 }
@@ -1547,7 +1547,7 @@
 		DBG_RETURN(FAIL);
 	}

-	query_len = spprintf(&query, 0, "SET NAMES %s", csname);
+	query_len = mnd_sprintf(&query, 0, "SET NAMES %s", csname);

 	if (FAIL == conn->m->query(conn, query, query_len TSRMLS_CC)) {
 		php_error_docref(NULL TSRMLS_CC, E_WARNING, "Error executing query");
@@ -1556,7 +1556,7 @@
 	} else {
 		conn->charset = charset;
 	}
-	efree(query); /* allocated by spprintf */
+	mnd_sprintf_free(query);

 	DBG_INF(ret == PASS? "PASS":"FAIL");
 	DBG_RETURN(ret);
@@ -2033,11 +2033,11 @@
 			{
 				char * plugin_name = NULL;

-				spprintf(&plugin_name, 0, "auth_plugin_%s", requested_protocol);
+				mnd_sprintf(&plugin_name, 0, "auth_plugin_%s", requested_protocol);

 				DBG_INF_FMT("looking for %s auth plugin", plugin_name);
 				auth_plugin = mysqlnd_plugin_find(plugin_name);
-				efree(plugin_name);
+				mnd_sprintf_free(plugin_name);

 				if (!auth_plugin) {
 					php_error_docref(NULL TSRMLS_CC, E_WARNING, "The server requested authentication method unknown to the client [%s]", requested_protocol);

Modified: php/php-src/trunk/ext/mysqlnd/mysqlnd_alloc.c
===================================================================
--- php/php-src/trunk/ext/mysqlnd/mysqlnd_alloc.c	2011-03-18 14:54:02 UTC (rev 309387)
+++ php/php-src/trunk/ext/mysqlnd/mysqlnd_alloc.c	2011-03-18 14:56:20 UTC (rev 309388)
@@ -510,6 +510,35 @@
 }
 /* }}} */

+
+/* {{{ _mysqlnd_sprintf */
+PHPAPI int _mysqlnd_sprintf(char ** pbuf, size_t max_len, const char *format, ...)
+{
+	int len;
+	va_list ap;
+	va_start(ap, format);
+	len = vspprintf(pbuf, max_len, format, ap);
+	va_end(ap);
+	return len;
+}
+/* }}} */
+
+
+/* {{{ _mysqlnd_sprintf_free */
+PHPAPI void _mysqlnd_sprintf_free(char * p)
+{
+	efree(p);
+}
+/* }}} */
+
+
+PHPAPI int _mysqlnd_vsprintf(char ** pbuf, size_t max_len, const char * format, va_list ap)
+{
+	return vspprintf(pbuf, max_len, format, ap);
+}
+/* }}} */
+
+
 #define MYSQLND_DEBUG_MEMORY 1

 #if MYSQLND_DEBUG_MEMORY == 0
@@ -644,7 +673,10 @@
 	_mysqlnd_realloc,
 	_mysqlnd_free,
 	_mysqlnd_pestrndup,
-	_mysqlnd_pestrdup
+	_mysqlnd_pestrdup,
+	_mysqlnd_sprintf,
+	_mysqlnd_vsprintf,
+	_mysqlnd_sprintf_free
 #else
 	mysqlnd_zend_mm_emalloc,
 	mysqlnd_zend_mm_pemalloc,
@@ -660,6 +692,8 @@
 	mysqlnd_zend_mm_free,
 	mysqlnd_zend_mm_pestrndup,
 	mysqlnd_zend_mm_pestrdup
+	sprintf,
+	mysqlnd_zend_mm_efree,
 #endif
 };


Modified: php/php-src/trunk/ext/mysqlnd/mysqlnd_alloc.h
===================================================================
--- php/php-src/trunk/ext/mysqlnd/mysqlnd_alloc.h	2011-03-18 14:54:02 UTC (rev 309387)
+++ php/php-src/trunk/ext/mysqlnd/mysqlnd_alloc.h	2011-03-18 14:56:20 UTC (rev 309388)
@@ -45,6 +45,9 @@
 	void	(*m_free)(void *ptr MYSQLND_MEM_D);
 	char *	(*m_pestrndup)(const char * const ptr, size_t size, zend_bool persistent MYSQLND_MEM_D);
 	char *	(*m_pestrdup)(const char * const ptr, zend_bool persistent MYSQLND_MEM_D);
+	int		(*m_sprintf)(char **pbuf, size_t max_len, const char *format, ...);
+	int		(*m_vsprintf)(char **pbuf, size_t max_len, const char *format, va_list ap);
+	void	(*m_sprintf_free)(char * p);
 };

 PHPAPI extern struct st_mysqlnd_allocator_methods mysqlnd_allocator;
@@ -63,8 +66,10 @@
 PHPAPI void		_mysqlnd_free(void *ptr MYSQLND_MEM_D);
 PHPAPI char *	_mysqlnd_pestrndup(const char * const ptr, size_t size, zend_bool persistent MYSQLND_MEM_D);
 PHPAPI char *	_mysqlnd_pestrdup(const char * const ptr, zend_bool persistent MYSQLND_MEM_D);
+PHPAPI int		_mysqlnd_sprintf(char **pbuf, size_t max_len, const char *format, ...);
+PHPAPI void		_mysqlnd_sprintf_free(char * p);
+PHPAPI int		_mysqlnd_vsprintf(char **pbuf, size_t max_len, const char *format, va_list ap);

-
 #define mnd_emalloc(size)				mysqlnd_allocator.m_emalloc((size) MYSQLND_MEM_C)
 #define mnd_pemalloc(size, pers)		mysqlnd_allocator.m_pemalloc((size), (pers) MYSQLND_MEM_C)
 #define mnd_ecalloc(nmemb, size)		mysqlnd_allocator.m_ecalloc((nmemb), (size) MYSQLND_MEM_C)
@@ -79,6 +84,9 @@
 #define mnd_free(ptr)					mysqlnd_allocator.m_free((ptr) MYSQLND_MEM_C)
 #define mnd_pestrndup(ptr, size, pers)	mysqlnd_allocator.m_pestrndup((ptr), (size), (pers) MYSQLND_MEM_C)
 #define mnd_pestrdup(ptr, pers)			mysqlnd_allocator.m_pestrdup((ptr), (pers) MYSQLND_MEM_C)
+#define mnd_sprintf(p, mx_len, fmt,...) mysqlnd_allocator.m_sprintf((p), (mx_len), (fmt), __VA_ARGS__)
+#define mnd_vsprintf(p, mx_len, fmt,ap) mysqlnd_allocator.m_vsprintf((p), (mx_len), (fmt), (ap))
+#define mnd_sprintf_free(p)				mysqlnd_allocator.m_sprintf_free((p))

 #endif /* MYSQLND_ALLOC_H */


Modified: php/php-src/trunk/ext/mysqlnd/mysqlnd_debug.c
===================================================================
--- php/php-src/trunk/ext/mysqlnd/mysqlnd_debug.c	2011-03-18 14:54:02 UTC (rev 309387)
+++ php/php-src/trunk/ext/mysqlnd/mysqlnd_debug.c	2011-03-18 14:56:20 UTC (rev 309388)
@@ -129,7 +129,7 @@
 		level_buffer[sizeof(level_buffer) - 1 ] = '\0';
 	}

-	message_line_len = spprintf(&message_line, 0, "%s%s%s%s%s%s%s%s\n",
+	message_line_len = mnd_sprintf(&message_line, 0, "%s%s%s%s%s%s%s%s\n",
 								flags & MYSQLND_DEBUG_DUMP_PID? pid_buffer:"",
 								flags & MYSQLND_DEBUG_DUMP_TIME? time_buffer:"",
 								flags & MYSQLND_DEBUG_DUMP_FILE? file_buffer:"",
@@ -138,7 +138,7 @@
 								pipe_buffer, type? type:"", message);

 	ret = php_stream_write(self->stream, message_line, message_line_len)? PASS:FAIL;
-	efree(message_line); /* allocated by spprintf */
+	mnd_sprintf_free(message_line);
 	if (flags & MYSQLND_DEBUG_FLUSH) {
 		self->m->close(self);
 		self->m->open(self, TRUE);
@@ -227,21 +227,20 @@
 		level_buffer[sizeof(level_buffer) - 1 ] = '\0';
 	}

-
 	va_start(args, format);
-	vspprintf(&buffer, 0, format, args);
+	mnd_vsprintf(&buffer, 0, format, args);
 	va_end(args);

-	message_line_len = spprintf(&message_line, 0, "%s%s%s%s%s%s%s%s\n",
+	message_line_len = mnd_sprintf(&message_line, 0, "%s%s%s%s%s%s%s%s\n",
 								flags & MYSQLND_DEBUG_DUMP_PID? pid_buffer:"",
 								flags & MYSQLND_DEBUG_DUMP_TIME? time_buffer:"",
 								flags & MYSQLND_DEBUG_DUMP_FILE? file_buffer:"",
 								flags & MYSQLND_DEBUG_DUMP_LINE? line_buffer:"",
 								flags & MYSQLND_DEBUG_DUMP_LEVEL? level_buffer:"",
 								pipe_buffer, type? type:"", buffer);
-	efree(buffer);
+	mnd_sprintf_free(buffer);
 	ret = php_stream_write(self->stream, message_line, message_line_len)? PASS:FAIL;
-	efree(message_line); /* allocated by spprintf */
+	mnd_sprintf_free(message_line);

 	if (flags & MYSQLND_DEBUG_FLUSH) {
 		self->m->close(self);

Modified: php/php-src/trunk/ext/mysqlnd/mysqlnd_net.c
===================================================================
--- php/php-src/trunk/ext/mysqlnd/mysqlnd_net.c	2011-03-18 14:54:02 UTC (rev 309387)
+++ php/php-src/trunk/ext/mysqlnd/mysqlnd_net.c	2011-03-18 14:56:20 UTC (rev 309388)
@@ -124,7 +124,7 @@
 		net->stream = php_stream_open_wrapper((char*) scheme + sizeof("pipe://") - 1, "r+", streams_options, NULL);
 	} else {
 		if (persistent) {
-			hashed_details_len = spprintf(&hashed_details, 0, "%p", net);
+			hashed_details_len = mnd_sprintf(&hashed_details, 0, "%p", net);
 			DBG_INF_FMT("hashed_details=%s", hashed_details);
 		}

@@ -140,7 +140,7 @@
 	}
 	if (*errstr || !net->stream) {
 		if (hashed_details) {
-			efree(hashed_details); /* allocated by spprintf */
+			mnd_sprintf_free(hashed_details);
 		}
 		*errcode = CR_CONNECTION_ERROR;
 		DBG_RETURN(FAIL);
@@ -168,7 +168,7 @@
 		/* Shut-up the streams, they don't know what they are doing */
 		net->stream->__exposed = 1;
 #endif
-		efree(hashed_details);
+		mnd_sprintf_free(hashed_details);
 	}
 	/*
 	  Streams are not meant for C extensions! Thus we need a hack. Every connected stream will

Modified: php/php-src/trunk/ext/mysqlnd/mysqlnd_ps.c
===================================================================
--- php/php-src/trunk/ext/mysqlnd/mysqlnd_ps.c	2011-03-18 14:54:02 UTC (rev 309387)
+++ php/php-src/trunk/ext/mysqlnd/mysqlnd_ps.c	2011-03-18 14:56:20 UTC (rev 309388)
@@ -660,11 +660,11 @@
 		}
 		if (not_bound) {
 			char * msg;
-			spprintf(&msg, 0, "No data supplied for %u parameter%s in prepared statement",
-					 not_bound, not_bound>1 ?"s":"");
+			mnd_sprintf(&msg, 0, "No data supplied for %u parameter%s in prepared statement",
+						not_bound, not_bound>1 ?"s":"");
 			SET_STMT_ERROR(stmt, CR_PARAMS_NOT_BOUND, UNKNOWN_SQLSTATE, msg);
 			if (msg) {
-				efree(msg); /* allocated by spprintf */
+				mnd_sprintf_free(msg);
 			}
 			DBG_INF("FAIL");
 			DBG_RETURN(FAIL);

Modified: php/php-src/trunk/ext/mysqlnd/mysqlnd_ps_codec.c
===================================================================
--- php/php-src/trunk/ext/mysqlnd/mysqlnd_ps_codec.c	2011-03-18 14:54:02 UTC (rev 309387)
+++ php/php-src/trunk/ext/mysqlnd/mysqlnd_ps_codec.c	2011-03-18 14:56:20 UTC (rev 309388)
@@ -258,18 +258,14 @@
 		t.time_type = MYSQLND_TIMESTAMP_TIME;
 	}

-	/*
-	  QQ : How to make this unicode without copying two times the buffer -
-	  Unicode equivalent of spprintf?
-	*/
-	length = spprintf(&value, 0, "%s%02u:%02u:%02u", (t.neg ? "-" : ""), t.hour, t.minute, t.second);
+	length = mnd_sprintf(&value, 0, "%s%02u:%02u:%02u", (t.neg ? "-" : ""), t.hour, t.minute, t.second);

 	DBG_INF_FMT("%s", value);
 #if MYSQLND_UNICODE
 	if (!as_unicode) {
 #endif
 		ZVAL_STRINGL(zv, value, length, 1);
-		efree(value);  /* allocated by spprintf */
+		mnd_sprintf_free(value);
 #if MYSQLND_UNICODE
 	} else {
 		ZVAL_UTF8_STRINGL(zv, value, length, ZSTR_AUTOFREE);
@@ -309,18 +305,14 @@
 		t.time_type = MYSQLND_TIMESTAMP_DATE;
 	}

-	/*
-	  QQ : How to make this unicode without copying two times the buffer -
-	  Unicode equivalent of spprintf?
-	*/
-	length = spprintf(&value, 0, "%04u-%02u-%02u", t.year, t.month, t.day);
+	length = mnd_sprintf(&value, 0, "%04u-%02u-%02u", t.year, t.month, t.day);

 	DBG_INF_FMT("%s", value);
 #if MYSQLND_UNICODE
 	if (!as_unicode) {
 #endif
 		ZVAL_STRINGL(zv, value, length, 1);
-		efree(value); /* allocated by spprintf */
+		mnd_sprintf_free(value);
 #if MYSQLND_UNICODE
 	} else {
 		ZVAL_UTF8_STRINGL(zv, value, length, ZSTR_AUTOFREE);
@@ -367,19 +359,14 @@
 		t.time_type = MYSQLND_TIMESTAMP_DATETIME;
 	}

-	/*
-	  QQ : How to make this unicode without copying two times the buffer -
-	  Unicode equivalent of spprintf?
-	*/
-	length = spprintf(&value, 0, "%04u-%02u-%02u %02u:%02u:%02u",
-					  t.year, t.month, t.day, t.hour, t.minute, t.second);
+	length = mnd_sprintf(&value, 0, "%04u-%02u-%02u %02u:%02u:%02u", t.year, t.month, t.day, t.hour, t.minute, t.second);

 	DBG_INF_FMT("%s", value);
 #if MYSQLND_UNICODE
 	if (!as_unicode) {
 #endif
 		ZVAL_STRINGL(zv, value, length, 1);
-		efree(value); /* allocated by spprintf */
+		mnd_sprintf_free(value);
 #if MYSQLND_UNICODE
 	} else {
 		ZVAL_UTF8_STRINGL(zv, to, length, ZSTR_AUTOFREE);
-- 
PHP CVS Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Reply via email to