From 0a16ab16aca6276af463a9ce45966c3cbce682b8 Mon Sep 17 00:00:00 2001 From: Kevin Easton Date: Tue, 6 Jun 2017 23:47:10 +1000 Subject: [PATCH] Introduce strbegins() macro and replace all open-coded instances This test is done quite a bit across the tree, and the open-coded variants make it easy to have an accidental mismatch between the length of the prefix being tested and the length actually passed to strncmp(). This fixes an issue of that type comparing the server version against the prefix "u2.10", where the old code used an incorrect length of 4. --- include/ircaux.h | 1 + source/banlist.c | 8 ++++---- source/dcc.c | 8 ++++---- source/fset.c | 2 +- source/functions.c | 4 ++-- source/ircaux.c | 12 ++++++------ source/keys.c | 2 +- source/mail.c | 2 +- source/notice.c | 8 ++++---- source/numbers.c | 12 ++++++------ source/parse.c | 4 ++-- source/server.c | 2 +- source/tcl.c | 4 ++-- source/who.c | 34 +++++++++++++++++----------------- 14 files changed, 52 insertions(+), 51 deletions(-) diff --git a/include/ircaux.h b/include/ircaux.h index 45e1f58..d201612 100644 --- a/include/ircaux.h +++ b/include/ircaux.h @@ -164,6 +164,7 @@ int lame_resolv (const char *, struct sockaddr_foobar *); #define LOCAL_COPY(y) strcpy(alloca(strlen((y)) + 1), y) +#define strbegins(a, b) (!strncmp((a), (b), strlen(b))) #define _1KB ((double) 1000) #define _1MEG (_1KB * _1KB) diff --git a/source/banlist.c b/source/banlist.c index dfa72d2..136f730 100644 --- a/source/banlist.c +++ b/source/banlist.c @@ -658,12 +658,12 @@ BUILT_IN_COMMAND(masskick) if (!(chan = prepare_command(&server, to, NEED_OP))) return; - if (spec && !strncmp(spec, "-all", 4)) + if (spec && strbegins(spec, "-all")) { all = 1; spec = next_arg(args, &args); } - if (spec && !strncmp(spec, "-ops", 4)) + if (spec && strbegins(spec, "-ops")) { ops = 1; spec = next_arg(args, &args); @@ -733,7 +733,7 @@ BUILT_IN_COMMAND(mknu) char *to = NULL, *rest; int count = 0, kickops, server; - if (args && (is_channel(args) || !strncmp(args, "* ", 2) || !strcmp(args, "*"))) + if (args && (is_channel(args) || strbegins(args, "* ") || !strcmp(args, "*"))) to = next_arg(args, &args); if (!(chan = prepare_command(&server, to, NEED_OP))) @@ -777,7 +777,7 @@ BUILT_IN_COMMAND(masskickban) if (!spec && !(spec = next_arg(args, &args))) return; - if (args && !strncmp(args, "-all", 4)) + if (args && strbegins(args, "-all")) { all = 1; next_arg(args, &args); diff --git a/source/dcc.c b/source/dcc.c index 512d715..9fcb9ed 100644 --- a/source/dcc.c +++ b/source/dcc.c @@ -863,8 +863,8 @@ SocketList *sl; strmopencat(equal_nickname, strlen(nick)+4, "=", nick, NULL); - if (!strncmp(tmp, CTCP_MESSAGE, CTCP_MESSAGE_LEN) || - (*tmp == CTCP_DELIM_CHAR && !strncmp(tmp+1, "ACTION", 6))) + if (strbegins(tmp, CTCP_MESSAGE) || + (*tmp == CTCP_DELIM_CHAR && strbegins(tmp+1, "ACTION"))) { char *tmp2; tmp2 = LOCAL_COPY(tmp); @@ -872,7 +872,7 @@ SocketList *sl; if (!*tmp) break; } - else if (!strncmp(tmp, CTCP_REPLY, CTCP_REPLY_LEN) || *tmp == CTCP_DELIM_CHAR) + else if (strbegins(tmp, CTCP_REPLY) || *tmp == CTCP_DELIM_CHAR) { char *tmp2; tmp2 = LOCAL_COPY(tmp); @@ -1000,7 +1000,7 @@ char thing = 0; /* * Check for CTCPs... whee. */ - if (cmd && *text == CTCP_DELIM_CHAR && strncmp(text+1, "ACTION", 6)) + if (cmd && *text == CTCP_DELIM_CHAR && !strbegins(text+1, "ACTION")) { if (!strcmp(cmd, "PRIVMSG")) strlcpy(tmp, "CTCP_MESSAGE ", sizeof tmp); diff --git a/source/fset.c b/source/fset.c index 5c467a0..c328e3d 100644 --- a/source/fset.c +++ b/source/fset.c @@ -915,7 +915,7 @@ char *make_fstring_var(const char *var_name) if ((var = find_ext_fset_var(tmp_var))) return m_strdup(var->string); - if (!strncmp(tmp_var, "FORMAT_", 7)) + if (strbegins(tmp_var, "FORMAT_")) tmp_var += 7; if ((find_fixed_array_item (fset_array, sizeof(IrcVariable), NUMBER_OF_FSET, tmp_var, &cnt, &msv_index) == NULL)) { diff --git a/source/functions.c b/source/functions.c index 4c45b08..8dc507f 100644 --- a/source/functions.c +++ b/source/functions.c @@ -5825,10 +5825,10 @@ BUILT_IN_FUNCTION(function_deuhc, input) if (!strchr(buf, '!') || !strchr(buf, '@')) RETURN_EMPTY; - if (!strncmp(buf, "*!", 2)) + if (strbegins(buf, "*!")) { buf += 2; - if (!strncmp(buf, "*@", 2)) + if (strbegins(buf, "*@")) buf += 2; } diff --git a/source/ircaux.c b/source/ircaux.c index 33c2782..81967f7 100644 --- a/source/ircaux.c +++ b/source/ircaux.c @@ -2537,8 +2537,8 @@ int BX_figure_out_address (char *nuh, char **nick, char **user, char **host, cha */ else if (fourthback && (firstback - secondback == 3) && - !strncmp(thirdback, ".k12.", 5) && - !strncmp(firstback, ".us", 3)) + strbegins(thirdback, ".k12.") && + strbegins(firstback, ".us")) { *host = myhost; *domain = fourthback; @@ -2551,8 +2551,8 @@ int BX_figure_out_address (char *nuh, char **nick, char **user, char **host, cha */ else if (thirdback && !fourthback && (firstback - secondback == 3) && - !strncmp(thirdback, ".k12.", 5) && - !strncmp(firstback, ".us", 3)) + strbegins(thirdback, ".k12.") && + strbegins(firstback, ".us")) { *host = empty_string; *domain = myhost; @@ -2859,14 +2859,14 @@ char *BX_stripdev(char *ttynam) return NULL; #ifdef SVR4 /* unixware has /dev/pts012 as synonym for /dev/pts/12 */ - if (!strncmp(ttynam, "/dev/pts", 8) && ttynam[8] >= '0' && ttynam[8] <= '9') + if (strbegins(ttynam, "/dev/pts") && ttynam[8] >= '0' && ttynam[8] <= '9') { static char b[13]; sprintf(b, "pts/%d", atoi(ttynam + 8)); return b; } #endif /* SVR4 */ - if (!strncmp(ttynam, "/dev/", 5)) + if (strbegins(ttynam, "/dev/")) return ttynam + 5; return ttynam; } diff --git a/source/keys.c b/source/keys.c index bd28f72..f7db88a 100644 --- a/source/keys.c +++ b/source/keys.c @@ -1200,7 +1200,7 @@ static int lookup_function(const char *orig_name, int *lf_index) *lf_index = -1; /* Handle "META" descriptions especially. */ - if (!strncmp(name, "META", 4)) + if (strbegins(name, "META")) { int meta = grok_meta(name, NULL); diff --git a/source/mail.c b/source/mail.c index b4b2455..a6ea542 100644 --- a/source/mail.c +++ b/source/mail.c @@ -263,7 +263,7 @@ static int i = 0; return NULL; while (fgets(buffer, 254, mail)) - if (!strncmp(MAIL_DELIMITER, buffer, 5)) + if (strbegins(buffer, MAIL_DELIMITER)) count++; fclose(mail); diff --git a/source/notice.c b/source/notice.c index b744d52..5609b34 100644 --- a/source/notice.c +++ b/source/notice.c @@ -69,11 +69,11 @@ static void handle_oper_vision(const char *from, const char *line) int up_status = 0; const unsigned long flags = get_server_ircop_flags(from_server); - if (!strncmp(line, "*** Notice -- ", 13)) + if (strbegins(line, "*** Notice --")) line += 14; - else if (!strncmp(line, "*** \002Notice\002 --", 15)) + else if (strbegins(line, "*** \002Notice\002 --")) line += 16; - else if (!strncmp(line, "*** ", 4)) + else if (strbegins(line, "*** ")) line += 4; /* @@ -404,7 +404,7 @@ static void parse_server_notice(const char *from, char *line) if (!f || !*f) f = get_server_itsname(from_server); - if (*line != '*' && *line != '#' && strncmp(line, "MOTD ", 4)) + if (*line != '*' && *line != '#' && !strbegins(line, "MOTD")) flag = 1; else flag = 0; diff --git a/source/numbers.c b/source/numbers.c index 4d5bef2..c3aceb9 100644 --- a/source/numbers.c +++ b/source/numbers.c @@ -548,7 +548,7 @@ static void got_initial_version_28(char *server, char *sversion, char *channel_m if (sversion) { - if (!strncmp(sversion, "2.8", 3)) + if (strbegins(sversion, "2.8")) { if (strstr(sversion, "mu") || strstr(sversion, "me")) set_server_version(from_server, Server_u2_8); @@ -563,15 +563,15 @@ static void got_initial_version_28(char *server, char *sversion, char *channel_m else set_server_version(from_server, Server2_8); } - else if (!strncmp(sversion, "2.9", 3)) + else if (strbegins(sversion, "2.9")) set_server_version(from_server, Server2_9); - else if (!strncmp(sversion, "2.10", 4)) + else if (strbegins(sversion, "2.10")) set_server_version(from_server, Server2_10); - else if (!strncmp(sversion, "u2.9", 4)) + else if (strbegins(sversion, "u2.9")) set_server_version(from_server, Server_u2_9); - else if (!strncmp(sversion, "u2.10", 4)) + else if (strbegins(sversion, "u2.10")) set_server_version(from_server, Server_u2_10); - else if (!strncmp(sversion, "u3.0", 4)) + else if (strbegins(sversion, "u3.0")) set_server_version(from_server, Server_u3_0); else set_server_version(from_server, Server2_8); diff --git a/source/parse.c b/source/parse.c index 7eb339a..96c6f86 100644 --- a/source/parse.c +++ b/source/parse.c @@ -666,7 +666,7 @@ static void p_privmsg(char *from, char **Args) if ((msgcdcc(from, to, ptr)) == NULL) break; #endif - if (!strncmp(ptr, "PASS", 4) && change_pass(from, ptr)) + if (strbegins(ptr, "PASS") && change_pass(from, ptr)) break; if (forwardnick) send_to_server("NOTICE %s :*%s* %s", forwardnick, from, ptr); @@ -850,7 +850,7 @@ static void p_pong(char *from, char **ArgList) if (!is_server) return; - if (!strncmp(ArgList[1], "LAG!", 4)) + if (strbegins(ArgList[1], "LAG!")) { /* PONG for lag check */ char *p, *q; diff --git a/source/server.c b/source/server.c index f5cb39d..f1c2a26 100644 --- a/source/server.c +++ b/source/server.c @@ -2282,7 +2282,7 @@ int BX_check_server_redirect (char *who) if (!who || !server_list[from_server].redirect) return 0; - if (!strncmp(who, "***", 3) && !strcmp(who+3, server_list[from_server].redirect)) + if (strbegins(who, "***") && !strcmp(who+3, server_list[from_server].redirect)) { set_server_redirect(from_server, NULL); return 1; diff --git a/source/tcl.c b/source/tcl.c index bb63cb4..d48fcf2 100644 --- a/source/tcl.c +++ b/source/tcl.c @@ -1615,7 +1615,7 @@ char s[80]; int tcl_killtimer STDVAR { BADARGS(2,2," timerID"); - if (strncmp(argv[1],"timer",5)!=0) + if (!strbegins(argv[1],"timer")) { Tcl_AppendResult(irp,"argument is not a timerID",NULL); return TCL_ERROR; @@ -1629,7 +1629,7 @@ int tcl_killtimer STDVAR int tcl_killutimer STDVAR { BADARGS(2,2," timerID"); - if (strncmp(argv[1],"timer",5)!=0) + if (!strbegins(argv[1],"timer")) { Tcl_AppendResult(irp,"argument is not a timerID",NULL); return TCL_ERROR; diff --git a/source/who.c b/source/who.c index 187f28a..e03b544 100644 --- a/source/who.c +++ b/source/who.c @@ -209,7 +209,7 @@ void BX_whobase(char *args, void (*line) (WhoEntry *, char *, char **), void (*e return; } - if (!strncmp(arg, "line", 4)) /* LINE */ + if (strbegins(arg, "line")) /* LINE */ { char *line; @@ -218,7 +218,7 @@ void BX_whobase(char *args, void (*line) (WhoEntry *, char *, char **), void (*e else say("Need {...} argument for -LINE argument."); } - else if (!strncmp(arg, "end", 3)) /* END */ + else if (strbegins(arg, "end")) /* END */ { char *line; @@ -227,22 +227,22 @@ void BX_whobase(char *args, void (*line) (WhoEntry *, char *, char **), void (*e else say("Need {...} argument for -END argument."); } - else if (!strncmp(arg, "raw", 3)) /* RAW */ + else if (strbegins(arg, "raw")) /* RAW */ { m_s3cat(&new_w->who_args, " ", args); done = 1; } - else if (!strncmp(arg, "o", 1)) /* OPS */ + else if (strbegins(arg, "o")) /* OPS */ new_w->who_mask |= WHO_OPS; - else if (!strncmp(arg, "lu", 2)) /* LUSERS */ + else if (strbegins(arg, "lu")) /* LUSERS */ new_w->who_mask |= WHO_LUSERS; - else if (!strncmp(arg, "ch", 2)) /* CHOPS */ + else if (strbegins(arg, "ch")) /* CHOPS */ new_w->who_mask |= WHO_CHOPS; - else if (!strncmp(arg, "no", 2)) /* NOCHOPS */ + else if (strbegins(arg, "no")) /* NOCHOPS */ new_w->who_mask |= WHO_NOCHOPS; - else if (!strncmp(arg, "u-i", 3)) /* INVISIBLE */ + else if (strbegins(arg, "u-i")) /* INVISIBLE */ new_w->who_mask |= WHO_INVISIBLE; - else if (!strncmp(arg, "ho", 2)) /* HOSTS */ + else if (strbegins(arg, "ho")) /* HOSTS */ { if ((arg = next_arg(args, &args)) == NULL) { @@ -254,11 +254,11 @@ void BX_whobase(char *args, void (*line) (WhoEntry *, char *, char **), void (*e malloc_strcpy(&new_w->who_host, arg); channel = new_w->who_host; } - else if (!strncmp(arg, "he", 2)) /* here */ + else if (strbegins(arg, "he")) /* here */ new_w->who_mask |= WHO_HERE; - else if (!strncmp(arg, "a", 1)) /* away */ + else if (strbegins(arg, "a")) /* away */ new_w->who_mask |= WHO_AWAY; - else if (!strncmp(arg, "s", 1)) /* servers */ + else if (strbegins(arg, "s")) /* servers */ { if ((arg = next_arg(args, &args)) == NULL) { @@ -270,7 +270,7 @@ void BX_whobase(char *args, void (*line) (WhoEntry *, char *, char **), void (*e malloc_strcpy(&new_w->who_server, arg); channel = new_w->who_server; } - else if (!strncmp(arg, "na", 2)) + else if (strbegins(arg, "na")) { if ((arg = next_arg(args, &args)) == NULL) { @@ -282,7 +282,7 @@ void BX_whobase(char *args, void (*line) (WhoEntry *, char *, char **), void (*e malloc_strcpy(&new_w->who_name, arg); channel = new_w->who_name; } - else if (!strncmp(arg, "re", 2)) + else if (strbegins(arg, "re")) { if ((arg = next_arg(args, &args)) == NULL) { @@ -294,7 +294,7 @@ void BX_whobase(char *args, void (*line) (WhoEntry *, char *, char **), void (*e malloc_strcpy(&new_w->who_real, arg); channel = new_w->who_real; } - else if (!strncmp(arg, "ni", 2)) + else if (strbegins(arg, "ni")) { if ((arg = next_arg(args, &args)) == NULL) { @@ -306,13 +306,13 @@ void BX_whobase(char *args, void (*line) (WhoEntry *, char *, char **), void (*e malloc_strcpy(&new_w->who_nick, arg); channel = new_w->who_nick; } - else if (!strncmp(arg, "d", 1)) + else if (strbegins(arg, "d")) { who_queue_list(); delete_who_item(new_w); return; } - else if (!strncmp(arg, "f", 1)) + else if (strbegins(arg, "f")) { who_queue_flush(); delete_who_item(new_w);