pola-rs/polars · error

offset exceeds usize limits

Error message

offset exceeds usize limits

What it means

`Column::split_at(offset)` takes an i64 offset; for negative offsets the absolute value is converted to usize before being subtracted from the length. The conversion panics if the absolute value cannot be represented in usize, which on the negative branch happens only when offset == i64::MIN (whose absolute value overflows i64) or, on 32-bit targets, when |offset| > usize::MAX.

Solutions

  1. Pass offsets within a safe range: for negative offsets use offset > i64::MIN (e.g. >= i64::MIN + 1), and keep |offset| within usize range on the target platform.
  2. Validate the offset before calling: reject i64::MIN explicitly and clamp to the column length.
  3. On 32-bit targets, pre-check `offset >= -(usize::MAX as i64)` before splitting.

Example fix

// before
col.split_at(i64::MIN);

// after
let offset = offset.max(i64::MIN + 1).min(col.len() as i64);
col.split_at(offset);
Defensive patterns

Strategy: validation

Validate before calling

assert!(offset != i64::MIN, "offset.abs() would overflow");
let off = offset.clamp(-(col.len() as i64), col.len() as i64);

Prevention

When it happens

Trigger: Calling `column.split_at(i64::MIN)`; on 32-bit platforms calling `split_at` with a negative offset whose absolute value exceeds u32 range; any code path computing a split offset from untrusted input that can be i64::MIN.

Common situations: Generic numeric code passing sentinel values like i64::MIN as a 'beginning of column' marker; FFI/PyO3 boundaries where a Python int -9223372036854775808 is forwarded; 32-bit embedded/WASM builds using large negative offsets.

Understand the failure class

Background: "value must be between 0 and 1" / "out of range" / "must not be negative" errors: fixing range-validation failures across open-source libraries — this error's family across 42 libraries.

Related errors


AI-assisted analysis of pola-rs/polars@fe841f959e (2026-09-18). Data as JSON: /api/errors/7fcba5e7f059b2fc. Report an issue: GitHub.

Appendix: source

Thrown at crates/polars-core/src/frame/column/mod.rs:637

        self.slice(-(len as i64), len)
    }
    pub fn slice(&self, offset: i64, length: usize) -> Column {
        match self {
            Column::Series(s) => s.slice(offset, length).into(),
            Column::Scalar(s) => {
                let (_, length) = slice_offsets(offset, length, s.len());
                s.resize(length).into()
            },
        }
    }

    pub fn split_at(&self, offset: i64) -> (Column, Column) {
        match self {
            Column::Scalar(c) => {
                let len = c.len();
                let offset = if offset < 0 {
                    let offset_abs = usize::try_from(offset.strict_abs())
                        .expect("offset exceeds usize limits")
                        .min(len);
                    len - offset_abs
                } else {
                    usize::try_from(offset)
                        .expect("offset exceeds usize limits")
                        .min(len)
                };
                (
                    Column::Scalar(c.resize(offset)),
                    Column::Scalar(c.resize(len - offset)),
                )
            },
            Column::Series(_) => {
                let (l, r) = self.as_materialized_series().split_at(offset);
                (l.into(), r.into())
            },
        }
    }

View on GitHub (pinned to fe841f959e)