From 9a03b24eb2cc94a2e95f6a50db60ea6ed3004130 Mon Sep 17 00:00:00 2001
From: Kaz Kylheku <kaz@kylheku.com>
Date: Thu, 14 Apr 2016 06:52:01 -0700
Subject: Fix error handling in dgram case of sock-accept.

* socket.c (sock_accept): Reduce scope of unwind catch around
recvfrom call. Add missing check nbytes == -1.  Branch to
failed label in dup failed case, instead of throwing
exception with truncated "unable to" message.
---
 socket.c | 21 +++++++++++----------
 1 file changed, 11 insertions(+), 10 deletions(-)

(limited to 'socket.c')

diff --git a/socket.c b/socket.c
index e90d374e..e3f040e8 100644
--- a/socket.c
+++ b/socket.c
@@ -835,6 +835,16 @@ static val sock_accept(val sock, val mode_str, val timeout_in)
 
     sig_restore_enable;
 
+    uw_unwind {
+      if (nbytes == -1)
+        free(dgram);
+    }
+
+    uw_catch_end;
+
+    if (nbytes == -1)
+      goto failed;
+
     if (family == num_fast(AF_INET))
       peer = sockaddr_in_unpack(coerce(struct sockaddr_in *, &sa));
     else if (family == num_fast(AF_INET6))
@@ -843,18 +853,10 @@ static val sock_accept(val sock, val mode_str, val timeout_in)
       peer = sockaddr_un_unpack(coerce(struct sockaddr_un *, &sa));
     else {
       free(dgram);
-      dgram = 0;
       uw_throwf(socket_error_s, lit("sock-accept: ~s isn't a supported socket family"),
                 family, nao);
     }
 
-    uw_unwind {
-      if (nbytes == -1)
-        free(dgram);
-    }
-
-    uw_catch_end;
-
     {
       int afd = dup(fd);
       struct stdio_mode mode_rpb = stdio_mode_init_rpb;
@@ -866,8 +868,7 @@ static val sock_accept(val sock, val mode_str, val timeout_in)
       if (afd == -1) {
         free(dgram);
         dgram = 0;
-        uw_throwf(socket_error_s, lit("sock-accept: unable to "),
-                family, nao);
+        goto failed;
       }
       return make_dgram_sock_stream(afd, family, peer, dgram, nbytes,
                                     coerce(struct sockaddr *, &sa), salen,
-- 
cgit v1.2.3