Skip to content

Commit dcf44b6

Browse files
committed
Convert one more warning and add tests
1 parent 5f09ee6 commit dcf44b6

File tree

4 files changed

+87
-2
lines changed

4 files changed

+87
-2
lines changed

ext/mysqli/tests/fake_server.inc

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -611,6 +611,13 @@ class my_mysqli_fake_server_conn
611611
$packets = $this->packet_generator->server_query_execute_data_response($field_name);
612612
$this->send($this->packets_to_bytes($packets), "Query execute data $field_name");
613613
}
614+
615+
public function send_too_short_server_ok()
616+
{
617+
$packet = $this->packet_generator->server_ok();
618+
$packet->packet_length = "060000";
619+
$this->send($packet->to_bytes(), "Server OK");
620+
}
614621
}
615622

616623
class my_mysqli_fake_server_process
@@ -807,6 +814,18 @@ function my_mysqli_test_query_response_row_read_two_fields(my_mysqli_fake_server
807814
}
808815
}
809816

817+
function my_mysqli_test_invalid_greeting_packet(my_mysqli_fake_server_conn $conn): void
818+
{
819+
// Send nothing at all to the client
820+
}
821+
822+
function my_mysqli_test_ok_packet_too_short(my_mysqli_fake_server_conn $conn): void
823+
{
824+
$conn->send_server_greetings();
825+
$conn->read();
826+
$conn->send_too_short_server_ok();
827+
}
828+
810829
function run_fake_server(string $test_function, $port = 33305): void
811830
{
812831
$address = '127.0.0.1';
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
--TEST--
2+
AUTH_RESPONSE packet is shorter than expected
3+
--EXTENSIONS--
4+
mysqli
5+
--FILE--
6+
<?php
7+
require_once __DIR__ . '/../fake_server.inc';
8+
9+
$port = 50001;
10+
$servername = "127.0.0.1";
11+
$username = "root";
12+
$password = "";
13+
14+
$process = run_fake_server_in_background('ok_packet_too_short', $port);
15+
$process->wait();
16+
17+
try {
18+
$conn = new mysqli( $servername, $username, $password, "", $port );
19+
} catch (Exception $e) {
20+
echo 'ERROR: ' . $e->getMessage() . PHP_EOL;
21+
}
22+
23+
$process->terminate();
24+
25+
print "done!";
26+
?>
27+
--EXPECTF--
28+
[*] Server started
29+
[*] Connection established
30+
[*] Sending - Server Greeting: 580000000a352e352e352d31302e352e31382d4d6172696144420003000000473e3f6047257c6700fef7080200ff81150000000000000f0000006c6b55463f49335f686c6431006d7973716c5f6e61746976655f70617373776f7264
31+
[*] Received: 6900000185a21a00000000c0080000000000000000000000000000000000000000000000726f6f7400006d7973716c5f6e61746976655f70617373776f7264002c0c5f636c69656e745f6e616d65076d7973716c6e640c5f7365727665725f686f7374093132372e302e302e31
32+
[*] Sending - Server OK: 0600000200000002000000
33+
34+
Warning: mysqli::__construct(): Premature end of data (mysqlnd_wireprotocol.c:732) in %s
35+
ERROR: AUTH_RESPONSE packet is shorter than expected
36+
done!
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
--TEST--
2+
Error while reading greeting packet
3+
--EXTENSIONS--
4+
mysqli
5+
--FILE--
6+
<?php
7+
require_once __DIR__ . '/../fake_server.inc';
8+
9+
$port = 50001;
10+
$servername = "127.0.0.1";
11+
$username = "root";
12+
$password = "";
13+
14+
$process = run_fake_server_in_background('invalid_greeting_packet', $port);
15+
$process->wait();
16+
17+
try {
18+
$conn = new mysqli( $servername, $username, $password, "", $port );
19+
} catch (Exception $e) {
20+
echo 'ERROR: ' . $e->getMessage() . PHP_EOL;
21+
}
22+
23+
$process->terminate();
24+
25+
print "done!";
26+
?>
27+
--EXPECTF--
28+
[*] Server started
29+
[*] Connection established
30+
ERROR: Error while reading greeting packet
31+
done!

ext/mysqlnd/mysqlnd_commands.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -594,8 +594,7 @@ MYSQLND_METHOD(mysqlnd_command, handshake)(MYSQLND_CONN_DATA * const conn, const
594594
conn->payload_decoder_factory->m.init_greet_packet(&greet_packet);
595595

596596
if (FAIL == PACKET_READ(conn, &greet_packet)) {
597-
DBG_ERR("Error while reading greeting packet");
598-
php_error_docref(NULL, E_WARNING, "Error while reading greeting packet. PID=%d", getpid());
597+
SET_CLIENT_ERROR(conn->error_info, CR_MALFORMED_PACKET, UNKNOWN_SQLSTATE, "Error while reading greeting packet");
599598
goto err;
600599
} else if (greet_packet.error_no) {
601600
DBG_ERR_FMT("errorno=%u error=%s", greet_packet.error_no, greet_packet.error);

0 commit comments

Comments
 (0)