Add tests and fix for mdns NameSliceIter next_back - #104
Conversation
The backwards labels iterator for NameSlice would always return None. This adds some tests for the forward and backwards iterator that would fail with the current implementation. To fix the issue I added a second index for tracking the backwards iterator seperate from the forward iterator. The docs for DoubleEndedIterator says "It is important to note that both back and forth work on the same range, and do not cross: iteration is over when they meet in the middle." Then a small bit of refactoring to extract common code from the back and next_back.
|
@mlandauer I'm so sorry - I was on a vacation at the time you opened the PR and I forgot about it upon my return. Let me review... |
|
@ivmarkov Thanks for letting me know. All good. Let me know if there's anything missing or needs adding. I also realise I haven't at all explained why I hit the problem I'm trying to fix. Don't know if that's helpful or not. |
There was a problem hiding this comment.
🟢 Approval recommended
The change is a small, self-contained, correct iterator bug fix that I verified across all cases (including empty slices and underflow safety) and that is backed by new forward, backward, and interleaved tests.
Pull request overview
This PR fixes a bug in edge-mdns's NameSliceIter, the DoubleEndedIterator over the labels of a NameSlice. Previously next_back() on a fresh iterator always returned None because it shared the single forward index (which starts at 0), making backward iteration unusable. The fix introduces a separate index_back cursor so forward and backward iteration operate over the same range without crossing, per the DoubleEndedIterator contract, and extracts the shared label-resolution logic into a label() helper. New unit tests cover forward, backward, and interleaved iteration.
I traced the new logic across forward-only, backward-only, interleaved, and empty-slice cases, and confirmed: the half-open range [index, index_back) with index_back initialized to len + 1 correctly yields all len labels plus the root label; index_back - 1 cannot underflow because the subtraction only runs when index_back != index; and the removed core::cmp::Ordering import has no remaining uses. The mod test naming and in-file test placement match the convention used in edge-http and edge-dhcp.
Changes:
- Add a separate
index_backcursor and initialize it tolen + 1so backward iteration works and meets forward iteration correctly in the middle. - Extract common label-resolution code into a
NameSliceIter::label()helper used by bothnext()andnext_back(). - Add unit tests for forward, backward, and both-direction label iteration; remove the now-unused
Orderingimport.
File summaries
| File | Description |
|---|---|
| edge-mdns/src/lib.rs | Adds index_back cursor and label() helper to fix next_back, initializes index_back in iter_labels, removes unused Ordering import, and adds tests for forward/backward/interleaved iteration. |
Review details
- Files reviewed: 1/1 changed files
- Comments generated: 0
- Review effort level: Balanced
💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.
|
Thanks! |
Certainly helpful if you explain it here, so that we have it as part of the PR tracking. |
|
I hit against the issue when I was trying to find mdns results matching a wildcard domain. Testing each found domain name (as a ParsedName) against a known domain using something like This would always give true whatever value |
) Subject says it all. Really two unrelated fixes, but since the memory fix in the `respond` module was a one-liner, I combined it with the BTP timeout fix. EDIT 1: Also back-ported an mDNS fix from this: sysgrok/edge-net#104 EDIT 2: The memory fix in `respond` (a port of sysgrok/edge-net#105) _might_ have far-reaching consequences, like completely getting rid of the [bump allocator in downstream crates](https://github.com/sysgrok/rs-matter-stack/blob/25113f98577c32e2b22feff3b6eb32a181d16e57/examples/light.rs#L50).
The backwards labels iterator for NameSlice would always return None. This adds some tests for the forward and backwards iterator that would fail with the current implementation.
To fix the issue I added a second index for tracking the backwards iterator seperate from the forward iterator.
The docs for DoubleEndedIterator says
"It is important to note that both back and forth work on the same range, and do not cross: iteration is over when they meet in the middle."
Then a small bit of refactoring to extract common code from the back and next_back.