From 80881f04e70b1708a303ae71774b87301f8deb38 Mon Sep 17 00:00:00 2001 From: Luke Bratch Date: Wed, 21 Oct 2020 23:56:50 +0100 Subject: Don't have arrindex() return 0 on failure as 0 is a valid index. Instead return -1 and change callers to check this. --- blabouncer.c | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) (limited to 'blabouncer.c') diff --git a/blabouncer.c b/blabouncer.c index 9604ba3..982d504 100644 --- a/blabouncer.c +++ b/blabouncer.c @@ -764,6 +764,14 @@ void dochat(int *serversockfd, int *clientsockfd, struct settings *settings) { debugprint(DEBUG_FULL, "checking client socket %d out of %d.\n", i, fdmax); if (FD_ISSET(i, &rfds)) { debugprint(DEBUG_FULL, "fd %d is FD_ISSET and it is a...\n", i); + + // Index of client fd in clients array for use later + int clientindex = arrindex(clients, i); + if (clientindex < 0 && i != *clientsockfd) { + debugprint(DEBUG_CRIT, "dochat(): error: arrindex() returned '%d', exiting!\n", clientindex); + exit(1); + } + // if value of clientsockfd then must be a new connection, if greater must be an existing connection if (i == *clientsockfd) { debugprint(DEBUG_SOME, "...new connection!\n"); @@ -845,15 +853,15 @@ void dochat(int *serversockfd, int *clientsockfd, struct settings *settings) { debugprint(DEBUG_FULL, "bouncer-client: total client connections: %d\n", numclients(clients)); } // If using client TLS and still pending SSL_accept() then re-try SSL_accept() (it can't be real client data yet) - } else if (clients[arrindex(clients, i)].pendingsslaccept) { + } else if (clients[clientindex].pendingsslaccept) { debugprint(DEBUG_FULL, "...previous connection pending SSL_accept!\n"); // Try to SSL_accept(), not interested in return code here since openssl_accept() does the right thing. - openssl_accept(clients[arrindex(clients, i)].fd, clients, &ircdstate, settings, clientcodes); + openssl_accept(clients[clientindex].fd, clients, &ircdstate, settings, clientcodes); } else { debugprint(DEBUG_FULL, "...previous connection!\n"); // handle data from a client - if ((clientnumbytes = sockread(clients[arrindex(clients, i)].ssl, clientbuf, MAXRCVSIZE - 1, settings->clienttls)) <= 0) { + if ((clientnumbytes = sockread(clients[clientindex].ssl, clientbuf, MAXRCVSIZE - 1, settings->clienttls)) <= 0) { // got error or connection closed by client if (clientnumbytes == 0) { // connection closed -- cgit v1.2.3