From c3aee13b3f174829fb822ecc88d1bed6d89aff29 Mon Sep 17 00:00:00 2001 From: Jianhui Zhao Date: Thu, 9 Sep 2021 01:56:08 +0000 Subject: [PATCH] perf: disconnect early when an invalid message was received Signed-off-by: Jianhui Zhao --- src/rtty.c | 8 ++++++-- src/rtty.h | 19 ++++++++++--------- 2 files changed, 16 insertions(+), 11 deletions(-) diff --git a/src/rtty.c b/src/rtty.c index ac9ed65..23be4c3 100644 --- a/src/rtty.c +++ b/src/rtty.c @@ -313,6 +313,11 @@ static int parse_msg(struct rtty *rtty) return 0; msgtype = buffer_pull_u8(rb); + if (msgtype > MSG_TYPE_MAX) { + log_err("invalid message type: %d\n", msgtype); + return -1; + } + buffer_pull_u16(rb); switch (msgtype) { @@ -360,8 +365,7 @@ static int parse_msg(struct rtty *rtty) break; default: - log_err("invalid message type: %d\n", msgtype); - buffer_pull(rb, NULL, msglen); + /* never to here */ break; } } diff --git a/src/rtty.h b/src/rtty.h index ca4587f..529924e 100644 --- a/src/rtty.h +++ b/src/rtty.h @@ -41,15 +41,16 @@ #define RTTY_HEARTBEAT_INTEVAL 5.0 enum { - MSG_TYPE_REGISTER = 0x00, - MSG_TYPE_LOGIN = 0x01, - MSG_TYPE_LOGOUT = 0x02, - MSG_TYPE_TERMDATA = 0x03, - MSG_TYPE_WINSIZE = 0x04, - MSG_TYPE_CMD = 0x05, - MSG_TYPE_HEARTBEAT = 0x06, - MSG_TYPE_FILE = 0x07, - MSG_TYPE_WEB = 0x08 + MSG_TYPE_REGISTER, + MSG_TYPE_LOGIN, + MSG_TYPE_LOGOUT, + MSG_TYPE_TERMDATA, + MSG_TYPE_WINSIZE, + MSG_TYPE_CMD, + MSG_TYPE_HEARTBEAT, + MSG_TYPE_FILE, + MSG_TYPE_WEB, + MSG_TYPE_MAX = MSG_TYPE_WEB }; struct rtty;