xref: /php-src/ext/mysqli/tests/gh15432.phpt (revision a1ab8462)
1--TEST--
2Bug GH-15432 (Heap corruption when querying a vector/PHP crashes when processing a MySQL DB query with a new Vector format introduced in MySQL 9.0)
3--EXTENSIONS--
4mysqli
5--SKIPIF--
6<?php
7require 'connect.inc';
8$link = @my_mysqli_connect($host, $user, $passwd, $db, $port, $socket);
9if ($link === false) {
10    die(sprintf("skip Can't connect to MySQL Server - [%d] %s", mysqli_connect_errno(), mysqli_connect_error()));
11}
12if ($link->server_version < 90000 || $link->server_version >= 10_00_00) {
13    die("skip MySQL 9.0.0+ needed");
14}
15?>
16--FILE--
17<?php
18require 'connect.inc';
19
20mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
21
22$link = my_mysqli_connect($host, $user, $passwd, $db, $port, $socket);
23
24$expected = '00000040000040400000a040';
25
26mysqli_query($link, "DROP TABLE IF EXISTS test");
27mysqli_query($link, "CREATE TABLE test(vectorfield VECTOR)");
28mysqli_query($link, 'INSERT INTO test VALUES (TO_VECTOR("[2, 3, 5]"))');
29
30// Textual protocol
31$result = mysqli_query($link, "SELECT vectorfield FROM test")->fetch_column();
32$value = bin2hex($result);
33if($value !== $expected) {
34    printf("[001] Expecting %s/%s, got %s/%s\n",
35        gettype($expected), $expected,
36        gettype($value), $value);
37}
38
39// Binary protocol
40$result = $link->execute_query("SELECT vectorfield FROM test")->fetch_column();
41$value = bin2hex($result);
42if($value !== $expected) {
43    printf("[002] Expecting %s/%s, got %s/%s\n",
44        gettype($expected), $expected,
45        gettype($value), $value);
46}
47
48// Testing inverse to make sure the value hasn't been changed
49$expected = '[2.00000e+00,3.00000e+00,5.00000e+00]';
50$result = $link->execute_query("SELECT VECTOR_TO_STRING(0x". $value .")")->fetch_column();
51if($result !== $expected) {
52    printf("[002] Expecting %s/%s, got %s/%s\n",
53        gettype($expected), $expected,
54        gettype($result), $result);
55}
56
57echo "OK";
58?>
59--CLEAN--
60<?php
61require_once 'clean_table.inc';
62?>
63--EXPECT--
64OK
65