From 2e7e7e5d6c4a9ef960f1e649af2a1176b837e9f9 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Lo=C3=AFc=20Gomez?= <bip@kyoshiro.org>
Date: Sun, 9 Jan 2022 21:20:49 +0100
Subject: [PATCH 06/24] sanitize: trivial fixes and casts to types expected by
 various functions

---
 src/bip.c        | 14 +++++---
 src/bip_main.c   |  4 +--
 src/connection.c | 23 +++++++++-----
 src/irc.c        | 35 ++++++++++----------
 src/irc.h        |  3 +-
 src/line.h       |  2 +-
 src/log.c        | 83 ++++++++++++++++++++++++------------------------
 src/md5.c        |  8 ++---
 src/util.c       |  6 ++--
 9 files changed, 94 insertions(+), 84 deletions(-)

diff --git a/src/bip.c b/src/bip.c
index 787ab09..f32024d 100644
--- a/src/bip.c
+++ b/src/bip.c
@@ -78,7 +78,7 @@ static void hash_binary(char *hex, unsigned char **password, unsigned int *seed)
 	if (strlen(hex) != 40)
 		fatal("Incorrect password format %s\n", hex);
 
-	md5 = bip_malloc(20);
+	md5 = bip_malloc((size_t) 20);
 	for (i = 0; i < 20; i++) {
 		sscanf(hex + 2 * i, "%02x", &buf);
 		md5[i] = buf;
@@ -174,7 +174,7 @@ try_again:
 	f = fopen(conf_pid_file, "r");
 	if (f)
 		goto pid_is_there;
-	if (gethostname(hname, 511) == -1)
+	if (gethostname(hname, (size_t)511) == -1)
 		fatal("%s %s", "gethostname", strerror(errno));
 	hname[511] = 0;
 	snprintf(longpath, longpath_max - 1, "%s.%s.%ld", conf_pid_file, hname,
@@ -298,7 +298,7 @@ static int add_network(bip_t *bip, list_t *data)
 		n->serverv = NULL;
 		n->serverc = 0;
 	} else {
-		n = bip_calloc(sizeof(struct network), 1);
+		n = bip_calloc(sizeof(struct network), (size_t)1);
 		hash_insert(&bip->networks, name, n);
 	}
 
@@ -526,6 +526,10 @@ static int add_connection(bip_t *bip, struct bipuser *user, list_t *data)
 				case LEX_BACKLOG:
 					ci->backlog = t2->ndata;
 					break;
+				default:
+					conf_die(bip, "Unknown keyword in channel block (%d)",
+							t2->type);
+					return 0;
 				}
 				if (t2->tuple_type == TUPLE_STR && t2->pdata)
 					free(t2->pdata);
@@ -693,7 +697,7 @@ static int add_user(bip_t *bip, list_t *data, struct historical_directives *hds)
 	}
 	u = hash_get(&bip->users, name);
 	if (!u) {
-		u = bip_calloc(sizeof(struct bipuser), 1);
+		u = bip_calloc(sizeof(struct bipuser), (size_t) 1);
 		hash_insert(&bip->users, name, u);
 		hash_init(&u->connections, HASH_NOCASE);
 		u->admin = 0;
@@ -1756,7 +1760,7 @@ void _bip_notify(struct link_client *ic, char *fmt, va_list ap)
 	else
 		nick = LINK(ic)->prev_nick;
 
-	vsnprintf(str, 4095, fmt, ap);
+	vsnprintf(str, (size_t)4095, fmt, ap);
 	str[4095] = 0;
 	WRITE_LINE2(CONN(ic), P_IRCMASK, (LINK(ic)->user->bip_use_notice ?
 				"NOTICE" : "PRIVMSG"), nick, str);
diff --git a/src/bip_main.c b/src/bip_main.c
index daa4b2c..d0a9dd5 100644
--- a/src/bip_main.c
+++ b/src/bip_main.c
@@ -98,7 +98,7 @@ static void log_file_setup(void)
 	if (conf_log_system && conf_daemonize) {
 		if (conf_global_log_file && conf_global_log_file != stderr)
 			fclose(conf_global_log_file);
-		snprintf(buf, 4095, "%s/bip.log", conf_log_root);
+		snprintf(buf, (size_t) 4095, "%s/bip.log", conf_log_root);
 		FILE *f = fopen(buf, "a");
 		if (!f)
 			fatal("Can't open %s: %s", buf, strerror(errno));
@@ -294,7 +294,7 @@ int main(int argc, char **argv)
 		pid = daemonize();
 	else
 		pid = getpid();
-	snprintf(buf, 29, "%ld\n", (long unsigned int)pid);
+	snprintf(buf, (size_t) 29, "%lu\n", (unsigned long int)pid);
 	write(fd, buf, strlen(buf));
 	close(fd);
 
diff --git a/src/connection.c b/src/connection.c
index 44a1e04..cf3471d 100644
--- a/src/connection.c
+++ b/src/connection.c
@@ -319,7 +319,12 @@ static int _write_socket(connection_t *cn, char *message)
 	 * Shitty: we might have written a partial line, so we hack the line...
 	 * Callers of _write_socket muse provide a writable message
 	 */
+// this might be the same
+#if EWOULDBLOCK == EAGAIN
+	if (errno == EAGAIN || errno == EINPROGRESS) {
+#else
 	if (errno == EAGAIN || errno == EWOULDBLOCK || errno == EINPROGRESS) {
+#endif
 		memmove(message, message + tcount, size - tcount + 1);
 		return WRITE_KEEP;
 	}
@@ -1101,8 +1106,8 @@ static connection_t *connection_init(int anti_flood, int ssl, int timeout,
 	char *incoming;
 	list_t *outgoing;
 
-	conn = (connection_t *)bip_calloc(sizeof(connection_t), 1);
-	incoming = (char *)bip_malloc(CONN_BUFFER_SIZE);
+	conn = (connection_t *)bip_calloc(sizeof(connection_t), (size_t)1);
+	incoming = (char *)bip_malloc((size_t)CONN_BUFFER_SIZE);
 	outgoing = list_new(NULL);
 
 	conn->anti_flood = anti_flood;
@@ -1260,7 +1265,7 @@ connection_t *listen_new(char *hostname, int port, int ssl)
 	connection_t *conn;
 	char portbuf[20];
 	/* TODO: allow litteral service name in the function interface */
-	if (snprintf(portbuf, 20, "%d", port) >= 20)
+	if (snprintf(portbuf, (size_t)20, "%d", port) >= 20)
 		portbuf[19] = '\0';
 
 	/*
@@ -1296,7 +1301,7 @@ static SSL_CTX *SSL_init_context(char *ciphers)
 		SSL_load_error_strings();
 		errbio = BIO_new_fp(conf_global_log_file, BIO_NOCLOSE);
 
-		ssl_cx_idx = SSL_get_ex_new_index(0, "bip connection_t",
+		ssl_cx_idx = SSL_get_ex_new_index((size_t)0, "bip connection_t",
 			NULL, NULL,NULL);
 
 		flags = O_RDONLY;
@@ -1309,7 +1314,7 @@ static SSL_CTX *SSL_init_context(char *ciphers)
 		}
 
 		do {
-			ret = read(fd, buf, 1024);
+			ret = read(fd, buf, (size_t)1024);
 			if (ret <= 0) {
 				mylog(LOG_ERROR,"/dev/random: %s",
 						strerror(errno));
@@ -1573,6 +1578,8 @@ static connection_t *_connection_new_SSL(char *dsthostname, char *dstport,
 		mylog(LOG_ERROR, "Can't open SSL certificate check store! Check path "
 				"and permissions.");
 		return conn;
+	default:
+		fatal("Unknown SSL cert check mode.");
 	}
 
 	switch (conn->ssl_check_mode) {
@@ -1639,10 +1646,10 @@ connection_t *connection_new(char *dsthostname, int dstport, char *srchostname,
 	(void)ssl_client_certfile;
 #endif
 	/* TODO: allow litteral service name in the function interface */
-	if (snprintf(dstportbuf, 20, "%d", dstport) >= 20)
+	if (snprintf(dstportbuf, (size_t)20, "%d", dstport) >= 20)
 		dstportbuf[19] = '\0';
 	if (srcport) {
-		if (snprintf(srcportbuf, 20, "%d", srcport) >= 20)
+		if (snprintf(srcportbuf, (size_t)20, "%d", srcport) >= 20)
 			srcportbuf[19] = '\0';
 		tmp = srcportbuf;
 	} else
@@ -1807,7 +1814,7 @@ static char *socket_ip(int fd, int remote)
 		return NULL;
 	}
 
-	ip = bip_malloc(65);
+	ip = bip_malloc((size_t)65);
 
 	switch (addr.sa_family) {
 	case AF_INET:
diff --git a/src/irc.c b/src/irc.c
index e816947..9fcfbed 100644
--- a/src/irc.c
+++ b/src/irc.c
@@ -81,7 +81,7 @@ static char *sasl_mechanism_to_text(int sasl_mechanism);
 struct channel *channel_new(const char *name)
 {
 	struct channel *chan;
-	chan = bip_calloc(sizeof(struct channel), 1);
+	chan = bip_calloc(sizeof(struct channel), (size_t)1);
 	chan->name = bip_strdup(name);
 	hash_init(&chan->ovmasks, HASH_NOCASE);
 	return chan;
@@ -113,7 +113,7 @@ list_t *channel_name_list(struct link_server *server, struct channel *c)
 	list_t *ret;
 	hash_iterator_t hi;
 	size_t len = 0;
-	char *str = bip_malloc(NAMESIZE + 1);
+	char *str = bip_malloc((size_t)(NAMESIZE + 1));
 
 	ret = list_new(NULL);
 	*str = 0;
@@ -126,7 +126,7 @@ list_t *channel_name_list(struct link_server *server, struct channel *c)
 
 		if (len + strlen(nick) + 2 + (ovmask ? 1 : 0) >= NAMESIZE) {
 			list_add_last(ret, str);
-			str = bip_malloc(NAMESIZE + 1);
+			str = bip_malloc((size_t)(NAMESIZE + 1));
 			*str = 0;
 			len = 0;
 		}
@@ -249,7 +249,7 @@ static void irc_server_connected(struct link_server *server)
 	list_iterator_t itocs;
 	for (list_it_init(&LINK(server)->on_connect_send, &itocs);
 				list_it_item(&itocs); list_it_next(&itocs)) {
-		ssize_t len = strlen(list_it_item(&itocs)) + 2;
+		size_t len = strlen(list_it_item(&itocs)) + 2;
 		char *str = bip_malloc(len + 1);
 		sprintf(str, "%s\r\n", (char *)list_it_item(&itocs));
 		write_line(CONN(server), str);
@@ -516,9 +516,9 @@ int irc_dispatch_server(bip_t *bip, struct link_server *server,
 				if (LINK(server)->ignore_server_capab &&
 						irc_line_elem_equals(line, i, "CAPAB"))
 					irc_line_drop(line, i);
-				else if (!strncmp(irc_line_elem(line, i), "CHANMODES=", 10))
+				else if (!strncmp(irc_line_elem(line, i), "CHANMODES=", (size_t)10))
 					server_set_chanmodes(server, irc_line_elem(line, i) + 10);
-				else if (!strncmp(irc_line_elem(line, i), "PREFIX=(", 8))
+				else if (!strncmp(irc_line_elem(line, i), "PREFIX=(", (size_t)8))
 					server_set_prefix(server, irc_line_elem(line, i) + 7);
 			}
 		}
@@ -1789,8 +1789,8 @@ static int irc_mode_channel(struct link_server *s, struct channel *channel,
 
 static char *irc_timestamp(void)
 {
-	char *ts = bip_malloc(21);
-	snprintf(ts, 20, "%ld", (long int)time(NULL));
+	char *ts = bip_malloc((size_t)23);
+	snprintf(ts, (size_t)22, "%ld", (long int)time(NULL));
 	return ts;
 }
 
@@ -2148,7 +2148,7 @@ static struct link_client *irc_accept_new(connection_t *conn)
 	if (!newconn)
 		return NULL;
 
-	ircc = bip_calloc(sizeof(struct link_client), 1);
+	ircc = bip_calloc(sizeof(struct link_client), (size_t)1);
 	CONN(ircc) = newconn;
 	TYPE(ircc) = IRC_TYPE_LOGGING_CLIENT;
 	CONN(ircc)->user_data = ircc;
@@ -2253,7 +2253,7 @@ struct link_client *irc_client_new(void)
 {
 	struct link_client *c;
 
-	c = bip_calloc(sizeof(struct link_client), 1);
+	c = bip_calloc(sizeof(struct link_client), (size_t)1);
 	list_init(&c->who_queue, list_ptr_cmp);
 
 	return c;
@@ -2263,7 +2263,7 @@ struct link_server *irc_server_new(struct link *link, connection_t *conn)
 {
 	struct link_server *s;
 
-	s = bip_calloc(sizeof(struct link_server), 1);
+	s = bip_calloc(sizeof(struct link_server), (size_t)1);
 
 	TYPE(s) = IRC_TYPE_SERVER;
 	hash_init(&s->channels, HASH_NOCASE);
@@ -2444,7 +2444,7 @@ void oidentd_dump(bip_t *bip)
 
 		content = (char *)bip_malloc(stats.st_size + 1);
 
-		if (fread(content, 1, stats.st_size, f) !=
+		if (fread(content, (size_t)1, stats.st_size, f) !=
 				(size_t)stats.st_size) {
 			mylog(LOG_WARN, "Can't read %s fully",
 					bip->oidentdpath);
@@ -2457,10 +2457,9 @@ void oidentd_dump(bip_t *bip)
 
 		bipstart = strstr(content, BIP_OIDENTD_START);
 		if (bipstart != NULL) {
-			/* We have some config left, rewrite the file
-			 * completely */
-			fseek(f, SEEK_SET, 0);
-			if (ftruncate(fileno(f), 0) == -1) {
+			// We have some config left, rewrite the file completely
+			fseek(f, (long)SEEK_SET, (int)0);
+			if (ftruncate(fileno(f), (off_t)0) == -1) {
 				mylog(LOG_DEBUG, "Can't reset %s size",
 						bip->oidentdpath);
 				free(content);
@@ -2755,7 +2754,7 @@ void irc_client_free(struct link_client *cli)
 struct link *irc_link_new(void)
 {
 	struct link *link;
-	link = bip_calloc(sizeof(struct link), 1);
+	link = bip_calloc(sizeof(struct link), (size_t)1);
 
 	link->l_server = NULL;
 	hash_init(&link->chan_infos, HASH_NOCASE);
@@ -2844,7 +2843,7 @@ static void server_set_chanmodes(struct link_server *l, const char *modes)
 			modes = cur + 1;
 		} else {
 			// emptry string
-			dup = bip_calloc(1, sizeof(char));
+			dup = bip_calloc((size_t)1, sizeof(char));
 		}
 		mylog(LOG_DEBUGVERB, "[%s] Modes: '%s'", LINK(l)->name, dup);
 		array_push(&l->chanmodes, dup);
diff --git a/src/irc.h b/src/irc.h
index 718f199..d9541a8 100644
--- a/src/irc.h
+++ b/src/irc.h
@@ -224,7 +224,7 @@ struct chan_info {
 	int backlog;
 };
 
-#define chan_info_new() bip_calloc(sizeof(struct chan_info), 1)
+#define chan_info_new() bip_calloc(sizeof(struct chan_info), (size_t)1)
 
 struct link_server {
 	struct link_connection _link_c;
@@ -273,7 +273,6 @@ struct link_server *irc_server_new(struct link *link, connection_t *conn);
 void irc_server_free(struct link_server *is);
 struct client *client_new(void);
 void irc_main(bip_t *);
-int ischannel(char p);
 void irc_client_close(struct link_client *);
 void irc_client_free(struct link_client *);
 struct link *irc_link_new(void);
diff --git a/src/line.h b/src/line.h
index 52a8d93..8431c64 100644
--- a/src/line.h
+++ b/src/line.h
@@ -84,7 +84,7 @@ struct line {
 
 void irc_line_init(struct line *l);
 void _irc_line_deinit(struct line *l);
-struct line *irc_line_new();
+struct line *irc_line_new(void);
 void irc_line_write(struct line *l, connection_t *c);
 void irc_line_append(struct line *l, const char *s);
 struct line *irc_line_new_from_string(char *str);
diff --git a/src/log.c b/src/log.c
index b6fdc55..fab405b 100644
--- a/src/log.c
+++ b/src/log.c
@@ -83,7 +83,7 @@ int check_dir_r(char *dirname)
 		int slash_ok = 1;
 		while (*tmp == '/') {
 			if (slash_ok) {
-				strncpy(dir + count, "/", 2);
+				strncpy(dir + count, "/", (size_t)2);
 				count++;
 				slash_ok = 0;
 			}
@@ -140,15 +140,15 @@ char *log_build_filename(log_t *logdata, const char *destination)
 	char *dest = bip_strdup(destination);
 
 	strtolower(dest);
-	logfile = (char *)bip_malloc(MAX_PATH_LEN + 1);
+	logfile = (char *)bip_malloc((size_t)MAX_PATH_LEN + 1);
 
 	time(&s);
 	now = localtime(&s);
-	strftime(year, 5, "%Y", now);
-	strftime(day, 3, "%d", now);
-	strftime(month, 3, "%m", now);
-	strftime(hour, 3, "%H", now);
-	snprintf(logfile, MAX_PATH_LEN, "%s/%s", conf_log_root,
+	strftime(year, (size_t)5, "%Y", now);
+	strftime(day, (size_t)3, "%d", now);
+	strftime(month, (size_t)3, "%m", now);
+	strftime(hour, (size_t)3, "%H", now);
+	snprintf(logfile, (size_t)MAX_PATH_LEN, "%s/%s", conf_log_root,
 			conf_log_format);
 	replace_var(logfile, "%u", logdata->user->name, MAX_PATH_LEN);
 	replace_var(logfile, "%n", logdata->network, MAX_PATH_LEN);
@@ -280,7 +280,7 @@ static int log_add_file(log_t *logdata, const char *destination,
 			return 0;
 		}
 
-		if (fseek(f, 0, SEEK_END) == -1) {
+		if (fseek(f, (long)0, SEEK_END) == -1) {
 			mylog(LOG_ERROR, "fseek(%s) %s", uniq_fname,
 					strerror(errno));
 			free(uniq_fname);
@@ -299,7 +299,7 @@ static int log_add_file(log_t *logdata, const char *destination,
 
 	store = hash_get(&logdata->logfgs, destination);
 	if (!store) {
-		store = bip_calloc(sizeof(logstore_t), 1);
+		store = bip_calloc(sizeof(logstore_t), (size_t)1);
 		list_init(&store->file_group, NULL);
 		store->name = bip_strdup(destination);
 		store->skip_advance = 0;
@@ -433,7 +433,7 @@ logstore_t *log_find_file(log_t *logdata, const char *destination)
 
 void log_join(log_t *logdata, const char *ircmask, const char *channel)
 {
-	snprintf(logdata->buffer, LOGLINE_MAXLEN,
+	snprintf(logdata->buffer, (size_t)LOGLINE_MAXLEN,
 			"%s -!- %s has joined %s", timestamp(), ircmask,
 			channel);
 	log_write(logdata, channel, logdata->buffer);
@@ -443,11 +443,11 @@ void log_part(log_t *logdata, const char *ircmask, const char *channel,
 		const char *message)
 {
 	if (message)
-		snprintf(logdata->buffer, LOGLINE_MAXLEN,
+		snprintf(logdata->buffer, (size_t)LOGLINE_MAXLEN,
 			"%s -!- %s has left %s [%s]", timestamp(), ircmask,
 			channel, message);
 	else
-		snprintf(logdata->buffer, LOGLINE_MAXLEN,
+		snprintf(logdata->buffer, (size_t)LOGLINE_MAXLEN,
 			"%s -!- %s has left %s", timestamp(), ircmask,
 			channel);
 	log_write(logdata, channel, logdata->buffer);
@@ -456,7 +456,7 @@ void log_part(log_t *logdata, const char *ircmask, const char *channel,
 void log_kick(log_t *logdata, const char *ircmask, const char *channel,
 		const char *who, const char *message)
 {
-	snprintf(logdata->buffer, LOGLINE_MAXLEN,
+	snprintf(logdata->buffer, (size_t)LOGLINE_MAXLEN,
 			"%s -!- %s has been kicked by %s [%s]", timestamp(),
 			who, ircmask, message);
 	log_write(logdata, channel, logdata->buffer);
@@ -465,7 +465,7 @@ void log_kick(log_t *logdata, const char *ircmask, const char *channel,
 void log_quit(log_t *logdata, const char *ircmask, const char *channel,
 		const char *message)
 {
-	snprintf(logdata->buffer, LOGLINE_MAXLEN,
+	snprintf(logdata->buffer, (size_t)LOGLINE_MAXLEN,
 			"%s -!- %s has quit [%s]", timestamp(), ircmask,
 			message);
 	log_write(logdata, channel, logdata->buffer);
@@ -487,7 +487,7 @@ void log_nick(log_t *logdata, const char *ircmask, const char *channel,
 	}
 	free(oldnick);
 
-	snprintf(logdata->buffer, LOGLINE_MAXLEN,
+	snprintf(logdata->buffer, (size_t)LOGLINE_MAXLEN,
 			"%s -!- %s is now known as %s",
 			timestamp(), ircmask, newnick);
 	log_write(logdata, channel, logdata->buffer);
@@ -514,16 +514,16 @@ static void do_log_privmsg(log_t *logdata, const char *storage, int src,
 		if (*message == '+' || *message == '-')
 			real_message++;
 
-		if (strncmp(real_message, "\001ACTION ", 8) != 0)
+		if (strncmp(real_message, "\001ACTION ", (size_t)8) != 0)
 			return;
 		msg = bip_strdup(real_message);
 		*(msg + strlen(msg) - 1) = '\0';
-		snprintf(logdata->buffer, LOGLINE_MAXLEN,
+		snprintf(logdata->buffer, (size_t)LOGLINE_MAXLEN,
 					"%s %c * %s %s", timestamp(), dir,
 					from, msg + 8);
 		free(msg);
 	} else {
-		snprintf(logdata->buffer, LOGLINE_MAXLEN,
+		snprintf(logdata->buffer, (size_t)LOGLINE_MAXLEN,
 					"%s %c %s: %s", timestamp(), dir,
 					from, message);
 	}
@@ -571,7 +571,7 @@ void log_cli_notice(log_t *logdata, const char *ircmask,
 void log_topic(log_t *logdata, const char *ircmask, const char *channel,
 		const char *message)
 {
-	snprintf(logdata->buffer, LOGLINE_MAXLEN,
+	snprintf(logdata->buffer, (size_t)LOGLINE_MAXLEN,
 			"%s -!- %s changed topic of %s to: %s", timestamp(),
 			ircmask, channel, message);
 	log_write(logdata, channel, logdata->buffer);
@@ -579,7 +579,7 @@ void log_topic(log_t *logdata, const char *ircmask, const char *channel,
 
 void log_init_topic(log_t *logdata, const char *channel, const char *message)
 {
-	snprintf(logdata->buffer, LOGLINE_MAXLEN,
+	snprintf(logdata->buffer, (size_t)LOGLINE_MAXLEN,
 			"%s -!- Topic for %s: %s", timestamp(), channel,
 			message);
 	log_write(logdata, channel, logdata->buffer);
@@ -594,13 +594,13 @@ void log_init_topic_time(log_t *logdata, const char *channel, const char *who,
 
 	seconds = atoi(when);
 	time = localtime(&seconds);
-	timestr = (char *)bip_malloc(50 + 1);
+	timestr = (char *)bip_malloc((size_t)50 + 1);
 	timestr[0] = '\0';
 	if (time)
-		strftime(timestr, 50, "%A %d %B %Y, %H:%M:%S", time);
+		strftime(timestr, (size_t)50, "%A %d %B %Y, %H:%M:%S", time);
 	timestr[50] = '\0';
 
-	snprintf(logdata->buffer, LOGLINE_MAXLEN,
+	snprintf(logdata->buffer, (size_t)LOGLINE_MAXLEN,
 			"%s -!- Topic set by %s [%s]", timestamp(), who,
 			timestr);
 	free(timestr);
@@ -611,15 +611,15 @@ void log_mode(log_t *logdata, const char *ircmask, const char *channel,
 		const char *modes, array_t *mode_args)
 {
 	int i;
-	char *tmpbuf = bip_malloc(LOGLINE_MAXLEN + 1);
-	char *tmpbuf2 = bip_malloc(LOGLINE_MAXLEN + 1);
+	char *tmpbuf = bip_malloc((size_t)LOGLINE_MAXLEN + 1);
+	char *tmpbuf2 = bip_malloc((size_t)LOGLINE_MAXLEN + 1);
 	char *tmp;
 
-	snprintf(tmpbuf, LOGLINE_MAXLEN, "%s -!- mode/%s [%s", timestamp(),
-			channel, modes);
+	snprintf(tmpbuf, (size_t)LOGLINE_MAXLEN,
+			"%s -!- mode/%s [%s", timestamp(), channel, modes);
 	if (mode_args) {
 		for (i = 0; i < array_count(mode_args); i++) {
-			snprintf(tmpbuf2, LOGLINE_MAXLEN, "%s %s", tmpbuf,
+			snprintf(tmpbuf2, (size_t)LOGLINE_MAXLEN, "%s %s", tmpbuf,
 					(char *)array_get(mode_args, i));
 			tmp = tmpbuf;
 			tmpbuf = tmpbuf2;
@@ -627,7 +627,8 @@ void log_mode(log_t *logdata, const char *ircmask, const char *channel,
 		}
 	}
 
-	snprintf(logdata->buffer, LOGLINE_MAXLEN, "%s] by %s", tmpbuf, ircmask);
+	snprintf(logdata->buffer, (size_t)LOGLINE_MAXLEN, "%s] by %s",
+			tmpbuf, ircmask);
 	log_write(logdata, channel, logdata->buffer);
 
 	free(tmpbuf);
@@ -637,7 +638,7 @@ void log_mode(log_t *logdata, const char *ircmask, const char *channel,
 void log_disconnected(log_t *logdata)
 {
 	hash_iterator_t hi;
-	snprintf(logdata->buffer, LOGLINE_MAXLEN, "%s -!- Disconnected"
+	snprintf(logdata->buffer, (size_t)LOGLINE_MAXLEN, "%s -!- Disconnected"
 			" from server...", timestamp());
 	for (hash_it_init(&logdata->logfgs, &hi); hash_it_item(&hi);
 			hash_it_next(&hi))
@@ -649,7 +650,7 @@ void log_ping_timeout(log_t *logdata)
 	list_t *l = log_backlogs(logdata);
 	char *s;
 
-	snprintf(logdata->buffer, LOGLINE_MAXLEN,
+	snprintf(logdata->buffer, (size_t)LOGLINE_MAXLEN,
 			"%s -!- Ping timeout with server...", timestamp());
 	while ((s = list_remove_first(l))) {
 		log_write(logdata, s, logdata->buffer);
@@ -663,7 +664,7 @@ void log_connected(log_t *logdata)
 {
 	hash_iterator_t hi;
 
-	snprintf(logdata->buffer, LOGLINE_MAXLEN, "%s -!- Connected to"
+	snprintf(logdata->buffer, (size_t)LOGLINE_MAXLEN, "%s -!- Connected to"
 			" server...", timestamp());
 	for (hash_it_init(&logdata->logfgs, &hi); hash_it_item(&hi);
 			hash_it_next(&hi)) {
@@ -860,7 +861,7 @@ char *log_beautify(log_t *logdata, const char *buf, const char *storename,
 	lots = p - sots;
 	p++;
 
-	if (strncmp(p, "-!-", 3) == 0) {
+	if (strncmp(p, "-!-", (size_t)3) == 0) {
 		if (logdata->user->bl_msg_only)
 			return NULL;
 		else
@@ -1005,7 +1006,7 @@ static int log_backread_file(log_t *log, logstore_t *store, logfile_t *lf,
 		}
 	} else {
 		//mylog(LOG_ERROR, "bread Seeking %s to %d", lf->filename, 0);
-		if (fseek(lf->file, 0, SEEK_SET)) {
+		if (fseek(lf->file, (long)0, SEEK_SET)) {
 			mylog(LOG_ERROR, "Can't seek in %s", lf->filename);
 			list_add_last(res, _log_wrap(store->name,
 						"Error seeking in logfile"));
@@ -1013,7 +1014,7 @@ static int log_backread_file(log_t *log, logstore_t *store, logfile_t *lf,
 		}
 	}
 
-	buf = bip_malloc(LOGLINE_MAXLEN + 1);
+	buf = bip_malloc((size_t)LOGLINE_MAXLEN + 1);
 	for(;;) {
 		if (!fgets(buf, LOGLINE_MAXLEN, lf->file)) {
 			if (ferror(lf->file)) {
@@ -1108,8 +1109,8 @@ static char *_log_wrap(const char *dest, const char *line)
 	char *buf;
 	size_t count;
 
-	buf = bip_malloc(LOGLINE_MAXLEN + 1);
-	count = snprintf(buf, LOGLINE_MAXLEN + 1,
+	buf = bip_malloc((size_t)LOGLINE_MAXLEN + 1);
+	count = snprintf(buf, (size_t)LOGLINE_MAXLEN + 1,
 			":" P_IRCMASK " PRIVMSG %s :%s\r\n", dest, line);
 	if (count >= LOGLINE_MAXLEN + 1) {
 		mylog(LOG_DEBUG, "line too long");
@@ -1127,7 +1128,7 @@ static int _log_write(log_t *logdata, logstore_t *store,
 	size_t len;
 	static char tmpstr[LOGLINE_MAXLEN + 1];
 
-	strncpy(tmpstr, str, LOGLINE_MAXLEN);
+	strncpy(tmpstr, str, (size_t)LOGLINE_MAXLEN);
 	tmpstr[LOGLINE_MAXLEN] = 0;
 
 	if (store->memlog) {
@@ -1149,7 +1150,7 @@ static int _log_write(log_t *logdata, logstore_t *store,
 
 	len = strlen(tmpstr);
 	nbwrite = fwrite(tmpstr, sizeof(char), len, lf->file);
-	nbwrite += fwrite("\n", sizeof(char), 1, lf->file);
+	nbwrite += fwrite("\n", sizeof(char), (size_t)1, lf->file);
 	log_updatelast(lf);
 	if (nbwrite != len + 1)
 		mylog(LOG_ERROR, "Error writing to %s logfile", lf->filename);
@@ -1202,11 +1203,11 @@ log_t *log_new(struct bipuser *user, const char *network)
 {
 	log_t *logdata;
 
-	logdata = (log_t *)bip_calloc(sizeof(log_t), 1);
+	logdata = (log_t *)bip_calloc(sizeof(log_t), (size_t)1);
 	logdata->user = user;
 	logdata->network = bip_strdup(network);
 	hash_init(&logdata->logfgs, HASH_NOCASE);
-	logdata->buffer = (char *)bip_malloc(LOGLINE_MAXLEN + 1);
+	logdata->buffer = (char *)bip_malloc((size_t)LOGLINE_MAXLEN + 1);
 	logdata->buffer[LOGLINE_MAXLEN - 1] = 0; // debug
 	logdata->buffer[LOGLINE_MAXLEN] = 0;
 	logdata->connected = 0;
diff --git a/src/md5.c b/src/md5.c
index 8f4876b..5ea136b 100644
--- a/src/md5.c
+++ b/src/md5.c
@@ -238,7 +238,7 @@ void md5_finish( md5_context *ctx, uint8 digest[16] )
     padn = ( last < 56 ) ? ( 56 - last ) : ( 120 - last );
 
     md5_update( ctx, md5_padding, padn );
-    md5_update( ctx, msglen, 8 );
+    md5_update( ctx, msglen, (unsigned long)8 );
 
     PUT_UINT32( ctx->state[0], digest,  0 );
     PUT_UINT32( ctx->state[1], digest,  4 );
@@ -365,15 +365,15 @@ unsigned char *chash_double(char *str, unsigned int seed)
 	ptr[3] = seed & 0xff;
 	memcpy(ptr + 4, str, length - 4);
 
-	md5 = bip_malloc(16 + 4);
-	memcpy(md5, ptr, 4);
+	md5 = bip_malloc((size_t)16 + 4);
+	memcpy(md5, ptr, (size_t)4);
 
 	md5_starts(&ctx);
 	md5_update(&ctx, ptr, length);
 	md5_finish(&ctx, md5 + 4);
 
 	md5_starts(&ctx);
-	md5_update(&ctx, md5, 20);
+	md5_update(&ctx, md5, (unsigned long)20);
 	md5_finish(&ctx, md5 + 4);
 	free(ptr);
 	return md5;
diff --git a/src/util.c b/src/util.c
index 45206e7..61b67a1 100644
--- a/src/util.c
+++ b/src/util.c
@@ -35,7 +35,7 @@ void memory_fatal(void)
 {
 	fflush(conf_global_log_file);
 #define OOMMSG "Out of memory.\n"
-	fwrite(OOMMSG, 1, strlen(OOMMSG), conf_global_log_file);
+	fwrite(OOMMSG, (size_t)1, strlen(OOMMSG), conf_global_log_file);
 #undef OOMMSG
 	fflush(conf_global_log_file);
 	exit(28);
@@ -198,7 +198,7 @@ char *timestamp(void)
 	time(&tv);
 	tm = localtime(&tv);
 
-	strftime(ts, 20, "%d-%m-%Y %H:%M:%S", tm);
+	strftime(ts, (size_t)20, "%d-%m-%Y %H:%M:%S", tm);
 	return ts;
 }
 
@@ -211,7 +211,7 @@ char *hrtime(time_t s)
 		return "never";
 	tm = localtime(&s);
 
-	strftime(ts, 20, "%d-%m-%Y %H:%M:%S", tm);
+	strftime(ts, (size_t)20, "%d-%m-%Y %H:%M:%S", tm);
 	return ts;
 }
 
-- 
2.30.2

