{"record":{"id":"6451943cdcc88333","repo":"can1357/oh-my-pi","slug":"number-of-bytes-or-lines-is-too-large","errorCode":null,"errorMessage":"number of -bytes or -lines is too large","messagePattern":"number of -bytes or -lines is too large","errorType":"error_code","errorClass":"HeadError","httpStatus":null,"severity":"error","filePath":"crates/pi-builtins/src/head.rs","lineNumber":993,"sourceCode":"\t\tassert_eq!(Some(String::from(\"b\")), iter.next());\n\t\tassert_eq!(Some(String::from(\"c\")), iter.next());\n\t\tassert_eq!(None, iter.next());\n\t}\n}\n}\n\nuse take::{copy_all_but_n_bytes, copy_all_but_n_lines, take_lines};\n\n#[derive(Error, Debug)]\nenum HeadError {\n\t/// Wrapper around `io::Error`\n\t#[error(\"error reading {}: {}\", name.quote(), err)]\n\tIo { name: PathBuf, err: io::Error },\n\n\t#[error(\"{0}\")]\n\tParseError(String),\n\n\t#[error(\"number of -bytes or -lines is too large\")]\n\tNumTooLarge(#[from] TryFromIntError),\n\n\n\t#[error(\"{0}\")]\n\tMatchOption(String),\n}\n\ntype HeadResult<T> = Result<T, HeadError>;\n\n#[derive(Debug, PartialEq)]\nenum Mode {\n\tFirstLines(u64),\n\tAllButLastLines(u64),\n\tFirstBytes(u64),\n\tAllButLastBytes(u64),\n}\n\nimpl Default for Mode {","sourceCodeStart":975,"sourceCodeEnd":1011,"githubUrl":"https://github.com/can1357/oh-my-pi/blob/969062200754ea02cfac922e5ebb8c608c079e15/crates/pi-builtins/src/head.rs#L975-L1011","documentation":"HeadError::NumTooLarge is raised (via #[from] TryFromIntError) when the requested number of bytes or lines overflows the integer type used internally, meaning the supplied count cannot be represented. The fixed message 'number of -bytes or -lines is too large' mirrors GNU head's diagnostic.","triggerScenarios":"Passing an extremely large -n or -c value to the head builtin that exceeds the target integer type's range during a TryFrom conversion, e.g. counts larger than u64/i64 limits or values parsed as huge numbers that overflow during casting.","commonSituations":"Scripts computing counts dynamically and producing astronomically large values (multiplication overflow upstream); using sentinel values like '999999999999999999999999'; passing signed negatives or float strings that fail conversion differently.","solutions":["Reduce the -n/-c count to a value within u64 range","Sanitize or clamp computed counts before invoking the builtin","Guard the conversion yourself with u64::try_from and an explicit cap"],"exampleFix":"// before\nlet n: u64 = big_value; // may exceed representable range\nhead_opts([\"-c\", &n.to_string()]);\n// after\nlet n = big_value.min(u64::MAX / 2);\nhead_opts([\"-c\", &n.to_string()]);","handlingStrategy":"validation","validationCode":"fn validate_count(raw: &str) -> Result<u64, String> {\n\tlet n: u64 = raw.parse().map_err(|_| format!(\"count not representable: {raw}\"))?;\n\tif n > (1 << 62) {\n\t\treturn Err(format!(\"count {n} too large for -n/-c\"));\n\t}\n\tOk(n)\n}","typeGuard":null,"tryCatchPattern":"match head_result {\n\tErr(HeadError::NumTooLarge(_)) => eprintln!(\"requested -n/-c value overflows; reduce the count\"),\n\tErr(other) => return Err(other),\n\tOk(v) => /* ... */,\n}","preventionTips":["Clamp computed counts to a sane maximum before passing them to head","Never pass sentinel 'very large' values; compute the actual needed count","Use checked arithmetic upstream when deriving counts from sizes"],"tags":["rust","integer-overflow","cli-options","thiserror"],"backgroundTag":"integer-overflow","analyzedSha":"969062200754ea02cfac922e5ebb8c608c079e15","analyzedAt":"2026-08-31T10:29:35.737Z","schemaVersion":2},"datasetVersion":"2026-08-31T14:17:45.589Z"}