@@ -147,28 +147,11 @@ int TerminationCheck(int status, const Descriptor *cmdstat,
147
147
return exitStatusVal;
148
148
}
149
149
150
- const char *ensureNullTerminated (
151
- const char *str, size_t length, Terminator &terminator) {
152
- if (length <= strlen (str)) {
153
- char *newCmd{(char *)malloc (length + 1 )};
154
- if (newCmd == NULL ) {
155
- terminator.Crash (" Command not null-terminated, memory allocation failed "
156
- " for null-terminated newCmd." );
157
- }
158
-
159
- strncpy (newCmd, str, length);
160
- newCmd[length] = ' \0 ' ;
161
- return newCmd;
162
- } else {
163
- return str;
164
- }
165
- }
166
-
167
150
void RTNAME (ExecuteCommandLine)(const Descriptor &command, bool wait,
168
151
const Descriptor *exitstat, const Descriptor *cmdstat,
169
152
const Descriptor *cmdmsg, const char *sourceFile, int line) {
170
153
Terminator terminator{sourceFile, line};
171
- const char *newCmd{ensureNullTerminated (
154
+ const char *newCmd{EnsureNullTerminated (
172
155
command.OffsetElement (), command.ElementBytes (), terminator)};
173
156
174
157
if (exitstat) {
@@ -202,22 +185,18 @@ void RTNAME(ExecuteCommandLine)(const Descriptor &command, bool wait,
202
185
203
186
// append "cmd.exe /c " to the beginning of command
204
187
const char *prefix{" cmd.exe /c " };
205
- char *newCmdWin{
206
- (char *)malloc (std::strlen (prefix) + std::strlen (newCmd) + 1 )};
207
- if (newCmd != NULL ) {
208
- std::strcpy (newCmdWin, prefix);
209
- std::strcat (newCmdWin, newCmd);
210
- } else {
211
- terminator.Crash (" Memory allocation failed for newCmd" );
212
- }
188
+ char *newCmdWin{(char *)AllocateMemoryOrCrash (
189
+ terminator, std::strlen (prefix) + std::strlen (newCmd) + 1 )};
190
+ std::strcpy (newCmdWin, prefix);
191
+ std::strcat (newCmdWin, newCmd);
213
192
214
193
// Convert the char to wide char
215
194
const size_t sizeNeeded{mbstowcs (NULL , newCmdWin, 0 ) + 1 };
216
195
wchar_t *wcmd{new wchar_t [sizeNeeded]};
217
196
if (std::mbstowcs (wcmd, newCmdWin, sizeNeeded) == static_cast <size_t >(-1 )) {
218
197
terminator.Crash (" Char to wide char failed for newCmd" );
219
198
}
220
- free ( newCmdWin);
199
+ FreeMemory (( void *) newCmdWin);
221
200
222
201
if (CreateProcess (nullptr , wcmd, nullptr , nullptr , FALSE , 0 , nullptr ,
223
202
nullptr , &si, &pi)) {
@@ -233,7 +212,7 @@ void RTNAME(ExecuteCommandLine)(const Descriptor &command, bool wait,
233
212
CheckAndCopyToDescriptor (cmdmsg, " CreateProcess failed." );
234
213
}
235
214
}
236
- delete[] wcmd;
215
+ FreeMemory (( void *) wcmd) ;
237
216
#else
238
217
// terminated children do not become zombies
239
218
signal (SIGCHLD, SIG_IGN);
@@ -251,6 +230,7 @@ void RTNAME(ExecuteCommandLine)(const Descriptor &command, bool wait,
251
230
exit (status);
252
231
}
253
232
#endif
233
+ FreeMemory ((void *)newCmd);
254
234
}
255
235
}
256
236
0 commit comments