r76514 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r76513‎ | r76514 | r76515 >
Date:12:18, 11 November 2010
Author:aaron
Status:ok (Comments)
Tags:
Comment:
* Followed-up r76267:
** Made RE_IPV6_ADD and sanitizeIP() hande '::'.
** RE_IPV6_ADD is no longer over-inclusive (# of octets and '::' must be valid)
* Removed weird debugging code
* Broke long line
Modified paths:
  • /trunk/phase3/includes/IP.php (modified) (history)

Diff [purge]

Index: trunk/phase3/includes/IP.php
@@ -35,16 +35,16 @@
3636 define( 'RE_IPV6_V4_PREFIX', '0*' . RE_IPV6_GAP . '(?:ffff:)?' );
3737 // An IPv6 block is an IP address and a prefix (d1 to d128)
3838 define( 'RE_IPV6_PREFIX', '(12[0-8]|1[01][0-9]|[1-9]?\d)');
39 -// An IPv6 IP is made up of 8 octets. However abbreviations like "::" can be used.
40 -// This is lax! The number of colon groups is checked (1 to 7) but
41 -// the number of double colons is not validated (must be 0 to 1).
 39+// An IPv6 address is made up of 8 octets. However, the "::" abbreviations can be used.
4240 define( 'RE_IPV6_ADD',
43 - '(' .
44 - ':(:' . RE_IPV6_WORD . '){1,7}' . // starts with "::"
45 - '|' .
46 - RE_IPV6_WORD . '(:{1,2}' . RE_IPV6_WORD . '){0,6}::' . // ends with "::"
47 - '|' .
48 - RE_IPV6_WORD . '(:{1,2}' . RE_IPV6_WORD . '){1,7}' . // neither of the above
 41+ '(' . // starts with "::" (includes the address "::")
 42+ '(::|:(:' . RE_IPV6_WORD . '){1,7})' .
 43+ '|' . // ends with "::" (not including the address "::")
 44+ RE_IPV6_WORD . '(:' . RE_IPV6_WORD . '){0,6}::' .
 45+ '|' . // has no "::"
 46+ RE_IPV6_WORD . '(:' . RE_IPV6_WORD . '){7}' .
 47+ '|' . // contains one "::" in the middle ("^" check always fails if no "::" found)
 48+ RE_IPV6_WORD . '(:(?P<abbr>(?(abbr)|:))?' . RE_IPV6_WORD . '){1,6}(?(abbr)|^)' .
4949 ')'
5050 );
5151 define( 'RE_IPV6_BLOCK', RE_IPV6_ADD . '\/' . RE_IPV6_PREFIX );
@@ -72,11 +72,7 @@
7373 if ( !$ip ) {
7474 return false;
7575 }
76 - if ( is_array( $ip ) ) {
77 - throw new MWException( 'invalid value passed to ' . __METHOD__ );
78 - }
79 - return preg_match( '/^' . IP_ADDRESS_STRING . '$/', $ip )
80 - && ( substr_count( $ip, '::' ) <= 1 ); // IPv6 IPs with 2+ "::" are ambiguous
 76+ return preg_match( '/^' . IP_ADDRESS_STRING . '$/', $ip );
8177 }
8278
8379 /**
@@ -89,11 +85,7 @@
9086 if ( !$ip ) {
9187 return false;
9288 }
93 - if ( is_array( $ip ) ) {
94 - throw new MWException( 'invalid value passed to ' . __METHOD__ );
95 - }
96 - return preg_match( '/^' . RE_IPV6_ADD . '(\/' . RE_IPV6_PREFIX . '|)$/', $ip )
97 - && ( substr_count( $ip, '::' ) <= 1 ); // IPv6 IPs with 2+ "::" are ambiguous
 89+ return preg_match( '/^' . RE_IPV6_ADD . '(\/' . RE_IPV6_PREFIX . '|)$/', $ip );
9890 }
9991
10092 /**
@@ -188,7 +180,7 @@
189181 // If the '::' is at the beginning...
190182 if( $abbrevPos == 0 ) {
191183 $repeat = '0:';
192 - $extra = '';
 184+ $extra = ( $ip == '::' ) ? '0' : ''; // for the address '::'
193185 $pad = 9; // 7+2 (due to '::')
194186 // If the '::' is at the end...
195187 } elseif( $abbrevPos == ( $addressEnd - 1 ) ) {
@@ -449,7 +441,9 @@
450442 public static function toHex( $ip ) {
451443 $n = self::toUnsigned( $ip );
452444 if ( $n !== false ) {
453 - $n = self::isIPv6( $ip ) ? 'v6-' . wfBaseConvert( $n, 10, 16, 32, false ) : wfBaseConvert( $n, 10, 16, 8, false );
 445+ $n = self::isIPv6( $ip )
 446+ ? 'v6-' . wfBaseConvert( $n, 10, 16, 32, false )
 447+ : wfBaseConvert( $n, 10, 16, 8, false );
454448 }
455449 return $n;
456450 }

Follow-up revisions

RevisionCommit summaryAuthorDate
r76560* Changes in IP.php:...aaron01:33, 12 November 2010

Past revisions this follows-up on

RevisionCommit summaryAuthorDate
r76267* Fixed RE_IPV6_ADD for IP networks ending in "::", like "abcd::/y" or "a::/y"...aaron20:31, 7 November 2010

Comments

#Comment by Hashar (talk | contribs)   18:07, 11 November 2010

todo : tests need an update

failure	IPTest::testisIPAddress()	
failure	IPTest::testArrayIsNotIPAddress()	
failure	IPTest::testArrayIsNotIPv6()

http://ci.tesla.usability.wikimedia.org/cruisecontrol/buildresults/mw?log=log20101111124439

#Comment by MaxSem (talk | contribs)   18:08, 11 November 2010

Breaks unit tests[1]:

IPTest::testArrayIsNotIPAddress
Expected exception MWException

IPTest::testArrayIsNotIPv6
Expected exception MWException
#Comment by Aaron Schulz (talk | contribs)   23:57, 11 November 2010

Well aware of the tests. I'm removing the array one since I don't see the purpose.

#Comment by Aaron Schulz (talk | contribs)   01:35, 12 November 2010

Tests updated.

#Comment by Hashar (talk | contribs)   12:10, 12 November 2010

Thanks Aaron. Marking ok.

Status & tagging log