Commit 614d8ab
committed
Fix divide-by-zero vulnerability in NestedLearner
Addresses critical divide-by-zero errors in Train and AdaptToNewTask methods
when processing empty datasets.
## Issue
Both methods called _numOps.Divide(..., _numOps.FromDouble(dataList.Count))
without checking if dataList.Count == 0, causing runtime divide-by-zero errors.
## Locations Fixed
1. Train method (line 164): Computing average loss over training data
2. AdaptToNewTask method (line 228): Computing average new task loss
## Solution
Added empty dataset guards immediately after building dataList:
**Train method:**
- Returns MetaTrainingResult with:
- FinalMetaLoss = Zero
- FinalTaskLoss = Zero
- FinalAccuracy = Zero
- TotalIterations = current _globalStep
- TotalTimeMs = elapsed time from stopwatch
- Converged = false
**AdaptToNewTask method:**
- Returns MetaAdaptationResult with:
- NewTaskLoss = Zero
- ForgettingMetric = Zero
- AdaptationSteps = 0
- AdaptationTimeMs = elapsed time from stopwatch
## Behavior
- Preserves stopwatch timing (starts, stops, records elapsed time)
- Returns sensible default values for empty datasets
- No divide operations executed when count is zero
- Maintains method contracts and return types
- Does not throw exceptions for empty input (graceful handling)
## Impact
- ✅ Prevents runtime divide-by-zero errors
- ✅ Gracefully handles edge case of empty datasets
- ✅ Maintains timing accuracy
- ✅ Returns semantically correct results (zero loss for no data)1 parent a34e212 commit 614d8ab
1 file changed
+29
-0
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
145 | 145 | | |
146 | 146 | | |
147 | 147 | | |
| 148 | + | |
| 149 | + | |
| 150 | + | |
| 151 | + | |
| 152 | + | |
| 153 | + | |
| 154 | + | |
| 155 | + | |
| 156 | + | |
| 157 | + | |
| 158 | + | |
| 159 | + | |
| 160 | + | |
| 161 | + | |
| 162 | + | |
148 | 163 | | |
149 | 164 | | |
150 | 165 | | |
| |||
205 | 220 | | |
206 | 221 | | |
207 | 222 | | |
| 223 | + | |
| 224 | + | |
| 225 | + | |
| 226 | + | |
| 227 | + | |
| 228 | + | |
| 229 | + | |
| 230 | + | |
| 231 | + | |
| 232 | + | |
| 233 | + | |
| 234 | + | |
| 235 | + | |
| 236 | + | |
208 | 237 | | |
209 | 238 | | |
210 | 239 | | |
| |||
0 commit comments