fix PKT_NIO memory bloat when receiving unexpected packet

This commit is contained in:
liucc1997
2023-07-11 12:48:31 +00:00
committed by ob-robot
parent 2b30190d8e
commit 3a44c6db38
2 changed files with 13 additions and 4 deletions

View File

@ -14,9 +14,10 @@ static uint32_t calc_crc(const void* b, size_t len)
}
#endif
#define EASY_HEADER_MAGIC 0xcedbdb01
inline void eh_set(easy_head_t* h, uint32_t len, uint32_t pkt_id)
{
h->magic_ = 0xcedbdb01;
h->magic_ = EASY_HEADER_MAGIC;
h->len_ = rk_bswap32(len);
h->pkt_id_ = rk_bswap32(pkt_id);
h->reserved_ = 0;
@ -32,8 +33,16 @@ static int64_t eh_decode(char* b, int64_t s)
int64_t bytes = sizeof(easy_head_t);
if (s >= bytes) {
easy_head_t* h = (typeof(h))b;
bytes += rk_bswap32(h->len_);
PNIO_CRC(assert(s < bytes || h->reserved_ == calc_crc(b + sizeof(easy_head_t), bytes - sizeof(easy_head_t))));
uint32_t len = rk_bswap32(h->len_);
const uint32_t max_size = 64 * 1024 * 1024; // max packet size, 64MB
if (h->magic_ != EASY_HEADER_MAGIC || len > max_size) {
int err = PNIO_ERROR;
bytes = -1;
rk_warn("unexpected packet, magic=%x, len=%x,pkt_id=%x, reserved=%x", h->magic_, h->len_, h->pkt_id_, h->reserved_);
} else {
bytes += len;
PNIO_CRC(assert(s < bytes || h->reserved_ == calc_crc(b + sizeof(easy_head_t), bytes - sizeof(easy_head_t))));
}
}
return bytes;
}

View File

@ -50,7 +50,7 @@ int my_sk_consume(my_sk_t* s, int64_t time_limit, int64_t* avail_bytes) {
while(0 == err && !is_epoll_handle_timeout(time_limit)) {
if (0 != (err = my_sk_do_decode(s, &msg, avail_bytes))) {
if (EAGAIN != err) {
rk_info("do_decode fail: %d", err);
rk_warn("do_decode fail: %d", err);
}
} else if (NULL == msg.payload) {
// not read a complete package yet