*: don't use static char buffer in srv6 zapi code - #21884
Conversation
Greptile SummaryReplaces the
Confidence Score: 5/5Safe to merge β a clean removal of a static buffer with correct bounds checks and stream alignment on all code paths. The change is narrowly scoped: one function signature update and three matching call-site adjustments. The new bounds guard ( No files require special attention. Important Files Changed
Sequence DiagramsequenceDiagram
participant BGP as bgpd/bgp_zebra.c
participant ISIS as isisd/isis_zebra.c
participant STATIC as staticd/static_zebra.c
participant DEC as zapi_srv6_sid_notify_decode()
BGP->>DEC: loc_name[SRV6_LOCNAME_SIZE], sizeof(loc_name)
DEC->>DEC: STREAM_GETW(len)
DEC->>DEC: "if len >= loc_size return false"
DEC->>DEC: STREAM_GET(locator_name, s, len)
DEC->>DEC: "locator_name[len] = 0"
DEC-->>BGP: true, loc_name populated
ISIS->>DEC: NULL, 0
DEC->>DEC: STREAM_GETW(len)
DEC->>DEC: "locator_name==NULL skip size check"
DEC->>DEC: "else if len>0 stream_forward_getp(s, len)"
DEC-->>ISIS: true, stream correctly advanced
STATIC->>DEC: NULL, 0
DEC->>DEC: STREAM_GETW(len)
DEC->>DEC: "locator_name==NULL skip size check"
DEC->>DEC: "else if len>0 stream_forward_getp(s, len)"
DEC-->>STATIC: true, stream correctly advanced
Reviews (2): Last reviewed commit: "*: don't use static char buffer in srv6 ..." | Re-trigger Greptile |
| if (locator_name != NULL) { | ||
| /* This is a cstring, and it looks like it's normally sent without | ||
| * its terminal NULL... | ||
| */ | ||
| if (len == 0) { | ||
| locator_name[0] = 0; | ||
| } else { | ||
| STREAM_GET(locator_name, s, len); | ||
| *p_locator_name = locator_name; | ||
| locator_name[len] = 0; | ||
| } | ||
| } | ||
| return true; |
There was a problem hiding this comment.
When
locator_name is NULL, STREAM_GETW has already advanced the stream past the 2-byte length field, but the len data bytes that follow are never consumed. The function returns true, leaving the stream offset short by len bytes. This is preserved behaviour from the original code, but if any future caller reads from the stream after this call (or if a protocol extension appends data after the locator name), the stream will be silently misaligned. Adding stream_forward_getp(s, len) in the else if (len > 0) branch would make the success path semantically consistent and future-proof.
| if (locator_name != NULL) { | |
| /* This is a cstring, and it looks like it's normally sent without | |
| * its terminal NULL... | |
| */ | |
| if (len == 0) { | |
| locator_name[0] = 0; | |
| } else { | |
| STREAM_GET(locator_name, s, len); | |
| *p_locator_name = locator_name; | |
| locator_name[len] = 0; | |
| } | |
| } | |
| return true; | |
| if (locator_name != NULL) { | |
| /* This is a cstring, and it looks like it's normally sent without | |
| * its terminal NULL... | |
| */ | |
| if (len == 0) { | |
| locator_name[0] = 0; | |
| } else { | |
| STREAM_GET(locator_name, s, len); | |
| locator_name[len] = 0; | |
| } | |
| } else if (len > 0) { | |
| stream_forward_getp(s, len); | |
| } | |
| return true; |
Prompt To Fix With AI
This is a comment left during a code review.
Path: lib/zclient.c
Line: 2365-2376
Comment:
When `locator_name` is `NULL`, `STREAM_GETW` has already advanced the stream past the 2-byte length field, but the `len` data bytes that follow are never consumed. The function returns `true`, leaving the stream offset short by `len` bytes. This is preserved behaviour from the original code, but if any future caller reads from the stream after this call (or if a protocol extension appends data after the locator name), the stream will be silently misaligned. Adding `stream_forward_getp(s, len)` in the `else if (len > 0)` branch would make the success path semantically consistent and future-proof.
```suggestion
if (locator_name != NULL) {
/* This is a cstring, and it looks like it's normally sent without
* its terminal NULL...
*/
if (len == 0) {
locator_name[0] = 0;
} else {
STREAM_GET(locator_name, s, len);
locator_name[len] = 0;
}
} else if (len > 0) {
stream_forward_getp(s, len);
}
return true;
```
How can I resolve this? If you propose a fix, please make it concise.There was a problem hiding this comment.
yes, that's a valid comment, I will fix it
Don't use a static buffer for the "locator name" in some zapi apis - the one caller can supply a buffer. Signed-off-by: Mark Stapp <mjs@cisco.com>
1faef95 to
c6aeb74
Compare
|
@greptileai review |
|
once ci finishes I will get this in. |
Don't use a static buffer for the "locator name" in some zapi apis - the one caller who uses this can supply a buffer.