Skip to content

Commit a183f11

Browse files
lacatoireNickSdot
andauthored
ext/sockets: socket_cmsg_space() returns int, never null (#23345)
* ext/sockets: socket_cmsg_space() returns int, never null The nullable return type dates from the stub introduction, when the error paths were warnings followed by a bare return. PHP 8.0 turned them into ValueError, so every exit is now either RETURN_LONG or RETURN_THROWS. Co-authored-by: NickSdot <32384907+NickSdot@users.noreply.github.com>
1 parent dcdc8ab commit a183f11

4 files changed

Lines changed: 53 additions & 3 deletions

File tree

UPGRADING

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -604,6 +604,9 @@ PHP 8.6 UPGRADE NOTES
604604
. socket_addrinfo_lookup() now has an additional optional argument $error
605605
when not null, and on failure, gives the error code (one of the EAI_*
606606
constants).
607+
. socket_cmsg_space() return type has been narrowed from ?int to int. Every
608+
failure path has thrown a ValueError since PHP 8.0, so null was never
609+
returned.
607610

608611
- Standard:
609612
. ini_get_all() now includes a "builtin_default_value" element for each

ext/sockets/sockets.stub.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2313,7 +2313,7 @@ function socket_sendmsg(Socket $socket, array $message, int $flags = 0): int|fal
23132313

23142314
function socket_recvmsg(Socket $socket, array &$message, int $flags = 0): int|false {}
23152315

2316-
function socket_cmsg_space(int $level, int $type, int $num = 0): ?int {}
2316+
function socket_cmsg_space(int $level, int $type, int $num = 0): int {}
23172317

23182318
/**
23192319
* @return array<int, AddressInfo>|false

ext/sockets/sockets_arginfo.h

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
--TEST--
2+
socket_cmsg_space() always returns int, never null
3+
--EXTENSIONS--
4+
sockets
5+
--SKIPIF--
6+
<?php
7+
if (PHP_OS_FAMILY === 'Windows') {
8+
die('skip SCM_RIGHTS not available on Windows');
9+
}
10+
if (!defined('SCM_RIGHTS')) {
11+
die('skip SCM_RIGHTS not defined on this platform');
12+
}
13+
?>
14+
--FILE--
15+
<?php
16+
// Happy path: returns int, never null
17+
$r = socket_cmsg_space(SOL_SOCKET, SCM_RIGHTS, 1);
18+
var_dump(get_debug_type($r));
19+
20+
// Unknown level/type pair
21+
try {
22+
socket_cmsg_space(999999, 999999);
23+
} catch (Throwable $e) {
24+
echo $e::class, ': ', $e->getMessage(), "\n";
25+
}
26+
27+
// Negative $num
28+
try {
29+
socket_cmsg_space(SOL_SOCKET, SCM_RIGHTS, -1);
30+
} catch (Throwable $e) {
31+
echo $e::class, ': ', $e->getMessage(), "\n";
32+
}
33+
34+
// $num overflows int (64-bit only: PHP_INT_MAX > INT_MAX)
35+
if (PHP_INT_SIZE >= 8) {
36+
try {
37+
socket_cmsg_space(SOL_SOCKET, SCM_RIGHTS, PHP_INT_MAX);
38+
} catch (Throwable $e) {
39+
echo $e::class, ': ', $e->getMessage(), "\n";
40+
}
41+
}
42+
?>
43+
--EXPECT--
44+
string(3) "int"
45+
ValueError: Pair level 999999 and/or type 999999 is not supported
46+
ValueError: socket_cmsg_space(): Argument #3 ($num) must be greater than or equal to 0
47+
ValueError: socket_cmsg_space(): Argument #3 ($num) must be between -2147483648 and 2147483647

0 commit comments

Comments
 (0)