can1357/oh-my-pi · error · HeadError

number of -bytes or -lines is too large

Error message

number of -bytes or -lines is too large

What it means

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.

Source

Thrown at crates/pi-builtins/src/head.rs:993

		assert_eq!(Some(String::from("b")), iter.next());
		assert_eq!(Some(String::from("c")), iter.next());
		assert_eq!(None, iter.next());
	}
}
}

use take::{copy_all_but_n_bytes, copy_all_but_n_lines, take_lines};

#[derive(Error, Debug)]
enum HeadError {
	/// Wrapper around `io::Error`
	#[error("error reading {}: {}", name.quote(), err)]
	Io { name: PathBuf, err: io::Error },

	#[error("{0}")]
	ParseError(String),

	#[error("number of -bytes or -lines is too large")]
	NumTooLarge(#[from] TryFromIntError),


	#[error("{0}")]
	MatchOption(String),
}

type HeadResult<T> = Result<T, HeadError>;

#[derive(Debug, PartialEq)]
enum Mode {
	FirstLines(u64),
	AllButLastLines(u64),
	FirstBytes(u64),
	AllButLastBytes(u64),
}

impl Default for Mode {

View on GitHub (pinned to 9690622007)

Solutions

  1. Reduce the -n/-c count to a value within u64 range
  2. Sanitize or clamp computed counts before invoking the builtin
  3. Guard the conversion yourself with u64::try_from and an explicit cap

Example fix

// before
let n: u64 = big_value; // may exceed representable range
head_opts(["-c", &n.to_string()]);
// after
let n = big_value.min(u64::MAX / 2);
head_opts(["-c", &n.to_string()]);
Defensive patterns

Strategy: validation

Validate before calling

fn validate_count(raw: &str) -> Result<u64, String> {
	let n: u64 = raw.parse().map_err(|_| format!("count not representable: {raw}"))?;
	if n > (1 << 62) {
		return Err(format!("count {n} too large for -n/-c"));
	}
	Ok(n)
}

Try / catch

match head_result {
	Err(HeadError::NumTooLarge(_)) => eprintln!("requested -n/-c value overflows; reduce the count"),
	Err(other) => return Err(other),
	Ok(v) => /* ... */,
}

Prevention

When it happens

Trigger: 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.

Common situations: 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.

Related errors


AI-assisted analysis of can1357/oh-my-pi@9690622007 (2026-08-31). Data as JSON: /api/errors/6451943cdcc88333. Report an issue: GitHub.