From 5ca93f7762f21f3f4059be24dfc37688c8cae4bc Mon Sep 17 00:00:00 2001 From: dmiller Date: Sat, 11 Jul 2026 22:06:29 +0000 Subject: [PATCH] Don't decrement connection count on listener or stdin error Possibly the cause of #1410, since calling close_fd() decrements conn_inc, but stdin and listeners aren't accounted for there, which would cause get_conn_count() to underflow. Theoretical, since I have not been able to repro #1410. --- ncat/ncat_listen.c | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/ncat/ncat_listen.c b/ncat/ncat_listen.c index ead17cbb4..60e013820 100644 --- a/ncat/ncat_listen.c +++ b/ncat/ncat_listen.c @@ -364,6 +364,16 @@ restart_fd_loop: int cfd = fdi->fd; /* If we saw an error, close this fd */ if (fdi->lasterr != 0) { + if (checked_fd_isset(cfd, &listen_fds)) { + /* We may want to reopen this listener instead of quitting here. */ + bye("Listening socket error %d: %s", + fdi->lasterr, socket_strerror(fdi->lasterr)); + } + else if (cfd == STDIN_FILENO) { + /* We may want to close STDIN and continue instead of quitting here. */ + bye("STDIN error %d: %s", + fdi->lasterr, socket_strerror(fdi->lasterr)); + } close_fd(fdi, 0); goto restart_fd_loop; } @@ -636,6 +646,8 @@ static void post_handle_connection(struct fdinfo *sinfo) static void close_fd(struct fdinfo *fdn, int eof) { /* rm_fd invalidates fdn, so save what we need here. */ int fd = fdn->fd; + /* This should never be used to close stdin or a listening socket. */ + ncat_assert(fd != STDIN_FILENO); if (o.debug) logdebug("Closing connection.\n"); #ifdef HAVE_OPENSSL