From 742232d5759ecbe193d725ff67c75c7e8689f05b Mon Sep 17 00:00:00 2001 From: Lemongrass3110 Date: Tue, 18 Dec 2018 02:32:37 +0100 Subject: [PATCH] Fixed zero termination for bg messages (#3759) Fixes #3104 Thanks to @Rolfxx and @vstumpf --- src/map/clif.cpp | 30 +++++++++++++++--------------- 1 file changed, 15 insertions(+), 15 deletions(-) diff --git a/src/map/clif.cpp b/src/map/clif.cpp index da84ae84b3..a572449c00 100644 --- a/src/map/clif.cpp +++ b/src/map/clif.cpp @@ -17285,32 +17285,32 @@ void clif_bg_xy_remove(struct map_session_data *sd) clif_send(buf, packet_len(0x2df), &sd->bl, BG_SAMEMAP_WOS); } +/// Notifies clients of a battleground message. +/// 02DC .W .L .24B .?B (ZC_BATTLEFIELD_CHAT) +void clif_bg_message( struct battleground_data *bg, int src_id, const char *name, const char *mes, int len ){ + struct map_session_data *sd = bg_getavailablesd( bg ); -/// Notifies clients of a battleground message (ZC_BATTLEFIELD_CHAT). -/// 02dc .W .L .24B .?B -void clif_bg_message(struct battleground_data *bg, int src_id, const char *name, const char *mes, int len) -{ - struct map_session_data *sd; - unsigned char *buf; - if( (sd = bg_getavailablesd(bg)) == NULL ) + if( sd == nullptr ){ return; + } - buf = (unsigned char*)aMalloc((len + NAME_LENGTH + 8)*sizeof(unsigned char)); + // limit length + len = min( len + 1, CHAT_SIZE_MAX ); + + unsigned char buf[8 + NAME_LENGTH + CHAT_SIZE_MAX]; WBUFW(buf,0) = 0x2dc; WBUFW(buf,2) = len + NAME_LENGTH + 8; WBUFL(buf,4) = src_id; safestrncpy(WBUFCP(buf,8), name, NAME_LENGTH); - memcpy(WBUFP(buf,32), mes, len); - clif_send(buf,WBUFW(buf,2), &sd->bl, BG); + safestrncpy(WBUFCP(buf,8+NAME_LENGTH), mes, len ); - if( buf ) - aFree(buf); + clif_send(buf,WBUFW(buf,2), &sd->bl, BG); } - -/// Validates and processes battlechat messages [pakpil] (CZ_BATTLEFIELD_CHAT). -/// 0x2db .W .?B ( : ) 00 +/// Validates and processes battlechat messages. +/// All messages that are sent after enabling battleground chat with /battlechat. +/// 02DB .W .?B (CZ_BATTLEFIELD_CHAT) void clif_parse_BattleChat(int fd, struct map_session_data* sd){ char name[NAME_LENGTH], message[CHAT_SIZE_MAX], output[CHAT_SIZE_MAX+NAME_LENGTH*2];