Skip to content

Commit ba42c45

Browse files
committed
fix leaks on failure
1 parent 459a2ce commit ba42c45

File tree

2 files changed

+13
-20
lines changed

2 files changed

+13
-20
lines changed

ext/sockets/sockets.c

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1655,9 +1655,6 @@ PHP_FUNCTION(socket_recvfrom)
16551655

16561656
zval obj;
16571657
object_init_ex(&obj, socket_ethinfo_ce);
1658-
zend_update_property_string(Z_OBJCE(obj), Z_OBJ(obj), ZEND_STRL("macsrc"), ether_ntoa((struct ether_addr *)e->h_source));
1659-
zend_update_property_string(Z_OBJCE(obj), Z_OBJ(obj), ZEND_STRL("macdst"), ether_ntoa((struct ether_addr *)e->h_dest));
1660-
zend_update_property_long(Z_OBJCE(obj), Z_OBJ(obj), ZEND_STRL("ethprotocol"), protocol);
16611658
array_init(&zpayload);
16621659

16631660
switch (protocol) {
@@ -1708,10 +1705,16 @@ PHP_FUNCTION(socket_recvfrom)
17081705
break;
17091706
}
17101707
default:
1708+
zend_string_efree(recv_buf);
1709+
zval_ptr_dtor(&zpayload);
1710+
zval_ptr_dtor(&obj);
17111711
zend_value_error("unsupported ethernet protocol");
17121712
RETURN_THROWS();
17131713
}
17141714

1715+
zend_update_property_string(Z_OBJCE(obj), Z_OBJ(obj), ZEND_STRL("macsrc"), ether_ntoa((struct ether_addr *)e->h_source));
1716+
zend_update_property_string(Z_OBJCE(obj), Z_OBJ(obj), ZEND_STRL("macdst"), ether_ntoa((struct ether_addr *)e->h_dest));
1717+
zend_update_property_long(Z_OBJCE(obj), Z_OBJ(obj), ZEND_STRL("ethprotocol"), protocol);
17151718
zend_update_property(Z_OBJCE(obj), Z_OBJ(obj), ZEND_STRL("payload"), &zpayload);
17161719

17171720
ZEND_TRY_ASSIGN_REF_COPY(arg2, &obj);
@@ -1743,6 +1746,7 @@ PHP_FUNCTION(socket_sendto)
17431746
#endif
17441747
#ifdef AF_PACKET
17451748
struct sockaddr_ll sll;
1749+
unsigned char halen;
17461750
#endif
17471751
int retval;
17481752
size_t buf_len;
@@ -1825,17 +1829,20 @@ PHP_FUNCTION(socket_sendto)
18251829
RETURN_THROWS();
18261830
}
18271831

1832+
halen = addr_len > ETH_ALEN ? ETH_ALEN : (unsigned char)addr_len;
1833+
18281834
memset(&sll, 0, sizeof(sll));
1835+
memcpy(sll.sll_addr, addr, halen);
18291836
sll.sll_family = AF_PACKET;
18301837
sll.sll_ifindex = port;
1831-
sll.sll_halen = ETH_ALEN;
1838+
sll.sll_halen = halen;
18321839

18331840
// TODO allows to use more user friendly type to replace raw buffer usage
18341841
retval = sendto(php_sock->bsd_socket, buf, ((size_t)len > buf_len) ? buf_len : (size_t)len, flags, (struct sockaddr *) &sll, sizeof(sll));
18351842
break;
18361843
#endif
18371844
default:
1838-
zend_argument_value_error(1, "must be one of AF_UNIX, AF_INET, or AF_INET6");
1845+
zend_argument_value_error(1, "must be one of AF_UNIX, AF_INET, AF_PACKET or AF_INET6");
18391846
RETURN_THROWS();
18401847
}
18411848

ext/sockets/tests/socket_afpacket.phpt

Lines changed: 1 addition & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ if (!function_exists("posix_getuid") || posix_getuid() != 0) {
1515
?>
1616
--FILE--
1717
<?php
18-
$s_c = socket_create(AF_PACKET, SOCK_RAW, ETH_P_ALL);
18+
$s_c = socket_create(AF_PACKET, SOCK_RAW, ETH_P_IP);
1919
$s_bind = socket_bind($s_c, 'lo');
2020
var_dump($s_bind);
2121

@@ -26,19 +26,6 @@ if (!function_exists("posix_getuid") || posix_getuid() != 0) {
2626
var_dump($iindex);
2727

2828
socket_getpeername($s_c, $istr2, $iindex2);
29-
30-
$s_s = socket_create(AF_PACKET, SOCK_RAW, ETH_P_ALL);
31-
$v_bind = socket_bind($s_s, 'lo');
32-
33-
$buf = str_repeat("0", ETH_FRAME_LEN) .
34-
str_repeat("\xFF", 6) .
35-
str_repeat("\x11", 6) .
36-
"\x08\x00" .
37-
str_pad(str_repeat("test", 512), 2048, "\x00");
38-
39-
var_dump(socket_sendto($s_s, $buf, strlen($buf), 0, "lo", 1));
40-
41-
socket_close($s_s);
4229
socket_close($s_c);
4330
?>
4431
--EXPECTF--
@@ -48,4 +35,3 @@ string(2) "lo"
4835
int(%i)
4936

5037
Warning: socket_getpeername(): unable to retrieve peer name [95]: %sot supported in %s on line %d
51-
int(3576)

0 commit comments

Comments
 (0)