fix PKT_NIO memory bloat when receiving unexpected packet
This commit is contained in:
15
deps/oblib/src/rpc/pnio/nio/easy_head.h
vendored
15
deps/oblib/src/rpc/pnio/nio/easy_head.h
vendored
@ -14,9 +14,10 @@ static uint32_t calc_crc(const void* b, size_t len)
|
|||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
#define EASY_HEADER_MAGIC 0xcedbdb01
|
||||||
inline void eh_set(easy_head_t* h, uint32_t len, uint32_t pkt_id)
|
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->len_ = rk_bswap32(len);
|
||||||
h->pkt_id_ = rk_bswap32(pkt_id);
|
h->pkt_id_ = rk_bswap32(pkt_id);
|
||||||
h->reserved_ = 0;
|
h->reserved_ = 0;
|
||||||
@ -32,8 +33,16 @@ static int64_t eh_decode(char* b, int64_t s)
|
|||||||
int64_t bytes = sizeof(easy_head_t);
|
int64_t bytes = sizeof(easy_head_t);
|
||||||
if (s >= bytes) {
|
if (s >= bytes) {
|
||||||
easy_head_t* h = (typeof(h))b;
|
easy_head_t* h = (typeof(h))b;
|
||||||
bytes += rk_bswap32(h->len_);
|
uint32_t len = rk_bswap32(h->len_);
|
||||||
PNIO_CRC(assert(s < bytes || h->reserved_ == calc_crc(b + sizeof(easy_head_t), bytes - sizeof(easy_head_t))));
|
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;
|
return bytes;
|
||||||
}
|
}
|
||||||
|
|||||||
2
deps/oblib/src/rpc/pnio/nio/handle_io.t.h
vendored
2
deps/oblib/src/rpc/pnio/nio/handle_io.t.h
vendored
@ -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)) {
|
while(0 == err && !is_epoll_handle_timeout(time_limit)) {
|
||||||
if (0 != (err = my_sk_do_decode(s, &msg, avail_bytes))) {
|
if (0 != (err = my_sk_do_decode(s, &msg, avail_bytes))) {
|
||||||
if (EAGAIN != err) {
|
if (EAGAIN != err) {
|
||||||
rk_info("do_decode fail: %d", err);
|
rk_warn("do_decode fail: %d", err);
|
||||||
}
|
}
|
||||||
} else if (NULL == msg.payload) {
|
} else if (NULL == msg.payload) {
|
||||||
// not read a complete package yet
|
// not read a complete package yet
|
||||||
|
|||||||
Reference in New Issue
Block a user