Fix bounds and unsigned decoding in ETCP packet dumps
This commit is contained in:
1 parent
ac45554439
commit
d7fb0c83f9
2 files changed
+86
-8
No files matched your search
@@ -12,8 +12,9 @@ IP_STR ip_to_string(uint32_t ip) {
|
||||
return ip_str;
|
||||
}
|
||||
|
||||
// Строит дамп только при включённой категории; длинные цепочки секций безопасно усекаются.
|
||||
void etcp_dump_pkt_sections(struct ETCP_DGRAM* pkt, struct ETCP_LINK* link, int is_send) {
|
||||
if (!pkt) return;
|
||||
if (!pkt || !debug_should_output(DEBUG_LEVEL_DEBUG, DEBUG_CATEGORY_DUMP)) return;
|
||||
|
||||
char buf[256];
|
||||
int pos = 0;
|
||||
@@ -34,8 +35,7 @@ void etcp_dump_pkt_sections(struct ETCP_DGRAM* pkt, struct ETCP_LINK* link, int
|
||||
int has_payload = 0;
|
||||
|
||||
uint16_t offset = 0;
|
||||
while (offset < len) {
|
||||
if (offset >= len) break;
|
||||
while (offset < len && pos < (int)sizeof(buf) - 1) {
|
||||
uint8_t type = data[offset];
|
||||
|
||||
switch (type) {
|
||||
@@ -43,8 +43,8 @@ void etcp_dump_pkt_sections(struct ETCP_DGRAM* pkt, struct ETCP_LINK* link, int
|
||||
if (offset + 2 > len) { offset = len; break; }
|
||||
ack_count = data[offset + 1];
|
||||
if (offset + 8 > len) { offset = len; break; }
|
||||
ack_last_delivered = data[offset + 2] | (data[offset + 3] << 8) |
|
||||
(data[offset + 4] << 16) | (data[offset + 5] << 24);
|
||||
ack_last_delivered = (uint32_t)data[offset + 2] | ((uint32_t)data[offset + 3] << 8) |
|
||||
((uint32_t)data[offset + 4] << 16) | ((uint32_t)data[offset + 5] << 24);
|
||||
uint16_t ack_rx_dup = data[offset + 6] | (data[offset + 7] << 8);
|
||||
pos += snprintf(buf + pos, sizeof(buf) - pos, " ack=%u/%d dup=%u", ack_last_delivered, ack_count, ack_rx_dup);
|
||||
offset += 8 + ack_count * 8;
|
||||
@@ -61,8 +61,8 @@ void etcp_dump_pkt_sections(struct ETCP_DGRAM* pkt, struct ETCP_LINK* link, int
|
||||
}
|
||||
case ETCP_SECTION_PAYLOAD: {
|
||||
if (offset + 5 > len) { offset = len; break; }
|
||||
payload_seq = data[offset + 1] | (data[offset + 2] << 8) |
|
||||
(data[offset + 3] << 16) | (data[offset + 4] << 24);
|
||||
payload_seq = (uint32_t)data[offset + 1] | ((uint32_t)data[offset + 2] << 8) |
|
||||
((uint32_t)data[offset + 3] << 16) | ((uint32_t)data[offset + 4] << 24);
|
||||
payload_size = len - offset - 5;
|
||||
has_payload = 1;
|
||||
pos += snprintf(buf + pos, sizeof(buf) - pos, " payload=%u(seq=%u)", payload_size, payload_seq);
|
||||
@@ -75,5 +75,11 @@ void etcp_dump_pkt_sections(struct ETCP_DGRAM* pkt, struct ETCP_LINK* link, int
|
||||
}
|
||||
}
|
||||
|
||||
if (offset < len || pos >= (int)sizeof(buf)) {
|
||||
const char marker[] = " [truncated]";
|
||||
memcpy(buf + sizeof(buf) - sizeof(marker), marker, sizeof(marker));
|
||||
DEBUG_WARN(DEBUG_CATEGORY_DUMP, "ETCP packet dump truncated: dir=%s data_len=%u offset=%u buffer=%zu",
|
||||
dir, len, offset, sizeof(buf));
|
||||
}
|
||||
DEBUG_DEBUG(DEBUG_CATEGORY_DUMP, "%s", buf);
|
||||
}
|
||||
Reference in new issue
Block a user