From 4b282dd670c7263232d19412f9735d670a1b1b76 Mon Sep 17 00:00:00 2001 From: Luke Bratch Date: Tue, 19 Jan 2021 00:12:11 +0000 Subject: Fix various issues where strncmp was only comparing a substring. This fixes issues such as when JOINing a channel whose name is a substring of another channel, things like PARTing don't work properly. --- functions.c | 24 ++++++++++++++---------- 1 file changed, 14 insertions(+), 10 deletions(-) (limited to 'functions.c') diff --git a/functions.c b/functions.c index 6ed1d2f..e89d836 100644 --- a/functions.c +++ b/functions.c @@ -675,7 +675,7 @@ int createchannel(struct channel *channels, struct ircdstate *ircdstate, char *n if (arrslot < 0) { arrslot = ircdstate->maxchannelcount; ircdstate->maxchannelcount++; - debugprint(DEBUG_FULL, "createchannel(): using new slot slot %d.\n", arrslot); + debugprint(DEBUG_FULL, "createchannel(): using new slot %d.\n", arrslot); } // If we got a valid slot... @@ -713,16 +713,18 @@ int setchanneltopicwhotime(struct channel *channels, int maxchannelcount, char * debugprint(DEBUG_FULL, "setchanneltopicwhotime(): when: '%s' with length '%ld'.\n", when, strlen(when)); for (int i = 0; i < maxchannelcount; i++) { - if (strncmp(channels[i].name, channelname, strlen(channelname)) == 0) { + if ((strlen(channels[i].name) == strlen(channelname)) && (strncmp(channels[i].name, channelname, strlen(channelname)) == 0)) { strncpy(channels[i].topicwho, who, strlen(who)); channels[i].topicwho[strlen(who)] = '\0'; strncpy(channels[i].topicwhen, when, strlen(when)); channels[i].topicwhen[strlen(when)] = '\0'; + debugprint(DEBUG_FULL, "setchanneltopicwhotime(): set channel '%s' to '%s', '%s'.\n", channels[i].name, channels[i].topicwho, channels[i].topicwhen); return 1; } } // TODO - Make a failed return do something to callers + debugprint(DEBUG_FULL, "setchanneltopicwhotime(): failed to set channel topic who/when.\n"); return 0; } @@ -730,9 +732,10 @@ int setchanneltopic(struct channel *channels, int maxchannelcount, char *channel debugprint(DEBUG_FULL, "setchanneltopic(): given '%s' and '%s'.\n", channelname, topic); for (int i = 0; i < maxchannelcount; i++) { - if (strncmp(channels[i].name, channelname, strlen(channelname)) == 0) { + if ((strlen(channels[i].name) == strlen(channelname)) && (strncmp(channels[i].name, channelname, strlen(channelname)) == 0)) { strncpy(channels[i].topic, topic, strlen(topic)); channels[i].topic[strlen(topic)] = '\0'; + debugprint(DEBUG_FULL, "setchanneltopic(): set channel '%s' to '%s'.\n", channels[i].name, channels[i].topic); return 1; } } @@ -763,7 +766,7 @@ int removechannel(struct channel *channels, int maxchannelcount, char *name) { // Find the channel in the channel array... for (int i = 0; i < maxchannelcount; i++) { - if (strncmp(channels[i].name, name, strlen(name)) == 0) { + if ((strlen(channels[i].name) == strlen(name)) && (strncmp(channels[i].name, name, strlen(name)) == 0)) { // ..and NULL its name (0th character = '\0') channels[i].name[0] = '\0'; // Set nicks to blank @@ -782,11 +785,11 @@ int removechannel(struct channel *channels, int maxchannelcount, char *name) { } // Check if we have the NAMES for the channel 'name' already. -// Return the 1 if we do, 0 if we don't, or -1 if there's an error. +// Return 1 if we do, 0 if we don't, or -1 if there's an error. int channelgotnames(struct channel *channels, int maxchannelcount, char *name) { debugprint(DEBUG_FULL, "channelgotnames(): given '%s'.\n", name); for (int i = 0; i < maxchannelcount; i++) { - if (strncmp(channels[i].name, name, strlen(name)) == 0) { + if ((strlen(channels[i].name) == strlen(name)) && strncmp(channels[i].name, name, strlen(name)) == 0) { if (channels[i].gotnames) { debugprint(DEBUG_FULL, "channelgotnames(): channel '%s' gotnames was set, returning '%d'.\n", channels[i].name, channels[i].gotnames); return 1; @@ -814,7 +817,7 @@ int inchannel(struct channel *channels, int maxchannelcount, char *name) { } for (int i = 0; i < maxchannelcount; i++) { - if (strncmp(channels[i].name, name, strlen(name)) == 0) { + if ((strlen(channels[i].name) == strlen(name)) && (strncmp(channels[i].name, name, strlen(name)) == 0)) { debugprint(DEBUG_FULL, "inchannel(): in channel '%s'.\n", name); return 1; } @@ -831,7 +834,7 @@ int inchannel(struct channel *channels, int maxchannelcount, char *name) { int channelindex(struct channel *channels, int maxchannelcount, char *name) { debugprint(DEBUG_FULL, "channelindex(): given '%s'.\n", name); for (int i = 0; i < maxchannelcount; i++) { - if (strncmp(channels[i].name, name, strlen(name)) == 0) { + if ((strlen(channels[i].name) == strlen(name)) && (strncmp(channels[i].name, name, strlen(name)) == 0)) { return i; } } @@ -913,7 +916,8 @@ int doreplay(int sourcefd, int replayseconds, struct client *clients, struct set extractnickfromprefix(tokens[0], 1); // Check if we're currently in this channel or if the log line is from us - if (!inchannel(channels, ircdstate->maxchannelcount, tokens[2] + offset) || strncmp(tokens[0], ircdstate->ircnick, strlen(tokens[0])) == 0) { + if (!inchannel(channels, ircdstate->maxchannelcount, tokens[2] + offset) || + ((strlen(tokens[0]) == strlen(ircdstate->ircnick)) && (strncmp(tokens[0], ircdstate->ircnick, strlen(tokens[0])) == 0))) { debugprint(DEBUG_FULL, "Not sending '%s' replay line '%s'.\n", tokens[1], outgoingmsg); free(strcopyPtr); continue; @@ -924,7 +928,7 @@ int doreplay(int sourcefd, int replayseconds, struct client *clients, struct set if (strncmp(tokens[1], "NICK", strlen("NICK")) == 0) { extractnickfromprefix(tokens[0], 1); - if (strncmp(tokens[0], ircdstate->ircnick, strlen(tokens[0])) == 0) { + if ((strlen(tokens[0]) == strlen(ircdstate->ircnick)) && (strncmp(tokens[0], ircdstate->ircnick, strlen(tokens[0])) == 0)) { debugprint(DEBUG_FULL, "Not sending '%s' replay line '%s'.\n", tokens[1], outgoingmsg); free(strcopyPtr); continue; -- cgit v1.2.3