[2/2] gdbstub: Handle errors in gdb_accept()

Message ID 20180514173044.5025-3-peter.maydell@linaro.org
State Accepted
Headers show
Series
  • gdbstub: fix a couple of coverity nits
Related show

Commit Message

Peter Maydell May 14, 2018, 5:30 p.m.
In gdb_accept(), we both fail to check all errors (notably
that from socket_set_nodelay(), as Coverity notes in CID 1005666),
and fail to return an error status back to our caller. Correct
both of these things, so that errors in accept() result in our
stopping with a useful error message rather than ignoring it.

Signed-off-by: Peter Maydell <peter.maydell@linaro.org>

---
 gdbstub.c | 16 ++++++++++++----
 1 file changed, 12 insertions(+), 4 deletions(-)

-- 
2.17.0

Comments

Philippe Mathieu-Daudé May 14, 2018, 6:58 p.m. | #1
On 05/14/2018 02:30 PM, Peter Maydell wrote:
> In gdb_accept(), we both fail to check all errors (notably

> that from socket_set_nodelay(), as Coverity notes in CID 1005666),

> and fail to return an error status back to our caller. Correct

> both of these things, so that errors in accept() result in our

> stopping with a useful error message rather than ignoring it.

> 

> Signed-off-by: Peter Maydell <peter.maydell@linaro.org>


Reviewed-by: Philippe Mathieu-Daudé <f4bug@amsat.org>


> ---

>  gdbstub.c | 16 ++++++++++++----

>  1 file changed, 12 insertions(+), 4 deletions(-)

> 

> diff --git a/gdbstub.c b/gdbstub.c

> index cc7626c790..7c2bceb040 100644

> --- a/gdbstub.c

> +++ b/gdbstub.c

> @@ -1804,7 +1804,7 @@ void gdb_signalled(CPUArchState *env, int sig)

>      put_packet(s, buf);

>  }

>  

> -static void gdb_accept(void)

> +static bool gdb_accept(void)

>  {

>      GDBState *s;

>      struct sockaddr_in sockaddr;

> @@ -1816,7 +1816,7 @@ static void gdb_accept(void)

>          fd = accept(gdbserver_fd, (struct sockaddr *)&sockaddr, &len);

>          if (fd < 0 && errno != EINTR) {

>              perror("accept");

> -            return;

> +            return false;

>          } else if (fd >= 0) {

>              qemu_set_cloexec(fd);

>              break;

> @@ -1824,7 +1824,10 @@ static void gdb_accept(void)

>      }

>  

>      /* set short latency */

> -    socket_set_nodelay(fd);

> +    if (socket_set_nodelay(fd)) {

> +        perror("setsockopt");

> +        return false;

> +    }

>  

>      s = g_malloc0(sizeof(GDBState));

>      s->c_cpu = first_cpu;

> @@ -1833,6 +1836,7 @@ static void gdb_accept(void)

>      gdb_has_xml = false;

>  

>      gdbserver_state = s;

> +    return true;

>  }

>  

>  static int gdbserver_open(int port)

> @@ -1873,7 +1877,11 @@ int gdbserver_start(int port)

>      if (gdbserver_fd < 0)

>          return -1;

>      /* accept connections */

> -    gdb_accept();

> +    if (!gdb_accept()) {

> +        close(gdbserver_fd);

> +        gdbserver_fd = -1;

> +        return -1;

> +    }

>      return 0;

>  }

>  

>
Thomas Huth May 15, 2018, 5:24 a.m. | #2
On 14.05.2018 19:30, Peter Maydell wrote:
> In gdb_accept(), we both fail to check all errors (notably

> that from socket_set_nodelay(), as Coverity notes in CID 1005666),

> and fail to return an error status back to our caller. Correct

> both of these things, so that errors in accept() result in our

> stopping with a useful error message rather than ignoring it.

> 

> Signed-off-by: Peter Maydell <peter.maydell@linaro.org>

> ---

>  gdbstub.c | 16 ++++++++++++----

>  1 file changed, 12 insertions(+), 4 deletions(-)


Reviewed-by: Thomas Huth <thuth@redhat.com>

Patch

diff --git a/gdbstub.c b/gdbstub.c
index cc7626c790..7c2bceb040 100644
--- a/gdbstub.c
+++ b/gdbstub.c
@@ -1804,7 +1804,7 @@  void gdb_signalled(CPUArchState *env, int sig)
     put_packet(s, buf);
 }
 
-static void gdb_accept(void)
+static bool gdb_accept(void)
 {
     GDBState *s;
     struct sockaddr_in sockaddr;
@@ -1816,7 +1816,7 @@  static void gdb_accept(void)
         fd = accept(gdbserver_fd, (struct sockaddr *)&sockaddr, &len);
         if (fd < 0 && errno != EINTR) {
             perror("accept");
-            return;
+            return false;
         } else if (fd >= 0) {
             qemu_set_cloexec(fd);
             break;
@@ -1824,7 +1824,10 @@  static void gdb_accept(void)
     }
 
     /* set short latency */
-    socket_set_nodelay(fd);
+    if (socket_set_nodelay(fd)) {
+        perror("setsockopt");
+        return false;
+    }
 
     s = g_malloc0(sizeof(GDBState));
     s->c_cpu = first_cpu;
@@ -1833,6 +1836,7 @@  static void gdb_accept(void)
     gdb_has_xml = false;
 
     gdbserver_state = s;
+    return true;
 }
 
 static int gdbserver_open(int port)
@@ -1873,7 +1877,11 @@  int gdbserver_start(int port)
     if (gdbserver_fd < 0)
         return -1;
     /* accept connections */
-    gdb_accept();
+    if (!gdb_accept()) {
+        close(gdbserver_fd);
+        gdbserver_fd = -1;
+        return -1;
+    }
     return 0;
 }