Fix CreateProcessW to throw an error when input parameters are too long
Create process to throw an error in cases of too long:
- command path
- command line
- dir name
If the CreateProcessW fails the error is thrown too.
--- a/Win32OperatingSystem.st Mon Jun 21 15:00:09 2021 +0100
+++ b/Win32OperatingSystem.st Mon Oct 18 10:03:14 2021 +0200
@@ -4029,9 +4029,11 @@
- same as nil
"
- |handle commandPathUni16 commandLineUni16 dirNameUni16 envString16|
+ |handle errorMessage errorNumber commandPathUni16 commandLineUni16 dirNameUni16 envString16|
handle := Win32ProcessHandle new.
+ errorMessage := nil.
+ errorNumber := nil.
commandPathUni16 := commandPath.
commandLineUni16 := commandLine.
@@ -4114,15 +4116,13 @@
if (commandPathUni16 != nil) {
l = __unicode16StringSize(commandPathUni16);
if (l >= 4096) { // >= need 1 space for terminator
-# ifdef PROCESSDEBUGWIN32
- if (flag_PROCESSDEBUGWIN32) {
- console_fprintf(stderr, "argument #commandPathUni16 is to long\n");
- }
-# endif
- RETURN(nil);
+ errorMessage = __MKSTRING("commandPathUni16 exceeded size limit (4096 characters)");
+ errorNumber = __mkSmallInteger(__WIN32_ERR(ERROR_INVALID_PARAMETER));
+ goto err;
+ //RETURN(nil);
}
for (i = 0; i < l; i++) {
- cmdPathW[i] = __unicode16StringVal(commandPathUni16)[i];
+ cmdPathW[i] = __unicode16StringVal(commandPathUni16)[i];
}
cmdPathW[i] = 0; // set terminator
cmdPathWP = &cmdPathW[0];
@@ -4131,12 +4131,10 @@
// commandLineUni16
l = __unicode16StringSize(commandLineUni16);
if (l >= 4096) { // >= need 1 space for terminator
-# ifdef PROCESSDEBUGWIN32
- if (flag_PROCESSDEBUGWIN32) {
- console_fprintf(stderr, "argument #commandLineUni16 is to long\n");
- }
-# endif
- RETURN(nil);
+ errorMessage = __MKSTRING("commandLineUni16 exceeded size limit (4096 characters)");
+ errorNumber = __mkSmallInteger(__WIN32_ERR(ERROR_INVALID_PARAMETER));
+ goto err;
+ //RETURN(nil);
}
for (i = 0; i < l; i++) {
cmdLineW[i] = __unicode16StringVal(commandLineUni16)[i];
@@ -4148,12 +4146,10 @@
if (__isUnicode16String(dirNameUni16)) {
l = __unicode16StringSize(dirNameUni16);
if (l >= 4096) { // >= need 1 space for terminator
-# ifdef PROCESSDEBUGWIN32
- if (flag_PROCESSDEBUGWIN32) {
- console_fprintf(stderr, "argument #dirNameUni16 is to long\n");
- }
-# endif
- RETURN(nil);
+ errorMessage = __MKSTRING("dirNameUni16 exceeded size limit (4096 characters)");
+ errorNumber = __mkSmallInteger(__WIN32_ERR(ERROR_INVALID_PARAMETER));
+ goto err;
+ //RETURN(nil);
}
for (i = 0; i < l; i++) {
dirNameW[i] = __unicode16StringVal(dirNameUni16)[i];
@@ -4385,25 +4381,27 @@
((struct __Win32OperatingSystem__Win32ProcessHandle_struct *)(handle))->pid = __mkSmallInteger(lppiProcInfo.dwProcessId);
RETURN (handle);
}
-#ifdef PROCESSDEBUGWIN32
- if (flag_PROCESSDEBUGWIN32) {
- console_fprintf(stderr, "created process error %d\n", GetLastError());
- }
-#endif
- RETURN (nil);
+ errorMessage = __MKSTRING("CreateProcessW() has failed");
+ errorNumber = __mkSmallInteger(__WIN32_ERR(GetLastError()));
+ goto err;
} else {
; /* should never be called that way */
}
}
-%}.
- "
- path-argument not string
- or argArray not an array/nil
- or malloc failed
- or not supported by OS
- "
- ^ self primitiveFailed
-
+ err:;
+%}.
+ errorNumber isNil ifTrue:[
+ "
+ path-argument not string
+ or argArray not an array/nil
+ or malloc failed
+ or not supported by OS
+ "
+ ^ self primitiveFailed:'create process failed'
+ ] ifFalse: [
+ (OperatingSystem errorHolderForNumber:errorNumber) reportError: errorMessage.
+ ^ nil
+ ]
"Created: / 15-11-2016 / 19:39:49 / cg"
!