#8481·onnx

Slice shape inference aborts the process (SIGABRT) on a negative input dim

Author: bfrantzdale-nvidiaCreated Sep 21, 2026Updated Sep 22, 2026

Version: onnx 1.21.0 and 1.23.0 (latest); Linux x86_64 & aarch64 wheels (built with gcc-toolset-14 → libstdc++ std::clamp assertions active)

What happens processSliceInputs in onnx/defs/tensor/defs.cc short-circuits dim == 0 but not a negative dim, then does std::clamp(start, int64_t{0}, dim - 1) (https://github.com/onnx/onnx/blob/main/onnx/defs/tensor/defs.cc#L744-L756). When a sliced axis has a negative dim_value, the upper bound dim - 1 is below the lower bound 0, so libstdc++'s hardened std::clamp precondition !(hi < lo) fails and the process aborts (SIGABRT) — no catchable exception:

stl_algo.h:3626: std::clamp(...) [_Tp = long int]: Assertion '!(__hi < __lo)' failed.

onnx.checker.check_model() does NOT reject the negative dim, so nothing upstream guards it. A library aborting the host process on checker-passing input is the defect regardless of how the negative dim arose. (Same unguarded pattern in onnx/defs/tensor/old.cc for older Slice opsets.)

Minimal repro (deterministic)

python
import onnx, numpy as np
from onnx import helper, TensorProto, numpy_helper
mk = lambda n, v: numpy_helper.from_array(np.array([v], dtype=np.int64), n)
X = helper.make_tensor_value_info("X", TensorProto.FLOAT, [3, -1])   # negative dim_value
Y = helper.make_tensor_value_info("Y", TensorProto.FLOAT, [3, "M"])
node = helper.make_node("Slice", ["X","starts","ends","axes","steps"], ["Y"])
g = helper.make_graph([node], "g", [X], [Y],
        initializer=[mk("starts",0), mk("ends",2**63-1), mk("axes",1), mk("steps",1)])
m = helper.make_model(g, opset_imports=[helper.make_opsetid("",21)]); m.ir_version = 10
onnx.checker.check_model(m)            # passes
onnx.shape_inference.infer_shapes(m)   # SIGABRT

Expected: raise fail_shape_inference(...) (catchable ValidationError) or treat
dim < 0 like dim == 0 (unknown/empty) — never abort().

Fix: change the early-return guard from == 0 to <= 0 (or clamp the upper bound std::max(int64_t{0}, dim - 1)), in both defs.cc and old.cc. That or we should assert non-negative or throw if negative.

I think also onnx.checker.check_model(m) should fail too. Like maybe:

    case TypeProto::kTensorType: {
      const auto& type = value_info.type().tensor_type();
      enforce_has_field(type, elem_type);
      enforce_has_field(type, shape);
+     // IR: a tensor shape is a list of non-negative integers (docs/IR.md).
+     for (const auto& dim : type.shape().dim()) {
+       if (dim.has_dim_value() && dim.dim_value() < 0) {
+         fail_check(
+             "Invalid tensor shape (value_info name: ", value_info.name(),
+             "): dimension value must be non-negative, got ", dim.dim_value());
+       }
+     }
    } break;

?