@@ -84,11 +84,11 @@ bool CCommands::Execute(const char* szCommandLine)
8484bool CCommands::Execute (const char * szCommand, const char * szParametersIn, bool bHandleRemotely, bool bIsScriptedBind)
8585{
8686 // Copy szParametersIn so the contents can be changed
87- char * szParameters = nullptr ;
87+ std::unique_ptr< char []> szParameters = nullptr ;
8888 if (szParametersIn)
8989 {
90- szParameters = new char [strlen (szParametersIn) + 1 ] ;
91- std::strcpy (szParameters, szParametersIn);
90+ szParameters = std::make_unique< char []>( strlen (szParametersIn) + 1 ) ;
91+ std::strcpy (szParameters. get () , szParametersIn);
9292 }
9393
9494 // HACK: if its a 'chatboxsay' command, use the next parameter
@@ -98,21 +98,29 @@ bool CCommands::Execute(const char* szCommand, const char* szParametersIn, bool
9898 if (szParameters)
9999 {
100100 // His line starts with '/'?
101- if (* szParameters == ' /' )
101+ if (szParameters[ 0 ] == ' /' )
102102 {
103103 // Copy the characters after the slash to the 0 terminator to a seperate buffer
104104 char szBuffer[256 ];
105- strncpy (szBuffer, szParameters + 1 , 256 );
105+ strncpy (szBuffer, szParameters. get () + 1 , 256 );
106106 szBuffer[255 ] = 0 ;
107107
108108 // Split it into command and arguments
109109 szCommand = strtok (szBuffer, " " );
110- szParameters = strtok (nullptr , " \0 " );
111110 if (!szCommand)
112111 return false ;
113112
114- if (!szParameters)
115- std::strcpy (szParameters, " " );
113+ if (char * szNewParameters = strtok (nullptr , " \0 " ); szNewParameters)
114+ {
115+ szParameters = std::make_unique<char []>(strlen (szNewParameters) + 1 );
116+ std::strcpy (szParameters.get (), szNewParameters);
117+ }
118+ else
119+ {
120+ // Åñëè íåò ïàðàìåòðîâ, ñîçäàåì ïóñòóþ ñòðîêó
121+ szParameters = std::make_unique<char []>(1 );
122+ szParameters[0 ] = ' \0 ' ;
123+ }
116124 }
117125 }
118126 else
@@ -128,12 +136,12 @@ bool CCommands::Execute(const char* szCommand, const char* szParametersIn, bool
128136 {
129137 // Execute it
130138 if (!bIsScriptedBind || pEntry->bAllowScriptedBind )
131- ExecuteHandler (pEntry->pfnCmdFunc , szParameters);
139+ ExecuteHandler (pEntry->pfnCmdFunc , szParameters. get () );
132140 }
133141 }
134142
135143 // Recompose the original command text
136- std::string val = std::string (szCommand) + " " + std::string (szParameters ? szParameters : " " );
144+ std::string val = std::string (szCommand) + " " + std::string (szParameters ? szParameters. get () : " " );
137145
138146 // Is it a cvar? (syntax: cvar[ = value])
139147 {
@@ -177,10 +185,6 @@ bool CCommands::Execute(const char* szCommand, const char* szParametersIn, bool
177185 ss << key << " = " << val;
178186 val = ss.str ();
179187 CCore::GetSingleton ().GetConsole ()->Print (val.c_str ());
180-
181- if (szParameters)
182- delete[] szParameters;
183-
184188 return true ;
185189 }
186190 }
@@ -189,13 +193,13 @@ bool CCommands::Execute(const char* szCommand, const char* szParametersIn, bool
189193 bool bIsNickCommand = !stricmp (szCommand, " nick" );
190194 if (bIsNickCommand && szParameters && !bIsScriptedBind)
191195 {
192- if (CCore::GetSingleton ().IsValidNick (szParameters))
196+ if (CCore::GetSingleton ().IsValidNick (szParameters. get () ))
193197 {
194- CVARS_SET (" nick" , std::string (szParameters));
198+ CVARS_SET (" nick" , std::string (szParameters. get () ));
195199
196200 if (!CCore::GetSingleton ().IsConnected ())
197201 {
198- CCore::GetSingleton ().GetConsole ()->Printf (" nick: You are now known as %s" , szParameters);
202+ CCore::GetSingleton ().GetConsole ()->Printf (" nick: You are now known as %s" , szParameters. get () );
199203 }
200204 }
201205 else if (!CCore::GetSingleton ().IsConnected ())
@@ -208,13 +212,8 @@ bool CCommands::Execute(const char* szCommand, const char* szParametersIn, bool
208212 if (m_pfnExecuteHandler)
209213 {
210214 bool bAllowScriptedBind = (!pEntry || pEntry->bAllowScriptedBind );
211- if (m_pfnExecuteHandler (szCommand, szParameters, bHandleRemotely, (pEntry != NULL ), bIsScriptedBind, bAllowScriptedBind))
212- {
213- if (szParameters)
214- delete[] szParameters;
215-
215+ if (m_pfnExecuteHandler (szCommand, szParameters.get (), bHandleRemotely, (pEntry != NULL ), bIsScriptedBind, bAllowScriptedBind))
216216 return true ;
217- }
218217 }
219218
220219 // Unknown command
0 commit comments