1--TEST--
2INSERT and packet overflow
3--SKIPIF--
4<?php
5require_once('skipif.inc');
6require_once('skipifconnectfailure.inc');
7
8if (!$link = my_mysqli_connect($host, $user, $passwd, $db, $port, $socket))
9    die(sprintf("SKIP [%d] %s\n", mysqli_connect_errno(), mysqli_connect_error()));
10
11$max_len = pow(2, 24);
12if (!$res = mysqli_query($link, "SHOW GLOBAL VARIABLES LIKE 'max_allowed_packet'"))
13    die(sprintf("SKIP [%d] %s\n", mysqli_errno($link), mysqli_error($link)));
14
15if (!mysqli_query($link, "SET NAMES 'latin1'"))
16    die(sprintf("SKIP [%d] %s\n", mysqli_errno($link), mysqli_error($link)));
17
18mysqli_close($link);
19?>
20--INI--
21memory_limit=256M
22--FILE--
23<?php
24    require('connect.inc');
25    if (!$link = my_mysqli_connect($host, $user, $passwd, $db, $port, $socket))
26        printf("[001] [%d] %s\n", mysqli_connect_errno(), mysqli_connect_error());
27
28    if (!$res = mysqli_query($link, "SHOW GLOBAL VARIABLES LIKE 'max_allowed_packet'"))
29        printf("[002] [%d] %s\n", mysqli_errno($link), mysqli_error($link));
30
31    if (!$row = mysqli_fetch_assoc($res))
32        printf("[003] [%d] %s\n", mysqli_errno($link), mysqli_error($link));
33
34    mysqli_free_result($res);
35
36    if (0 === ($org_max_allowed_packet = (int)$row['Value']))
37        printf("[004] Cannot determine max_allowed_packet size and/or bogus max_allowed_packet setting used.\n");
38
39    $max_len = pow(2, 24);
40    if ($org_max_allowed_packet < $max_len) {
41        if (!mysqli_query($link, "SET GLOBAL max_allowed_packet = " . ($max_len + 100))) {
42            if (1227 == mysqli_errno($link)) {
43                /* [1227] Access denied; you need the SUPER privilege for this operation */
44                print "done!";
45                exit(0);
46            } else {
47                printf("[005] [%d] %s\n", mysqli_errno($link), mysqli_error($link));
48            }
49        }
50    }
51    mysqli_close($link);
52    if (!$link = my_mysqli_connect($host, $user, $passwd, $db, $port, $socket))
53        printf("[006] [%d] %s\n", mysqli_connect_errno(), mysqli_connect_error());
54
55    if (!mysqli_query($link, "SET NAMES 'latin1'"))
56        printf("[007] [%d] %s\n", mysqli_connect_errno(), mysqli_connect_error());
57
58    if (!$res = mysqli_query($link, "SHOW GLOBAL VARIABLES LIKE 'max_allowed_packet'"))
59        printf("[008] [%d] %s\n", mysqli_errno($link), mysqli_error($link));
60
61    if (!$row = mysqli_fetch_assoc($res))
62        printf("[009] [%d] %s\n", mysqli_errno($link), mysqli_error($link));
63
64    mysqli_free_result($res);
65
66    if (0 === ($max_allowed_packet = (int)$row['Value']))
67        printf("[010] Cannot determine max_allowed_packet size and/or bogus max_allowed_packet setting used.\n");
68
69    $max_len = pow(2, 24);
70    if ($max_allowed_packet < $max_len) {
71        printf("[011] Failed to change max_allowed_packet");
72    }
73
74    $table_name = 'test__mysqli_insert_packet_overflow';
75    if (!mysqli_query($link, "DROP TABLE IF EXISTS {$table_name}")) {
76        printf("[012] Failed to drop old test table: [%d] %s\n", mysqli_errno($link), mysqli_error($link));
77    }
78
79    if (!mysqli_query($link, "CREATE TABLE {$table_name}(col_blob LONGBLOB) ENGINE=" . $engine))
80        printf("[013] [%d] %s\n", mysqli_errno($link), mysqli_error($link));
81
82    $query_prefix = "INSERT INTO {$table_name}(col_blob) VALUES ('";
83    $query_postfix = "')";
84    $query_len = strlen($query_prefix) + strlen($query_postfix);
85    $com_query_len = 2;
86
87
88    $blob = str_repeat('a', $max_len - $com_query_len - $query_len);
89    $query = sprintf("%s%s%s", $query_prefix, $blob, $query_postfix);
90
91    if (!mysqli_query($link, $query))
92        printf("[014] max_allowed_packet = %d, strlen(query) = %d, [%d] %s\n", $max_allowed_packet, strlen($query), mysqli_errno($link), mysqli_error($link));
93
94    if (!$res = mysqli_query($link, "SELECT col_blob FROM {$table_name}"))
95        printf("[015] [%d] %s\n", mysqli_errno($link), mysqli_error($link));
96
97    if (!$row = mysqli_fetch_assoc($res)) {
98        printf("[016] [%d] %s\n", mysqli_errno($link), mysqli_error($link));
99    } else {
100        if ($row['col_blob'] != $blob) {
101            printf("[017] Blob seems wrong, dumping data\n");
102            var_dump(strlen($row['col_blob']));
103            var_dump(strlen($blob));
104        }
105        mysqli_free_result($res);
106    }
107
108    if (!mysqli_query($link, "SET GLOBAL max_allowed_packet = " . $org_max_allowed_packet))
109        if (1227 != mysqli_errno($link))
110            printf("[018] [%d] %s\n", mysqli_errno($link), mysqli_error($link));
111
112    mysqli_close($link);
113
114    print "done!";
115?>
116--EXPECT--
117done!
118