Skip to content

Commit 40850b2

Browse files
committed
fix mem leaks and tests
1 parent 0baa9d0 commit 40850b2

File tree

2 files changed

+12
-4
lines changed

2 files changed

+12
-4
lines changed

‎ext/sockets/sockets.c

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1698,6 +1698,7 @@ PHP_FUNCTION(socket_recvfrom)
16981698
zval *index = arg6;
16991699
if (recv_flags > 0 && !(recv_flags & (MSG_PEEK|MSG_DONTWAIT|MSG_ERRQUEUE))) {
17001700
zend_argument_value_error(4, "must set one the flags MSG_PEEK, MSG_DONTWAIT, MSG_ERRQUEUE");
1701+
zend_string_efree(recv_buf);
17011702
RETURN_THROWS();
17021703
}
17031704

@@ -1706,6 +1707,7 @@ PHP_FUNCTION(socket_recvfrom)
17061707
// TODO: SOCK_DGRAM support
17071708
if (protoid != SOCK_RAW) {
17081709
zend_argument_value_error(1, "must be SOCK_RAW socket type");
1710+
zend_string_efree(recv_buf);
17091711
RETURN_THROWS();
17101712
}
17111713
slen = sizeof(sll);
@@ -1724,9 +1726,8 @@ PHP_FUNCTION(socket_recvfrom)
17241726

17251727
ZEND_ASSERT(slen >= ETH_HLEN);
17261728

1727-
if (retval < recv_len) {
1728-
recv_buf = zend_string_truncate(recv_buf, retval, 0);
1729-
}
1729+
ZSTR_LEN(recv_buf) = retval;
1730+
ZSTR_VAL(recv_buf)[ZSTR_LEN(recv_buf)] = '\0';
17301731

17311732
if (UNEXPECTED(!if_indextoname(sll.sll_ifindex, ifrname))) {
17321733
PHP_SOCKET_ERROR(php_sock, "unable to get the interface name", errno);

‎ext/sockets/tests/socket_afpacket_error.phpt

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,12 @@ if (PHP_INT_SIZE != 8) {
3838
echo $e->getMessage(), PHP_EOL;
3939
}
4040

41+
try {
42+
socket_recvfrom($s_c, $rsp, strlen($buf), MSG_TRUNC, $addr);
43+
} catch (\ValueError $e) {
44+
echo $e->getMessage(), PHP_EOL;
45+
}
46+
4147
socket_close($s_s);
4248
socket_close($s_c);
4349

@@ -267,6 +273,7 @@ if (PHP_INT_SIZE != 8) {
267273
--EXPECTF--
268274
int(%d)
269275
unsupported ethernet protocol
276+
socket_recvfrom(): Argument #4 ($flags) must set one the flags MSG_PEEK, MSG_DONTWAIT, MSG_ERRQUEUE
270277
int(60)
271278
invalid transport header length
272279
socket_sendto(): Argument #3 ($length) must be at least 60 for AF_PACKET
@@ -280,9 +287,9 @@ int(%d)
280287
bool(true)
281288
bool(true)
282289
int(%d)
283-
invalid ethernet loop header
284290
socket_sendto(): Argument #3 ($length) must be at least 60 for AF_PACKET
285291
int(%d)
292+
object(Socket\EthernetPacket)#1 (7) {
286293
["headerSize"]=>
287294
int(14)
288295
["rawPacket"]=>

0 commit comments

Comments
 (0)