Skip to content

Commit

Permalink
E25571: Island RTL: AV with "Process" class
Browse files Browse the repository at this point in the history
Also, this issue surfaced E25575: Island-x64: array[..] of X are not passed correctly as parameter
  • Loading branch information
degoo committed Dec 15, 2021
1 parent a4328e8 commit 1ef61ba
Showing 1 changed file with 13 additions and 6 deletions.
19 changes: 13 additions & 6 deletions Source/Process.pas
Expand Up @@ -32,6 +32,7 @@ interface
fErrPipe: array[0..1] of Int32;
method StartAsync(aStdOutCallback: block(aLine: String); aErrorCallback: block(aLine: String); aFinishedCallback: block(aExitCode: Integer));
method WaitForAsync;
method CheckHandle(aHandle: Integer; aMessage: String);
{$ENDIF}
{$IF WINDOWS OR (POSIX AND NOT IOS)}
fLastIncompleteOutputLog: String := '';
Expand Down Expand Up @@ -234,7 +235,7 @@ constructor Process(aCommand: String; aArguments: ImmutableList<String>; aEnviro
var lBuffer := new AnsiChar[1024];
while(true) do begin
var lCount := rtl.read(fOutPipe[0], @lBuffer[0], sizeOf(lBuffer));
if lCount = 0 then
if lCount <= 0 then
break;
if lCount > 0 then
fOutput := fOutput + String.FromPAnsiChars(@lBuffer[0], lCount);
Expand Down Expand Up @@ -280,7 +281,7 @@ constructor Process(aCommand: String; aArguments: ImmutableList<String>; aEnviro
var lBuffer := new AnsiChar[1024];
while(true) do begin
var lCount := rtl.read(fErrPipe[0], @lBuffer[0], sizeOf(lBuffer));
if lCount = 0 then
if lCount <= 0 then
break;
if lCount > 0 then
fErr := fErr + String.FromPAnsiChars(@lBuffer[0], lCount);
Expand Down Expand Up @@ -404,18 +405,18 @@ constructor Process(aCommand: String; aArguments: ImmutableList<String>; aEnviro
lEnvp[i] := @(lItem.Key + '=' + lItem.Value).ToAnsiChars(true)[0];
inc(i);
end;
lEnvp[Arguments.Count] := nil;
lEnvp[Environment.Count] := nil;

fProcessId := rtl.fork();
if fProcessId = 0 then begin
if RedirectInput then begin
rtl.dup2(fOutPipe[0], rtl.STDIN_FILENO);
CheckHandle(rtl.dup2(fOutPipe[0], rtl.STDIN_FILENO), 'input');
rtl.close(fOutPipe[1]);
end;
if RedirectOutput then begin
rtl.dup2(fOutPipe[1], rtl.STDOUT_FILENO);
CheckHandle(rtl.dup2(fOutPipe[1], rtl.STDOUT_FILENO), 'output');
rtl.close(fOutPipe[0]);
rtl.dup2(fErrPipe[1], rtl.STDERR_FILENO);
CheckHandle(rtl.dup2(fErrPipe[1], rtl.STDERR_FILENO), 'output');
rtl.close(fErrPipe[0]);
end;

Expand Down Expand Up @@ -553,6 +554,12 @@ constructor Process(aCommand: String; aArguments: ImmutableList<String>; aEnviro
var lStatus: Int32;
rtl.waitpid(fProcessId, @lStatus, 0);
end;

method Process.CheckHandle(aHandle: Integer; aMessage: String);
begin
if aHandle = -1 then
raise new Exception('Can not create handles to redirect ' + aMessage);
end;
{$ENDIF}

{$IF WINDOWS OR (POSIX AND NOT IOS)}
Expand Down

0 comments on commit 1ef61ba

Please sign in to comment.