From 209a0094c4b1a8603b73beb77f28ea6c314d5875 Mon Sep 17 00:00:00 2001 From: Vadim Godunko Date: Wed, 18 Sep 2019 08:32:14 +0000 Subject: [PATCH] [Ada] Raise exception on call to Expect for a dead process Call to Expect for a dead process results in SIGBUS signal on Linux systems. Process_Died exception is raised in this case now. 2019-09-18 Vadim Godunko 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. From-SVN: r275847 --- gcc/ada/ChangeLog | 7 +++++++ gcc/ada/libgnat/g-expect.adb | 12 ++++++++++- gcc/testsuite/ChangeLog | 4 ++++ gcc/testsuite/gnat.dg/expect4.adb | 35 +++++++++++++++++++++++++++++++ 4 files changed, 57 insertions(+), 1 deletion(-) create mode 100644 gcc/testsuite/gnat.dg/expect4.adb diff --git a/gcc/ada/ChangeLog b/gcc/ada/ChangeLog index 38c5a7ee164..b110539176e 100644 --- a/gcc/ada/ChangeLog +++ b/gcc/ada/ChangeLog @@ -1,3 +1,10 @@ +2019-09-18 Vadim Godunko + + * 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. + 2019-09-18 Frederic Konrad * adaint.c: Include dosFsLib.h and vwModNum.h for VxWorks 6. diff --git a/gcc/ada/libgnat/g-expect.adb b/gcc/ada/libgnat/g-expect.adb index 0ee010e4cfb..4efd98d8504 100644 --- a/gcc/ada/libgnat/g-expect.adb +++ b/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 diff --git a/gcc/testsuite/ChangeLog b/gcc/testsuite/ChangeLog index c3f82a5f1a8..e0ac52957f0 100644 --- a/gcc/testsuite/ChangeLog +++ b/gcc/testsuite/ChangeLog @@ -1,3 +1,7 @@ +2019-09-18 Vadim Godunko + + * gnat.dg/expect4.adb: New testcase. + 2019-09-18 Steve Baird * gnat.dg/ghost7.adb, gnat.dg/ghost7.ads: New testcase. diff --git a/gcc/testsuite/gnat.dg/expect4.adb b/gcc/testsuite/gnat.dg/expect4.adb new file mode 100644 index 00000000000..7a974cc758e --- /dev/null +++ b/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 -- 2.30.2