{"record":{"id":"a36eb92618a789ae","repo":"rectorphp/rector","slug":"invalid-memory-limit-format-s","errorCode":null,"errorMessage":"Invalid memory limit format \"%s\".","messagePattern":"Invalid memory limit format \"(.+?)\"\\.","errorType":"validation","errorClass":"InvalidConfigurationException","httpStatus":null,"severity":"error","filePath":"src/Util/MemoryLimiter.php","lineNumber":39,"sourceCode":"        $memoryLimit = $configuration->getMemoryLimit();\n        if ($memoryLimit === null) {\n            return;\n        }\n        $this->validateMemoryLimitFormat($memoryLimit);\n        $memorySetResult = ini_set('memory_limit', $memoryLimit);\n        if ($memorySetResult === \\false) {\n            $errorMessage = sprintf('Memory limit \"%s\" cannot be set.', $memoryLimit);\n            throw new InvalidConfigurationException($errorMessage);\n        }\n    }\n    private function validateMemoryLimitFormat(string $memoryLimit): void\n    {\n        $memoryLimitFormatMatch = Strings::match($memoryLimit, self::VALID_MEMORY_LIMIT_REGEX);\n        if ($memoryLimitFormatMatch !== null) {\n            return;\n        }\n        $errorMessage = sprintf('Invalid memory limit format \"%s\".', $memoryLimit);\n        throw new InvalidConfigurationException($errorMessage);\n    }\n}\n","sourceCodeStart":21,"sourceCodeEnd":42,"githubUrl":"https://github.com/rectorphp/rector/blob/408fcb0ff1833e3d26cbc9b04c23a77565814a16/src/Util/MemoryLimiter.php#L21-L42","documentation":"MemoryLimiter::validateMemoryLimitFormat() enforces VALID_MEMORY_LIMIT_REGEX '#^-?\\d+[kMG]?$#i' before touching ini_set: an optional minus, digits, then at most one single-letter unit k/M/G (case-insensitive) or none (bytes). Anything else — '512MB', '1 g', 'unlimited', '2G5' — throws InvalidConfigurationException('Invalid memory limit format \"%s\".') and the ini_set is never attempted.","triggerScenarios":"Passing --memory-limit=512MB (double-letter unit), --memory-limit=2048M with a stray space, or a value read from an env var containing a typo; also 'max'/'default' style words.","commonSituations":"Copying values from memory_limit=512MB php.ini syntax (where 'B' suffix is legal) into the CLI flag where it is not; env-var templating that appends units conditionally.","solutions":["Use the compact format: 512M, 1G, 1024K, plain bytes like 268435456, or -1 for unlimited","Strip trailing 'B' and whitespace from sourced values: $limit = rtrim(strtoupper(trim($limit)), 'B')","Add a pre-flight regex check in your wrapper script so the run fails before Rector boots"],"exampleFix":"# before\nvendor/bin/rector process src --memory-limit=512MB\n\n# after\nvendor/bin/rector process src --memory-limit=512M","handlingStrategy":"validation","validationCode":"// validate the same shape Rector enforces, before launching\n$limit = strtoupper(trim((string) ($argv[2] ?? '')));\nif (preg_match('#^-?\\d+[KMG]?$#', $limit) !== 1) {\n    fwrite(STDERR, \"Invalid memory limit format; use e.g. 512M, 1G, or -1\\n\");\n    exit(1);\n}\nshell_exec('vendor/bin/rector process src --memory-limit=' . escapeshellarg($limit));","typeGuard":"function isValidMemoryLimit(string $limit): bool\n{\n    return preg_match('#^-?\\d+[kMG]?$#i', $limit) === 1;\n}","tryCatchPattern":null,"preventionTips":["Use php.ini-compatible single-letter units without the trailing B: 512M not 512MB","Normalize env-sourced values (trim, uppercase, strip B) in your wrapper before passing them on","Fail CI early on malformed --memory-limit values with your own regex check"],"tags":["php","memory-limit","cli","format-validation","rector"],"backgroundTag":"invalid-memory-limit-format","analyzedSha":"408fcb0ff1833e3d26cbc9b04c23a77565814a16","analyzedAt":"2026-08-21T05:11:02.643Z","schemaVersion":2},"datasetVersion":"2026-08-21T11:28:35.574Z"}