Skip to content

Commit

Permalink
file_get_contents leaks memory! Implemented fread() replacement where…
Browse files Browse the repository at this point in the history
… it can be called a million times. Fixes #18031. Thanks for reporting krzysztof
  • Loading branch information
kvz committed Nov 9, 2010
1 parent d1249fe commit ec70815
Showing 1 changed file with 23 additions and 6 deletions.
29 changes: 23 additions & 6 deletions System/Daemon.php
Expand Up @@ -952,7 +952,6 @@ protected static function _ilog($level, $str)
$function = (string)@$dbg_bt[($history-1)]['function'];
$file = (string)@$dbg_bt[$history]['file'];
$line = (string)@$dbg_bt[$history]['line'];

return self::log($level, $str, $file, $class, $function, $line);
}

Expand Down Expand Up @@ -989,7 +988,6 @@ static public function log($level, $str, $file = false, $class = false,
// fair enough, but we have to init some log options
self::_optionsInit(true);
}

if (!self::opt('appName')) {
// Not logging for anything without a name
return false;
Expand Down Expand Up @@ -1203,20 +1201,22 @@ static public function isDying()
*/
static public function isRunning()
{
if (!file_exists(self::opt('appPidLocation'))) {
$appPidLocation = self::opt('appPidLocation');

if (!file_exists($appPidLocation)) {
unset($appPidLocation);
return false;
}

$pid = @file_get_contents(self::opt('appPidLocation'));

$pid = self::fileread($appPidLocation);
if (!$pid) {
return false;
}

// Ping app
if (!posix_kill(intval($pid), 0)) {
// Not responding so unlink pidfile
@unlink(self::opt('appPidLocation'));
@unlink($appPidLocation);
return self::warning(
'Orphaned pidfile found and removed: ' .
'{appPidLocation}. Previous process crashed?'
Expand Down Expand Up @@ -1377,6 +1377,23 @@ static protected function _writePid($pidFilePath = null, $pid = null)
return true;
}

/**
* Read a file. file_get_contents() leaks memory! (#18031 for more info)
*
* @param string $filepath
*
* @return string
*/
static public function fileread ($filepath) {
$f = fopen($filepath, 'r');
if (!$f) {
return false;
}
$data = fread($f, filesize($filepath));
fclose($f);
return $data;
}

/**
* Recursive alternative to mkdir
*
Expand Down

0 comments on commit ec70815

Please sign in to comment.