Author Topic: Bad report : Increase your php memory limit, which is must too low to run VM  (Read 1795 times)

Studio 42

  • Contributing Developer
  • Full Member
  • *
  • Posts: 2359
  • Joomla & Virtuemart addon developper
    • Studio 42 - Virtuemart & Joomla extentions
  • VirtueMart Version: 2.6 & 3.0.x.y
If you set php memory limit too 2048M, you get message
 
Quote
Increase your php memory limit, which is must too low to run VM ...

in config.php replace :
Code: [Select]
static function getMemoryLimitBytes(){
static $mLimit;
if($mLimit===null){
$mL = ini_get('memory_limit');
$mLimit = 0;
if(!empty($mL)){
$u = strtoupper(substr($mL,-1));
$mLimit = (int)substr($mL,0,-1);
if($mLimit>0){
if($u == 'M' or $u == 'MB'){
$mLimit *= 1048576;
} else if($u == 'G' or $u == 'GB'){
$mLimit *= 1073741824;
} else if($u == 'K' or $u == 'KB'){
$mLimit *= 1024;
}
$mLimit = (int) $mLimit - 5242880; // 5 MB reserve
if($mLimit<=0){
$mLimit = 1;
$m = 'Increase your php memory limit, which is must too low to run VM, your current memory limit is set as '.$mL.' = '.$mLimit.'B';
vmError($m,$m);
}
}
}

if($mLimit<=0) $mLimit = 2147483648;
vmdebug('My Memory Limit in Bytes '.$mLimit);
}

return $mLimit;
}

with :


Code: [Select]
static function getMemoryLimitBytes(){
static $mLimit;
if($mLimit===null){
$mL = ini_get('memory_limit');
$mLimit = 0;
if(!empty($mL)){
$u = strtoupper(substr($mL,-1));
$mLimit = (int)substr($mL,0,-1);
if($mLimit>0){
if($u == 'M' or $u == 'MB'){
$mLimit *= 1048576;
} else if($u == 'G' or $u == 'GB'){
$mLimit *= 1073741824;
} else if($u == 'K' or $u == 'KB'){
$mLimit *= 1024;
}
if($mLimit<5242880){
$mLimit = 1;

$m .= 'Increase your php memory limit, which is must too low to run VM, your current memory limit is set as '.$mL.' = '.$mLimit.'B';
vmError($m,$m);
}
}
}

if($mLimit<=0) $mLimit = 2147483648;
vmdebug('My Memory Limit in Bytes '.$mLimit);
}

return $mLimit;
}

(int) $mLimit - 5242880 do not return the right calculation and give a negative value

Milbo

  • Virtuemart Projectleader
  • Administrator
  • Super Hero
  • *
  • Posts: 9382
  • VM3.2 Cached and Optimized
    • VM3 Extensions
  • VirtueMart Version: VirtueMart 3 on joomla 3
The error is the cast to int, which is too small for the 2048 MB expressed in bytes.
I should fix your bug, please support the VirtueMart project and become a member
______________________________________
Extensions approved by the core team: http://extensions.virtuemart.net/

Studio 42

  • Contributing Developer
  • Full Member
  • *
  • Posts: 2359
  • Joomla & Virtuemart addon developper
    • Studio 42 - Virtuemart & Joomla extentions
  • VirtueMart Version: 2.6 &amp; 3.0.x.y
Code: [Select]
if($mLimit<5242880){ work always

Code: [Select]
$mLimit = (int) $mLimit - 5242880; // 5 MB reserve
if($mLimit<=0){
do not work always

Milbo

  • Virtuemart Projectleader
  • Administrator
  • Super Hero
  • *
  • Posts: 9382
  • VM3.2 Cached and Optimized
    • VM3 Extensions
  • VirtueMart Version: VirtueMart 3 on joomla 3
Studio, your code is wrong. The original code delivers the amount of mb available for VM. the limit is then set to 1, and so on.

the error is mainly "(int) $mLimit - 5242880;"
cast $mLimit to int, which has as biggest size http://php.net/manual/de/language.types.integer.php

The size of an integer is platform-dependent, although a maximum value of about two billion is the usual value (that's 32 bits signed). 64-bit platforms usually have a maximum value of about 9E18, except on Windows prior to PHP 7, where it was always 32 bit. PHP does not support unsigned integers. Integer size can be determined using the constant PHP_INT_SIZE, maximum value using the constant PHP_INT_MAX since PHP 5.0.5, and minimum value using the constant PHP_INT_MIN since PHP 7.0.0.

Windows => 32 bit. It is fixed now, but your code is just wrong. But thanks for note about the error.
I should fix your bug, please support the VirtueMart project and become a member
______________________________________
Extensions approved by the core team: http://extensions.virtuemart.net/

Studio 42

  • Contributing Developer
  • Full Member
  • *
  • Posts: 2359
  • Joomla & Virtuemart addon developper
    • Studio 42 - Virtuemart & Joomla extentions
  • VirtueMart Version: 2.6 &amp; 3.0.x.y
i get reported  $mLimit = 2147483648
So 2147483648<5242880 is right.
I checked before sending the fix.
If PHP cannot handle 2147483648 as int, then no one can use it.
Check my solution, if it not work, then your do not work better, i don't understand  why "your code is wrong" ?

Milbo

  • Virtuemart Projectleader
  • Administrator
  • Super Hero
  • *
  • Posts: 9382
  • VM3.2 Cached and Optimized
    • VM3 Extensions
  • VirtueMart Version: VirtueMart 3 on joomla 3
lol, I have no time to explain you now the INT, I sent already the manual. Your code does not work as the function is meant.


in config.php replace :
Code: [Select]
static function getMemoryLimitBytes(){
...
$mLimit = (int) $mLimit - 5242880; // 5 MB reserve
if($mLimit<=0){
$mLimit = 1;
$m = 'Increase your php memory limit, which is must too low to run VM, your current memory limit is set as '.$mL.' = '.$mLimit.'B';
vmError($m,$m);
}
The function should return the available memory. We need 5 MB at least for joomla. So when less than 5 MB is set => throw the warning. And set the return to 1 (1 Byte), just to not return 0 (which can be interpreted as false, you know), ELSE return the set memory limit -5MB


Code: [Select]
if($mLimit<=0) $mLimit = 2147483648;

This just return 2 GB in case someone set as php memory_limit 0, because 0 is "unlimited", which is usually not more than 2 GB

with :

Quote from: Studio 42 link=topic=136707.msg477950
Code: [Select]
if($mLimit<5242880){
$mLimit = 1;

$m .= 'Increase your php memory limit, which is must too low to run VM, your current memory limit is set as '.$mL.' = '.$mLimit.'B';
vmError($m,$m);
}

Sets the returned memory limit to 1 in case less than 5 MB is set. BUT it does NOT reduce the returned memory limit by 5 MB !

I should fix your bug, please support the VirtueMart project and become a member
______________________________________
Extensions approved by the core team: http://extensions.virtuemart.net/

Studio 42

  • Contributing Developer
  • Full Member
  • *
  • Posts: 2359
  • Joomla & Virtuemart addon developper
    • Studio 42 - Virtuemart & Joomla extentions
  • VirtueMart Version: 2.6 &amp; 3.0.x.y
Max, recheck the code.
I really don't understand the problem, PHP return right INT, but casting the operation give bad result.
IF you need to check for memory limit = 0 , add the condition and it work for this case too.

Milbo

  • Virtuemart Projectleader
  • Administrator
  • Super Hero
  • *
  • Posts: 9382
  • VM3.2 Cached and Optimized
    • VM3 Extensions
  • VirtueMart Version: VirtueMart 3 on joomla 3
lol, useless.

Your code " BUT it does NOT reduce the returned memory limit by 5 MB !"

The solution is to write instead of this
Code: [Select]
$mLimit = (int) $mLimit - 5242880; // 5 MB reserve
just this
Code: [Select]
$mLimit = $mLimit - 5242880; // 5 MB reserve
I should fix your bug, please support the VirtueMart project and become a member
______________________________________
Extensions approved by the core team: http://extensions.virtuemart.net/