diff --git a/src/wp-includes/load.php b/src/wp-includes/load.php index 6699495fc4..ebaaa353a0 100644 --- a/src/wp-includes/load.php +++ b/src/wp-includes/load.php @@ -956,7 +956,7 @@ function wp_installing( $is_installing = null ) { * Determines if SSL is used. * * @since 2.6.0 - * @since 4.6.0 Moved from functions.php to load.php + * @since 4.6.0 Moved from functions.php to load.php. * * @return bool True if SSL, otherwise false. */ @@ -979,19 +979,26 @@ function is_ssl() { * Converts a shorthand byte value to an integer byte value. * * @since 2.3.0 - * @since 4.6.0 Moved from media.php to load.php + * @since 4.6.0 Moved from media.php to load.php. * - * @param string $size A shorthand byte value. + * @link http://php.net/manual/en/function.ini-get.php + * @link http://php.net/manual/en/faq.using.php#faq.using.shorthandbytes + * + * @param string $value A (PHP ini) byte value, either shorthand or ordinary. * @return int An integer byte value. */ -function wp_convert_hr_to_bytes( $size ) { - $size = strtolower( $size ); - $bytes = (int) $size; - if ( strpos( $size, 'k' ) !== false ) - $bytes = intval( $size ) * KB_IN_BYTES; - elseif ( strpos( $size, 'm' ) !== false ) - $bytes = intval($size) * MB_IN_BYTES; - elseif ( strpos( $size, 'g' ) !== false ) - $bytes = intval( $size ) * GB_IN_BYTES; - return $bytes; +function wp_convert_hr_to_bytes( $value ) { + $value = strtolower( trim( $value ) ); + $bytes = (int) $value; + + if ( false !== strpos( $value, 'g' ) ) { + $bytes *= GB_IN_BYTES; + } elseif ( false !== strpos( $value, 'm' ) ) { + $bytes *= MB_IN_BYTES; + } elseif ( false !== strpos( $value, 'k' ) ) { + $bytes *= KB_IN_BYTES; + } + + // Deal with large (float) values which run into the maximum integer size. + return min( $bytes, PHP_INT_MAX ); } diff --git a/tests/phpunit/tests/load/convertHrToBytes.php b/tests/phpunit/tests/load/convertHrToBytes.php new file mode 100644 index 0000000000..17d3559968 --- /dev/null +++ b/tests/phpunit/tests/load/convertHrToBytes.php @@ -0,0 +1,66 @@ +assertSame( $expected, wp_convert_hr_to_bytes( $value ) ); + } + + /** + * Data provider for test_wp_convert_hr_to_bytes(). + * + * @return array { + * @type array { + * @type int|string $value The value passed to wp_convert_hr_to_bytes(). + * @type int $expected The expected output of wp_convert_hr_to_bytes(). + * } + * } + */ + function data_wp_convert_hr_to_bytes() { + $array = array( + // Integer input. + array( -1, -1 ), // = no memory limit. + array( 8388608, 8388608 ), // 8M. + + // String input (memory limit shorthand values). + array( '32k', 32768 ), + array( '64K', 65536 ), + array( '128m', 134217728 ), + array( '256M', 268435456 ), + array( '1g', 1073741824 ), + array( '128m ', 134217728 ), // Leading/trailing whitespace gets trimmed. + array( '1024', 1024 ), // No letter will be interpreted as integer value. + + // Edge cases. + array( 'g', 0 ), + array( 'g1', 0 ), + array( 'null', 0 ), + array( 'off', 0 ), + ); + + // Test for running into maximum integer size limit on 32bit systems. + if ( 2147483647 === PHP_INT_MAX ) { + $array[] = array( '2G', 2147483647 ); + $array[] = array( '4G', 2147483647 ); + } else { + $array[] = array( '2G', 2147483648 ); + $array[] = array( '4G', 4294967296 ); + } + + return $array; + } +}