{"record":{"id":"efc7e3ea796fec7e","repo":"symfony/polyfill-mbstring","slug":"argument-2-length-must-be-greater-than-0","errorCode":null,"errorMessage":"Argument #2 ($length) must be greater than 0","messagePattern":"Argument #2 \\(\\$length\\) must be greater than 0","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"Mbstring.php","lineNumber":626,"sourceCode":"        return false !== $pos ? $offset + $pos : false;\n    }\n\n    public static function mb_str_split($string, $split_length = 1, $encoding = null)\n    {\n        if (null !== $string && !\\is_scalar($string) && !(\\is_object($string) && method_exists($string, '__toString'))) {\n            trigger_error('mb_str_split() expects parameter 1 to be string, '.\\gettype($string).' given', \\E_USER_WARNING);\n\n            return null;\n        }\n\n        if (1 > $split_length = (int) $split_length) {\n            if (80000 > \\PHP_VERSION_ID) {\n                trigger_error('The length of each segment must be greater than zero', \\E_USER_WARNING);\n\n                return false;\n            }\n\n            throw new \\ValueError('Argument #2 ($length) must be greater than 0');\n        }\n\n        if (null === $encoding) {\n            $encoding = mb_internal_encoding();\n        }\n\n        if ('UTF-8' === $encoding = self::getEncoding($encoding)) {\n            $rx = '/(';\n            while (65535 < $split_length) {\n                $rx .= '.{65535}';\n                $split_length -= 65535;\n            }\n            $rx .= '.{'.$split_length.'})/us';\n\n            return preg_split($rx, $string, -1, \\PREG_SPLIT_DELIM_CAPTURE | \\PREG_SPLIT_NO_EMPTY);\n        }\n\n        $result = [];","sourceCodeStart":608,"sourceCodeEnd":644,"githubUrl":"https://github.com/symfony/polyfill-mbstring/blob/d3d318bad5e7a1bfbd026009c8bfb8d8f99ae6b6/Mbstring.php#L608-L644","documentation":"Mbstring::mb_str_split() throws this ValueError when the $length argument is not greater than 0. Splitting a string into zero-length or negative-length segments is meaningless, so the polyfill rejects it; on PHP < 8.0 it triggers an E_USER_WARNING and returns false instead.","triggerScenarios":"Calling mb_str_split($string, 0), mb_str_split($string, -1), or any $length <= 0 — typically from a computed/unvalidated length value.","commonSituations":"A config or CLI value of 0 or a negative number used as chunk size; an off-by-one or wrong-variable bug when computing segment length; user input parsed with (int) cast producing 0 for non-numeric input.","solutions":["Pass a positive integer for $length (>= 1)","Clamp or validate the computed length before calling: max(1, $n)","Cast carefully and reject non-numeric/zero user input before splitting","Catch \\ValueError (PHP 8+) or check for false + warning (PHP < 8)"],"exampleFix":"// before\nmb_str_split($s, $chunkSize); // $chunkSize could be 0\n// after\nmb_str_split($s, max(1, (int) $chunkSize));","handlingStrategy":"validation","validationCode":"$length = (int) $length;\nif ($length < 1) {\n    throw new \\InvalidArgumentException('length must be >= 1');\n}\nmb_str_split($string, $length);","typeGuard":"function isPositiveInt($length): bool\n{\n    return is_int($length) && $length > 0;\n}","tryCatchPattern":"try {\n    $chunks = mb_str_split($string, $length);\n} catch (\\ValueError $e) {\n    $chunks = mb_str_split($string, 1);\n}","preventionTips":["Clamp computed lengths with max(1, $n)","Validate config/CLI chunk sizes are positive integers","Beware (int) casts producing 0 from non-numeric input","Unit-test edge inputs of 0 and negative lengths"],"tags":["php","mbstring","polyfill","valueerror","argument-out-of-range"],"backgroundTag":"value-out-of-range","analyzedSha":"d3d318bad5e7a1bfbd026009c8bfb8d8f99ae6b6","analyzedAt":"2026-09-13T19:34:02.044Z","contentChangedAt":"2026-09-13T19:34:02.044Z","schemaVersion":2},"datasetVersion":"2026-09-15T23:17:13.987Z"}