From 3dc384fb585ded6c6160f5eee9d56223c74706b7 Mon Sep 17 00:00:00 2001 From: Johannes Meyer Date: Wed, 19 Jan 2022 10:56:19 +0100 Subject: [PATCH] Avoid passing `null` to non-nullable arguments --- .../ApplicationStateController.php | 2 +- application/forms/Security/RoleForm.php | 2 +- library/Icinga/Application/Benchmark.php | 2 +- library/Icinga/Application/Cli.php | 6 ++--- library/Icinga/Authentication/Auth.php | 2 +- .../Authentication/User/DbUserBackend.php | 4 ++++ .../Authentication/User/LdapUserBackend.php | 8 +++---- .../UserGroup/LdapUserGroupBackend.php | 12 +++++----- library/Icinga/Chart/SVGRenderer.php | 2 +- library/Icinga/Cli/Loader.php | 2 +- .../Icinga/Data/Filter/FilterQueryString.php | 2 +- library/Icinga/Date/DateFormatter.php | 2 +- library/Icinga/Repository/LdapRepository.php | 22 +------------------ library/Icinga/Repository/RepositoryQuery.php | 4 +++- library/Icinga/User.php | 4 +--- library/Icinga/Util/StringHelper.php | 20 +++++++++++++++++ library/Icinga/Web/Response.php | 8 +++---- library/Icinga/Web/View.php | 2 +- library/Icinga/Web/Widget/SortBox.php | 2 +- .../vendor/Zend/Controller/Router/Route.php | 5 +++-- .../vendor/Zend/Db/Adapter/Pdo/Abstract.php | 4 +++- library/vendor/Zend/Db/Statement/Pdo.php | 6 +++++ .../Zend/Form/Decorator/Description.php | 2 +- .../vendor/Zend/Form/Decorator/HtmlTag.php | 2 +- library/vendor/Zend/Form/Decorator/Label.php | 2 +- .../controllers/TimelineController.php | 4 ++-- .../library/Monitoring/Plugin/PerfdataSet.php | 2 +- 27 files changed, 72 insertions(+), 63 deletions(-) diff --git a/application/controllers/ApplicationStateController.php b/application/controllers/ApplicationStateController.php index 0d3fe8814..b828ca287 100644 --- a/application/controllers/ApplicationStateController.php +++ b/application/controllers/ApplicationStateController.php @@ -41,7 +41,7 @@ class ApplicationStateController extends Controller setcookie( 'icingaweb2-session', $now, - null, + 0, $params['path'], $params['domain'], $params['secure'], diff --git a/application/forms/Security/RoleForm.php b/application/forms/Security/RoleForm.php index acd408840..1ddcf4aa7 100644 --- a/application/forms/Security/RoleForm.php +++ b/application/forms/Security/RoleForm.php @@ -448,7 +448,7 @@ class RoleForm extends RepositoryForm $b = array_shift($bParts); } while ($a === $b); - return strnatcmp($a, $b); + return strnatcmp($a ?? '', $b ?? ''); }); } diff --git a/library/Icinga/Application/Benchmark.php b/library/Icinga/Application/Benchmark.php index 28c62497c..6514ad569 100644 --- a/library/Icinga/Application/Benchmark.php +++ b/library/Icinga/Application/Benchmark.php @@ -232,7 +232,7 @@ class Benchmark round(($m->timestamp - floor($m->timestamp)) * 1000) ); $vals = array( - date('H:i:s', $m->timestamp) . '.' . $micro, + date('H:i:s', (int) $m->timestamp) . '.' . $micro, $m->message ); diff --git a/library/Icinga/Application/Cli.php b/library/Icinga/Application/Cli.php index d07e76602..719bf9279 100644 --- a/library/Icinga/Application/Cli.php +++ b/library/Icinga/Application/Cli.php @@ -119,11 +119,11 @@ class Cli extends ApplicationBootstrap if ($this->params->shift('autocomplete')) { $this->params->unshift('autocomplete'); } + $watch = $this->params->shift('watch'); if ($watch === true) { - $watch = 5; - } - if (preg_match('~^\d+$~', $watch)) { + $this->watchTimeout = 5; + } elseif (is_numeric($watch)) { $this->watchTimeout = (int) $watch; } diff --git a/library/Icinga/Authentication/Auth.php b/library/Icinga/Authentication/Auth.php index d56d3789b..c1cfd473f 100644 --- a/library/Icinga/Authentication/Auth.php +++ b/library/Icinga/Authentication/Auth.php @@ -331,7 +331,7 @@ class Auth setcookie( 'icingaweb2-session', time(), - null, + 0, $params['path'], $params['domain'], $params['secure'], diff --git a/library/Icinga/Authentication/User/DbUserBackend.php b/library/Icinga/Authentication/User/DbUserBackend.php index 58127fa64..8b8062350 100644 --- a/library/Icinga/Authentication/User/DbUserBackend.php +++ b/library/Icinga/Authentication/User/DbUserBackend.php @@ -203,6 +203,10 @@ class DbUserBackend extends DbRepository implements UserBackendInterface, Inspec $lob = stream_get_contents($lob); } + if ($lob === null) { + return ''; + } + return $this->ds->getDbType() === 'pgsql' ? pg_unescape_bytea($lob) : $lob; } diff --git a/library/Icinga/Authentication/User/LdapUserBackend.php b/library/Icinga/Authentication/User/LdapUserBackend.php index 87687fe59..8c8a23098 100644 --- a/library/Icinga/Authentication/User/LdapUserBackend.php +++ b/library/Icinga/Authentication/User/LdapUserBackend.php @@ -88,7 +88,7 @@ class LdapUserBackend extends LdapRepository implements UserBackendInterface, Do */ public function setBaseDn($baseDn) { - if (($baseDn = trim($baseDn))) { + if ($baseDn && ($baseDn = trim($baseDn))) { $this->baseDn = $baseDn; } @@ -160,7 +160,7 @@ class LdapUserBackend extends LdapRepository implements UserBackendInterface, Do */ public function setFilter($filter) { - if (($filter = trim($filter))) { + if ($filter && ($filter = trim($filter))) { if ($filter[0] === '(') { $filter = substr($filter, 1, -1); } @@ -195,9 +195,7 @@ class LdapUserBackend extends LdapRepository implements UserBackendInterface, Do */ public function setDomain($domain) { - $domain = trim($domain); - - if (strlen($domain)) { + if ($domain && ($domain = trim($domain))) { $this->domain = $domain; } diff --git a/library/Icinga/Authentication/UserGroup/LdapUserGroupBackend.php b/library/Icinga/Authentication/UserGroup/LdapUserGroupBackend.php index 925062aec..54ccaa962 100644 --- a/library/Icinga/Authentication/UserGroup/LdapUserGroupBackend.php +++ b/library/Icinga/Authentication/UserGroup/LdapUserGroupBackend.php @@ -169,7 +169,7 @@ class LdapUserGroupBackend extends LdapRepository implements Inspectable, UserGr */ public function setUserBaseDn($baseDn) { - if (($baseDn = trim($baseDn))) { + if ($baseDn && ($baseDn = trim($baseDn))) { $this->userBaseDn = $baseDn; } @@ -195,7 +195,7 @@ class LdapUserGroupBackend extends LdapRepository implements Inspectable, UserGr */ public function setGroupBaseDn($baseDn) { - if (($baseDn = trim($baseDn))) { + if ($baseDn && ($baseDn = trim($baseDn))) { $this->groupBaseDn = $baseDn; } @@ -336,7 +336,7 @@ class LdapUserGroupBackend extends LdapRepository implements Inspectable, UserGr */ public function setUserFilter($filter) { - if (($filter = trim($filter))) { + if ($filter && ($filter = trim($filter))) { if ($filter[0] === '(') { $filter = substr($filter, 1, -1); } @@ -366,7 +366,7 @@ class LdapUserGroupBackend extends LdapRepository implements Inspectable, UserGr */ public function setGroupFilter($filter) { - if (($filter = trim($filter))) { + if ($filter && ($filter = trim($filter))) { $this->groupFilter = $filter; } @@ -431,9 +431,7 @@ class LdapUserGroupBackend extends LdapRepository implements Inspectable, UserGr */ public function setDomain($domain) { - $domain = trim($domain); - - if (strlen($domain)) { + if ($domain && ($domain = trim($domain))) { $this->domain = $domain; } diff --git a/library/Icinga/Chart/SVGRenderer.php b/library/Icinga/Chart/SVGRenderer.php index 344ddd550..d3891f224 100644 --- a/library/Icinga/Chart/SVGRenderer.php +++ b/library/Icinga/Chart/SVGRenderer.php @@ -131,7 +131,7 @@ class SVGRenderer 'http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd' ); - $this->document = $implementation->createDocument(null, null, $docType); + $this->document = $implementation->createDocument(null, '', $docType); $this->svg = $this->createOuterBox(); $this->document->appendChild($this->svg); } diff --git a/library/Icinga/Cli/Loader.php b/library/Icinga/Cli/Loader.php index dbd853200..ef2782bb0 100644 --- a/library/Icinga/Cli/Loader.php +++ b/library/Icinga/Cli/Loader.php @@ -358,7 +358,7 @@ class Loader } elseif (! empty($step['object'])) { $object = (string) $step['object'] . $step['type']; } - if (is_array($step['args'])) { + if (isset($step['args']) && is_array($step['args'])) { foreach ($step['args'] as & $arg) { if (is_object($arg)) { $arg = sprintf('[%s]', get_class($arg)); diff --git a/library/Icinga/Data/Filter/FilterQueryString.php b/library/Icinga/Data/Filter/FilterQueryString.php index 06b2b24f0..8535df53a 100644 --- a/library/Icinga/Data/Filter/FilterQueryString.php +++ b/library/Icinga/Data/Filter/FilterQueryString.php @@ -263,7 +263,7 @@ class FilterQueryString $this->string = $string; - $this->length = strlen($string); + $this->length = $string ? strlen($string) : 0; if ($this->length === 0) { return Filter::matchAll(); diff --git a/library/Icinga/Date/DateFormatter.php b/library/Icinga/Date/DateFormatter.php index cc8a2d3a4..a186b0ce1 100644 --- a/library/Icinga/Date/DateFormatter.php +++ b/library/Icinga/Date/DateFormatter.php @@ -106,7 +106,7 @@ class DateFormatter */ public static function formatDateTime($dateTime) { - return date('Y-m-d H:i:s', (float) $dateTime); + return date('Y-m-d H:i:s', (int) $dateTime); } /** diff --git a/library/Icinga/Repository/LdapRepository.php b/library/Icinga/Repository/LdapRepository.php index 46b33313b..e7a380b76 100644 --- a/library/Icinga/Repository/LdapRepository.php +++ b/library/Icinga/Repository/LdapRepository.php @@ -61,31 +61,11 @@ abstract class LdapRepository extends Repository */ protected function getNormedAttribute($name) { - $loweredName = strtolower($name); + $loweredName = strtolower($name ?? ''); if (array_key_exists($loweredName, $this->normedAttributes)) { return $this->normedAttributes[$loweredName]; } return $name; } - - /** - * Return whether the given object DN is related to the given base DN - * - * Will use the current connection's root DN if $baseDn is not given. - * - * @deprecated This was only used by LdapUserGroupBackend::isMemberAttributeAmbiguous - * It will be removed with 2.6.0! - * - * @param string $dn The object DN to check - * @param string $baseDn The base DN to compare the object DN with - * - * @return bool - */ - protected function isRelatedDn($dn, $baseDn = null) - { - $normalizedDn = strtolower(join(',', array_map('trim', explode(',', $dn)))); - $normalizedBaseDn = strtolower(join(',', array_map('trim', explode(',', $baseDn ?: $this->ds->getDn())))); - return strpos($normalizedDn, $normalizedBaseDn) !== false; - } } diff --git a/library/Icinga/Repository/RepositoryQuery.php b/library/Icinga/Repository/RepositoryQuery.php index 31395324c..84f7c6e2a 100644 --- a/library/Icinga/Repository/RepositoryQuery.php +++ b/library/Icinga/Repository/RepositoryQuery.php @@ -344,7 +344,9 @@ class RepositoryQuery implements QueryInterface, SortRules, FilterColumns, Itera } } - $baseDirection = strtoupper($sortColumns['order']) === static::SORT_DESC ? static::SORT_DESC : static::SORT_ASC; + $baseDirection = isset($sortColumns['order']) && strtoupper($sortColumns['order']) === static::SORT_DESC + ? static::SORT_DESC + : static::SORT_ASC; foreach ($sortColumns['columns'] as $column) { list($column, $specificDirection) = $this->splitOrder($column); diff --git a/library/Icinga/User.php b/library/Icinga/User.php index 529018f64..8610dd0fe 100644 --- a/library/Icinga/User.php +++ b/library/Icinga/User.php @@ -432,9 +432,7 @@ class User */ public function setDomain($domain) { - $domain = trim($domain); - - if (strlen($domain)) { + if ($domain && ($domain = trim($domain))) { $this->domain = $domain; } diff --git a/library/Icinga/Util/StringHelper.php b/library/Icinga/Util/StringHelper.php index 59bef3d9c..67a836b6d 100644 --- a/library/Icinga/Util/StringHelper.php +++ b/library/Icinga/Util/StringHelper.php @@ -19,6 +19,10 @@ class StringHelper */ public static function trimSplit($value, $delimiter = ',', $limit = null) { + if ($value === null) { + return []; + } + if ($limit !== null) { $exploded = explode($delimiter, $value, $limit); } else { @@ -40,6 +44,10 @@ class StringHelper */ public static function cname($name, $separator = '_') { + if ($name === null) { + return ''; + } + return str_replace(' ', '', ucwords(str_replace($separator, ' ', strtolower($name)))); } @@ -54,6 +62,10 @@ class StringHelper */ public static function ellipsis($string, $maxLength, $ellipsis = '...') { + if ($string === null) { + return ''; + } + if (strlen($string) > $maxLength) { return substr($string, 0, $maxLength - strlen($ellipsis)) . $ellipsis; } @@ -72,6 +84,10 @@ class StringHelper */ public static function ellipsisCenter($string, $maxLength, $ellipsis = '...') { + if ($string === null) { + return ''; + } + $start = ceil($maxLength / 2.0); $end = floor($maxLength / 2.0); if (strlen($string) > $maxLength) { @@ -117,6 +133,10 @@ class StringHelper */ public static function endsWith($string, $suffix) { + if ($string === null) { + return false; + } + $stringSuffix = substr($string, -strlen($suffix)); return $stringSuffix !== false ? $stringSuffix === $suffix : false; } diff --git a/library/Icinga/Web/Response.php b/library/Icinga/Web/Response.php index 4782103d0..df0b842d5 100644 --- a/library/Icinga/Web/Response.php +++ b/library/Icinga/Web/Response.php @@ -389,12 +389,12 @@ class Response extends Zend_Controller_Response_Http /** @var Cookie $cookie */ setcookie( $cookie->getName(), - $cookie->getValue(), - $cookie->getExpire(), + $cookie->getValue() ?? '', + $cookie->getExpire() ?? 0, $cookie->getPath(), - $cookie->getDomain(), + $cookie->getDomain() ?? '', $cookie->isSecure(), - $cookie->isHttpOnly() + $cookie->isHttpOnly() ?? true ); } } diff --git a/library/Icinga/Web/View.php b/library/Icinga/Web/View.php index bdc7b6d6b..6a5dffc7d 100644 --- a/library/Icinga/Web/View.php +++ b/library/Icinga/Web/View.php @@ -104,7 +104,7 @@ class View extends Zend_View_Abstract */ public function escape($value) { - return htmlspecialchars($value, ENT_COMPAT | ENT_SUBSTITUTE | ENT_HTML5, self::CHARSET, true); + return htmlspecialchars($value ?: '', ENT_COMPAT | ENT_SUBSTITUTE | ENT_HTML5, self::CHARSET, true); } /** diff --git a/library/Icinga/Web/Widget/SortBox.php b/library/Icinga/Web/Widget/SortBox.php index 399ff74b2..72b6f587c 100644 --- a/library/Icinga/Web/Widget/SortBox.php +++ b/library/Icinga/Web/Widget/SortBox.php @@ -225,7 +225,7 @@ class SortBox extends AbstractWidget // TODO(el): ToggleButton :) $toggle = array('asc' => 'sort-name-down', 'desc' => 'sort-name-up'); - unset($toggle[strtolower($direction) ?: 'asc']); + unset($toggle[isset($direction) ? strtolower($direction) : 'asc']); $newDirection = key($toggle); $icon = current($toggle); diff --git a/library/vendor/Zend/Controller/Router/Route.php b/library/vendor/Zend/Controller/Router/Route.php index d7db76155..e001a4908 100644 --- a/library/vendor/Zend/Controller/Router/Route.php +++ b/library/vendor/Zend/Controller/Router/Route.php @@ -271,11 +271,12 @@ class Zend_Controller_Router_Route extends Zend_Controller_Router_Route_Abstract // Translate value if required $part = $this->_parts[$pos]; if ($this->_isTranslated + && $part !== null && (substr($part, 0, 1) === '@' && substr($part, 1, 1) !== '@' && $name === null) || $name !== null && in_array($name, $this->_translatable) ) { - if (substr($part, 0, 1) === '@') { + if ($part && substr($part, 0, 1) === '@') { $part = substr($part, 1); } @@ -284,7 +285,7 @@ class Zend_Controller_Router_Route extends Zend_Controller_Router_Route_Abstract } } - if (substr($part, 0, 2) === '@@') { + if ($part && substr($part, 0, 2) === '@@') { $part = substr($part, 1); } diff --git a/library/vendor/Zend/Db/Adapter/Pdo/Abstract.php b/library/vendor/Zend/Db/Adapter/Pdo/Abstract.php index c3ae8721c..3a0e13ebf 100644 --- a/library/vendor/Zend/Db/Adapter/Pdo/Abstract.php +++ b/library/vendor/Zend/Db/Adapter/Pdo/Abstract.php @@ -286,7 +286,9 @@ abstract class Zend_Db_Adapter_Pdo_Abstract extends Zend_Db_Adapter_Abstract */ protected function _quote($value) { - if (is_int($value) || is_float($value)) { + if ($value === null) { + $value = ''; + } elseif (is_int($value) || is_float($value)) { return $value; } $this->_connect(); diff --git a/library/vendor/Zend/Db/Statement/Pdo.php b/library/vendor/Zend/Db/Statement/Pdo.php index cf0150a59..47f903145 100644 --- a/library/vendor/Zend/Db/Statement/Pdo.php +++ b/library/vendor/Zend/Db/Statement/Pdo.php @@ -240,6 +240,12 @@ class Zend_Db_Statement_Pdo extends Zend_Db_Statement implements IteratorAggrega if ($style === null) { $style = $this->_fetchMode; } + if ($cursor === null) { + $cursor = PDO::FETCH_ORI_NEXT; + } + if ($offset === null) { + $offset = 0; + } try { return $this->_stmt->fetch($style, $cursor, $offset); } catch (PDOException $e) { diff --git a/library/vendor/Zend/Form/Decorator/Description.php b/library/vendor/Zend/Form/Decorator/Description.php index f5d693653..00530f766 100644 --- a/library/vendor/Zend/Form/Decorator/Description.php +++ b/library/vendor/Zend/Form/Decorator/Description.php @@ -158,7 +158,7 @@ class Zend_Form_Decorator_Description extends Zend_Form_Decorator_Abstract } $description = $element->getDescription(); - $description = trim($description); + $description = trim($description ?: ''); if (!empty($description) && (null !== ($translator = $element->getTranslator()))) { $description = $translator->translate($description); diff --git a/library/vendor/Zend/Form/Decorator/HtmlTag.php b/library/vendor/Zend/Form/Decorator/HtmlTag.php index 532a3e2c5..47b9e21df 100644 --- a/library/vendor/Zend/Form/Decorator/HtmlTag.php +++ b/library/vendor/Zend/Form/Decorator/HtmlTag.php @@ -91,7 +91,7 @@ class Zend_Form_Decorator_HtmlTag extends Zend_Form_Decorator_Abstract $val = implode(' ', $val); } } - $val = htmlspecialchars($val, ENT_COMPAT, $enc); + $val = htmlspecialchars($val ?? '', ENT_COMPAT, $enc); $xhtml .= " $key=\"$val\""; } return $xhtml; diff --git a/library/vendor/Zend/Form/Decorator/Label.php b/library/vendor/Zend/Form/Decorator/Label.php index 6648a569a..c70c61e07 100644 --- a/library/vendor/Zend/Form/Decorator/Label.php +++ b/library/vendor/Zend/Form/Decorator/Label.php @@ -298,7 +298,7 @@ class Zend_Form_Decorator_Label extends Zend_Form_Decorator_Abstract } $label = $element->getLabel(); - $label = trim($label); + $label = trim($label ?? ''); if (empty($label)) { return ''; diff --git a/modules/monitoring/application/controllers/TimelineController.php b/modules/monitoring/application/controllers/TimelineController.php index 99a56cbec..deeeb365a 100644 --- a/modules/monitoring/application/controllers/TimelineController.php +++ b/modules/monitoring/application/controllers/TimelineController.php @@ -275,7 +275,7 @@ class TimelineController extends Controller { $startTime = new DateTime(); $startParam = $this->_request->getParam('start'); - $startTimestamp = is_numeric($startParam) ? intval($startParam) : strtotime($startParam); + $startTimestamp = is_numeric($startParam) ? intval($startParam) : strtotime($startParam ?? ''); if ($startTimestamp !== false) { $startTime->setTimestamp($startTimestamp); } else { @@ -284,7 +284,7 @@ class TimelineController extends Controller $endTime = clone $startTime; $endParam = $this->_request->getParam('end'); - $endTimestamp = is_numeric($endParam) ? intval($endParam) : strtotime($endParam); + $endTimestamp = is_numeric($endParam) ? intval($endParam) : strtotime($endParam ?? ''); if ($endTimestamp !== false) { $endTime->setTimestamp($endTimestamp); } else { diff --git a/modules/monitoring/library/Monitoring/Plugin/PerfdataSet.php b/modules/monitoring/library/Monitoring/Plugin/PerfdataSet.php index cc545a234..ef1ca0ce2 100644 --- a/modules/monitoring/library/Monitoring/Plugin/PerfdataSet.php +++ b/modules/monitoring/library/Monitoring/Plugin/PerfdataSet.php @@ -37,7 +37,7 @@ class PerfdataSet implements IteratorAggregate */ protected function __construct($perfdataStr) { - if (($perfdataStr = trim($perfdataStr)) !== '') { + if ($perfdataStr && ($perfdataStr = trim($perfdataStr))) { $this->perfdataStr = $perfdataStr; $this->parse(); }