[OB/PUSHED,GDBserver] Avoid stale errno

Message ID 53C53CE1.6010903@redhat.com
State Committed
Headers

Commit Message

Pedro Alves July 15, 2014, 2:38 p.m. UTC
  Noticed this by inspection the other day.

Pushed.

----------------
From: Pedro Alves <palves@redhat.com>
Subject: [PATCH] [GDBserver] Avoid stale errno

Although most compilers follow right-to-left evaluation order, the
order of evaluation of a function call's arguments is really
unspecified.  target_pid_to_str or ptid_of may well clobber errno when
we get to evaluate the third argument to debug_printf.

gdb/gdbserver/
2014-07-15  Pedro Alves  <palves@redhat.com>

	* linux-low.c (linux_kill_one_lwp): Save errno and work with saved
	copy.
---
 gdb/gdbserver/ChangeLog   |  5 +++++
 gdb/gdbserver/linux-low.c | 20 ++++++++++++++------
 2 files changed, 19 insertions(+), 6 deletions(-)
  

Patch

diff --git a/gdb/gdbserver/ChangeLog b/gdb/gdbserver/ChangeLog
index e6b0a84..4658abf 100644
--- a/gdb/gdbserver/ChangeLog
+++ b/gdb/gdbserver/ChangeLog
@@ -1,3 +1,8 @@ 
+2014-07-15  Pedro Alves  <palves@redhat.com>
+
+	* linux-low.c (linux_kill_one_lwp): Save errno and work with saved
+	copy.
+
 2014-07-11  Pedro Alves  <palves@redhat.com>
 
 	* linux-low.c (kill_wait_lwp): New function, based on
diff --git a/gdb/gdbserver/linux-low.c b/gdb/gdbserver/linux-low.c
index 215a80c..0f4dbe2 100644
--- a/gdb/gdbserver/linux-low.c
+++ b/gdb/gdbserver/linux-low.c
@@ -897,16 +897,24 @@  linux_kill_one_lwp (struct lwp_info *lwp)
   errno = 0;
   kill (pid, SIGKILL);
   if (debug_threads)
-    debug_printf ("LKL:  kill (SIGKILL) %s, 0, 0 (%s)\n",
-		  target_pid_to_str (ptid_of (thr)),
-		  errno ? strerror (errno) : "OK");
+    {
+      int save_errno = errno;
+
+      debug_printf ("LKL:  kill_lwp (SIGKILL) %s, 0, 0 (%s)\n",
+		    target_pid_to_str (ptid_of (thr)),
+		    save_errno ? strerror (save_errno) : "OK");
+    }
 
   errno = 0;
   ptrace (PTRACE_KILL, pid, (PTRACE_TYPE_ARG3) 0, (PTRACE_TYPE_ARG4) 0);
   if (debug_threads)
-    debug_printf ("LKL:  PTRACE_KILL %s, 0, 0 (%s)\n",
-		  target_pid_to_str (ptid_of (thr)),
-		  errno ? strerror (errno) : "OK");
+    {
+      int save_errno = errno;
+
+      debug_printf ("LKL:  PTRACE_KILL %s, 0, 0 (%s)\n",
+		    target_pid_to_str (ptid_of (thr)),
+		    save_errno ? strerror (save_errno) : "OK");
+    }
 }
 
 /* Kill LWP and wait for it to die.  */