Skip to content

Commit

Permalink
Fixed "passing null to parameter" in Mage_Core_Helper_String (#3341)
Browse files Browse the repository at this point in the history
  • Loading branch information
kiatng authored Jun 21, 2023
1 parent bda0b6f commit 9b8eded
Showing 1 changed file with 23 additions and 7 deletions.
30 changes: 23 additions & 7 deletions app/code/core/Mage/Core/Helper/String.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ class Mage_Core_Helper_String extends Mage_Core_Helper_Abstract
public function truncate($string, $length = 80, $etc = '...', &$remainder = '', $breakWords = true)
{
$remainder = '';
if ($length == 0) {
if (is_null($string) || $length == 0) {
return '';
}

Expand Down Expand Up @@ -75,7 +75,7 @@ public function truncate($string, $length = 80, $etc = '...', &$remainder = '',
*/
public function strlen($string)
{
return iconv_strlen($string, self::ICONV_CHARSET);
return is_null($string) ? 0 : iconv_strlen($string, self::ICONV_CHARSET);
}

/**
Expand All @@ -88,6 +88,9 @@ public function strlen($string)
*/
public function substr($string, $offset, $length = null)
{
if (is_null($string)) {
return '';
}
$string = $this->cleanString($string);
if (is_null($length)) {
$length = $this->strlen($string) - $offset;
Expand Down Expand Up @@ -246,6 +249,9 @@ public function str_split($str, $length = 1, $keepWords = false, $trim = false,
*/
public function splitWords($str, $uniqueOnly = false, $maxWordLength = 0, $wordSeparatorRegexp = '\s')
{
if (is_null($str)) {
return [];
}
$result = [];
$split = preg_split('#' . $wordSeparatorRegexp . '#siu', $str, -1, PREG_SPLIT_NO_EMPTY);
foreach ($split as $word) {
Expand All @@ -269,8 +275,12 @@ public function splitWords($str, $uniqueOnly = false, $maxWordLength = 0, $wordS
*/
public function cleanString($string)
{
return '"libiconv"' == ICONV_IMPL ?
iconv(self::ICONV_CHARSET, self::ICONV_CHARSET . '//IGNORE', $string) : $string;
if (is_null($string)) {
return '';
}
return '"libiconv"' == ICONV_IMPL
? iconv(self::ICONV_CHARSET, self::ICONV_CHARSET . '//IGNORE', $string)
: $string;
}

/**
Expand All @@ -279,11 +289,11 @@ public function cleanString($string)
* @param string $haystack
* @param string $needle
* @param int $offset
* @return int
* @return int|false
*/
public function strpos($haystack, $needle, $offset = null)
public function strpos($haystack, $needle, $offset = 0)
{
return iconv_strpos($haystack, $needle, $offset, self::ICONV_CHARSET);
return iconv_strpos((string) $haystack, (string) $needle, $offset, self::ICONV_CHARSET);
}

/**
Expand Down Expand Up @@ -315,6 +325,9 @@ public function ksortMultibyte(array &$sort)
*/
public function parseQueryStr($str)
{
if (is_null($str)) {
return [];
}
$argSeparator = '&';
$result = [];
$partsQueryStr = explode($argSeparator, $str);
Expand Down Expand Up @@ -510,6 +523,9 @@ public function uniOrd($c)
*/
public function unserialize($str)
{
if (is_null($str)) {
return null;
}
$reader = new Unserialize_Reader_ArrValue('data');
$prevChar = null;
for ($i = 0; $i < strlen($str); $i++) {
Expand Down

0 comments on commit 9b8eded

Please sign in to comment.