-
Notifications
You must be signed in to change notification settings - Fork 7.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
ext/sockets: Additional refactorings to
socket_set_option()
(#17186)
* ext/socket: Reduce scope of variables * ext/socket: Throw TypeErrors when not passing a string for options requiring one * ext/sockets: Throw ValueError when string has null bytes for options not passing string length * ext/sockets: Throw ValueError when string is too long And replace calls to strlcpy to memcpy
- Loading branch information
Showing
7 changed files
with
243 additions
and
27 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
44 changes: 44 additions & 0 deletions
44
ext/sockets/tests/socket_set_option_invalid_value_for_SOL_FILTER_SO_ATTACH.phpt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
--TEST-- | ||
socket_set_option($socket, SOL_SOCKET, SO_BINDTODEVICE, INVALID_TYPE_FOR_OPTION) | ||
--EXTENSIONS-- | ||
sockets | ||
--SKIPIF-- | ||
<?php | ||
|
||
if (!defined('SOL_FILTER')) { | ||
die('skip SOL_FILTER not available.'); | ||
} | ||
if (!defined('FIL_ATTACH')) { | ||
die('skip FIL_ATTACH not available.'); | ||
} | ||
if (!defined('FIL_DETACH')) { | ||
die('skip FIL_DETACH not available.'); | ||
} | ||
|
||
?> | ||
--FILE-- | ||
<?php | ||
$socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP); | ||
if (!$socket) { | ||
die('Unable to create AF_INET socket [socket]'); | ||
} | ||
|
||
// TODO Warning when using FIL_ATTACH/FIL_DETACH option when level is not SOL_FILTER | ||
try { | ||
$ret = socket_set_option($socket, SOL_FILTER, FIL_ATTACH, new stdClass()); | ||
var_dump($ret); | ||
} catch (Throwable $e) { | ||
echo $e::class, ': ', $e->getMessage(), PHP_EOL; | ||
} | ||
try { | ||
$ret = socket_set_option($socket, SOL_FILTER, FIL_DETACH, new stdClass()); | ||
var_dump($ret); | ||
} catch (Throwable $e) { | ||
echo $e::class, ': ', $e->getMessage(), PHP_EOL; | ||
} | ||
|
||
socket_close($socket); | ||
?> | ||
--EXPECT-- | ||
TypeError: socket_set_option(): Argument #4 ($value) must be of type string when argument #3 ($option) is FIL_ATTACH or FIL_DETACH, stdClass given | ||
TypeError: socket_set_option(): Argument #4 ($value) must be of type string when argument #3 ($option) is FIL_ATTACH or FIL_DETACH, stdClass given |
47 changes: 47 additions & 0 deletions
47
ext/sockets/tests/socket_set_option_invalid_value_for_SOL_SOCKET_SO_ACCEPTFILTER.phpt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
--TEST-- | ||
socket_set_option($socket, SOL_SOCKET, SO_ACCEPTFILTER, INVALID_TYPE_FOR_OPTION) | ||
--EXTENSIONS-- | ||
sockets | ||
--SKIPIF-- | ||
<?php | ||
|
||
if (!defined('SOL_SOCKET')) { | ||
die('skip SOL_SOCKET not available.'); | ||
} | ||
if (!defined('SO_ACCEPTFILTER')) { | ||
die('skip SO_ACCEPTFILTER not available.'); | ||
} | ||
|
||
?> | ||
--FILE-- | ||
<?php | ||
$socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP); | ||
if (!$socket) { | ||
die('Unable to create AF_INET socket [socket]'); | ||
} | ||
|
||
try { | ||
$ret = socket_set_option($socket, SOL_SOCKET, SO_ACCEPTFILTER, new stdClass()); | ||
var_dump($ret); | ||
} catch (Throwable $e) { | ||
echo $e::class, ': ', $e->getMessage(), PHP_EOL; | ||
} | ||
try { | ||
$ret = socket_set_option($socket, SOL_SOCKET, SO_ACCEPTFILTER, "string\0with\0null\0bytes"); | ||
var_dump($ret); | ||
} catch (Throwable $e) { | ||
echo $e::class, ': ', $e->getMessage(), PHP_EOL; | ||
} | ||
try { | ||
$ret = socket_set_option($socket, SOL_SOCKET, SO_ACCEPTFILTER, str_repeat("a", 2048)); | ||
var_dump($ret); | ||
} catch (Throwable $e) { | ||
echo $e::class, ': ', $e->getMessage(), PHP_EOL; | ||
} | ||
|
||
socket_close($socket); | ||
?> | ||
--EXPECTF-- | ||
TypeError: socket_set_option(): Argument #4 ($value) must be of type string when argument #3 ($option) is SO_ACCEPTFILTER, stdClass given | ||
ValueError: socket_set_option(): Argument #4 ($value) must not contain null bytes when argument #3 ($option) is SO_ACCEPTFILTER | ||
ValueError: socket_set_option(): Argument #4 ($value) must be less than %d bytes when argument #3 ($option) is SO_ACCEPTFILTER |
33 changes: 33 additions & 0 deletions
33
ext/sockets/tests/socket_set_option_invalid_value_for_SOL_SOCKET_SO_BINDTODEVICE.phpt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
--TEST-- | ||
socket_set_option($socket, SOL_SOCKET, SO_BINDTODEVICE, INVALID_TYPE_FOR_OPTION) | ||
--EXTENSIONS-- | ||
sockets | ||
--SKIPIF-- | ||
<?php | ||
|
||
if (!defined('SOL_SOCKET')) { | ||
die('skip SOL_SOCKET not available.'); | ||
} | ||
if (!defined('SO_BINDTODEVICE')) { | ||
die('skip SO_BINDTODEVICE not available.'); | ||
} | ||
|
||
?> | ||
--FILE-- | ||
<?php | ||
$socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP); | ||
if (!$socket) { | ||
die('Unable to create AF_INET socket [socket]'); | ||
} | ||
|
||
try { | ||
$ret = socket_set_option($socket, SOL_SOCKET, SO_BINDTODEVICE, new stdClass()); | ||
var_dump($ret); | ||
} catch (Throwable $e) { | ||
echo $e::class, ': ', $e->getMessage(), PHP_EOL; | ||
} | ||
|
||
socket_close($socket); | ||
?> | ||
--EXPECT-- | ||
TypeError: socket_set_option(): Argument #4 ($value) must be of type string when argument #3 ($option) is SO_BINDTODEVICE, stdClass given |
30 changes: 30 additions & 0 deletions
30
ext/sockets/tests/socket_set_option_invalid_value_for_SOL_TCP_TCP_CONGESTION.phpt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
--TEST-- | ||
socket_set_option($socket, SOL_TCP, TCP_CONGESTION, INVALID_TYPE_FOR_OPTION) | ||
--EXTENSIONS-- | ||
sockets | ||
--SKIPIF-- | ||
<?php | ||
|
||
if (!defined('TCP_CONGESTION')) { | ||
die('skip TCP_CONGESTION not available.'); | ||
} | ||
|
||
?> | ||
--FILE-- | ||
<?php | ||
$socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP); | ||
if (!$socket) { | ||
die('Unable to create AF_INET socket [socket]'); | ||
} | ||
|
||
try { | ||
$ret = socket_set_option($socket, SOL_TCP, TCP_CONGESTION, new stdClass()); | ||
var_dump($ret); | ||
} catch (Throwable $e) { | ||
echo $e::class, ': ', $e->getMessage(), PHP_EOL; | ||
} | ||
|
||
socket_close($socket); | ||
?> | ||
--EXPECT-- | ||
TypeError: socket_set_option(): Argument #4 ($value) must be of type string when argument #3 ($option) is TCP_CONGESTION, stdClass given |
44 changes: 44 additions & 0 deletions
44
ext/sockets/tests/socket_set_option_invalid_value_for_SOL_TCP_TCP_FUNCTION_BLK.phpt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
--TEST-- | ||
socket_set_option($socket, SOL_TCP, TCP_FUNCTION_BLK, INVALID_TYPE_FOR_OPTION) | ||
--EXTENSIONS-- | ||
sockets | ||
--SKIPIF-- | ||
<?php | ||
|
||
if (!defined('TCP_FUNCTION_BLK')) { | ||
die('skip TCP_FUNCTION_BLK not available.'); | ||
} | ||
|
||
?> | ||
--FILE-- | ||
<?php | ||
$socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP); | ||
if (!$socket) { | ||
die('Unable to create AF_INET socket [socket]'); | ||
} | ||
|
||
try { | ||
$ret = socket_set_option($socket, SOL_TCP, TCP_FUNCTION_BLK, new stdClass()); | ||
var_dump($ret); | ||
} catch (Throwable $e) { | ||
echo $e::class, ': ', $e->getMessage(), PHP_EOL; | ||
} | ||
try { | ||
$ret = socket_set_option($socket, SOL_TCP, TCP_FUNCTION_BLK, "string\0with\0null\0bytes"); | ||
var_dump($ret); | ||
} catch (Throwable $e) { | ||
echo $e::class, ': ', $e->getMessage(), PHP_EOL; | ||
} | ||
try { | ||
$ret = socket_set_option($socket, SOL_TCP, TCP_FUNCTION_BLK, str_repeat("a", 2048)); | ||
var_dump($ret); | ||
} catch (Throwable $e) { | ||
echo $e::class, ': ', $e->getMessage(), PHP_EOL; | ||
} | ||
|
||
socket_close($socket); | ||
?> | ||
--EXPECTF-- | ||
TypeError: socket_set_option(): Argument #4 ($value) must be of type string when argument #3 ($option) is TCP_FUNCTION_BLK, stdClass given | ||
ValueError: socket_set_option(): Argument #4 ($value) must not contain null bytes when argument #3 ($option) is TCP_FUNCTION_BLK | ||
ValueError: socket_set_option(): Argument #4 ($value) must be less than %d bytes when argument #3 ($option) is TCP_FUNCTION_BLK |