bgpd: Fix integer truncation of SRLG count when parsing SRLG TLV - #21077
Merged
Jafaral merged 1 commit intoMar 11, 2026
Merged
Conversation
`count` is declared as `uint8_t` but receives `length / 4` where `length` is a `uint16_t`. For `length >= 1024`, the division result exceeds 255, which cannot be stored in `count` since `uint8_t` holds at most 255, and the value silently truncates. The result is then stored in `attr->srlg_count`, also `uint8_t`, which would re-truncate it. Widen `count` in `parse_srlg()` and `srlg_count` in `struct bgp_ls_attr` to `uint16_t`. Signed-off-by: Carmine Scarpitta <cscarpit@cisco.com>
Greptile SummaryThis PR fixes a silent integer truncation bug in
Confidence Score: 5/5
Important Files Changed
Flowchart%%{init: {'theme': 'neutral'}}%%
flowchart TD
A["Receive SRLG TLV\n(length: uint16_t)"] --> B{"length % 4 != 0?"}
B -- Yes --> C["flog_warn + return -1"]
B -- No --> D["count = length / 4\n(uint16_t β fixed from uint8_t)"]
D --> E{"count > BGP_LS_MAX_SRLG\n(64)?"}
E -- Yes --> F["flog_warn + return -1"]
E -- No --> G["XMALLOC count Γ 4 bytes"]
G --> H["Loop: read count uint32_t SRLGs\nfrom stream"]
H --> I["attr->srlg_count = count\n(uint16_t β fixed from uint8_t)"]
I --> J["Set BGP_LS_ATTR_SRLG_BIT\nreturn 0"]
style D fill:#d4edda,stroke:#28a745
style I fill:#d4edda,stroke:#28a745
style C fill:#f8d7da,stroke:#dc3545
style F fill:#f8d7da,stroke:#dc3545
Last reviewed commit: ef8a4c6 |
Jafaral
approved these changes
Mar 11, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
countis declared asuint8_tbut receiveslength / 4wherelengthis auint16_t. Forlength >= 1024, the division result exceeds 255, which cannot be stored incountsinceuint8_tholds at most 255, and the value silently truncates. The result is then stored inattr->srlg_count, alsouint8_t, which would re-truncate it.Fix the issue by widening
countinparse_srlg()andsrlg_countinstruct bgp_ls_attrtouint16_t.