andrey                                   Wed, 31 Aug 2011 18:18:23 +0000

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

Log:
Fix valgrind error (sending of initialized bytes over the network).
When the compression was successful the compressed data + uninitialized data
at the end was sent to the server, because the length of the compressed payload
wasn't correctly calculated (actually the length of the uncompressed payload as 
assumed).
However, the uncompress() function has internally the length of the real 
payload and skips
the binary trash at the end - thus no data damage occurs!

Changed paths:
    U   php/php-src/branches/PHP_5_3/ext/mysqlnd/mysqlnd_net.c
    U   php/php-src/branches/PHP_5_3/ext/mysqlnd/mysqlnd_structs.h
    U   php/php-src/branches/PHP_5_4/ext/mysqlnd/mysqlnd_net.c
    U   php/php-src/branches/PHP_5_4/ext/mysqlnd/mysqlnd_structs.h
    U   php/php-src/trunk/ext/mysqlnd/mysqlnd_net.c
    U   php/php-src/trunk/ext/mysqlnd/mysqlnd_structs.h

Modified: php/php-src/branches/PHP_5_3/ext/mysqlnd/mysqlnd_net.c
===================================================================
--- php/php-src/branches/PHP_5_3/ext/mysqlnd/mysqlnd_net.c	2011-08-31 18:13:04 UTC (rev 315910)
+++ php/php-src/branches/PHP_5_3/ext/mysqlnd/mysqlnd_net.c	2011-08-31 18:18:23 UTC (rev 315911)
@@ -265,7 +265,7 @@
 			STORE_HEADER_SIZE(safe_storage, uncompressed_payload);
 			int3store(uncompressed_payload, to_be_sent);
 			int1store(uncompressed_payload + 3, net->packet_no);
-			if (PASS == net->m.encode((compress_buf + COMPRESSED_HEADER_SIZE + MYSQLND_HEADER_SIZE), tmp_complen,
+			if (PASS == net->m.encode((compress_buf + COMPRESSED_HEADER_SIZE + MYSQLND_HEADER_SIZE), &tmp_complen,
 									   uncompressed_payload, to_be_sent + MYSQLND_HEADER_SIZE TSRMLS_CC))
 			{
 				int3store(compress_buf + MYSQLND_HEADER_SIZE, to_be_sent + MYSQLND_HEADER_SIZE);
@@ -491,20 +491,22 @@

 /* {{{ mysqlnd_net::encode */
 static enum_func_status
-MYSQLND_METHOD(mysqlnd_net, encode)(zend_uchar * compress_buffer, size_t compress_buffer_len,
+MYSQLND_METHOD(mysqlnd_net, encode)(zend_uchar * compress_buffer, size_t * compress_buffer_len,
 									const zend_uchar * const uncompressed_data, size_t uncompressed_data_len TSRMLS_DC)
 {
 #ifdef MYSQLND_COMPRESSION_ENABLED
 	int error;
-	uLongf tmp_complen = compress_buffer_len;
+	uLongf tmp_complen = *compress_buffer_len;
 	DBG_ENTER("mysqlnd_net::encode");
 	error = compress(compress_buffer, &tmp_complen, uncompressed_data, uncompressed_data_len);

 	if (error != Z_OK) {
 		DBG_INF_FMT("compression NOT successful. error=%d Z_OK=%d Z_BUF_ERROR=%d Z_MEM_ERROR=%d", error, Z_OK, Z_BUF_ERROR, Z_MEM_ERROR);
 	} else {
+		*compress_buffer_len = tmp_complen;
 		DBG_INF_FMT("compression successful. compressed size=%lu", tmp_complen);
 	}
+
 	DBG_RETURN(error == Z_OK? PASS:FAIL);
 #else
 	DBG_ENTER("mysqlnd_net::encode");

Modified: php/php-src/branches/PHP_5_3/ext/mysqlnd/mysqlnd_structs.h
===================================================================
--- php/php-src/branches/PHP_5_3/ext/mysqlnd/mysqlnd_structs.h	2011-08-31 18:13:04 UTC (rev 315910)
+++ php/php-src/branches/PHP_5_3/ext/mysqlnd/mysqlnd_structs.h	2011-08-31 18:18:23 UTC (rev 315911)
@@ -258,7 +258,7 @@
 typedef enum_func_status	(*func_mysqlnd_net__network_read)(MYSQLND * conn, zend_uchar * buffer, size_t count TSRMLS_DC);
 typedef size_t				(*func_mysqlnd_net__network_write)(MYSQLND * const conn, const zend_uchar * const buf, size_t count TSRMLS_DC);
 typedef enum_func_status	(*func_mysqlnd_net__decode)(zend_uchar * uncompressed_data, size_t uncompressed_data_len, const zend_uchar * const compressed_data, size_t compressed_data_len TSRMLS_DC);
-typedef enum_func_status	(*func_mysqlnd_net__encode)(zend_uchar * compress_buffer, size_t compress_buffer_len, const zend_uchar * const uncompressed_data, size_t uncompressed_data_len TSRMLS_DC);
+typedef enum_func_status	(*func_mysqlnd_net__encode)(zend_uchar * compress_buffer, size_t * compress_buffer_len, const zend_uchar * const uncompressed_data, size_t uncompressed_data_len TSRMLS_DC);
 typedef size_t				(*func_mysqlnd_net__consume_uneaten_data)(MYSQLND_NET * const net, enum php_mysqlnd_server_command cmd TSRMLS_DC);
 typedef void				(*func_mysqlnd_net__free_contents)(MYSQLND_NET * net TSRMLS_DC);
 typedef enum_func_status	(*func_mysqlnd_net__enable_ssl)(MYSQLND_NET * const net TSRMLS_DC);

Modified: php/php-src/branches/PHP_5_4/ext/mysqlnd/mysqlnd_net.c
===================================================================
--- php/php-src/branches/PHP_5_4/ext/mysqlnd/mysqlnd_net.c	2011-08-31 18:13:04 UTC (rev 315910)
+++ php/php-src/branches/PHP_5_4/ext/mysqlnd/mysqlnd_net.c	2011-08-31 18:18:23 UTC (rev 315911)
@@ -272,7 +272,7 @@
 			STORE_HEADER_SIZE(safe_storage, uncompressed_payload);
 			int3store(uncompressed_payload, to_be_sent);
 			int1store(uncompressed_payload + 3, net->packet_no);
-			if (PASS == net->m.encode((compress_buf + COMPRESSED_HEADER_SIZE + MYSQLND_HEADER_SIZE), tmp_complen,
+			if (PASS == net->m.encode((compress_buf + COMPRESSED_HEADER_SIZE + MYSQLND_HEADER_SIZE), &tmp_complen,
 									   uncompressed_payload, to_be_sent + MYSQLND_HEADER_SIZE TSRMLS_CC))
 			{
 				int3store(compress_buf + MYSQLND_HEADER_SIZE, to_be_sent + MYSQLND_HEADER_SIZE);
@@ -498,20 +498,22 @@

 /* {{{ mysqlnd_net::encode */
 static enum_func_status
-MYSQLND_METHOD(mysqlnd_net, encode)(zend_uchar * compress_buffer, size_t compress_buffer_len,
+MYSQLND_METHOD(mysqlnd_net, encode)(zend_uchar * compress_buffer, size_t * compress_buffer_len,
 									const zend_uchar * const uncompressed_data, size_t uncompressed_data_len TSRMLS_DC)
 {
 #ifdef MYSQLND_COMPRESSION_ENABLED
 	int error;
-	uLongf tmp_complen = compress_buffer_len;
+	uLongf tmp_complen = *compress_buffer_len;
 	DBG_ENTER("mysqlnd_net::encode");
 	error = compress(compress_buffer, &tmp_complen, uncompressed_data, uncompressed_data_len);

 	if (error != Z_OK) {
 		DBG_INF_FMT("compression NOT successful. error=%d Z_OK=%d Z_BUF_ERROR=%d Z_MEM_ERROR=%d", error, Z_OK, Z_BUF_ERROR, Z_MEM_ERROR);
 	} else {
+		*compress_buffer_len = tmp_complen;
 		DBG_INF_FMT("compression successful. compressed size=%lu", tmp_complen);
 	}
+
 	DBG_RETURN(error == Z_OK? PASS:FAIL);
 #else
 	DBG_ENTER("mysqlnd_net::encode");

Modified: php/php-src/branches/PHP_5_4/ext/mysqlnd/mysqlnd_structs.h
===================================================================
--- php/php-src/branches/PHP_5_4/ext/mysqlnd/mysqlnd_structs.h	2011-08-31 18:13:04 UTC (rev 315910)
+++ php/php-src/branches/PHP_5_4/ext/mysqlnd/mysqlnd_structs.h	2011-08-31 18:18:23 UTC (rev 315911)
@@ -267,7 +267,7 @@
 typedef enum_func_status	(*func_mysqlnd_net__network_read)(MYSQLND * conn, zend_uchar * buffer, size_t count TSRMLS_DC);
 typedef size_t				(*func_mysqlnd_net__network_write)(MYSQLND * const conn, const zend_uchar * const buf, size_t count TSRMLS_DC);
 typedef enum_func_status	(*func_mysqlnd_net__decode)(zend_uchar * uncompressed_data, size_t uncompressed_data_len, const zend_uchar * const compressed_data, size_t compressed_data_len TSRMLS_DC);
-typedef enum_func_status	(*func_mysqlnd_net__encode)(zend_uchar * compress_buffer, size_t compress_buffer_len, const zend_uchar * const uncompressed_data, size_t uncompressed_data_len TSRMLS_DC);
+typedef enum_func_status	(*func_mysqlnd_net__encode)(zend_uchar * compress_buffer, size_t * compress_buffer_len, const zend_uchar * const uncompressed_data, size_t uncompressed_data_len TSRMLS_DC);
 typedef size_t				(*func_mysqlnd_net__consume_uneaten_data)(MYSQLND_NET * const net, enum php_mysqlnd_server_command cmd TSRMLS_DC);
 typedef void				(*func_mysqlnd_net__free_contents)(MYSQLND_NET * net TSRMLS_DC);
 typedef enum_func_status	(*func_mysqlnd_net__enable_ssl)(MYSQLND_NET * const net TSRMLS_DC);

Modified: php/php-src/trunk/ext/mysqlnd/mysqlnd_net.c
===================================================================
--- php/php-src/trunk/ext/mysqlnd/mysqlnd_net.c	2011-08-31 18:13:04 UTC (rev 315910)
+++ php/php-src/trunk/ext/mysqlnd/mysqlnd_net.c	2011-08-31 18:18:23 UTC (rev 315911)
@@ -272,7 +272,7 @@
 			STORE_HEADER_SIZE(safe_storage, uncompressed_payload);
 			int3store(uncompressed_payload, to_be_sent);
 			int1store(uncompressed_payload + 3, net->packet_no);
-			if (PASS == net->m.encode((compress_buf + COMPRESSED_HEADER_SIZE + MYSQLND_HEADER_SIZE), tmp_complen,
+			if (PASS == net->m.encode((compress_buf + COMPRESSED_HEADER_SIZE + MYSQLND_HEADER_SIZE), &tmp_complen,
 									   uncompressed_payload, to_be_sent + MYSQLND_HEADER_SIZE TSRMLS_CC))
 			{
 				int3store(compress_buf + MYSQLND_HEADER_SIZE, to_be_sent + MYSQLND_HEADER_SIZE);
@@ -498,20 +498,22 @@

 /* {{{ mysqlnd_net::encode */
 static enum_func_status
-MYSQLND_METHOD(mysqlnd_net, encode)(zend_uchar * compress_buffer, size_t compress_buffer_len,
+MYSQLND_METHOD(mysqlnd_net, encode)(zend_uchar * compress_buffer, size_t * compress_buffer_len,
 									const zend_uchar * const uncompressed_data, size_t uncompressed_data_len TSRMLS_DC)
 {
 #ifdef MYSQLND_COMPRESSION_ENABLED
 	int error;
-	uLongf tmp_complen = compress_buffer_len;
+	uLongf tmp_complen = *compress_buffer_len;
 	DBG_ENTER("mysqlnd_net::encode");
 	error = compress(compress_buffer, &tmp_complen, uncompressed_data, uncompressed_data_len);

 	if (error != Z_OK) {
 		DBG_INF_FMT("compression NOT successful. error=%d Z_OK=%d Z_BUF_ERROR=%d Z_MEM_ERROR=%d", error, Z_OK, Z_BUF_ERROR, Z_MEM_ERROR);
 	} else {
+		*compress_buffer_len = tmp_complen;
 		DBG_INF_FMT("compression successful. compressed size=%lu", tmp_complen);
 	}
+
 	DBG_RETURN(error == Z_OK? PASS:FAIL);
 #else
 	DBG_ENTER("mysqlnd_net::encode");

Modified: php/php-src/trunk/ext/mysqlnd/mysqlnd_structs.h
===================================================================
--- php/php-src/trunk/ext/mysqlnd/mysqlnd_structs.h	2011-08-31 18:13:04 UTC (rev 315910)
+++ php/php-src/trunk/ext/mysqlnd/mysqlnd_structs.h	2011-08-31 18:18:23 UTC (rev 315911)
@@ -267,7 +267,7 @@
 typedef enum_func_status	(*func_mysqlnd_net__network_read)(MYSQLND * conn, zend_uchar * buffer, size_t count TSRMLS_DC);
 typedef size_t				(*func_mysqlnd_net__network_write)(MYSQLND * const conn, const zend_uchar * const buf, size_t count TSRMLS_DC);
 typedef enum_func_status	(*func_mysqlnd_net__decode)(zend_uchar * uncompressed_data, size_t uncompressed_data_len, const zend_uchar * const compressed_data, size_t compressed_data_len TSRMLS_DC);
-typedef enum_func_status	(*func_mysqlnd_net__encode)(zend_uchar * compress_buffer, size_t compress_buffer_len, const zend_uchar * const uncompressed_data, size_t uncompressed_data_len TSRMLS_DC);
+typedef enum_func_status	(*func_mysqlnd_net__encode)(zend_uchar * compress_buffer, size_t * compress_buffer_len, const zend_uchar * const uncompressed_data, size_t uncompressed_data_len TSRMLS_DC);
 typedef size_t				(*func_mysqlnd_net__consume_uneaten_data)(MYSQLND_NET * const net, enum php_mysqlnd_server_command cmd TSRMLS_DC);
 typedef void				(*func_mysqlnd_net__free_contents)(MYSQLND_NET * net TSRMLS_DC);
 typedef enum_func_status	(*func_mysqlnd_net__enable_ssl)(MYSQLND_NET * const net TSRMLS_DC);
-- 
PHP CVS Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Reply via email to