Skip to content

Commit b5e42fc

Browse files
Fix: correct sliceInRange stop clamp for non-zero origin domains
The previous implementation clamped stop against domain.shape()[i] (size) instead of origin+shape, causing inverted intervals (start > stop) when origins were non-zero. Updated logic now computes the true exclusive upper bound and clamps correctly. Zero-origin behavior unchanged.
1 parent 2becd7a commit b5e42fc

File tree

1 file changed

+9
-7
lines changed

1 file changed

+9
-7
lines changed

mdio/variable.h

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -965,13 +965,15 @@ class Variable {
965965

966966
for (size_t idx = 0; idx < labels.size(); ++idx) {
967967
if (labels[idx] == desc.label) {
968-
// Clamp the descriptor to the domain.
969-
return {desc.label, // label
970-
desc.start < domain.origin()[idx] ? domain.origin()[idx]
971-
: desc.start, // start
972-
desc.stop > domain.shape()[idx] ? domain.shape()[idx]
973-
: desc.stop, // stop
974-
desc.step}; // step
968+
// Clamp the descriptor to the domain using absolute coordinates.
969+
// NOTE: domain.shape()[idx] is a size, not an absolute upper bound when
970+
// the origin is non-zero. The correct exclusive upper bound is
971+
// origin + shape.
972+
Index dim_origin = domain.origin()[idx];
973+
Index dim_upper = dim_origin + domain.shape()[idx]; // exclusive
974+
Index clamped_start = desc.start < dim_origin ? dim_origin : desc.start;
975+
Index clamped_stop = desc.stop > dim_upper ? dim_upper : desc.stop;
976+
return {desc.label, clamped_start, clamped_stop, desc.step};
975977
}
976978
}
977979
// We don't slice along a dimension that doesn't exist, so the descriptor is

0 commit comments

Comments
 (0)