{"record":{"id":"ffdb99f1350ef4a9","repo":"uutils/coreutils","slug":"formatting-width-too-large","errorCode":null,"errorMessage":"formatting width too large","messagePattern":"formatting width too large","errorType":"exception","errorClass":"io::Error","httpStatus":null,"severity":"warning","filePath":"src/uucore/src/lib/features/format/mod.rs","lineNumber":182,"sourceCode":"                \"digits\" => String::from_utf8_lossy(digits)\n            ),\n            Self::InvalidEncoding(no) => return no.fmt(f),\n        };\n        f.write_str(&message)\n    }\n}\n\n/// Maximum width for formatting to prevent memory allocation panics.\n/// Rust's formatter will panic when trying to allocate memory for very large widths.\n/// This limit is somewhat arbitrary but should be well above any practical use case\n/// while still preventing formatter panics.\nconst MAX_FORMAT_WIDTH: usize = 1_000_000;\n\n/// Check if a width is too large for formatting.\n/// Returns an error if the width exceeds MAX_FORMAT_WIDTH.\nfn check_width(width: usize) -> std::io::Result<()> {\n    if width > MAX_FORMAT_WIDTH {\n        Err(std::io::Error::new(\n            std::io::ErrorKind::OutOfMemory,\n            \"formatting width too large\",\n        ))\n    } else {\n        Ok(())\n    }\n}\n\n/// Reject a precision larger than printf/C allows (`i32::MAX`).\n///\n/// A precision near `usize::MAX` would otherwise overflow the precision/exponent\n/// arithmetic in the float formatters, so we cap it the way C `printf` does.\npub(crate) fn check_precision(precision: usize) -> Result<(), FormatError> {\n    if precision > i32::MAX as usize {\n        Err(FormatError::InvalidPrecision(precision.to_string()))\n    } else {\n        Ok(())\n    }","sourceCodeStart":164,"sourceCodeEnd":200,"githubUrl":"https://github.com/uutils/coreutils/blob/85295bbf788bfd7a6926ba692031563504b304b7/src/uucore/src/lib/features/format/mod.rs#L164-L200","documentation":"`check_width` in the format feature rejects any requested formatting width above `MAX_FORMAT_WIDTH` (1,000,000). Allocating padding for an enormous width could exhaust memory, so it returns `io::ErrorKind::OutOfMemory` with \"formatting width too large\" instead.","triggerScenarios":"Calling formatting APIs (e.g. printf-style helpers) with a computed width that exceeds 1,000,000 — often from parsing an oversized width field in a user-supplied format string (`%1000000000d`-like inputs).","commonSituations":"printf/seq-like utilities fed a format string with a gigantic width, or width computed from user data without clamping.","solutions":["Reduce the requested width below 1,000,000","Sanitize/clamp widths parsed from user input before formatting","If a large width is genuinely needed, build the padding manually in chunks"],"exampleFix":"// before\nlet width: usize = s.parse()?;\nformat_args_width(value, width)?;\n// after\nlet width: usize = s.parse()?.min(1_000_000);\nformat_args_width(value, width)?;","handlingStrategy":"validation","validationCode":"const MAX_FORMAT_WIDTH: usize = 1_000_000;\nif width > MAX_FORMAT_WIDTH {\n    return Err(\"formatting width too large\");\n}","typeGuard":"fn width_in_range(w: usize) -> Option<usize> {\n    (w <= 1_000_000).then_some(w)\n}","tryCatchPattern":"match format_with_width(v, w) {\n    Err(e) if e.kind() == std::io::ErrorKind::OutOfMemory => format_without_width(v),\n    other => other?,\n}","preventionTips":["Clamp widths parsed from user format strings","Never pass raw numeric fields from untrusted input as width","Add unit tests with huge width values"],"tags":["format","width","resource-limits"],"backgroundTag":"format-width-too-large","analyzedSha":"85295bbf788bfd7a6926ba692031563504b304b7","analyzedAt":"2026-08-31T11:11:36.175Z","contentChangedAt":"2026-08-31T11:11:36.175Z","schemaVersion":2},"datasetVersion":"2026-09-14T00:17:10.932Z"}