Skip to content

lib: Some smaller code fixes for typesafe hash _member function - #22233

Merged
donaldsharp merged 2 commits into
FRRouting:masterfrom
robinchrist:lib-fix-hash-member-return-value
Jun 8, 2026
Merged

lib: Some smaller code fixes for typesafe hash _member function#22233
donaldsharp merged 2 commits into
FRRouting:masterfrom
robinchrist:lib-fix-hash-member-return-value

Conversation

@robinchrist

Copy link
Copy Markdown
Contributor

While looking through the code for the typesafe hash, I noticed some smaller inconsistencies in the typeseafe hash _member function:

  1. if (!h->hh.tabshift) did return NULL. The functions return type is bool. While this works, it's misleading
  2. Unnecessary comparison loop. The result of the while loop was simply discarded in the for init and loop did not do anything useful. Replaced the for with another while, so this aligns e.g. with _del

@greptile-apps

greptile-apps Bot commented Jun 6, 2026

Copy link
Copy Markdown

Greptile Summary

This PR fixes two small issues in the typesafe hash _member macro in lib/typesafe.h. The changes are straightforward correctness improvements with no functional regressions.

  • Replaces return NULL with return false in a bool-returning function, making the early-exit semantically correct rather than relying on implicit integer-to-bool coercion.
  • Removes a dead while loop whose result was silently discarded by the for loop that re-initialized hitem = h->hh.entries[hbits], replacing the combined while+for with two while loops that match the pattern already used in _del and _const_find.

Confidence Score: 5/5

Both changes are safe to merge: the return false fix and the loop restructure are semantically correct and consistent with the existing patterns in _del and _const_find.

The dead while loop in the original code was harmless in practice since the for loop immediately reset hitem, and the new two-while pattern behaves identically. The return false fix is unconditionally correct. No edge cases are introduced.

No files require special attention.

Important Files Changed

Filename Overview
lib/typesafe.h Two correctness fixes in the _member macro: return NULL β†’ return false for a bool function, and a dead while loop + redundant for replaced with the two-while pattern consistent with _del and _const_find.

Flowchart

%%{init: {'theme': 'neutral'}}%%
flowchart TD
    A[_member called] --> B{tabshift == 0?}
    B -- yes --> C[return false]
    B -- no --> D[compute hval and hbits]
    D --> E[hitem = entries at hbits]
    E --> F{hitem exists AND hashval less than hval?}
    F -- yes --> G[advance hitem]
    G --> F
    F -- no --> H{hitem exists AND hashval equals hval?}
    H -- yes --> I{hitem is item field?}
    I -- yes --> J[return true]
    I -- no --> K[advance hitem]
    K --> H
    H -- no --> L[return false]
Loading

Reviews (1): Last reviewed commit: "lib: Remove unnecessary comparison loop ..." | Re-trigger Greptile

Typesafe hash _member has return type bool, but the initial check did
return NULL which is somehow valid due to C semantics, but misleading

Signed-off-by: Robin Christ <r.christ@partimus.com>
The typesafe hash _member function basically walked through the table twice
because the result of the first while loop was discarded in the for loop
init. Remove the for loop and replace with a while loop, this makes
the function similar to _del

Signed-off-by: Robin Christ <r.christ@partimus.com>
@robinchrist
robinchrist force-pushed the lib-fix-hash-member-return-value branch from dcc8ef0 to b3696d2 Compare June 6, 2026 12:04
@donaldsharp
donaldsharp merged commit 03b8b9f into FRRouting:master Jun 8, 2026
23 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants