From 20d4e3e719399ef57d8836f4a5af8dfa7bc296d5 Mon Sep 17 00:00:00 2001 From: Alibek Omarov Date: Wed, 22 Jan 2025 05:37:47 +0300 Subject: [PATCH] engine: fix flexible array member usage in case of null terminated strings (woops) --- engine/common/base_cmd.c | 4 ++-- engine/common/cmd.c | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/engine/common/base_cmd.c b/engine/common/base_cmd.c index 312689f4..9a7914f3 100644 --- a/engine/common/base_cmd.c +++ b/engine/common/base_cmd.c @@ -26,7 +26,7 @@ struct base_command_hashmap_s base_command_t *basecmd; // base command: cvar, alias or command base_command_hashmap_t *next; base_command_type_e type; // type for faster searching - char name[1]; // key for searching + char name[]; // key for searching }; static base_command_hashmap_t *hashed_cmds[HASH_SIZE]; @@ -129,7 +129,7 @@ void BaseCmd_Insert( base_command_type_e type, base_command_t *basecmd, const ch uint hash = BaseCmd_HashKey( name ); size_t len = Q_strlen( name ); - elem = Mem_Malloc( basecmd_pool, sizeof( base_command_hashmap_t ) + len ); + elem = Mem_Malloc( basecmd_pool, sizeof( base_command_hashmap_t ) + len + 1 ); elem->basecmd = basecmd; elem->type = type; Q_strncpy( elem->name, name, len + 1 ); diff --git a/engine/common/cmd.c b/engine/common/cmd.c index 8f2c4245..1b7f0bb7 100644 --- a/engine/common/cmd.c +++ b/engine/common/cmd.c @@ -709,7 +709,7 @@ int Cmd_AddCommandEx( const char *cmd_name, xcommand_t function, const char *cmd // unfortunately, we lose original command this way if( FBitSet( cmd->flags, CMD_OVERRIDABLE )) { - desc_len = Q_strlen( cmd->desc ); + desc_len = Q_strlen( cmd->desc ) + 1; Q_strncpy( cmd->desc, cmd_desc, desc_len ); cmd->function = function; cmd->flags = iFlags; @@ -725,7 +725,7 @@ int Cmd_AddCommandEx( const char *cmd_name, xcommand_t function, const char *cmd } // use a small malloc to avoid zone fragmentation - desc_len = Q_strlen( cmd_desc ); + desc_len = Q_strlen( cmd_desc ) + 1; cmd = Mem_Malloc( cmd_pool, sizeof( cmd_t ) + desc_len ); cmd->name = copystringpool( cmd_pool, cmd_name ); Q_strncpy( cmd->desc, cmd_desc, desc_len );