From 3a44c6db3895413a01ab3d1a17125419d8e9448c Mon Sep 17 00:00:00 2001 From: liucc1997 <1192520566@qq.com> Date: Tue, 11 Jul 2023 12:48:31 +0000 Subject: [PATCH] fix PKT_NIO memory bloat when receiving unexpected packet --- deps/oblib/src/rpc/pnio/nio/easy_head.h | 15 ++++++++++++--- deps/oblib/src/rpc/pnio/nio/handle_io.t.h | 2 +- 2 files changed, 13 insertions(+), 4 deletions(-) diff --git a/deps/oblib/src/rpc/pnio/nio/easy_head.h b/deps/oblib/src/rpc/pnio/nio/easy_head.h index e495b68ee4..38f990ab27 100644 --- a/deps/oblib/src/rpc/pnio/nio/easy_head.h +++ b/deps/oblib/src/rpc/pnio/nio/easy_head.h @@ -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; } diff --git a/deps/oblib/src/rpc/pnio/nio/handle_io.t.h b/deps/oblib/src/rpc/pnio/nio/handle_io.t.h index a31743f976..287054ee1f 100644 --- a/deps/oblib/src/rpc/pnio/nio/handle_io.t.h +++ b/deps/oblib/src/rpc/pnio/nio/handle_io.t.h @@ -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