Skip to content

Commit e38b76f

Browse files
committed
fix PHP 8.1 deprecations when $iv is null
1 parent c1f0014 commit e38b76f

File tree

1 file changed

+29
-4
lines changed

1 file changed

+29
-4
lines changed

lib/mcrypt.php

Lines changed: 29 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,31 @@
107107
}
108108

109109
if (!function_exists('phpseclib_mcrypt_list_algorithms')) {
110+
/**
111+
* Find whether the type of a variable is string (or could be converted to one)
112+
*
113+
* @param mixed $var
114+
* @return bool
115+
*/
116+
function phpseclib_is_stringable($var)
117+
{
118+
return is_string($var) || (is_object($var) && method_exists($var, '__toString'));
119+
}
120+
121+
/**
122+
* Returns the string length
123+
*
124+
* PHP8.1 emits a warning if $string isn't a string
125+
*
126+
* @param string $string
127+
* @return int
128+
* @access private
129+
*/
130+
function phpseclib_strlen($string)
131+
{
132+
return phpseclib_is_stringable($string) ? strlen($string) : 0;
133+
}
134+
110135
/**
111136
* Sets the key
112137
*
@@ -182,7 +207,7 @@ function phpseclib_set_iv(Base $td, $iv)
182207
{
183208
if ($td->mode != Base::MODE_ECB && $td->mode != Base::MODE_STREAM) {
184209
$length = $td->getBlockLength() >> 3;
185-
$iv = str_pad(substr($iv, 0, $length), $length, "\0");
210+
$iv = str_pad(substr(phpseclib_is_stringable($iv) ? $iv : '', 0, $length), $length, "\0");
186211
$td->setIV($iv);
187212
}
188213
}
@@ -648,10 +673,10 @@ function phpseclib_mcrypt_enc_self_test(Base $td)
648673
function phpseclib_mcrypt_generic_init(Base $td, $key, $iv)
649674
{
650675
$iv_size = phpseclib_mcrypt_enc_get_iv_size($td);
651-
if (strlen($iv) != $iv_size && $td->mode != Base::MODE_ECB) {
652-
trigger_error('mcrypt_generic_init(): Iv size incorrect; supplied length: ' . strlen($iv) . ', needed: ' . $iv_size, E_USER_WARNING);
676+
if (phpseclib_strlen($iv) != $iv_size && $td->mode != Base::MODE_ECB) {
677+
trigger_error('mcrypt_generic_init(): Iv size incorrect; supplied length: ' . phpseclib_strlen($iv) . ', needed: ' . $iv_size, E_USER_WARNING);
653678
}
654-
if (!strlen($key)) {
679+
if (!phpseclib_strlen($key)) {
655680
trigger_error('mcrypt_generic_init(): Key size is 0', E_USER_WARNING);
656681
return -3;
657682
}

0 commit comments

Comments
 (0)