Do not close the socket twice in netcat.
authorbluhm <bluhm@openbsd.org>
Thu, 6 Sep 2018 13:23:02 +0000 (13:23 +0000)
committerbluhm <bluhm@openbsd.org>
Thu, 6 Sep 2018 13:23:02 +0000 (13:23 +0000)
from Nan Xiao; OK tb@

usr.bin/nc/netcat.c

index 8e7f7e1..e02ddd6 100644 (file)
@@ -1,4 +1,4 @@
-/* $OpenBSD: netcat.c,v 1.192 2018/08/10 17:15:22 deraadt Exp $ */
+/* $OpenBSD: netcat.c,v 1.193 2018/09/06 13:23:02 bluhm Exp $ */
 /*
  * Copyright (c) 2001 Eric Jackson <ericj@monkey.org>
  * Copyright (c) 2015 Bob Beck.  All rights reserved.
@@ -564,8 +564,11 @@ main(int argc, char *argv[])
                }
                /* Allow only one connection at a time, but stay alive. */
                for (;;) {
-                       if (family != AF_UNIX)
+                       if (family != AF_UNIX) {
+                               if (s != -1)
+                                       close(s);
                                s = local_listen(host, uport, hints);
+                       }
                        if (s < 0)
                                err(1, NULL);
                        if (uflag && kflag) {
@@ -622,9 +625,7 @@ main(int argc, char *argv[])
                                }
                                close(connfd);
                        }
-                       if (family != AF_UNIX)
-                               close(s);
-                       else if (uflag) {
+                       if (family == AF_UNIX && uflag) {
                                if (connect(s, NULL, 0) < 0)
                                        err(1, "connect");
                        }