{"record":{"id":"8734aa4b9bce4f9a","repo":"symfony/polyfill-mbstring","slug":"s-dynamic-errorformat-passed-to-assertencoding-e-g-mb-str","errorCode":null,"errorMessage":"%s (dynamic: errorFormat passed to assertEncoding, e.g. 'mb_str_pad(): Argument #5 ($encoding) must be a valid encoding, \"%s\" given')","messagePattern":"(.+?) \\(dynamic: errorFormat passed to assertEncoding, e\\.g\\. 'mb_str_pad\\(\\): Argument #5 \\(\\$encoding\\) must be a valid encoding, \"(.+?)\" given'\\)","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"Mbstring.php","lineNumber":1132,"sourceCode":"            return $string;\n        }\n\n        return self::iconv('UTF-8', $encoding, $string);\n    }\n\n    private static function assertEncoding(string $encoding, string $errorFormat): bool\n    {\n        try {\n            $validEncoding = @self::mb_check_encoding('', $encoding);\n        } catch (\\ValueError $e) {\n            throw new \\ValueError(\\sprintf($errorFormat, $encoding));\n        }\n\n        if (!$validEncoding) {\n            if (80000 > \\PHP_VERSION_ID) {\n                trigger_error(\\sprintf($errorFormat, $encoding), \\E_USER_WARNING);\n            } else {\n                throw new \\ValueError(\\sprintf($errorFormat, $encoding));\n            }\n        }\n\n        return $validEncoding;\n    }\n}\n","sourceCodeStart":1114,"sourceCodeEnd":1139,"githubUrl":"https://github.com/symfony/polyfill-mbstring/blob/d3d318bad5e7a1bfbd026009c8bfb8d8f99ae6b6/Mbstring.php#L1114-L1139","documentation":"Same assertEncoding() validation path as error 6, but on the branch where the encoding check completes without throwing yet still reports the encoding as invalid; the ValueError message is formatted from the caller's $errorFormat, e.g. 'mb_str_pad(): Argument #5 ($encoding) must be a valid encoding, \"%s\" given'. Thrown for mb_scrub, mb_str_pad, mb_ucfirst, mb_lcfirst, mb_internal_trim when the $encoding argument names an unsupported encoding.","triggerScenarios":"Passing an explicit $encoding argument that mb_check_encoding('' ) rejects — e.g. mb_str_pad($s, 10, '.', STR_PAD_RIGHT, 'ASCII-8') or any unsupported name on PHP >= 8.0.","commonSituations":"Encoding names from config files or environment variables that were never validated; data pipelines passing through encodings inherited from legacy systems; assuming the polyfill supports the full native mbstring encoding list.","solutions":["Whitelist the encoding against supported values before calling","Normalize the encoding name (case/aliases) and default to 'UTF-8'","Convert input data to UTF-8 upstream instead of passing other encodings","Wrap calls in try/catch (\\ValueError) with a fallback to the default encoding"],"exampleFix":"// before\nmb_str_pad($s, 10, '.', STR_PAD_RIGHT, $userEncoding);\n// after\n$enc = @mb_check_encoding('', $userEncoding) ? $userEncoding : 'UTF-8';\nmb_str_pad($s, 10, '.', STR_PAD_RIGHT, $enc);","handlingStrategy":"validation","validationCode":"$encoding = @mb_check_encoding('', $encoding) ? $encoding : 'UTF-8';\nmb_str_pad($string, $length, $padString, $padType, $encoding);","typeGuard":"function normalizeEncoding(?string $encoding): string\n{\n    return ($encoding !== null && @mb_check_encoding('', $encoding)) ? $encoding : 'UTF-8';\n}","tryCatchPattern":"try {\n    $padded = mb_str_pad($s, $len, $pad, $padType, $encoding);\n} catch (\\ValueError $e) {\n    $padded = mb_str_pad($s, $len, $pad, $padType, 'UTF-8');\n}","preventionTips":["Centralize encoding normalization in one helper","Validate env/config-provided encodings at bootstrap","Convert legacy-encoded data to UTF-8 upstream","Add tests covering invalid encoding inputs for mb_* calls"],"tags":["php","encoding","mbstring","polyfill","valueerror"],"backgroundTag":"invalid-encoding","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"}