From 123c9260513f12500c8fa00d8348628245c2b866 Mon Sep 17 00:00:00 2001 From: Manuel Dalla Lana Date: Fri, 20 Sep 2024 16:32:36 +0200 Subject: [PATCH] Fix regex to properly validate non-negative integer strings Updated the regex in the validation logic to ensure the entire string consists solely of digits, preventing invalid inputs with mixed characters. --- src/Base32.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Base32.php b/src/Base32.php index ba0e134..a072335 100644 --- a/src/Base32.php +++ b/src/Base32.php @@ -241,7 +241,7 @@ private static function _extractBytes($byteString, $start, $length) { */ private static function _intStrToByteStr($intStr) { // Check if given value is a positive integer (string). - if (!preg_match('/[0-9]+/', (string) $intStr)) { + if (!preg_match('/^[0-9]+$/', (string) $intStr)) { $msg = 'Argument 1 must be a non-negative integer or a string representing a non-negative integer.'; throw new InvalidArgumentException($msg, self::E_NO_NON_NEGATIVE_INT); } @@ -611,7 +611,7 @@ private static function _encodeByteStr($byteStr, $alphabet, $pad) { */ private static function _crockfordEncodeIntStr($intStr, $alphabet) { // Check if given value is a non-negative integer(-string). - if (!preg_match('/[0-9]+/', (string) $intStr)) { + if (!preg_match('/^[0-9]+$/', (string) $intStr)) { $msg = 'Argument 1 must be a non-negative integer or a string representing a non-negative integer.'; throw new InvalidArgumentException($msg, self::E_NO_NON_NEGATIVE_INT); }