xref: /PHP-8.2/ext/mysqli/tests/bug52891.phpt (revision 62d393b1)
1--TEST--
2Bug #52891 (Wrong data inserted with mysqli/mysqlnd when using bind_param,value > LONG_MAX)
3--EXTENSIONS--
4mysqli
5--SKIPIF--
6<?php
7require_once('skipifconnectfailure.inc');
8?>
9--FILE--
10<?php
11    require_once("connect.inc");
12
13    if (!$link = my_mysqli_connect($host, $user, $passwd, $db, $port, $socket)) {
14        printf("[001] Connect failed, [%d] %s\n", mysqli_connect_errno(), mysqli_connect_error());
15    }
16
17    if (!$link->query("DROP TABLE IF EXISTS tuint") ||
18        !$link->query("DROP TABLE IF EXISTS tsint")) {
19        printf("[002] [%d] %s\n", $link->errno, $link->error);
20    }
21
22    if (!$link->query("CREATE TABLE tuint(a BIGINT UNSIGNED) ENGINE=" . $engine) ||
23        !$link->query("CREATE TABLE tsint(a BIGINT) ENGINE=" . $engine)) {
24        printf("[003] [%d] %s\n", $link->errno, $link->error);
25    }
26
27
28    if (!$stmt1 = $link->prepare("INSERT INTO tuint VALUES(?)"))
29        printf("[004] [%d] %s\n", $link->errno, $link->error);
30
31    if (!$stmt2 = $link->prepare("INSERT INTO tsint VALUES(?)"))
32        printf("[005] [%d] %s\n", $link->errno, $link->error);
33
34    $param = 42;
35
36    if (!$stmt1->bind_param("i", $param))
37        printf("[006] [%d] %s\n", $stmt1->errno, $stmt1->error);
38
39    if (!$stmt2->bind_param("i", $param))
40        printf("[007] [%d] %s\n", $stmt2->errno, $stmt2->error);
41
42    /* first insert normal value to force initial send of types */
43    if (!$stmt1->execute())
44        printf("[008] [%d] %s\n", $stmt1->errno, $stmt1->error);
45
46    if	(!$stmt2->execute())
47        printf("[009] [%d] %s\n", $stmt2->errno, $stmt2->error);
48
49    /* now try values that don't fit in long, on 32bit, new types should be sent or 0 will be inserted */
50    $param = -4294967297;
51    if (!$stmt2->execute())
52        printf("[010] [%d] %s\n", $stmt2->errno, $stmt2->error);
53
54    /* again normal value */
55    $param = 43;
56
57    if (!$stmt1->execute())
58        printf("[011] [%d] %s\n", $stmt1->errno, $stmt1->error);
59
60    if	(!$stmt2->execute())
61        printf("[012] [%d] %s\n", $stmt2->errno, $stmt2->error);
62
63    /* again conversion */
64    $param = -4294967295;
65    if (!$stmt2->execute())
66        printf("[013] [%d] %s\n", $stmt2->errno, $stmt2->error);
67
68    $param = 4294967295;
69    if (!$stmt1->execute())
70        printf("[014] [%d] %s\n", $stmt1->errno, $stmt1->error);
71
72    if	(!$stmt2->execute())
73        printf("[015] [%d] %s\n", $stmt2->errno, $stmt2->error);
74
75    $param = 4294967297;
76    if (!$stmt1->execute())
77        printf("[016] [%d] %s\n", $stmt1->errno, $stmt1->error);
78
79    if	(!$stmt2->execute())
80        printf("[017] [%d] %s\n", $stmt2->errno, $stmt2->error);
81
82    $result = $link->query("SELECT * FROM tsint ORDER BY a ASC");
83    $result2 = $link->query("SELECT * FROM tuint ORDER BY a ASC");
84
85    echo "tsint:\n";
86    while ($row = $result->fetch_assoc()) {
87        var_dump($row);
88    }
89    echo "tuint:\n";
90    while ($row = $result2->fetch_assoc()) {
91        var_dump($row);
92    }
93
94    echo "done";
95?>
96--CLEAN--
97<?php
98require_once('connect.inc');
99
100if (!$link = my_mysqli_connect($host, $user, $passwd, $db, $port, $socket)) {
101    printf("[clean] Cannot connect to the server using host=%s, user=%s, passwd=***, dbname=%s, port=%s, socket=%s\n",
102        $host, $user, $db, $port, $socket);
103}
104
105if (!mysqli_query($link, 'DROP TABLE IF EXISTS tuint')) {
106    printf("[clean] Failed to drop old test table: [%d] %s\n", mysqli_errno($link), mysqli_error($link));
107}
108
109if (!mysqli_query($link, 'DROP TABLE IF EXISTS tsint')) {
110    printf("[clean] Failed to drop old test table: [%d] %s\n", mysqli_errno($link), mysqli_error($link));
111}
112
113mysqli_close($link);
114?>
115--EXPECT--
116tsint:
117array(1) {
118  ["a"]=>
119  string(11) "-4294967297"
120}
121array(1) {
122  ["a"]=>
123  string(11) "-4294967295"
124}
125array(1) {
126  ["a"]=>
127  string(2) "42"
128}
129array(1) {
130  ["a"]=>
131  string(2) "43"
132}
133array(1) {
134  ["a"]=>
135  string(10) "4294967295"
136}
137array(1) {
138  ["a"]=>
139  string(10) "4294967297"
140}
141tuint:
142array(1) {
143  ["a"]=>
144  string(2) "42"
145}
146array(1) {
147  ["a"]=>
148  string(2) "43"
149}
150array(1) {
151  ["a"]=>
152  string(10) "4294967295"
153}
154array(1) {
155  ["a"]=>
156  string(10) "4294967297"
157}
158done
159