xref: /PHP-8.0/ext/mysqli/tests/bug49442.phpt (revision e3e67b72)
1--TEST--
2Bug #49422 (mysqlnd: mysqli_real_connect() and LOAD DATA INFILE crash)
3--SKIPIF--
4<?php
5require_once('skipif.inc');
6require_once('skipifconnectfailure.inc');
7
8$link = mysqli_init();
9if (!my_mysqli_real_connect($link, $host, $user, $passwd, $db, $port, $socket)) {
10    die(sprintf("skip Connect failed, [%d] %s\n", mysqli_connect_errno(), mysqli_connect_error()));
11}
12
13include_once("local_infile_tools.inc");
14if ($msg = check_local_infile_support($link, $engine))
15    die(sprintf("skip %s, [%d] %s", $msg, $link->errno, $link->error));
16
17mysqli_close($link);
18?>
19--INI--
20mysqli.allow_local_infile=1
21mysqli.allow_persistent=1
22mysqli.max_persistent=1
23--FILE--
24<?php
25    include ("connect.inc");
26
27    $link = mysqli_init();
28    if (!my_mysqli_real_connect($link, $host, $user, $passwd, $db, $port, $socket)) {
29        printf("[001] Connect failed, [%d] %s\n", mysqli_connect_errno(), mysqli_connect_error());
30    }
31
32    if (!mysqli_query($link, 'DROP TABLE IF EXISTS test')) {
33        printf("[002] Failed to drop old test table: [%d] %s\n", mysqli_errno($link), mysqli_error($link));
34    }
35
36    if (!mysqli_query($link, 'CREATE TABLE test(id INT, label CHAR(1), PRIMARY KEY(id)) ENGINE=' . $engine)) {
37        printf("[003] Failed to create test table: [%d] %s\n", mysqli_errno($link), mysqli_error($link));
38    }
39
40    include("local_infile_tools.inc");
41    $file = create_standard_csv(4);
42
43    if (!@mysqli_query($link, sprintf("LOAD DATA LOCAL INFILE '%s'
44            INTO TABLE test
45            FIELDS TERMINATED BY ';' OPTIONALLY ENCLOSED BY '\''
46            LINES TERMINATED BY '\n'",
47            mysqli_real_escape_string($link, $file)))) {
48            printf("[005] [%d] %s\n",  mysqli_errno($link), mysqli_error($link));
49    }
50
51    if (!$res = mysqli_query($link, "SELECT * FROM test ORDER BY id"))
52        printf("[006] [%d] %s\n",  mysqli_errno($link), mysqli_error($link));
53
54    $rows = array();
55    while ($row = mysqli_fetch_assoc($res)) {
56        var_dump($row);
57        $rows[] = $row;
58    }
59
60    mysqli_free_result($res);
61
62    mysqli_query($link, "DELETE FROM test");
63    mysqli_close($link);
64
65    if ($IS_MYSQLND) {
66        /*
67            mysqlnd makes a connection created through mysql_init()/mysqli_real_connect() always a 'persistent' one.
68            At this point 'persistent' is not to be confused with what a user calls a 'persistent' - in this case
69            'persistent' means that mysqlnd uses malloc() instead of emalloc(). nothing else. ext/mysqli will
70            not consider it as a 'persistent' connection in a user sense, ext/mysqli will not apply max_persistent etc.
71            Its only about malloc() vs. emalloc().
72
73            However, the bug is about malloc() and efree(). You can make make mysqlnd use malloc() by either using
74            pconnect or mysql_init() - so we should test pconnect as well..
75        */
76        $host = 'p:' . $host;
77        if (!$link = my_mysqli_connect($host, $user, $passwd, $db, $port, $socket)) {
78            printf("[007] Connect failed, [%d] %s\n", mysqli_connect_errno(), mysqli_connect_error());
79        }
80
81        /* bug happened during query processing */
82        if (!@mysqli_query($link, sprintf("LOAD DATA LOCAL INFILE '%s'
83            INTO TABLE test
84            FIELDS TERMINATED BY ';' OPTIONALLY ENCLOSED BY '\''
85            LINES TERMINATED BY '\n'",
86            mysqli_real_escape_string($link, $file)))) {
87            printf("[008] [%d] %s\n",  mysqli_errno($link), mysqli_error($link));
88        }
89
90        /* we survived? that's good enough... */
91
92        if (!$res = mysqli_query($link, "SELECT * FROM test ORDER BY id"))
93            printf("[009] [%d] %s\n",  mysqli_errno($link), mysqli_error($link));
94
95        $i = 0;
96        while ($row = mysqli_fetch_assoc($res)) {
97            if (($row['id'] != $rows[$i]['id']) || ($row['label'] != $rows[$i]['label'])) {
98                printf("[010] Wrong values, check manually!\n");
99            }
100            $i++;
101        }
102        mysqli_close($link);
103    }
104
105    print "done!";
106?>
107--CLEAN--
108<?php
109	require_once("clean_table.inc");
110?>
111--EXPECT--
112array(2) {
113  ["id"]=>
114  string(2) "97"
115  ["label"]=>
116  string(1) "x"
117}
118array(2) {
119  ["id"]=>
120  string(2) "98"
121  ["label"]=>
122  string(1) "y"
123}
124array(2) {
125  ["id"]=>
126  string(2) "99"
127  ["label"]=>
128  string(1) "z"
129}
130done!
131