Skip to content

Commit

Permalink
Merge pull request #252 from Kharhamel/socketWrite
Browse files Browse the repository at this point in the history
FIX: socket_write correctly handle the default value of its third parameter
  • Loading branch information
Kharhamel authored Oct 28, 2020
2 parents dcf6e2b + 0d8fc2d commit a8ab087
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 30 deletions.
30 changes: 0 additions & 30 deletions generated/sockets.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions generator/config/specialCasesFunctions.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,5 @@
'preg_replace',
'openssl_encrypt',
'readdir',
'socket_write',
];
30 changes: 30 additions & 0 deletions lib/special_cases.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

namespace Safe;

use Safe\Exceptions\SocketsException;
use const PREG_NO_ERROR;
use Safe\Exceptions\ApcException;
use Safe\Exceptions\ApcuException;
Expand Down Expand Up @@ -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;
}

0 comments on commit a8ab087

Please sign in to comment.