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.
This commit is contained in:
Kevin Easton
2017-06-06 23:47:10 +10:00
parent 09395464ff
commit 0a16ab16ac
14 changed files with 52 additions and 51 deletions

View File

@@ -164,6 +164,7 @@ int lame_resolv (const char *, struct sockaddr_foobar *);
#define LOCAL_COPY(y) strcpy(alloca(strlen((y)) + 1), y) #define LOCAL_COPY(y) strcpy(alloca(strlen((y)) + 1), y)
#define strbegins(a, b) (!strncmp((a), (b), strlen(b)))
#define _1KB ((double) 1000) #define _1KB ((double) 1000)
#define _1MEG (_1KB * _1KB) #define _1MEG (_1KB * _1KB)

View File

@@ -658,12 +658,12 @@ BUILT_IN_COMMAND(masskick)
if (!(chan = prepare_command(&server, to, NEED_OP))) if (!(chan = prepare_command(&server, to, NEED_OP)))
return; return;
if (spec && !strncmp(spec, "-all", 4)) if (spec && strbegins(spec, "-all"))
{ {
all = 1; all = 1;
spec = next_arg(args, &args); spec = next_arg(args, &args);
} }
if (spec && !strncmp(spec, "-ops", 4)) if (spec && strbegins(spec, "-ops"))
{ {
ops = 1; ops = 1;
spec = next_arg(args, &args); spec = next_arg(args, &args);
@@ -733,7 +733,7 @@ BUILT_IN_COMMAND(mknu)
char *to = NULL, *rest; char *to = NULL, *rest;
int count = 0, kickops, server; 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); to = next_arg(args, &args);
if (!(chan = prepare_command(&server, to, NEED_OP))) if (!(chan = prepare_command(&server, to, NEED_OP)))
@@ -777,7 +777,7 @@ BUILT_IN_COMMAND(masskickban)
if (!spec && !(spec = next_arg(args, &args))) if (!spec && !(spec = next_arg(args, &args)))
return; return;
if (args && !strncmp(args, "-all", 4)) if (args && strbegins(args, "-all"))
{ {
all = 1; all = 1;
next_arg(args, &args); next_arg(args, &args);

View File

@@ -863,8 +863,8 @@ SocketList *sl;
strmopencat(equal_nickname, strlen(nick)+4, "=", nick, NULL); strmopencat(equal_nickname, strlen(nick)+4, "=", nick, NULL);
if (!strncmp(tmp, CTCP_MESSAGE, CTCP_MESSAGE_LEN) || if (strbegins(tmp, CTCP_MESSAGE) ||
(*tmp == CTCP_DELIM_CHAR && !strncmp(tmp+1, "ACTION", 6))) (*tmp == CTCP_DELIM_CHAR && strbegins(tmp+1, "ACTION")))
{ {
char *tmp2; char *tmp2;
tmp2 = LOCAL_COPY(tmp); tmp2 = LOCAL_COPY(tmp);
@@ -872,7 +872,7 @@ SocketList *sl;
if (!*tmp) if (!*tmp)
break; 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; char *tmp2;
tmp2 = LOCAL_COPY(tmp); tmp2 = LOCAL_COPY(tmp);
@@ -1000,7 +1000,7 @@ char thing = 0;
/* /*
* Check for CTCPs... whee. * 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")) if (!strcmp(cmd, "PRIVMSG"))
strlcpy(tmp, "CTCP_MESSAGE ", sizeof tmp); strlcpy(tmp, "CTCP_MESSAGE ", sizeof tmp);

View File

@@ -915,7 +915,7 @@ char *make_fstring_var(const char *var_name)
if ((var = find_ext_fset_var(tmp_var))) if ((var = find_ext_fset_var(tmp_var)))
return m_strdup(var->string); return m_strdup(var->string);
if (!strncmp(tmp_var, "FORMAT_", 7)) if (strbegins(tmp_var, "FORMAT_"))
tmp_var += 7; tmp_var += 7;
if ((find_fixed_array_item (fset_array, sizeof(IrcVariable), NUMBER_OF_FSET, tmp_var, &cnt, &msv_index) == NULL)) if ((find_fixed_array_item (fset_array, sizeof(IrcVariable), NUMBER_OF_FSET, tmp_var, &cnt, &msv_index) == NULL))
{ {

View File

@@ -5825,10 +5825,10 @@ BUILT_IN_FUNCTION(function_deuhc, input)
if (!strchr(buf, '!') || !strchr(buf, '@')) if (!strchr(buf, '!') || !strchr(buf, '@'))
RETURN_EMPTY; RETURN_EMPTY;
if (!strncmp(buf, "*!", 2)) if (strbegins(buf, "*!"))
{ {
buf += 2; buf += 2;
if (!strncmp(buf, "*@", 2)) if (strbegins(buf, "*@"))
buf += 2; buf += 2;
} }

View File

@@ -2537,8 +2537,8 @@ int BX_figure_out_address (char *nuh, char **nick, char **user, char **host, cha
*/ */
else if (fourthback && else if (fourthback &&
(firstback - secondback == 3) && (firstback - secondback == 3) &&
!strncmp(thirdback, ".k12.", 5) && strbegins(thirdback, ".k12.") &&
!strncmp(firstback, ".us", 3)) strbegins(firstback, ".us"))
{ {
*host = myhost; *host = myhost;
*domain = fourthback; *domain = fourthback;
@@ -2551,8 +2551,8 @@ int BX_figure_out_address (char *nuh, char **nick, char **user, char **host, cha
*/ */
else if (thirdback && !fourthback && else if (thirdback && !fourthback &&
(firstback - secondback == 3) && (firstback - secondback == 3) &&
!strncmp(thirdback, ".k12.", 5) && strbegins(thirdback, ".k12.") &&
!strncmp(firstback, ".us", 3)) strbegins(firstback, ".us"))
{ {
*host = empty_string; *host = empty_string;
*domain = myhost; *domain = myhost;
@@ -2859,14 +2859,14 @@ char *BX_stripdev(char *ttynam)
return NULL; return NULL;
#ifdef SVR4 #ifdef SVR4
/* unixware has /dev/pts012 as synonym for /dev/pts/12 */ /* 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]; static char b[13];
sprintf(b, "pts/%d", atoi(ttynam + 8)); sprintf(b, "pts/%d", atoi(ttynam + 8));
return b; return b;
} }
#endif /* SVR4 */ #endif /* SVR4 */
if (!strncmp(ttynam, "/dev/", 5)) if (strbegins(ttynam, "/dev/"))
return ttynam + 5; return ttynam + 5;
return ttynam; return ttynam;
} }

View File

@@ -1200,7 +1200,7 @@ static int lookup_function(const char *orig_name, int *lf_index)
*lf_index = -1; *lf_index = -1;
/* Handle "META" descriptions especially. */ /* Handle "META" descriptions especially. */
if (!strncmp(name, "META", 4)) if (strbegins(name, "META"))
{ {
int meta = grok_meta(name, NULL); int meta = grok_meta(name, NULL);

View File

@@ -263,7 +263,7 @@ static int i = 0;
return NULL; return NULL;
while (fgets(buffer, 254, mail)) while (fgets(buffer, 254, mail))
if (!strncmp(MAIL_DELIMITER, buffer, 5)) if (strbegins(buffer, MAIL_DELIMITER))
count++; count++;
fclose(mail); fclose(mail);

View File

@@ -69,11 +69,11 @@ static void handle_oper_vision(const char *from, const char *line)
int up_status = 0; int up_status = 0;
const unsigned long flags = get_server_ircop_flags(from_server); const unsigned long flags = get_server_ircop_flags(from_server);
if (!strncmp(line, "*** Notice -- ", 13)) if (strbegins(line, "*** Notice --"))
line += 14; line += 14;
else if (!strncmp(line, "*** \002Notice\002 --", 15)) else if (strbegins(line, "*** \002Notice\002 --"))
line += 16; line += 16;
else if (!strncmp(line, "*** ", 4)) else if (strbegins(line, "*** "))
line += 4; line += 4;
/* /*
@@ -404,7 +404,7 @@ static void parse_server_notice(const char *from, char *line)
if (!f || !*f) if (!f || !*f)
f = get_server_itsname(from_server); f = get_server_itsname(from_server);
if (*line != '*' && *line != '#' && strncmp(line, "MOTD ", 4)) if (*line != '*' && *line != '#' && !strbegins(line, "MOTD"))
flag = 1; flag = 1;
else else
flag = 0; flag = 0;

View File

@@ -548,7 +548,7 @@ static void got_initial_version_28(char *server, char *sversion, char *channel_m
if (sversion) if (sversion)
{ {
if (!strncmp(sversion, "2.8", 3)) if (strbegins(sversion, "2.8"))
{ {
if (strstr(sversion, "mu") || strstr(sversion, "me")) if (strstr(sversion, "mu") || strstr(sversion, "me"))
set_server_version(from_server, Server_u2_8); 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 else
set_server_version(from_server, Server2_8); 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); 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); 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); 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); 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); set_server_version(from_server, Server_u3_0);
else else
set_server_version(from_server, Server2_8); set_server_version(from_server, Server2_8);

View File

@@ -666,7 +666,7 @@ static void p_privmsg(char *from, char **Args)
if ((msgcdcc(from, to, ptr)) == NULL) if ((msgcdcc(from, to, ptr)) == NULL)
break; break;
#endif #endif
if (!strncmp(ptr, "PASS", 4) && change_pass(from, ptr)) if (strbegins(ptr, "PASS") && change_pass(from, ptr))
break; break;
if (forwardnick) if (forwardnick)
send_to_server("NOTICE %s :*%s* %s", forwardnick, from, ptr); 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) if (!is_server)
return; return;
if (!strncmp(ArgList[1], "LAG!", 4)) if (strbegins(ArgList[1], "LAG!"))
{ {
/* PONG for lag check */ /* PONG for lag check */
char *p, *q; char *p, *q;

View File

@@ -2282,7 +2282,7 @@ int BX_check_server_redirect (char *who)
if (!who || !server_list[from_server].redirect) if (!who || !server_list[from_server].redirect)
return 0; 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); set_server_redirect(from_server, NULL);
return 1; return 1;

View File

@@ -1615,7 +1615,7 @@ char s[80];
int tcl_killtimer STDVAR int tcl_killtimer STDVAR
{ {
BADARGS(2,2," timerID"); 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); Tcl_AppendResult(irp,"argument is not a timerID",NULL);
return TCL_ERROR; return TCL_ERROR;
@@ -1629,7 +1629,7 @@ int tcl_killtimer STDVAR
int tcl_killutimer STDVAR int tcl_killutimer STDVAR
{ {
BADARGS(2,2," timerID"); 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); Tcl_AppendResult(irp,"argument is not a timerID",NULL);
return TCL_ERROR; return TCL_ERROR;

View File

@@ -209,7 +209,7 @@ void BX_whobase(char *args, void (*line) (WhoEntry *, char *, char **), void (*e
return; return;
} }
if (!strncmp(arg, "line", 4)) /* LINE */ if (strbegins(arg, "line")) /* LINE */
{ {
char *line; char *line;
@@ -218,7 +218,7 @@ void BX_whobase(char *args, void (*line) (WhoEntry *, char *, char **), void (*e
else else
say("Need {...} argument for -LINE argument."); say("Need {...} argument for -LINE argument.");
} }
else if (!strncmp(arg, "end", 3)) /* END */ else if (strbegins(arg, "end")) /* END */
{ {
char *line; char *line;
@@ -227,22 +227,22 @@ void BX_whobase(char *args, void (*line) (WhoEntry *, char *, char **), void (*e
else else
say("Need {...} argument for -END argument."); 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); m_s3cat(&new_w->who_args, " ", args);
done = 1; done = 1;
} }
else if (!strncmp(arg, "o", 1)) /* OPS */ else if (strbegins(arg, "o")) /* OPS */
new_w->who_mask |= WHO_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; 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; 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; 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; 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) 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); malloc_strcpy(&new_w->who_host, arg);
channel = new_w->who_host; channel = new_w->who_host;
} }
else if (!strncmp(arg, "he", 2)) /* here */ else if (strbegins(arg, "he")) /* here */
new_w->who_mask |= WHO_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; 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) 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); malloc_strcpy(&new_w->who_server, arg);
channel = new_w->who_server; channel = new_w->who_server;
} }
else if (!strncmp(arg, "na", 2)) else if (strbegins(arg, "na"))
{ {
if ((arg = next_arg(args, &args)) == NULL) 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); malloc_strcpy(&new_w->who_name, arg);
channel = new_w->who_name; channel = new_w->who_name;
} }
else if (!strncmp(arg, "re", 2)) else if (strbegins(arg, "re"))
{ {
if ((arg = next_arg(args, &args)) == NULL) 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); malloc_strcpy(&new_w->who_real, arg);
channel = new_w->who_real; channel = new_w->who_real;
} }
else if (!strncmp(arg, "ni", 2)) else if (strbegins(arg, "ni"))
{ {
if ((arg = next_arg(args, &args)) == NULL) 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); malloc_strcpy(&new_w->who_nick, arg);
channel = new_w->who_nick; channel = new_w->who_nick;
} }
else if (!strncmp(arg, "d", 1)) else if (strbegins(arg, "d"))
{ {
who_queue_list(); who_queue_list();
delete_who_item(new_w); delete_who_item(new_w);
return; return;
} }
else if (!strncmp(arg, "f", 1)) else if (strbegins(arg, "f"))
{ {
who_queue_flush(); who_queue_flush();
delete_who_item(new_w); delete_who_item(new_w);