Permalink
Browse files

Variety of long-needed code clean-up & fixes (coverity branch)

* Tons of small fixes, return value checks, etc
* Move logfile_suffix declaration to misc.c.
* Set botnetnick through accessor function so its size is known.
* Avoid repetition and alter u_addinvite and u_addban as well.
* Clean up TCL_PATCH_LEVEL usage.
  • Loading branch information...
1 parent 9bd9d1b commit afdd3a5e318ef2f6fa4ab7bc986d8a1960481314 @vanosg vanosg committed on GitHub Feb 14, 2017
View
@@ -123,7 +123,7 @@ static void bot_chan2(int idx, char *msg)
*p = 0;
p = strchr(from, '@');
if (p) {
- snprintf(TBUF, sizeof(TBUF), "<%s> %s", from, msg);
+ egg_snprintf(TBUF, sizeof(TBUF), "<%s> %s", from, msg);
*p = 0;
if (!partyidle(p + 1, from)) {
*p = '@';
@@ -954,7 +954,7 @@ static void bot_thisbot(int idx, char *par)
noshare = 1;
change_handle(dcc[idx].user, par);
noshare = 0;
- strcpy(dcc[idx].nick, par);
+ strncpyz(dcc[idx].nick, par, sizeof dcc[idx].nick);
}
static void bot_handshake(int idx, char *par)
View
@@ -136,6 +136,11 @@ int partysock(char *bot, char *nick)
return 0;
}
+/* Set the botnetnick and truncate as necessary */
+void set_botnetnick(const char *newnick) {
+ strncpyz(botnetnick, newnick, sizeof botnetnick);
+}
+
/* New botnet member
*/
int addparty(char *bot, char *nick, int chan, char flag, int sock,
@@ -1072,7 +1077,7 @@ static void botlink_resolve_success(int i)
changeover_dcc(i, &DCC_FORK_BOT, sizeof(struct bot_info));
dcc[i].timeval = now;
- strcpy(dcc[i].u.bot->linker, linker);
+ strncpyz(dcc[i].u.bot->linker, linker, sizeof dcc[i].u.bot->linker);
strcpy(dcc[i].u.bot->version, "(primitive bot)");
dcc[i].u.bot->numver = idx;
dcc[i].u.bot->port = dcc[i].port; /* Remember where i started */
View
@@ -323,14 +323,14 @@ void tell_verbose_status(int idx)
sprintf(&s[strlen(s)], "%02d:%02d", (int) hr, (int) min);
s1[0] = 0;
if (backgrd)
- strcpy(s1, MISC_BACKGROUND);
+ strncpyz(s1, MISC_BACKGROUND, sizeof s1);
else {
if (term_z)
- strcpy(s1, MISC_TERMMODE);
+ strncpyz(s1, MISC_TERMMODE, sizeof s1);
else if (con_chan)
- strcpy(s1, MISC_STATMODE);
+ strncpyz(s1, MISC_STATMODE, sizeof s1);
else
- strcpy(s1, MISC_LOGMODE);
+ strncpyz(s1, MISC_LOGMODE, sizeof s1);
}
cputime = getcputime();
if (cputime < 0)
@@ -361,8 +361,7 @@ void tell_verbose_status(int idx)
dprintf(idx, "%s %s (%s %s)\n", MISC_TCLVERSION,
((interp) && (Tcl_Eval(interp, "info patchlevel") == TCL_OK)) ?
tcl_resultstring() : (Tcl_Eval(interp, "info tclversion") == TCL_OK) ?
- tcl_resultstring() : "*unknown*", MISC_TCLHVERSION,
- TCL_PATCH_LEVEL ? TCL_PATCH_LEVEL : "*unknown*");
+ tcl_resultstring() : "*unknown*", MISC_TCLHVERSION, TCL_PATCH_LEVEL);
if (tcl_threaded())
dprintf(idx, "Tcl is threaded.\n");
@@ -490,7 +489,7 @@ void chanprog()
protect_readonly = 1;
if (!botnetnick[0])
- strncpyz(botnetnick, origbotname, HANDLEN + 1);
+ set_botnetnick(origbotname);
if (!botnetnick[0])
fatal("I don't have a botnet nick!!\n", 0);
View
@@ -656,7 +656,7 @@ static void cmd_console(struct userrec *u, int idx, char *par)
return;
}
get_user_flagrec(u, &fr, dcc[idx].u.chat->con_chan);
- strcpy(s1, par);
+ strncpyz(s1, par, sizeof s1);
nick = newsplit(&par);
/* Don't remove '+' as someone couldn't have '+' in CHANMETA cause
* he doesn't use IRCnet ++rtc.
@@ -1665,7 +1665,7 @@ static void cmd_chattr(struct userrec *u, int idx, char *par)
}
if (chan)
putlog(LOG_CMDS, "*", "#%s# (%s) chattr %s %s",
- dcc[idx].nick, chan ? chan->dname : "*", hand, chg ? chg : "");
+ dcc[idx].nick, chan->dname, hand, chg ? chg : "");
else
putlog(LOG_CMDS, "*", "#%s# chattr %s %s", dcc[idx].nick, hand,
chg ? chg : "");
@@ -2236,7 +2236,7 @@ static void cmd_su(struct userrec *u, int idx, char *par)
dcc[idx].u.chat->su_nick = get_data_ptr(strlen(dcc[idx].nick) + 1);
strcpy(dcc[idx].u.chat->su_nick, dcc[idx].nick);
dcc[idx].user = u;
- strcpy(dcc[idx].nick, par);
+ strncpyz(dcc[idx].nick, par, sizeof dcc[idx].nick);
dcc_chatter(idx);
}
}
@@ -2347,7 +2347,7 @@ static void cmd_set(struct userrec *u, int idx, char *msg)
dumplots(idx, "Global vars: ", tcl_resultstring());
return;
}
- strcpy(s + 4, msg);
+ strncpyz(s + 4, msg, sizeof s - 4);
code = Tcl_Eval(interp, s);
/* properly convert string to system encoding. */
View
@@ -702,7 +702,7 @@ static void dcc_chat_pass(int idx, char *buf, int atr)
if (dcc[idx].status & STAT_TELNET)
tputs(dcc[idx].sock, TLN_IAC_C TLN_WONT_C TLN_ECHO_C "\n", 4);
dcc[idx].user = get_user_by_handle(userlist, dcc[idx].u.chat->away);
- strcpy(dcc[idx].nick, dcc[idx].u.chat->away);
+ strncpyz(dcc[idx].nick, dcc[idx].u.chat->away, sizeof dcc[idx].nick);
nfree(dcc[idx].u.chat->away);
nfree(dcc[idx].u.chat->su_nick);
dcc[idx].u.chat->away = NULL;
@@ -1055,7 +1055,7 @@ static void dcc_chat(int idx, char *buf, int i)
if (dcc[idx].u.chat->su_nick) {
dcc[idx].user = get_user_by_handle(userlist,
dcc[idx].u.chat->su_nick);
- strcpy(dcc[idx].nick, dcc[idx].u.chat->su_nick);
+ strncpyz(dcc[idx].nick, dcc[idx].u.chat->su_nick, sizeof dcc[idx].nick);
dcc[idx].type = &DCC_CHAT;
dprintf(idx, "Returning to real nick %s!\n",
dcc[idx].u.chat->su_nick);
@@ -1173,7 +1173,7 @@ static int detect_telnet_flood(char *floodhost)
if (!flood_telnet_thr || (glob_friend(fr) && !par_telnet_flood))
return 0; /* No flood protection */
if (egg_strcasecmp(lasttelnethost, floodhost)) { /* New... */
- strcpy(lasttelnethost, floodhost);
+ strncpyz(lasttelnethost, floodhost, sizeof lasttelnethost);
lasttelnettime = now;
lasttelnets = 0;
return 0;
@@ -1567,7 +1567,7 @@ static void dcc_telnet_id(int idx, char *buf, int atr)
return;
}
correct_handle(buf);
- strcpy(dcc[idx].nick, buf);
+ strncpyz(dcc[idx].nick, buf, sizeof dcc[idx].nick);
if (glob_bot(fr)) {
if (!egg_strcasecmp(botnetnick, dcc[idx].nick)) {
dprintf(idx, "error You cannot link using my botnetnick.\n");
@@ -1733,7 +1733,7 @@ static void dcc_telnet_pass(int idx, int atr)
/* Turn off remote telnet echo (send IAC WILL ECHO). */
if (dcc[idx].status & STAT_TELNET) {
char buf[1030];
- snprintf(buf, sizeof buf, "\n%s%s\r\n", escape_telnet(DCC_ENTERPASS),
+ egg_snprintf(buf, sizeof buf, "\n%s%s\r\n", escape_telnet(DCC_ENTERPASS),
TLN_IAC_C TLN_WILL_C TLN_ECHO_C);
tputs(dcc[idx].sock, buf, strlen(buf));
} else
View
@@ -148,8 +148,7 @@ static int del_lang(char *lang)
lpo->next = lp->next;
else
langpriority = lp->next;
- if (lp->lang)
- nfree(lp->lang);
+ nfree(lp->lang);
nfree(lp);
debug1("LANG: Language unloaded: %s", lang);
return 1;
@@ -536,7 +535,7 @@ static int cmd_languagedump(struct userrec *u, int idx, char *par)
sscanf(par, "%x", &idx2);
else
idx2 = (int) strtol(par, (char **) NULL, 10);
- strcpy(ltext2, get_language(idx2));
+ strncpyz(ltext2, get_language(idx2), sizeof ltext2);
dprintf(idx, "0x%x: %s\n", idx2, ltext2);
return 0;
}
View
@@ -121,7 +121,6 @@ char helpdir[121] = "help/"; /* Directory of help files */
char textdir[121] = "text/"; /* Directory for text files */
int keep_all_logs = 0; /* Never erase logfiles? */
-char logfile_suffix[21] = ".%d%b%Y"; /* Format of logfile suffix */
int switch_logfiles_at = 300; /* When to switch logfiles */
time_t online_since; /* time that the bot was started */
@@ -265,8 +264,8 @@ static void write_debug()
* _not_ safe <cybah>
*/
x = creat("DEBUG.DEBUG", 0644);
- setsock(x, SOCK_NONSOCK);
if (x >= 0) {
+ setsock(x, SOCK_NONSOCK);
strncpyz(s, ctime(&now), sizeof s);
dprintf(-x, "Debug (%s) written %s\n", ver, s);
dprintf(-x, "Please report problem to bugs@eggheads.org\n");
@@ -310,8 +309,7 @@ static void write_debug()
dprintf(-x, "Tcl version: %s (header version %s)\n",
((interp) && (Tcl_Eval(interp, "info patchlevel") == TCL_OK)) ?
tcl_resultstring() : (Tcl_Eval(interp, "info tclversion") == TCL_OK) ?
- tcl_resultstring() : "*unknown*", TCL_PATCH_LEVEL ? TCL_PATCH_LEVEL :
- "*unknown*");
+ tcl_resultstring() : "*unknown*", TCL_PATCH_LEVEL);
if (tcl_threaded())
dprintf(-x, "Tcl is threaded\n");
@@ -902,7 +900,7 @@ int mainloop(int toplevel)
d = d->next;
}
if (ok) {
- strcpy(name, p->name);
+ strncpyz(name, p->name, sizeof name);
if (module_unload(name, botnetnick) == NULL) {
f = 1;
break;
@@ -1176,6 +1174,9 @@ int main(int arg_c, char **arg_v)
if (!backgrd && term_z) {
int n = new_dcc(&DCC_CHAT, sizeof(struct chat_info));
+ if (!n)
+ fatal("ERROR: Failed to initialize foreground chat.", 0);
+
getvhost(&dcc[n].sockname, AF_INET);
dcc[n].sock = STDOUT;
dcc[n].timeval = now;
View
@@ -30,6 +30,10 @@
#include "lush.h"
+#ifndef TCL_PATCH_LEVEL
+# define TCL_PATCH_LEVEL "*unknown*"
+#endif
+
#if defined(HAVE_TCL_NOTIFIER_INIT)
# define REPLACE_NOTIFIER
#endif
View
@@ -375,7 +375,7 @@ void *n_realloc(void *ptr, int size, const char *file, int line)
if (i == lastused) {
putlog(LOG_MISC, "*", "*** ATTEMPTING TO REALLOC NON-MALLOC'D PTR: %s (%d)",
file, line);
- return NULL;
+ /* If we reach this, realloc returned a pointer anyway. So we use it. */
}
memused -= memtbl[i].size;
memtbl[i].ptr = x;
View
@@ -45,14 +45,14 @@ extern struct dcc_t *dcc;
extern struct chanset_t *chanset;
extern char helpdir[], version[], origbotname[], botname[], admin[], network[],
- motdfile[], ver[], botnetnick[], bannerfile[], logfile_suffix[],
- textdir[];
+ motdfile[], ver[], botnetnick[], bannerfile[], textdir[];
extern int backgrd, con_chan, term_z, use_stderr, dcc_total, keep_all_logs,
quick_logs, strict_host;
extern time_t now;
extern Tcl_Interp *interp;
+char logfile_suffix[21] = ".%d%b%Y"; /* Format of logfile suffix */
char log_ts[32] = "[%H:%M:%S]"; /* Timestamp format for logfile entries */
int shtime = 1; /* Display the time with console output */
@@ -640,11 +640,11 @@ void logsuffix_change(char *s)
char *s2 = logfile_suffix;
/* If the suffix didn't really change, ignore. It's probably a rehash. */
- if (s && s2 && !strcmp(s, s2))
+ if (!s || (s && s2 && !strcmp(s, s2)))
return;
debug0("Logfile suffix changed. Closing all open logs.");
- strcpy(logfile_suffix, s);
+ strncpyz(logfile_suffix, s, sizeof logfile_suffix);
while (s2[0]) {
if (s2[0] == ' ')
s2[0] = '_';
@@ -1052,7 +1052,7 @@ void help_subst(char *s, char *nick, struct flag_record *flags,
}
}
if (cols) {
- strcpy(xx, s);
+ strncpyz(xx, s, sizeof xx);
s[0] = 0;
subst_addcol(s, xx);
}
@@ -151,7 +151,7 @@ static void set_mode_protect(struct chanset_t *chan, char *set)
if (pos) {
s1 = newsplit(&set);
if (s1[0])
- strcpy(chan->key_prot, s1);
+ strncpyz(chan->key_prot, s1, sizeof chan->key_prot);
}
break;
}
@@ -1528,10 +1528,9 @@ static void cmd_chanset(struct userrec *u, int idx, char *par)
parcpy = nmalloc(strlen(par) + 1);
strcpy(parcpy, par);
if (tcl_channel_modify(0, chan, 2, list) == TCL_OK) {
- strcat(answers, list[0]);
- strcat(answers, " { ");
- strcat(answers, parcpy);
- strcat(answers, " }");
+ char tocat[sizeof answers];
+ egg_snprintf(tocat, sizeof tocat, "%s { %s }", list[0], parcpy);
+ strncat(answers, tocat, sizeof answers - strlen(answers) - 1);
} else if (!all || !chan->next)
dprintf(idx, "Error trying to set %s for %s, invalid option\n",
list[0], all ? "all channels" : chname);
Oops, something went wrong.

0 comments on commit afdd3a5

Please sign in to comment.