Skip to content

Commit 973c0a7

Browse files
committed
ext/standard/basic_functions.c: perform basic input checks for ip2long()
1 parent 0528a1d commit 973c0a7

File tree

4 files changed

+31
-11
lines changed

4 files changed

+31
-11
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.phpt

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,21 +7,20 @@ 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
}
2323

24-
var_dump(ip2long(""));
2524
var_dump(ip2long("777.777.777.777"));
2625
var_dump(ip2long("111.111.111.111"));
2726

@@ -43,7 +42,6 @@ string(7) "0.0.0.0"
4342
int(1118019956)
4443
string(14) "66.163.161.116"
4544
bool(false)
46-
bool(false)
4745
int(1869573999)
4846
string(13) "255.254.82.80"
4947
Done
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
--TEST--
2+
ip2long() error conditions
3+
--FILE--
4+
<?php
5+
6+
try {
7+
var_dump(ip2long(""));
8+
} catch (Throwable $e) {
9+
echo $e::class, ': ', $e->getMessage(), PHP_EOL;
10+
}
11+
try {
12+
var_dump(ip2long("127\00.0.1"));
13+
} catch (Throwable $e) {
14+
echo $e::class, ': ', $e->getMessage(), PHP_EOL;
15+
}
16+
?>
17+
--EXPECT--
18+
ValueError: ip2long(): Argument #1 ($ip) must not be empty
19+
ValueError: ip2long(): Argument #1 ($ip) must not contain any null bytes
20+

ext/standard/tests/network/ip_x86_64.phpt

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,21 +7,19 @@ 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

@@ -45,7 +43,6 @@ string(7) "0.0.0.0"
4543
int(1118019956)
4644
string(14) "66.163.161.116"
4745
bool(false)
48-
bool(false)
4946
int(1869573999)
5047
string(13) "255.254.82.80"
5148
string(15) "255.255.255.255"

0 commit comments

Comments
 (0)