{"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":"error","filePath":"src/uucore/src/lib/features/format/mod.rs","lineNumber":183,"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":165,"sourceCodeEnd":201,"githubUrl":"https://github.com/uutils/coreutils/blob/92995feca29e324a7c559ede91db81c32d9f5d59/src/uucore/src/lib/features/format/mod.rs#L165-L201","documentation":"uucore's format feature caps any formatting width at MAX_FORMAT_WIDTH (1,000,000). check_width returns an io::Error with kind OutOfMemory and message 'formatting width too large' when a requested width exceeds that, protecting against enormous buffer allocation from absurd pad widths.","triggerScenarios":"Calling printf/seq-like formatting with a width larger than 1,000,000, e.g. `printf '%10000000d' 1`, or programmatic use of uucore::format with a parsed width spec beyond the cap; widths parsed from user-provided format strings.","commonSituations":"Scripts computing pad widths dynamically (e.g. width = big number from data) exceeding the cap; typos adding zeros to a width; porting from systems with different width limits where GNU allowed larger values.","solutions":["Reduce the format width to <= 1,000,000 in the format string.","If a very large width is genuinely needed, implement padding manually (repeat fill characters) instead of width specifiers.","Clamp computed widths in scripts before interpolating them into format strings.","Sanitize user-supplied width values at the script/tool boundary."],"exampleFix":"#!/bin/sh\nwidth=2000000\n# before: printf \"%${width}d\\n\" 42 -> formatting width too large\n[ \"$width\" -le 1000000 ] || width=1000000\n# after: clamped width\nprintf \"%${width}d\\n\" 42","handlingStrategy":"validation","validationCode":"// Clamp width before building a format spec\nconst MAX_WIDTH = 1_000_000;\nconst w = Number(widthInput);\nif (!Number.isInteger(w) || w < 0 || w > MAX_WIDTH) {\n  throw new RangeError(`width must be 0..${MAX_WIDTH}`);\n}\nconst spec = `%${w}d`;","typeGuard":"function isValidWidth(w) {\n  return Number.isInteger(w) && w >= 0 && w <= 1_000_000;\n}","tryCatchPattern":"match check_width(requested) {\n  Err(e) if e.kind() == io::ErrorKind::OutOfMemory => {\n    eprintln!(\"width too large (max 1000000); reduce or pad manually\");\n  }\n  Err(e) => return Err(e),\n  Ok(()) => format_with_width(requested),\n}","preventionTips":["Cap computed/dynamic widths before interpolating into format strings","Sanitize user-supplied width values at tool boundaries","Beware typos adding zeros to literal widths","Pad manually with repeat() if you legitimately need huge widths"],"tags":["format","width","printf","limits"],"backgroundTag":"value-out-of-range","analyzedSha":"92995feca29e324a7c559ede91db81c32d9f5d59","analyzedAt":"2026-09-20T12:39:35.476Z","contentChangedAt":"2026-09-20T12:39:35.476Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}