public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
* [Ada] Raise exception on call to Expect for a dead process
@ 2019-09-18  8:41 Pierre-Marie de Rodat
  0 siblings, 0 replies; only message in thread
From: Pierre-Marie de Rodat @ 2019-09-18  8:41 UTC (permalink / raw)
  To: gcc-patches; +Cc: Vadim Godunko

[-- Attachment #1: Type: text/plain, Size: 510 bytes --]

Call to Expect for a dead process results in SIGBUS signal on Linux
systems. Process_Died exception is raised in this case now.

Tested on x86_64-pc-linux-gnu, committed on trunk

2019-09-18  Vadim Godunko  <godunko@adacore.com>

gcc/ada/

	* libgnat/g-expect.adb (Expect_Internal): Don't include invalid
	file descriptors into the set of file descriptors for Poll.
	Raise Process_Died exception when computed set of file
	descriptors to monitor is empty.

gcc/testsuite/

	* gnat.dg/expect4.adb: New testcase.

[-- Attachment #2: patch.diff --]
[-- Type: text/x-diff, Size: 1655 bytes --]

--- gcc/ada/libgnat/g-expect.adb
+++ gcc/ada/libgnat/g-expect.adb
@@ -653,7 +653,9 @@ package body GNAT.Expect is
 
    begin
       for J in Descriptors'Range loop
-         if Descriptors (J) /= null then
+         if Descriptors (J) /= null
+           and then Descriptors (J).Output_Fd /= Invalid_FD
+         then
             Fds (Fds'First + Fds_Count) := Descriptors (J).Output_Fd;
             Fds_To_Descriptor (Fds'First + Fds_Count) := J;
             Fds_Count := Fds_Count + 1;
@@ -667,6 +669,14 @@ package body GNAT.Expect is
          end if;
       end loop;
 
+      if Fds_Count = 0 then
+         --  There are no descriptors to monitor, it means that process died.
+
+         Result := Expect_Process_Died;
+
+         return;
+      end if;
+
       declare
          Buffer : aliased String (1 .. Buffer_Size);
          --  Buffer used for input. This is allocated only once, not for

--- /dev/null
new file mode 100644
+++ gcc/testsuite/gnat.dg/expect4.adb
@@ -0,0 +1,35 @@
+--  { dg-do run }
+
+with GNAT.Expect.TTY;
+with GNAT.OS_Lib;
+
+procedure Expect4 is
+   Pid    : GNAT.Expect.TTY.TTY_Process_Descriptor;
+   Args   : GNAT.OS_Lib.Argument_List (1 .. 0);
+   Result : GNAT.Expect.Expect_Match;
+
+begin
+   Pid.Non_Blocking_Spawn ("true", Args);
+
+   begin
+      Pid.Expect (Result, ".*");
+
+      raise Program_Error;
+
+   exception
+      when GNAT.Expect.Process_Died =>
+         null;
+   end;
+
+   begin
+      Pid.Expect (Result, ".*");
+
+      raise Program_Error;
+
+   exception
+      when GNAT.Expect.Process_Died =>
+         null;
+   end;
+
+   Pid.Close;
+end Expect4;
\ No newline at end of file


^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~2019-09-18  8:41 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2019-09-18  8:41 [Ada] Raise exception on call to Expect for a dead process Pierre-Marie de Rodat

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).