diff options
author | Luke Bratch <luke@bratch.co.uk> | 2020-10-21 23:56:50 +0100 |
---|---|---|
committer | Luke Bratch <luke@bratch.co.uk> | 2020-10-21 23:56:50 +0100 |
commit | 80881f04e70b1708a303ae71774b87301f8deb38 (patch) | |
tree | 417b48760bc30491f7a975350feeb4e8414d90f7 /functions.c | |
parent | b09cbe8b3575ec018f7d73a0bcd751dba011fc72 (diff) |
Don't have arrindex() return 0 on failure as 0 is a valid index. Instead return -1 and change callers to check this.
Diffstat (limited to 'functions.c')
-rw-r--r-- | functions.c | 26 |
1 files changed, 20 insertions, 6 deletions
diff --git a/functions.c b/functions.c index 10e71a1..5dd7586 100644 --- a/functions.c +++ b/functions.c @@ -393,6 +393,7 @@ void updategreetings(char *greeting001, char *greeting002, char *greeting003, ch } // Return index of requested client FD within the clients array. +// Returns 0 or more on success, or -1 on failure. // TODO - Use this wherever we are calculating the position (various places) instead of // duplicating code. int arrindex(struct client *clients, int clientfd) { @@ -404,8 +405,8 @@ int arrindex(struct client *clients, int clientfd) { } // Something went wrong, we didn't find it - // TODO - Don't return 0 since 0 is a valid array index, return -1 and have callers check for that. - return 0; + debugprint(DEBUG_CRIT, "arrindex(): error: got to MAXCLIENTS (%d) but didn't find clientfd '%d' in clients struct.\n", MAXCLIENTS, clientfd); + return -1; } // Send whatever string to a specific client by providing the FD @@ -577,11 +578,17 @@ int sendtoserver(SSL *server_ssl, char *strsrc, int str_len, int clientfd, struc // Also set its authentication and registration statuses to 0. // Also set the pending statuses to 0 int disconnectclient(int fd, struct client *clients, struct ircdstate *ircdstate, struct settings *settings, struct clientcodes *clientcodes) { - debugprint(DEBUG_SOME, "disconnectclient(): disconnecting client %s with fd '%d'\n", clients[arrindex(clients, fd)].remoteip, fd); + // Index of client fd in clients array for use later + int clientindex = arrindex(clients, fd); + if (clientindex < 0) { + debugprint(DEBUG_CRIT, "disconnectclient(): error: arrindex() returned '%d', exiting!\n", clientindex); + exit(1); + } + debugprint(DEBUG_SOME, "disconnectclient(): disconnecting client %s with fd '%d'\n", clients[clientindex].remoteip, fd); // Alert other clients about the disconnection (don't send yet, we haven't removed from the clients array yet) char alertmsg[MAXDATASIZE]; - if (!snprintf(alertmsg, MAXDATASIZE, "NOTICE %s :blabouncer: client %s has disconnected.", ircdstate->ircnick, clients[arrindex(clients, fd)].remoteip)) { + if (!snprintf(alertmsg, MAXDATASIZE, "NOTICE %s :blabouncer: client %s has disconnected.", ircdstate->ircnick, clients[clientindex].remoteip)) { fprintf(stderr, "Error while preparing authentication failure NOTICE!\n"); debugprint(DEBUG_CRIT, "Error while preparing authentication failure NOTICE!\n"); alertmsg[0] = '\0'; @@ -1112,10 +1119,17 @@ void tryautonick(struct ircdstate *ircdstate) { void cleanexit(SSL *server_ssl, struct client *clients, int sourcefd, struct ircdstate *ircdstate, struct settings *settings, char *quitmsg) { char outgoingmsg[MAXDATASIZE]; + // Index of client fd in clients array for use later + int clientindex = arrindex(clients, sourcefd); + if (clientindex < 0) { + debugprint(DEBUG_CRIT, "cleanexit(): error: arrindex() returned '%d', exiting!\n", clientindex); + exit(1); + } + // Tell clients and debug log if (sourcefd) { - snprintf(outgoingmsg, MAXDATASIZE, "NOTICE %s :Exiting on request from client %s, message '%s'.", ircdstate->ircnick, clients[arrindex(clients, sourcefd)].remoteip, quitmsg); - debugprint(DEBUG_CRIT, "Exiting on request from client %s with fd '%d', message '%s'.\n", clients[arrindex(clients, sourcefd)].remoteip, sourcefd, quitmsg); + snprintf(outgoingmsg, MAXDATASIZE, "NOTICE %s :Exiting on request from client %s, message '%s'.", ircdstate->ircnick, clients[clientindex].remoteip, quitmsg); + debugprint(DEBUG_CRIT, "Exiting on request from client %s with fd '%d', message '%s'.\n", clients[clientindex].remoteip, sourcefd, quitmsg); } else { snprintf(outgoingmsg, MAXDATASIZE, "NOTICE %s :Exiting on request (not from a client), message '%s'.", ircdstate->ircnick, quitmsg); debugprint(DEBUG_CRIT, "Exiting on request (not from a client), message '%s'.\n", quitmsg); |