Skip to content

Commit 44db3e5

Browse files
committed
ext/standard/basic_functions.c: perform basic input checks for ip2long()
1 parent e108f98 commit 44db3e5

File tree

2 files changed

+21
-7
lines changed

2 files changed

+21
-7
lines changed

ext/standard/basic_functions.c

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -593,10 +593,15 @@ PHP_FUNCTION(ip2long)
593593
struct in_addr ip;
594594

595595
ZEND_PARSE_PARAMETERS_START(1, 1)
596-
Z_PARAM_STRING(addr, addr_len)
596+
Z_PARAM_PATH(addr, addr_len)
597597
ZEND_PARSE_PARAMETERS_END();
598598

599-
if (addr_len == 0 || inet_pton(AF_INET, addr, &ip) != 1) {
599+
if (addr_len == 0) {
600+
zend_argument_must_not_be_empty_error(1);
601+
RETURN_THROWS();
602+
}
603+
604+
if (inet_pton(AF_INET, addr, &ip) != 1) {
600605
RETURN_FALSE;
601606
}
602607
RETURN_LONG(ntohl(ip.s_addr));

ext/standard/tests/network/ip_x86_64.phpt

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,28 +7,36 @@ if (PHP_INT_SIZE == 4) die("skip this test is for >32bit platform only");
77
--FILE--
88
<?php
99

10-
$array = array(
10+
$array = [
1111
"127.0.0.1",
1212
"10.0.0.1",
1313
"255.255.255.255",
1414
"255.255.255.0",
1515
"0.0.0.0",
1616
"66.163.161.116",
17-
);
17+
];
1818

1919
foreach ($array as $ip) {
2020
var_dump($long = ip2long($ip));
2121
var_dump(long2ip($long));
2222
}
23-
24-
var_dump(ip2long(""));
2523
var_dump(ip2long("777.777.777.777"));
2624
var_dump(ip2long("111.111.111.111"));
2725

2826
var_dump(long2ip(-110000));
2927
var_dump(long2ip(PHP_INT_MAX));
3028
var_dump(long2ip(PHP_INT_MIN));
3129

30+
try {
31+
var_dump(ip2long(""));
32+
} catch (Throwable $e) {
33+
echo $e::class, ': ', $e->getMessage(), PHP_EOL;
34+
}
35+
try {
36+
var_dump(ip2long("127\00.0.1"));
37+
} catch (Throwable $e) {
38+
echo $e::class, ': ', $e->getMessage(), PHP_EOL;
39+
}
3240
echo "Done\n";
3341
?>
3442
--EXPECT--
@@ -45,9 +53,10 @@ string(7) "0.0.0.0"
4553
int(1118019956)
4654
string(14) "66.163.161.116"
4755
bool(false)
48-
bool(false)
4956
int(1869573999)
5057
string(13) "255.254.82.80"
5158
string(15) "255.255.255.255"
5259
string(7) "0.0.0.0"
60+
ValueError: ip2long(): Argument #1 ($ip) must not be empty
61+
ValueError: ip2long(): Argument #1 ($ip) must not contain any null bytes
5362
Done

0 commit comments

Comments
 (0)