Commit 4c275d7
authored
security: harden core data-path against OOM, UB, NaN, and integer-overflow (v0.4.11 candidate) (#367)
* fix(matrix): correct is_valid_view logic, checked stride_range arithmetic, fix is_empty on views, fix iterator_mut aliasing UB
- is_valid_view: fix inverted boolean and off-by-one (end must be <= bound, not <)
- stride_range: use checked_mul / checked_add; panic! with clear message on overflow
(panic-on-overflow is by-design: the library must not silently access wrong memory)
- DenseMatrixView::is_empty / DenseMatrixMutView::is_empty: fix inverted predicate
(was `> 0` i.e. true when NOT empty; correct is `== 0`)
- iterator_mut: assert no duplicate (row,col) indices are yielded so two live &mut T
to the same slot are impossible; adds a debug-mode uniqueness check via a
statically-sized index calculation that matches the actual ptr.add offset
* fix(dataset): guard deserialize_data against OOM, OOB, and NaN/Inf injection
- Check minimum byte length before reading headers
- Use checked_mul for num_samples * num_features to prevent overflow
- Validate computed data length against actual slice length before allocation
- Sanitize each f32 value: reject NaN and Inf bit patterns with a clear error
(attackers can craft bit patterns that silently corrupt downstream numerics)
* security: apply remaining fixes from PR plan (changes 7, 8, 9 + change 4 ndarray stride guard)
- Change 7: add zero-norm guard in CosinePair::distances_from and init
to prevent NaN cosine distances from poisoning BinaryHeap ordering.
- Change 8: add NaN guard to gradient descent convergence loop so that
a NaN gnorm panics immediately instead of silently exiting after 1
iteration with NaN weights.
- Change 9: add jagged-row validation in from_2d_vec so that rows with
different lengths return Err instead of panicking with index-out-of-bounds.
- Change 4 (ndarray): add debug_assert that strides are non-negative
before casting isize -> usize in ndarray/matrix.rs iterator_mut.
* security: Change 9 – reject jagged arrays in from_2d_vec
Previously, from_2d_vec took ncols from values[0] and then indexed
values[r][c] for every row r, which panics at runtime if any row is
shorter than values[0]. The fix iterates over all rows once up-front
and returns Err(Failed::input(…)) if any row differs in length from
the first row, turning the implicit panic into a proper Result error.
* fix(ndarray): remove all unsafe blocks; fix is_empty inversion on all three impls
- iterator_mut for both OwnedRepr and ArrayViewMut now delegates to
ndarray's own safe iter_mut() for axis-0 and constructs a column-major
Vec<&mut T> via the safe .get_mut([r, c]) accessor for axis-1.
No raw pointer arithmetic, no unsafe blocks, no FFI.
- is_empty was returning `self.len() > 0` (inverted) on all three
ndarray impls (OwnedRepr, ArrayView, ArrayViewMut). Fixed to
`self.len() == 0`, consistent with basic/matrix.rs.
- Removed the debug_assert!(...) stride negativity guards that were
paired with the now-deleted unsafe casts; the safe API makes them
unnecessary.
Design principles applied:
* No unsafe blocks
* No FFI, pure Rust
* Data access via iterator() / safe ndarray methods only
* panic! is acceptable for truly unrecoverable states (axis assert)
* fix: replace unsafe iterator_mut with safe axis_iter_mut(Axis(1)) and fix is_empty inversion on all three ndarray impls
* fix(ndarray): implement ArrayView2+MutArray+MutArrayView2 for ArrayViewMut to satisfy slice_mut return type; remove unsafe, fix is_empty
* fix(matrix): disambiguate Array::is_empty call in tests via fully-qualified syntax
* fix(gradient_descent): move NaN guard after each df call so degenerate gradients always panic
* fix(dataset): use fixed-width u64 header in deserialize_data so wasm32 reads xy files correctly
* style: apply rustfmt formatting to dataset/mod.rs (CI lint fix)
* style: apply rustfmt formatting to linalg/basic/matrix.rs (CI lint fix)
* style: apply rustfmt formatting to linalg/ndarray/matrix.rs (CI lint fix)1 parent a70676d commit 4c275d7
4 files changed
Lines changed: 492 additions & 205 deletions
File tree
- src
- dataset
- linalg
- basic
- ndarray
- optimization/first_order
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
62 | 62 | | |
63 | 63 | | |
64 | 64 | | |
65 | | - | |
66 | | - | |
| 65 | + | |
| 66 | + | |
| 67 | + | |
| 68 | + | |
67 | 69 | | |
68 | 70 | | |
69 | 71 | | |
| |||
84 | 86 | | |
85 | 87 | | |
86 | 88 | | |
| 89 | + | |
| 90 | + | |
| 91 | + | |
| 92 | + | |
| 93 | + | |
| 94 | + | |
| 95 | + | |
| 96 | + | |
| 97 | + | |
| 98 | + | |
| 99 | + | |
| 100 | + | |
| 101 | + | |
| 102 | + | |
87 | 103 | | |
88 | 104 | | |
89 | 105 | | |
90 | | - | |
91 | | - | |
| 106 | + | |
| 107 | + | |
| 108 | + | |
| 109 | + | |
| 110 | + | |
| 111 | + | |
| 112 | + | |
| 113 | + | |
| 114 | + | |
| 115 | + | |
| 116 | + | |
| 117 | + | |
| 118 | + | |
| 119 | + | |
| 120 | + | |
92 | 121 | | |
93 | | - | |
94 | | - | |
95 | | - | |
96 | | - | |
97 | | - | |
| 122 | + | |
| 123 | + | |
| 124 | + | |
| 125 | + | |
| 126 | + | |
98 | 127 | | |
99 | 128 | | |
100 | 129 | | |
101 | | - | |
| 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 | + | |
102 | 172 | | |
103 | 173 | | |
104 | | - | |
105 | | - | |
106 | | - | |
107 | | - | |
108 | | - | |
| 174 | + | |
| 175 | + | |
| 176 | + | |
| 177 | + | |
| 178 | + | |
| 179 | + | |
| 180 | + | |
| 181 | + | |
| 182 | + | |
| 183 | + | |
| 184 | + | |
| 185 | + | |
| 186 | + | |
| 187 | + | |
| 188 | + | |
| 189 | + | |
109 | 190 | | |
110 | 191 | | |
111 | 192 | | |
112 | | - | |
113 | | - | |
114 | | - | |
| 193 | + | |
| 194 | + | |
| 195 | + | |
| 196 | + | |
| 197 | + | |
| 198 | + | |
| 199 | + | |
| 200 | + | |
| 201 | + | |
| 202 | + | |
| 203 | + | |
| 204 | + | |
| 205 | + | |
115 | 206 | | |
116 | 207 | | |
117 | 208 | | |
| |||
144 | 235 | | |
145 | 236 | | |
146 | 237 | | |
| 238 | + | |
| 239 | + | |
| 240 | + | |
| 241 | + | |
| 242 | + | |
| 243 | + | |
| 244 | + | |
| 245 | + | |
| 246 | + | |
| 247 | + | |
| 248 | + | |
| 249 | + | |
| 250 | + | |
| 251 | + | |
| 252 | + | |
| 253 | + | |
| 254 | + | |
| 255 | + | |
| 256 | + | |
| 257 | + | |
| 258 | + | |
| 259 | + | |
| 260 | + | |
| 261 | + | |
| 262 | + | |
| 263 | + | |
| 264 | + | |
| 265 | + | |
| 266 | + | |
| 267 | + | |
| 268 | + | |
| 269 | + | |
| 270 | + | |
| 271 | + | |
| 272 | + | |
| 273 | + | |
| 274 | + | |
| 275 | + | |
| 276 | + | |
| 277 | + | |
| 278 | + | |
| 279 | + | |
| 280 | + | |
| 281 | + | |
| 282 | + | |
| 283 | + | |
| 284 | + | |
| 285 | + | |
| 286 | + | |
| 287 | + | |
| 288 | + | |
| 289 | + | |
| 290 | + | |
| 291 | + | |
| 292 | + | |
| 293 | + | |
| 294 | + | |
| 295 | + | |
| 296 | + | |
| 297 | + | |
| 298 | + | |
| 299 | + | |
| 300 | + | |
| 301 | + | |
| 302 | + | |
| 303 | + | |
| 304 | + | |
147 | 305 | | |
0 commit comments