]> granicus.if.org Git - php/commitdiff
Fixed bug #80808
authorNikita Popov <nikita.ppv@gmail.com>
Tue, 2 Mar 2021 09:58:35 +0000 (10:58 +0100)
committerNikita Popov <nikita.ppv@gmail.com>
Tue, 2 Mar 2021 09:59:34 +0000 (10:59 +0100)
If the ZEROFILL flag is set for a field, do not convert it into
an integer (text protocol) or convert it explicitly into a padded
string (binary protocol).

ext/mysqli/tests/003.phpt
ext/mysqli/tests/020.phpt
ext/mysqlnd/mysqlnd_ps_codec.c
ext/mysqlnd/mysqlnd_wireprotocol.c
ext/pdo_mysql/tests/bug80808.phpt [new file with mode: 0644]

index 56a26602dfc03a46847e6c630a282f2e0d28b15a..62f397587e8d442ead6032b2fbbd572ddf3d06af 100644 (file)
@@ -92,7 +92,7 @@ array(7) {
   [2]=>
   string(19) "2002-01-02 17:46:59"
   [3]=>
-  int(2010)
+  string(4) "2010"
   [4]=>
   string(19) "2010-07-10 00:00:00"
   [5]=>
index a2a8782f0fd5e844155c9c8bffaad5d9b153bc55..d3bbd593324fdab95099961e89752a569b43eab5 100644 (file)
@@ -87,7 +87,7 @@ array(7) {
   [2]=>
   %s(19) "2002-01-02 17:46:59"
   [3]=>
-  int(2010)
+  string(4) "2010"
   [4]=>
   %s(19) "2010-07-10 00:00:00"
   [5]=>
index e695d6ba79c725f7c74e5fb63fe35b9f4abe766c..eba4805b9ded329e638a593aa6dcb6e065e6df9f 100644 (file)
@@ -74,6 +74,11 @@ ps_fetch_from_1_to_8_bytes(zval * zv, const MYSQLND_FIELD * const field, const u
                        case 1:uval = (uint64_t) uint1korr(*row);break;
                }
 
+               if (field->flags & ZEROFILL_FLAG) {
+                       DBG_INF("stringify due to zerofill");
+                       tmp_len = sprintf((char *)&tmp, "%0*" PRIu64, (int) field->length, uval);
+                       DBG_INF_FMT("value=%s", tmp);
+               } else
 #if SIZEOF_ZEND_LONG==4
                if (uval > INT_MAX) {
                        DBG_INF("stringify");
index 8cb94b6598e5a6b64bbb4f44d86076e1087a723a..ed36c8404be29c8e2c05d328e90b3aac0e5f20eb 100644 (file)
@@ -1600,7 +1600,8 @@ php_mysqlnd_rowp_read_text_protocol(MYSQLND_ROW_BUFFER * row_buffer, zval * fiel
                                } else if (Z_TYPE_P(current_field) == IS_STRING) {
                                        /* nothing to do here, as we want a string and ps_fetch_from_1_to_8_bytes() has given us one */
                                }
-                       } else if (as_int_or_float && perm_bind.php_type == IS_LONG) {
+                       } else if (as_int_or_float && perm_bind.php_type == IS_LONG
+                                       && !(fields_metadata[i].flags & ZEROFILL_FLAG)) {
                                zend_uchar save = *(p + len);
                                /* We have to make it ASCIIZ temporarily */
                                *(p + len) = '\0';
diff --git a/ext/pdo_mysql/tests/bug80808.phpt b/ext/pdo_mysql/tests/bug80808.phpt
new file mode 100644 (file)
index 0000000..364b559
--- /dev/null
@@ -0,0 +1,27 @@
+--TEST--
+Bug #80808: PDO returns ZEROFILL integers without leading zeros
+--SKIPIF--
+<?php
+require_once(__DIR__ . DIRECTORY_SEPARATOR . 'skipif.inc');
+require_once(__DIR__ . DIRECTORY_SEPARATOR . 'mysql_pdo_test.inc');
+MySQLPDOTest::skip();
+?>
+--FILE--
+<?php
+
+require_once(__DIR__ . DIRECTORY_SEPARATOR . 'mysql_pdo_test.inc');
+$pdo = MySQLPDOTest::factory();
+
+$pdo->exec('DROP TABLE IF EXISTS test');
+$pdo->exec('CREATE TABLE test (postcode INT(4) UNSIGNED ZEROFILL)');
+$pdo->exec('INSERT INTO test (postcode) VALUES (\'0800\')');
+
+$pdo->setAttribute(PDO::ATTR_EMULATE_PREPARES, true);
+var_dump($pdo->query('SELECT * FROM test')->fetchColumn(0));
+$pdo->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
+var_dump($pdo->query('SELECT * FROM test')->fetchColumn(0));
+
+?>
+--EXPECT--
+string(4) "0800"
+string(4) "0800"