Skip to content

Commit

Permalink
Add null-coalesce safeguard to shell_exec for cases when command fail…
Browse files Browse the repository at this point in the history
…s or returns no output (matomo-org#22102)
  • Loading branch information
michalkleiner authored Apr 14, 2024
1 parent c1b3e88 commit 3923eeb
Show file tree
Hide file tree
Showing 4 changed files with 5 additions and 5 deletions.
2 changes: 1 addition & 1 deletion core/CliMulti/Process.php
Original file line number Diff line number Diff line change
Expand Up @@ -311,7 +311,7 @@ public static function getListOfRunningProcesses()
*/
public static function getRunningProcesses()
{
$ids = explode("\n", trim(shell_exec(self::PS_COMMAND . ' 2>/dev/null | ' . self::AWK_COMMAND . ' 2>/dev/null')));
$ids = explode("\n", trim(shell_exec(self::PS_COMMAND . ' 2>/dev/null | ' . self::AWK_COMMAND . ' 2>/dev/null') ?? ''));

$ids = array_map('intval', $ids);
$ids = array_filter($ids, function ($id) {
Expand Down
4 changes: 2 additions & 2 deletions core/Filechecks.php
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ public static function getUserAndGroup()
return $user . ':' . $user;
}

$group = trim(shell_exec('groups ' . $user . ' | cut -f3 -d" "'));
$group = trim(shell_exec('groups ' . $user . ' | cut -f3 -d" "') ?? '');

if (empty($group) && function_exists('posix_getegid') && function_exists('posix_getgrgid')) {
$currentGroupId = posix_getegid();
Expand All @@ -173,7 +173,7 @@ public static function getUserAndGroup()
public static function getUser()
{
if (function_exists('shell_exec')) {
return trim(shell_exec('whoami'));
return trim(shell_exec('whoami') ?? '');
}

$currentUser = get_current_user();
Expand Down
2 changes: 1 addition & 1 deletion plugins/CoreConsole/Commands/GitCommit.php
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ private function getSubmodulePaths()
protected function getStatusOfSubmodule($submodule)
{
$cmd = sprintf('cd %s/%s && git status --porcelain', PIWIK_DOCUMENT_ROOT, $submodule);
$status = trim(shell_exec($cmd));
$status = trim(shell_exec($cmd) ?? '');

return $status;
}
Expand Down
2 changes: 1 addition & 1 deletion plugins/TestRunner/Commands/TestsRun.php
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ function ($xdebugFile) {

$output->writeln("<info>using $xdebugFile as xdebug extension.</info>");

$phpunitPath = trim(shell_exec('which phpunit'));
$phpunitPath = trim(shell_exec('which phpunit') ?? '');

$command = sprintf('php -d zend_extension=%s %s', $xdebugFile, $phpunitPath);
}
Expand Down

0 comments on commit 3923eeb

Please sign in to comment.