From 0d8fc2da541715ad2344d8bd8cc54909c6feddf7 Mon Sep 17 00:00:00 2001 From: Kharhamel Date: Wed, 28 Oct 2020 15:30:29 +0100 Subject: [PATCH] FIX: socket_write correctly handle the default value of its third parameter --- generated/sockets.php | 30 ---------------------- generator/config/specialCasesFunctions.php | 1 + lib/special_cases.php | 30 ++++++++++++++++++++++ 3 files changed, 31 insertions(+), 30 deletions(-) diff --git a/generated/sockets.php b/generated/sockets.php index e530507c..d6cc892b 100644 --- a/generated/sockets.php +++ b/generated/sockets.php @@ -753,36 +753,6 @@ function socket_shutdown($socket, int $how = 2): void } -/** - * The function socket_write writes to the - * socket from the given - * buffer. - * - * @param resource $socket - * @param string $buffer The buffer to be written. - * @param int $length The optional parameter length can specify an - * alternate length of bytes written to the socket. If this length is - * greater than the buffer length, it is silently truncated to the length - * of the buffer. - * @return int Returns the number of bytes successfully written to the socket. - * The error code can be retrieved with - * socket_last_error. This code may be passed to - * socket_strerror to get a textual explanation of the - * error. - * @throws SocketsException - * - */ -function socket_write($socket, string $buffer, int $length = 0): int -{ - error_clear_last(); - $result = \socket_write($socket, $buffer, $length); - if ($result === false) { - throw SocketsException::createFromPhpError(); - } - return $result; -} - - /** * Exports the WSAPROTOCOL_INFO structure into shared memory and returns * an identifier to be used with socket_wsaprotocol_info_import. The diff --git a/generator/config/specialCasesFunctions.php b/generator/config/specialCasesFunctions.php index f56b5613..8943ff59 100644 --- a/generator/config/specialCasesFunctions.php +++ b/generator/config/specialCasesFunctions.php @@ -10,4 +10,5 @@ 'preg_replace', 'openssl_encrypt', 'readdir', + 'socket_write', ]; diff --git a/lib/special_cases.php b/lib/special_cases.php index 05682b8a..d18e2118 100644 --- a/lib/special_cases.php +++ b/lib/special_cases.php @@ -7,6 +7,7 @@ namespace Safe; +use Safe\Exceptions\SocketsException; use const PREG_NO_ERROR; use Safe\Exceptions\ApcException; use Safe\Exceptions\ApcuException; @@ -209,3 +210,32 @@ function openssl_encrypt(string $data, string $method, string $key, int $options } return $result; } + +/** + * The function socket_write writes to the + * socket from the given + * buffer. + * + * @param resource $socket + * @param string $buffer The buffer to be written. + * @param int $length The optional parameter length can specify an + * alternate length of bytes written to the socket. If this length is + * greater than the buffer length, it is silently truncated to the length + * of the buffer. + * @return int Returns the number of bytes successfully written to the socket. + * The error code can be retrieved with + * socket_last_error. This code may be passed to + * socket_strerror to get a textual explanation of the + * error. + * @throws SocketsException + * + */ +function socket_write($socket, string $buffer, int $length = 0): int +{ + error_clear_last(); + $result = $length === 0 ? \socket_write($socket, $buffer) : \socket_write($socket, $buffer, $length); + if ($result === false) { + throw SocketsException::createFromPhpError(); + } + return $result; +}