News:

Looking for documentation? Take a look on our wiki

Main Menu

Memory limit check fails to verify size unit

Started by maxxer, May 16, 2016, 19:18:15 PM

Previous topic - Next topic

maxxer

If PHP's memory_limit is set to 1G the check in administrator/components/com_virtuemart/models/product.php:840 fails, because $this->memory_limit is equal to -3.

This happens because, up in the model constructor:

                $memoryLimit = ini_get('memory_limit');
                if($memoryLimit!=-1){
                        $this->memory_limit = (int) substr($memoryLimit,0,-1) -4; // 4 MB reserve
                } else {
                        $this->memory_limit = '1024M';
                }   

the substr doesn't check for the size unit, which according to PHP's FAQ can be K (for Kilobytes), M (for Megabytes) and G (for Gigabytes; available since PHP 5.1.0)

maxxer

Possible fix:


--- administrator/components/com_virtuemart/models/product.php_ORIG 2016-05-16 19:19:55.684660125 +0200
+++ administrator/components/com_virtuemart/models/product.php 2016-05-16 19:23:19.512489385 +0200
@@ -57,7 +57,7 @@
$this->maxScriptTime = VmConfig::getExecutionTime() * 0.95 - 1;
$memoryLimit = ini_get('memory_limit');
if($memoryLimit!=-1){
- $this->memory_limit = (int) substr($memoryLimit,0,-1) -4; // 4 MB reserve
+ $this->memory_limit = $this->_return_bytes($memoryLimit) - 4; // 4 MB reserve
} else {
$this->memory_limit = '1024M';
}
@@ -2820,5 +2820,27 @@
}
return $shoppers;
}
+
+ /**
+ * Converts shorthand memory notation value to bytes
+ * From http://php.net/manual/en/function.ini-get.php
+ * @see http://stackoverflow.com/a/28978624/738852
+ * @param $val Memory size shorthand notation string
+ */
+ private function _return_bytes($val) {
+     $val = trim($val);
+     $last = strtolower($val[strlen($val)-1]);
+     switch($last) {
+ // The 'G' modifier is available since PHP 5.1.0
+ case 'g':
+     $val *= 1024;
+ case 'm':
+     $val *= 1024;
+ case 'k':
+     $val *= 1024;
+     }
+     return $val;
+ }
+
}
// No closing tag