Permalink
Browse files

MDL-15655 - address_in_subnet does not work on 64 bit architectures. …

…Also, we found some other issues with this funtion while diagnosing the problem.
  • Loading branch information...
tjhunt
tjhunt committed Sep 1, 2008
1 parent d7a113c commit 4c01616d7e2ec05f8b1e9d8432b801f4e061e2bf
Showing with 14 additions and 2 deletions.
  1. +7 −1 lib/moodlelib.php
  2. +7 −1 lib/simpletest/testmoodlelib.php
View
@@ -6018,6 +6018,9 @@ function address_in_subnet($addr, $subnetstr) {
$subnet = trim($subnet);
if (strpos($subnet, '/') !== false) { /// type 1
list($ip, $mask) = explode('/', $subnet);
+ if ($mask === '') {
+ $mask = 32;
+ }
$mask = 0xffffffff << (32 - $mask);
$found = ((ip2long($addr) & $mask) == (ip2long($ip) & $mask));
} else if (strpos($subnet, '-') !== false) {/// type 3
@@ -6030,7 +6033,10 @@ function address_in_subnet($addr, $subnetstr) {
$subnetrange[0] <= $lastaddrpart && $lastaddrpart <= $subnetrange[1]);
}
} else { /// type 2
- $found = (strpos($addr, $subnet) === 0);
+ if ($subnet[strlen($subnet) - 1] != '.') {
+ $subnet .= '.';
+ }
+ $found = (strpos($addr . '.', $subnet) === 0);
}
if ($found) {
@@ -32,9 +32,15 @@ function test_address_in_subnet() {
$this->assertTrue(address_in_subnet('123.121.234.15', '123.121.234.2/28'));
$this->assertFalse(address_in_subnet('123.121.234.16', '123.121.234.2/28'));
$this->assertFalse(address_in_subnet('123.121.234.255', '123.121.234.2/28'));
- $this->assertTrue(address_in_subnet('123.121.234.1', '123.121.'));
+ $this->assertTrue(address_in_subnet('123.121.234.0', '123.121.234.0/')); // / is like /32.
+ $this->assertFalse(address_in_subnet('123.121.234.1', '123.121.234.0/'));
+ $this->assertFalse(address_in_subnet('232.232.232.232', '123.121.234.0/0'));
$this->assertFalse(address_in_subnet('123.122.234.1', '123.121.'));
$this->assertFalse(address_in_subnet('223.121.234.1', '123.121.'));
+ $this->assertTrue(address_in_subnet('123.121.234.1', '123.121'));
+ $this->assertFalse(address_in_subnet('123.122.234.1', '123.121'));
+ $this->assertFalse(address_in_subnet('223.121.234.1', '123.121'));
+ $this->assertFalse(address_in_subnet('123.121.234.100', '123.121.234.10'));
$this->assertFalse(address_in_subnet('123.121.234.9', '123.121.234.10-20'));
$this->assertTrue(address_in_subnet('123.121.234.10', '123.121.234.10-20'));
$this->assertTrue(address_in_subnet('123.121.234.15', '123.121.234.10-20'));

0 comments on commit 4c01616

Please sign in to comment.