Commit bcc9d30
Handle non-finite ROI coordinates in CropAndResize (#29605)
### Description
The `CropAndResize` CPU contrib op (`com.microsoft::CropAndResize`)
computes bilinear/nearest interpolation indices from the ROI box
coordinates. The bounds guards were written as `if (in_y < 0 || in_y >
height - 1)` / `if (in_x < 0 || in_x > width - 1)`. Because every
comparison involving a NaN is false, a non-finite (NaN or ±inf) ROI
coordinate slipped past both comparisons, skipped the extrapolation
`continue`, and reached the integer index computation
(`(int)floorf(NaN)`) with an invalid value.
This is a robustness/correctness gap: ROI coordinates are a runtime
input, and non-finite values are not handled gracefully.
### Fix
- **NaN-safe bounds guards.** Rewrite both guards into
negated-conjunction form: `if (!(in_y >= 0 && in_y <= height - 1))` /
`if (!(in_x >= 0 && in_x <= width - 1))`. This is logically identical
for all finite coordinates, but is `true` for NaN/±inf, so non-finite
coordinates now take the extrapolation branch and are filled with
`extrapolation_value` (matching the documented behavior for out-of-range
coordinates).
- **crop_size validation.** Add a Status-based
`ORT_RETURN_IF_NOT(crop_height > 0 && crop_width > 0, ...)` check in
`Compute` so non-positive crop sizes are rejected with a clear error
rather than producing degenerate work.
- **Index arithmetic hardening.** Use `SafeInt<int64_t>` for the
interpolation index computation, which allows the now-unnecessary MSVC
26451 (arithmetic-overflow) suppression pragma to be removed.
No behavior change for valid finite ROIs. CPU-only — there is no CUDA
`CropAndResize` kernel.
### Tests
Added to `onnxruntime/test/contrib_ops/crop_and_resize_op_test.cc`:
- NaN ROI coordinate → extrapolation (bilinear height, bilinear width,
and nearest).
- ±inf ROI coordinate → extrapolation.
- Finite boundary values (exact `[0, 1]` identity crop and just-outside
`1.0001`) — no regression.
- Non-positive `crop_size` (`{0, 2}` and `{-1, 2}`) rejected.
- Out-of-range batch index rejected.
Run with:
```
onnxruntime_provider_test --gtest_filter='CropAndResizeTest.*'
```
All 10 CropAndResize tests pass (5 new + 5 pre-existing, no regression).
An AddressSanitizer build can additionally corroborate the index
handling in CI.
### Motivation and Context
Handles non-finite ROI coordinates gracefully and makes the
CropAndResize index arithmetic robust, consistent with how out-of-range
coordinates are already treated (extrapolation).
Signed-off-by: Tita Wang <titaiwang@microsoft.com>
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>1 parent 682936c commit bcc9d30
2 files changed
Lines changed: 139 additions & 14 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
17 | 17 | | |
18 | 18 | | |
19 | 19 | | |
| 20 | + | |
20 | 21 | | |
21 | 22 | | |
22 | 23 | | |
23 | 24 | | |
24 | 25 | | |
25 | | - | |
26 | | - | |
27 | | - | |
28 | | - | |
29 | | - | |
30 | 26 | | |
31 | 27 | | |
32 | 28 | | |
| |||
97 | 93 | | |
98 | 94 | | |
99 | 95 | | |
100 | | - | |
| 96 | + | |
| 97 | + | |
| 98 | + | |
| 99 | + | |
| 100 | + | |
101 | 101 | | |
102 | 102 | | |
103 | 103 | | |
| |||
126 | 126 | | |
127 | 127 | | |
128 | 128 | | |
129 | | - | |
| 129 | + | |
| 130 | + | |
130 | 131 | | |
131 | 132 | | |
132 | 133 | | |
| |||
140 | 141 | | |
141 | 142 | | |
142 | 143 | | |
143 | | - | |
144 | | - | |
145 | | - | |
146 | | - | |
| 144 | + | |
| 145 | + | |
| 146 | + | |
| 147 | + | |
147 | 148 | | |
148 | 149 | | |
149 | 150 | | |
150 | 151 | | |
151 | 152 | | |
152 | | - | |
| 153 | + | |
| 154 | + | |
153 | 155 | | |
154 | 156 | | |
155 | 157 | | |
| |||
162 | 164 | | |
163 | 165 | | |
164 | 166 | | |
165 | | - | |
| 167 | + | |
166 | 168 | | |
167 | 169 | | |
168 | 170 | | |
169 | 171 | | |
170 | 172 | | |
171 | | - | |
| 173 | + | |
| 174 | + | |
172 | 175 | | |
173 | 176 | | |
174 | 177 | | |
| |||
217 | 220 | | |
218 | 221 | | |
219 | 222 | | |
| 223 | + | |
| 224 | + | |
| 225 | + | |
220 | 226 | | |
221 | 227 | | |
222 | 228 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
4 | 4 | | |
5 | 5 | | |
6 | 6 | | |
| 7 | + | |
| 8 | + | |
7 | 9 | | |
8 | 10 | | |
9 | 11 | | |
| |||
123 | 125 | | |
124 | 126 | | |
125 | 127 | | |
| 128 | + | |
| 129 | + | |
| 130 | + | |
| 131 | + | |
| 132 | + | |
| 133 | + | |
| 134 | + | |
| 135 | + | |
| 136 | + | |
| 137 | + | |
| 138 | + | |
| 139 | + | |
| 140 | + | |
| 141 | + | |
| 142 | + | |
| 143 | + | |
| 144 | + | |
| 145 | + | |
| 146 | + | |
| 147 | + | |
| 148 | + | |
| 149 | + | |
| 150 | + | |
| 151 | + | |
| 152 | + | |
| 153 | + | |
| 154 | + | |
| 155 | + | |
| 156 | + | |
| 157 | + | |
| 158 | + | |
| 159 | + | |
| 160 | + | |
| 161 | + | |
| 162 | + | |
| 163 | + | |
| 164 | + | |
| 165 | + | |
| 166 | + | |
| 167 | + | |
| 168 | + | |
| 169 | + | |
| 170 | + | |
| 171 | + | |
| 172 | + | |
| 173 | + | |
| 174 | + | |
| 175 | + | |
| 176 | + | |
| 177 | + | |
| 178 | + | |
| 179 | + | |
| 180 | + | |
| 181 | + | |
| 182 | + | |
| 183 | + | |
| 184 | + | |
| 185 | + | |
| 186 | + | |
| 187 | + | |
| 188 | + | |
| 189 | + | |
| 190 | + | |
| 191 | + | |
| 192 | + | |
| 193 | + | |
| 194 | + | |
| 195 | + | |
| 196 | + | |
| 197 | + | |
| 198 | + | |
| 199 | + | |
| 200 | + | |
| 201 | + | |
| 202 | + | |
| 203 | + | |
| 204 | + | |
| 205 | + | |
| 206 | + | |
| 207 | + | |
| 208 | + | |
| 209 | + | |
| 210 | + | |
| 211 | + | |
| 212 | + | |
| 213 | + | |
| 214 | + | |
| 215 | + | |
| 216 | + | |
| 217 | + | |
| 218 | + | |
| 219 | + | |
| 220 | + | |
| 221 | + | |
| 222 | + | |
| 223 | + | |
| 224 | + | |
| 225 | + | |
| 226 | + | |
| 227 | + | |
| 228 | + | |
| 229 | + | |
| 230 | + | |
| 231 | + | |
| 232 | + | |
| 233 | + | |
| 234 | + | |
| 235 | + | |
| 236 | + | |
| 237 | + | |
| 238 | + | |
| 239 | + | |
| 240 | + | |
| 241 | + | |
| 242 | + | |
| 243 | + | |
| 244 | + | |
126 | 245 | | |
127 | 246 | | |
0 commit comments