Skip to content

Commit 8cbc0c5

Browse files
committed
Fix phpGH-17921 socket_read/socket_recv overflows on buffer size.
update the existing checks to be more straightforward instead of counting on undefined behavior. close phpGH-17923
1 parent 00a772b commit 8cbc0c5

File tree

3 files changed

+24
-2
lines changed

3 files changed

+24
-2
lines changed

NEWS

+4
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,10 @@ PHP NEWS
6565
. Fixed bug GH-15902 (Core dumped in ext/reflection/php_reflection.c).
6666
(DanielEScherzer)
6767

68+
- Sockets:
69+
. Fixed bug GH-17921 (socket_read/socket_recv overflow on buffer size).
70+
(David Carlier)
71+
6872
- Standard:
6973
. Fixed bug #72666 (stat cache clearing inconsistent between file:// paths
7074
and plain paths). (Jakub Zelenka)

ext/sockets/sockets.c

+2-2
Original file line numberDiff line numberDiff line change
@@ -884,7 +884,7 @@ PHP_FUNCTION(socket_read)
884884
ENSURE_SOCKET_VALID(php_sock);
885885

886886
/* overflow check */
887-
if ((length + 1) < 2) {
887+
if (length <= 0 || length == ZEND_LONG_MAX) {
888888
RETURN_FALSE;
889889
}
890890

@@ -1326,7 +1326,7 @@ PHP_FUNCTION(socket_recv)
13261326
ENSURE_SOCKET_VALID(php_sock);
13271327

13281328
/* overflow check */
1329-
if ((len + 1) < 2) {
1329+
if (len <= 0 || len == ZEND_LONG_MAX) {
13301330
RETURN_FALSE;
13311331
}
13321332

ext/sockets/tests/gh17921.phpt

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
--TEST--
2+
GH-16267 - overflow on socket_strerror argument
3+
--EXTENSIONS--
4+
sockets
5+
--FILE--
6+
<?php
7+
$s_c_l = socket_create_listen(0);
8+
var_dump(socket_read($s_c_l, PHP_INT_MAX));
9+
var_dump(socket_read($s_c_l, PHP_INT_MIN));
10+
$a = "";
11+
var_dump(socket_recv($s_c_l, $a, PHP_INT_MAX, 0));
12+
var_dump(socket_recv($s_c_l, $a, PHP_INT_MIN, 0));
13+
?>
14+
--EXPECT--
15+
bool(false)
16+
bool(false)
17+
bool(false)
18+
bool(false)

0 commit comments

Comments
 (0)