From c5765c20bc38647b7cf0a76429566023db7f0625 Mon Sep 17 00:00:00 2001 From: Alug Date: Mon, 16 Dec 2024 20:34:28 +0100 Subject: [PATCH 1/3] =?UTF-8?q?delay=20NetXCmd=C2=B4s=20instead=20of=20dro?= =?UTF-8?q?pping=20them=20when=20the=20buffer=20is=20full?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit credits and many thanks to Hanicef! https://git.do.srb2.org/STJr/SRB2/-/merge_requests/2568 --- src/d_clisrv.c | 73 +++++++++++++++++++++++++++++++++++++++++--------- 1 file changed, 60 insertions(+), 13 deletions(-) diff --git a/src/d_clisrv.c b/src/d_clisrv.c index 5b307f160..86a5ab2ca 100644 --- a/src/d_clisrv.c +++ b/src/d_clisrv.c @@ -187,6 +187,16 @@ typedef struct textcmdtic_s struct textcmdtic_s *next; } textcmdtic_t; +typedef struct textcmdbuf_s textcmdbuf_t; + +struct textcmdbuf_s +{ + textcmdbuf_t *next; + UINT8 cmd[MAXTEXTCMD]; +}; + +static textcmdbuf_t *textcmdbuf[MAXSPLITSCREENPLAYERS]; + ticcmd_t netcmds[TICQUEUE][MAXPLAYERS]; static textcmdtic_t *textcmds[TEXTCMD_HASH_SIZE] = {NULL}; @@ -271,25 +281,53 @@ void RegisterNetXCmd(netxcmd_t id, void (*cmd_f)(UINT8 **p, INT32 playernum)) listnetxcmd[id] = cmd_f; } -void SendNetXCmdForPlayer(UINT8 playerid, netxcmd_t id, const void *param, size_t nparam) +static void WriteNetXCmd(UINT8 *cmd, netxcmd_t id, const void *param, size_t nparam) { - if ((localtextcmd[playerid])[0]+2+nparam > MAXTEXTCMD) - { - // for future reference: if (cht_debug) != debug disabled. - CONS_Alert(CONS_ERROR, M_GetText("NetXCmd buffer full, cannot add netcmd %d! (size: %d, needed: %s)\n"), id, localtextcmd[playerid][0], sizeu1(nparam)); - return; - } - - localtextcmd[playerid][0]++; - localtextcmd[playerid][localtextcmd[playerid][0]] = (UINT8)id; - + cmd[0]++; + cmd[cmd[0]] = (UINT8)id; if (param && nparam) { - M_Memcpy(&localtextcmd[playerid][localtextcmd[playerid][0] + 1], param, nparam); - localtextcmd[playerid][0] = (UINT8)(localtextcmd[playerid][0] + (UINT8)nparam); + M_Memcpy(&cmd[cmd[0]+1], param, nparam); + cmd[0] = (UINT8)(cmd[0] + (UINT8)nparam); } } +void SendNetXCmdForPlayer(UINT8 playerid, netxcmd_t id, const void *param, size_t nparam) +{ + if (localtextcmd[playerid][0]+2+nparam > MAXTEXTCMD) + { + textcmdbuf_t *buf = textcmdbuf[playerid]; + + // for future reference: if (cv_debug) != debug disabled. + CONS_Alert(CONS_NOTICE, M_GetText("NetXCmd buffer full, delaying netcmd %d... (size: %d, needed: %s)\n"), id, localtextcmd[playerid][0], sizeu1(nparam)); + if (buf == NULL) + { + textcmdbuf[playerid] = Z_Malloc(sizeof(textcmdbuf_t), PU_STATIC, NULL); + textcmdbuf[playerid]->cmd[0] = 0; + textcmdbuf[playerid]->next = NULL; + WriteNetXCmd(textcmdbuf[playerid]->cmd, id, param, nparam); + return; + } + + while (buf->next != NULL) + buf = buf->next; + + if (buf->cmd[0]+2+nparam > MAXTEXTCMD) + { + WriteNetXCmd(buf->cmd, id, param, nparam); + } + else + { + buf->next = Z_Malloc(sizeof(textcmdbuf_t), PU_STATIC, NULL); + buf->next->cmd[0] = 0; + buf->next->next = NULL; + WriteNetXCmd(buf->next->cmd, id, param, nparam); + } + return; + } + WriteNetXCmd(localtextcmd[playerid], id, param, nparam); +} + /*UINT8 GetFreeXCmdSize(UINT8 playerid) { // -1 for the size and another -1 for the ID. @@ -5937,7 +5975,16 @@ static void CL_SendClientCmd(void) M_Memcpy(netbuffer->u.textcmd, localtextcmd[i], localtextcmd[i][0]+1); // All extra data have been sent if (HSendPacket(servernode, true, 0, localtextcmd[i][0]+1)) // Send can fail... + { localtextcmd[i][0] = 0; + if (textcmdbuf[i] != NULL) + { + textcmdbuf_t *buf = textcmdbuf[i]; + M_Memcpy(localtextcmd[i], textcmdbuf[i]->cmd, textcmdbuf[i]->cmd[0]+1); + textcmdbuf[i] = textcmdbuf[i]->next; + Z_Free(buf); + } + } } } } From 13579785968104d7b092da4c0cbeb31fa6a2cb16 Mon Sep 17 00:00:00 2001 From: Alug Date: Mon, 16 Dec 2024 20:40:36 +0100 Subject: [PATCH 2/3] init textcmdbuf to NULL just in case --- src/d_clisrv.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/d_clisrv.c b/src/d_clisrv.c index 86a5ab2ca..22139ad02 100644 --- a/src/d_clisrv.c +++ b/src/d_clisrv.c @@ -195,7 +195,7 @@ struct textcmdbuf_s UINT8 cmd[MAXTEXTCMD]; }; -static textcmdbuf_t *textcmdbuf[MAXSPLITSCREENPLAYERS]; +static textcmdbuf_t *textcmdbuf[MAXSPLITSCREENPLAYERS] = {NULL}; ticcmd_t netcmds[TICQUEUE][MAXPLAYERS]; static textcmdtic_t *textcmds[TEXTCMD_HASH_SIZE] = {NULL}; From 3174d8fb1c0480abac3bb1c790391d22e81aa81f Mon Sep 17 00:00:00 2001 From: Alug Date: Wed, 18 Dec 2024 18:57:02 +0100 Subject: [PATCH 3/3] Fix segfault when sending excessively large netcmds https://git.do.srb2.org/STJr/SRB2/-/merge_requests/2568/diffs?commit_id=457b761d9301b1904c414a8e8ba908f74b9ab959 --- src/d_clisrv.c | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/src/d_clisrv.c b/src/d_clisrv.c index 22139ad02..50e4a9183 100644 --- a/src/d_clisrv.c +++ b/src/d_clisrv.c @@ -298,6 +298,12 @@ void SendNetXCmdForPlayer(UINT8 playerid, netxcmd_t id, const void *param, size_ { textcmdbuf_t *buf = textcmdbuf[playerid]; + if (2+nparam > MAXTEXTCMD) + { + CONS_Alert(CONS_ERROR, M_GetText("packet too large to fit NetXCmd, cannot add netcmd %d! (size: %s, max: %d)\n"), id, sizeu1(2+nparam), MAXTEXTCMD); + return; + } + // for future reference: if (cv_debug) != debug disabled. CONS_Alert(CONS_NOTICE, M_GetText("NetXCmd buffer full, delaying netcmd %d... (size: %d, needed: %s)\n"), id, localtextcmd[playerid][0], sizeu1(nparam)); if (buf == NULL) @@ -313,16 +319,16 @@ void SendNetXCmdForPlayer(UINT8 playerid, netxcmd_t id, const void *param, size_ buf = buf->next; if (buf->cmd[0]+2+nparam > MAXTEXTCMD) - { - WriteNetXCmd(buf->cmd, id, param, nparam); - } - else { buf->next = Z_Malloc(sizeof(textcmdbuf_t), PU_STATIC, NULL); buf->next->cmd[0] = 0; buf->next->next = NULL; WriteNetXCmd(buf->next->cmd, id, param, nparam); } + else + { + WriteNetXCmd(buf->cmd, id, param, nparam); + } return; } WriteNetXCmd(localtextcmd[playerid], id, param, nparam);