fix: Use loop in FindByCommit::next to avoid stack overflow

Use an iterative loop with `return` and `continue` to avoid stack overflow.
A stack overflow could have occurred due to the recursion, and
potentially many jobs.
This commit is contained in:
Fintan Halpenny
2026-03-06 08:30:58 +00:00
parent 07feea6ea1
commit fd69685db8
2 changed files with 12 additions and 8 deletions
+5
View File
@@ -7,6 +7,11 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
## [Unreleased]
### Fixes
- Use an iterative loop to avoid stack overflow, through recursion, when using
`FindByCommit` iterator.
## 0.5.0
### Highlights
+7 -8
View File
@@ -657,15 +657,14 @@ impl Iterator for FindByCommit<'_> {
type Item = Result<(JobId, Job), cob::store::Error>;
fn next(&mut self) -> Option<Self::Item> {
let job = self.jobs.next()?;
job.and_then(|(id, job)| {
if self.wanted(&job) {
Ok(Some((JobId::from(id), job)))
} else {
self.next().transpose()
loop {
let job = self.jobs.next()?;
match job {
Ok((id, job)) if self.wanted(&job) => return Some(Ok((JobId::from(id), job))),
Ok(_) => continue,
Err(err) => return Some(Err(err)),
}
})
.transpose()
}
}
}