use proper types, safe casts (mostly size_t)

This commit is contained in:
Loïc Gomez 2022-01-09 20:59:59 +01:00 committed by Pierre-Louis Bonicoli
parent 94fe272018
commit dd5343b710
Signed by: pilou
GPG Key ID: 06914C4A5EDAA6DD
8 changed files with 112 additions and 86 deletions

View File

@ -136,7 +136,7 @@ extern int yyparse(void);
void conf_die(bip_t *bip, char *fmt, ...) void conf_die(bip_t *bip, char *fmt, ...)
{ {
va_list ap; va_list ap;
int size = ERRBUFSZ; size_t size = ERRBUFSZ;
int n; int n;
char *error = bip_malloc(size); char *error = bip_malloc(size);
@ -144,12 +144,12 @@ void conf_die(bip_t *bip, char *fmt, ...)
va_start(ap, fmt); va_start(ap, fmt);
n = vsnprintf(error, size, fmt, ap); n = vsnprintf(error, size, fmt, ap);
va_end(ap); va_end(ap);
if (n > -1 && n < size) { if (n > -1 && (unsigned int) n < size) {
list_add_last(&bip->errors, error); list_add_last(&bip->errors, error);
break; break;
} }
if (n > -1) if (n > -1)
size = n + 1; size = (unsigned int) n + 1;
else else
size *= 2; size *= 2;
error = bip_realloc(error, size); error = bip_realloc(error, size);
@ -167,7 +167,7 @@ int do_pid_stuff(void)
FILE *f; FILE *f;
int fd; int fd;
// size is conf_pid_file + hname max + %ld max + two '.'. // size is conf_pid_file + hname max + %ld max + two '.'.
int longpath_max = strlen(conf_pid_file) + 512 + 3 + 20; size_t longpath_max = strlen(conf_pid_file) + 512 + 3 + 20;
char *longpath = bip_malloc(longpath_max + 1); char *longpath = bip_malloc(longpath_max + 1);
try_again: try_again:
@ -1835,7 +1835,7 @@ void set_on_connect_send(struct link_client *ic, char *val)
#define ON_CONNECT_MAX_STRSIZE 1024 #define ON_CONNECT_MAX_STRSIZE 1024
void adm_on_connect_send(struct link_client *ic, struct line *line, void adm_on_connect_send(struct link_client *ic, struct line *line,
unsigned int privmsg) int privmsg)
{ {
size_t remaining = ON_CONNECT_MAX_STRSIZE; size_t remaining = ON_CONNECT_MAX_STRSIZE;
char buf[ON_CONNECT_MAX_STRSIZE]; char buf[ON_CONNECT_MAX_STRSIZE];
@ -1856,12 +1856,12 @@ void adm_on_connect_send(struct link_client *ic, struct line *line,
for (i = privmsg + 2; i < irc_line_count(line); i++) { for (i = privmsg + 2; i < irc_line_count(line); i++) {
mylog(LOG_DEBUG, "[%s] processing item %d, remaining %ld, %s", mylog(LOG_DEBUG, "[%s] processing item %d, remaining %ld, %s",
LINK(ic)->user->name, i, remaining, buf); LINK(ic)->user->name, i, remaining, buf);
if ((unsigned int)i > privmsg + 2) if (i > privmsg + 2)
bufpos = bip_strcatf_fit(&remaining, bufpos, " %s", bufpos = bip_strcatf_fit(&remaining, bufpos, " %s",
irc_line_elem(line, i)); irc_line_elem(line, i));
else else
bufpos = bip_strcat_fit(&remaining, bufpos, bufpos = bip_strcat_fit(&remaining, bufpos,
(char *)irc_line_elem(line, i)); irc_line_elem(line, i));
mylog(LOG_DEBUG, "[%s] processed item %d, remaining %ld, %s", mylog(LOG_DEBUG, "[%s] processed item %d, remaining %ld, %s",
LINK(ic)->user->name, i, remaining, buf); LINK(ic)->user->name, i, remaining, buf);
if (!bufpos) { if (!bufpos) {
@ -2009,7 +2009,7 @@ int adm_bip(bip_t *bip, struct link_client *ic, struct line *line, int privmsg)
if (privmsg) { if (privmsg) {
char *linestr, *elemstr; char *linestr, *elemstr;
char *ptr, *eptr; char *ptr, *eptr;
int slen; size_t slen;
if (irc_line_count(line) != 3) if (irc_line_count(line) != 3)
return OK_FORGET; return OK_FORGET;
@ -2021,7 +2021,10 @@ int adm_bip(bip_t *bip, struct link_client *ic, struct line *line, int privmsg)
elemstr = bip_malloc(strlen(linestr) + 1); elemstr = bip_malloc(strlen(linestr) + 1);
while((eptr = strstr(ptr, " "))) { while((eptr = strstr(ptr, " "))) {
slen = eptr - ptr; // eptr is either >= ptr or NULL from strstr()
// but it can't be NULL per while loop
// we can then assume slen is unsigned
slen = (size_t)(eptr - ptr);
if (slen == 0) { if (slen == 0) {
ptr++; ptr++;
continue; continue;
@ -2031,8 +2034,8 @@ int adm_bip(bip_t *bip, struct link_client *ic, struct line *line, int privmsg)
irc_line_append(line, elemstr); irc_line_append(line, elemstr);
ptr = eptr + 1; ptr = eptr + 1;
} }
eptr = ptr + strlen(ptr); slen = strlen(ptr);
slen = eptr - ptr; eptr = ptr + slen;
if (slen != 0) { if (slen != 0) {
memcpy(elemstr, ptr, slen); memcpy(elemstr, ptr, slen);
elemstr[slen] = 0; elemstr[slen] = 0;

View File

@ -207,7 +207,8 @@ static X509 *mySSL_get_cert(SSL *ssl)
static int _write_socket_SSL(connection_t *cn, char* message) static int _write_socket_SSL(connection_t *cn, char* message)
{ {
int count, size; int count;
size_t size;
size = sizeof(char)*strlen(message); size = sizeof(char)*strlen(message);
@ -305,7 +306,7 @@ static int _write_socket(connection_t *cn, char *message)
count = write(cn->handle, ((const char *)message) + tcount, count = write(cn->handle, ((const char *)message) + tcount,
size - tcount); size - tcount);
if (count > 0) { if (count > 0) {
tcount += count; tcount += (size_t)count;
if (tcount == size) if (tcount == size)
return WRITE_OK; return WRITE_OK;
} }
@ -806,7 +807,9 @@ static int cn_want_write(connection_t *cn)
* cn->lasttoken is the number of milliseconds when we * cn->lasttoken is the number of milliseconds when we
* last added a token to the bucket */ * last added a token to the bucket */
if (!clock_gettime(CLOCK_MONOTONIC, &tv)) { if (!clock_gettime(CLOCK_MONOTONIC, &tv)) {
now = tv.tv_sec * 1000 + tv.tv_nsec / 1000000; if (tv.tv_sec < 0 || tv.tv_nsec < 0)
fatal("clock_gettime returned negative time");
now = (unsigned long)(tv.tv_sec * 1000 + tv.tv_nsec / 1000000);
/* round now to TOKEN_INTERVAL multiple */ /* round now to TOKEN_INTERVAL multiple */
now -= now % TOKEN_INTERVAL; now -= now % TOKEN_INTERVAL;
if (now < cn->lasttoken) { if (now < cn->lasttoken) {
@ -814,8 +817,8 @@ static int cn_want_write(connection_t *cn)
cn->token = 1; cn->token = 1;
cn->lasttoken = now; cn->lasttoken = now;
} else if (now > cn->lasttoken + TOKEN_INTERVAL) { } else if (now > cn->lasttoken + TOKEN_INTERVAL) {
cn->token += (now - cn->lasttoken) / cn->token += (unsigned)((now - cn->lasttoken) /
TOKEN_INTERVAL; TOKEN_INTERVAL);
if (cn->token > TOKEN_MAX) if (cn->token > TOKEN_MAX)
cn->token = TOKEN_MAX; cn->token = TOKEN_MAX;
if (!cn->token) if (!cn->token)
@ -838,7 +841,7 @@ static int cn_want_write(connection_t *cn)
} }
list_t *wait_event(list_t *cn_list, int *msec, int *nc) list_t *wait_event(list_t *cn_list, time_t *msec, int *nc)
{ {
fd_set fds_read, fds_write, fds_except; fd_set fds_read, fds_write, fds_except;
int maxfd = -1, err, errtime; int maxfd = -1, err, errtime;
@ -1067,7 +1070,7 @@ static void create_listening_socket(char *hostname, char *port,
if (setsockopt(cn->handle, SOL_SOCKET, SO_REUSEADDR, if (setsockopt(cn->handle, SOL_SOCKET, SO_REUSEADDR,
(char *)&multi_client, (char *)&multi_client,
sizeof(multi_client)) < 0) { (socklen_t)sizeof(multi_client)) < 0) {
mylog(LOG_WARN, "setsockopt() : %s", strerror(errno)); mylog(LOG_WARN, "setsockopt() : %s", strerror(errno));
close(cn->handle); close(cn->handle);
cn->handle = -1; cn->handle = -1;
@ -1100,7 +1103,7 @@ static void create_listening_socket(char *hostname, char *port,
cn->connected = CONN_ERROR; cn->connected = CONN_ERROR;
} }
static connection_t *connection_init(int anti_flood, int ssl, int timeout, static connection_t *connection_init(int anti_flood, int ssl, time_t timeout,
int listen) int listen)
{ {
connection_t *conn; connection_t *conn;
@ -1142,7 +1145,7 @@ static int ctx_set_dh(SSL_CTX *ctx)
/* Return ephemeral DH parameters. */ /* Return ephemeral DH parameters. */
DH *dh = NULL; DH *dh = NULL;
FILE *f; FILE *f;
int ret; long ret;
if ((f = fopen(conf_client_dh_file, "r")) == NULL) { if ((f = fopen(conf_client_dh_file, "r")) == NULL) {
mylog(LOG_ERROR, "Unable to open DH parameters (%s): %s", mylog(LOG_ERROR, "Unable to open DH parameters (%s): %s",
@ -1273,14 +1276,14 @@ connection_t *listen_new(char *hostname, int port, int ssl)
* SSL flag is only here to tell program to convert socket to SSL after * SSL flag is only here to tell program to convert socket to SSL after
* accept(). Listening socket will NOT be SSL * accept(). Listening socket will NOT be SSL
*/ */
conn = connection_init(0, ssl, 0, 1); conn = connection_init(0, ssl, (time_t)0, 1);
create_listening_socket(hostname, portbuf, conn); create_listening_socket(hostname, portbuf, conn);
return conn; return conn;
} }
static connection_t *_connection_new(char *dsthostname, char *dstport, static connection_t *_connection_new(char *dsthostname, char *dstport,
char *srchostname, char *srcport, int timeout) char *srchostname, char *srcport, time_t timeout)
{ {
connection_t *conn; connection_t *conn;
@ -1293,7 +1296,8 @@ static connection_t *_connection_new(char *dsthostname, char *dstport,
#ifdef HAVE_LIBSSL #ifdef HAVE_LIBSSL
static SSL_CTX *SSL_init_context(char *ciphers) static SSL_CTX *SSL_init_context(char *ciphers)
{ {
int fd, flags, ret, rng; int fd, flags, rng;
ssize_t ret;
char buf[1025]; char buf[1025];
SSL_CTX *ctx; SSL_CTX *ctx;
@ -1323,7 +1327,7 @@ static SSL_CTX *SSL_init_context(char *ciphers)
} }
mylog(LOG_DEBUG, "PRNG seeded with %d /dev/random " mylog(LOG_DEBUG, "PRNG seeded with %d /dev/random "
"bytes", ret); "bytes", ret);
RAND_seed(buf, ret); RAND_seed(buf, (int)ret);
} while (!(rng = RAND_status())); } while (!(rng = RAND_status()));
prng_end: prng_end:
@ -1427,7 +1431,7 @@ static int bip_ssl_verify_callback(int preverify_ok, X509_STORE_CTX *ctx)
if (!result) { if (!result) {
/* We have a verify error! Log it */ /* We have a verify error! Log it */
mylog(LOG_ERROR, "SSL cert check failed at depth=%d: %s (%d)", mylog(LOG_ERROR, "SSL cert check failed at depth=%d: %s (%d)",
depth, X509_verify_cert_error_string(err), err); depth, X509_verify_cert_error_string((long)err), err);
} }
return result; return result;
@ -1436,6 +1440,7 @@ static int bip_ssl_verify_callback(int preverify_ok, X509_STORE_CTX *ctx)
static int SSLize(connection_t *cn, int *nc) static int SSLize(connection_t *cn, int *nc)
{ {
int err, err2; int err, err2;
long errl;
if (cn == NULL) if (cn == NULL)
return 1; return 1;
@ -1462,7 +1467,7 @@ static int SSLize(connection_t *cn, int *nc)
if (err2 == SSL_ERROR_NONE) { if (err2 == SSL_ERROR_NONE) {
const SSL_CIPHER *cipher; const SSL_CIPHER *cipher;
char buf[128]; char buf[128];
int len; size_t len;
cipher = SSL_get_current_cipher(cn->ssl_h); cipher = SSL_get_current_cipher(cn->ssl_h);
SSL_CIPHER_description(cipher, buf, 128); SSL_CIPHER_description(cipher, buf, 128);
@ -1481,17 +1486,17 @@ static int SSLize(connection_t *cn, int *nc)
case SSL_CHECK_NONE: case SSL_CHECK_NONE:
break; break;
case SSL_CHECK_BASIC: case SSL_CHECK_BASIC:
if((err = SSL_get_verify_result(cn->ssl_h)) != X509_V_OK) { if((errl = SSL_get_verify_result(cn->ssl_h)) != X509_V_OK) {
mylog(LOG_ERROR, "Certificate check failed: %s (%d)!", mylog(LOG_ERROR, "Certificate check failed: %s (%ld)!",
X509_verify_cert_error_string(err), err); X509_verify_cert_error_string(errl), errl);
cn->connected = CONN_UNTRUSTED; cn->connected = CONN_UNTRUSTED;
return 1; return 1;
} }
break; break;
case SSL_CHECK_CA: case SSL_CHECK_CA:
if((err = SSL_get_verify_result(cn->ssl_h)) != X509_V_OK) { if((errl = SSL_get_verify_result(cn->ssl_h)) != X509_V_OK) {
mylog(LOG_ERROR, "Certificate check failed: %s (%d)!", mylog(LOG_ERROR, "Certificate check failed: %s (%ld)!",
X509_verify_cert_error_string(err), err); X509_verify_cert_error_string(errl), errl);
cn->connected = CONN_UNTRUSTED; cn->connected = CONN_UNTRUSTED;
return 1; return 1;
} }
@ -1521,7 +1526,7 @@ static int SSLize(connection_t *cn, int *nc)
static connection_t *_connection_new_SSL(char *dsthostname, char *dstport, static connection_t *_connection_new_SSL(char *dsthostname, char *dstport,
char *srchostname, char *srcport, char *ciphers, int check_mode, char *srchostname, char *srcport, char *ciphers, int check_mode,
char *check_store, char *ssl_client_certfile, int timeout) char *check_store, char *ssl_client_certfile, time_t timeout)
{ {
connection_t *conn; connection_t *conn;
@ -1641,7 +1646,7 @@ static connection_t *_connection_new_SSL(char *dsthostname, char *dstport,
connection_t *connection_new(char *dsthostname, int dstport, char *srchostname, connection_t *connection_new(char *dsthostname, int dstport, char *srchostname,
int srcport, int ssl, char *ssl_ciphers, int ssl_check_mode, int srcport, int ssl, char *ssl_ciphers, int ssl_check_mode,
char *ssl_check_store, char *ssl_client_certfile, int timeout) char *ssl_check_store, char *ssl_client_certfile, time_t timeout)
{ {
char dstportbuf[20], srcportbuf[20], *tmp; char dstportbuf[20], srcportbuf[20], *tmp;
#ifndef HAVE_LIBSSL #ifndef HAVE_LIBSSL
@ -1727,7 +1732,7 @@ int main(int argc,char* argv[])
fprintf(stderr,"Usage: %s host port\n",argv[0]); fprintf(stderr,"Usage: %s host port\n",argv[0]);
exit(1); exit(1);
} }
conn = connection_init(0, 0, 0, 1); conn = connection_init(0, 0, (time_t)0, 1);
conn->connect_time = time(NULL); conn->connect_time = time(NULL);
create_listening_socket(argv[1],argv[2],&conn); create_listening_socket(argv[1],argv[2],&conn);
if (s == -1) { if (s == -1) {
@ -1755,41 +1760,41 @@ int main(int argc,char* argv[])
} }
#endif #endif
int connection_localport(connection_t *cn) uint16_t connection_localport(connection_t *cn)
{ {
struct sockaddr_in addr; struct sockaddr_in addr;
int err; int err;
socklen_t addrlen; socklen_t addrlen;
if (cn->handle <= 0) if (cn->handle <= 0)
return -1; return 0;
addrlen = sizeof(addr); addrlen = sizeof(addr);
err = getsockname(cn->handle, (struct sockaddr *)&addr, &addrlen); err = getsockname(cn->handle, (struct sockaddr *)&addr, &addrlen);
if (err != 0) { if (err != 0) {
mylog(LOG_ERROR, "in getsockname(%d): %s", cn->handle, mylog(LOG_ERROR, "in getsockname(%d): %s", cn->handle,
strerror(errno)); strerror(errno));
return -1; return 0;
} }
return ntohs(addr.sin_port); return ntohs(addr.sin_port);
} }
int connection_remoteport(connection_t *cn) uint16_t connection_remoteport(connection_t *cn)
{ {
struct sockaddr_in addr; struct sockaddr_in addr;
int err; int err;
socklen_t addrlen; socklen_t addrlen;
if (cn->handle <= 0) if (cn->handle <= 0)
return -1; return 0;
addrlen = sizeof(addr); addrlen = sizeof(addr);
err = getpeername(cn->handle, (struct sockaddr *)&addr, &addrlen); err = getpeername(cn->handle, (struct sockaddr *)&addr, &addrlen);
if (err != 0) { if (err != 0) {
mylog(LOG_ERROR, "in getpeername(%d): %s", cn->handle, mylog(LOG_ERROR, "in getpeername(%d): %s", cn->handle,
strerror(errno)); strerror(errno));
return -1; return 0;
} }
return ntohs(addr.sin_port); return ntohs(addr.sin_port);

View File

@ -2,7 +2,8 @@
* $Id: connection.h,v 1.40 2005/04/12 19:34:35 nohar Exp $ * $Id: connection.h,v 1.40 2005/04/12 19:34:35 nohar Exp $
* *
* This file is part of the bip project * This file is part of the bip project
* Copyright (C) 2004 2005 Arnaud Cornet and Loïc Gomez * Copyright (C) 2004,2005 Arnaud Cornet
* Copyright (C) 2004,2005,2022 Loïc Gomez
* *
* This program is free software; you can redistribute it and/or modify * This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by * it under the terms of the GNU General Public License as published by
@ -92,7 +93,7 @@ typedef struct connection {
connection_t *connection_new(char *dsthostname, int dstport, char *srchostname, connection_t *connection_new(char *dsthostname, int dstport, char *srchostname,
int srcport, int ssl, char *ssl_ciphers, int ssl_check_mode, int srcport, int ssl, char *ssl_ciphers, int ssl_check_mode,
char *ssl_check_store, char *ssl_client_certfile, int timeout); char *ssl_check_store, char *ssl_client_certfile, time_t timeout);
connection_t *listen_new(char *hostname, int port, int ssl); connection_t *listen_new(char *hostname, int port, int ssl);
connection_t *accept_new(connection_t *cn); connection_t *accept_new(connection_t *cn);
void connection_free(connection_t *cn); void connection_free(connection_t *cn);
@ -102,13 +103,13 @@ void write_line(connection_t *cn, char *line);
void write_lines(connection_t *cn, list_t *lines); void write_lines(connection_t *cn, list_t *lines);
void write_line_fast(connection_t *cn, char *line); void write_line_fast(connection_t *cn, char *line);
list_t *read_lines(connection_t *cn, int *error); list_t *read_lines(connection_t *cn, int *error);
list_t *wait_event(list_t *cn_list, int *msec, int *nc); list_t *wait_event(list_t *cn_list, time_t *msec, int *nc);
int cn_is_connected(connection_t *cn); int cn_is_connected(connection_t *cn);
int cn_is_listening(connection_t *cn); int cn_is_listening(connection_t *cn);
int connection_localport(connection_t *cn); uint16_t connection_localport(connection_t *cn);
int connection_remoteport(connection_t *cn); uint16_t connection_remoteport(connection_t *cn);
char *connection_localip(connection_t *cn); char *connection_localip(connection_t *cn);
char *connection_remoteip(connection_t *cn); char *connection_remoteip(connection_t *cn);
#endif #endif

View File

@ -54,7 +54,7 @@ static void server_set_chanmodes(struct link_server *l, const char *chanmodes);
static void server_set_prefix(struct link_server *l, const char *prefix); static void server_set_prefix(struct link_server *l, const char *prefix);
static void server_init_modes(struct link_server *s); static void server_init_modes(struct link_server *s);
static int bip_get_index(const char* str, char car); static int bip_get_index(const char* str, char car);
static int bip_fls(int v); static int bip_fls(long v);
void oidentd_dump(bip_t *bip); void oidentd_dump(bip_t *bip);
@ -100,7 +100,7 @@ char *nick_from_ircmask(const char *mask)
nick++; nick++;
if (!*nick) if (!*nick)
return bip_strdup(mask); return bip_strdup(mask);
len = nick - mask; len = (size_t)(nick - mask); // cannot be < 0
ret = bip_malloc(len + 1); ret = bip_malloc(len + 1);
memcpy(ret, mask, len); memcpy(ret, mask, len);
ret[len] = 0; ret[len] = 0;
@ -492,8 +492,8 @@ int irc_dispatch_server(bip_t *bip, struct link_server *server,
newnick[7] = '`'; newnick[7] = '`';
} }
} else { } else {
newnick[8] = rand() * newnick[8] = (char)('a' + ('z' - 'a') *
('z' - 'a') / RAND_MAX + 'a'; rand() / RAND_MAX);
} }
newnick[9] = 0; newnick[9] = 0;
} }
@ -1124,7 +1124,7 @@ static int irc_cli_join(struct link_client *irc, struct line *line)
ks = NULL; ks = NULL;
while ((e = strchr(s, ','))) { while ((e = strchr(s, ','))) {
size_t len = e - s; size_t len = (size_t)(e - s); // cannot be < 0 or NULL per while
char *p = bip_malloc(len + 1); char *p = bip_malloc(len + 1);
size_t klen; size_t klen;
char *kp = NULL; char *kp = NULL;
@ -1136,7 +1136,7 @@ static int irc_cli_join(struct link_client *irc, struct line *line)
ke = strchr(ks, ','); ke = strchr(ks, ',');
if (!ke) if (!ke)
ke = ks + strlen(ks); ke = ks + strlen(ks);
klen = ke - ks; klen = (size_t)(ke - ks);
kp = bip_malloc(klen + 1); kp = bip_malloc(klen + 1);
memcpy(kp, ks, klen); memcpy(kp, ks, klen);
kp[klen] = 0; kp[klen] = 0;
@ -1496,7 +1496,7 @@ static int irc_353(struct link_server *server, struct line *line)
while (*eon && *eon != ' ') while (*eon && *eon != ' ')
eon++; eon++;
len = eon - names; len = (size_t)(eon - names); // cannot be < 0
nick = bip_malloc(len + 1); nick = bip_malloc(len + 1);
memcpy(nick, names, len); memcpy(nick, names, len);
nick[len] = 0; nick[len] = 0;
@ -1623,7 +1623,7 @@ static int irc_part(struct link_server *server, struct line *line)
static void mode_add_letter_uniq(struct link_server *s, char c) static void mode_add_letter_uniq(struct link_server *s, char c)
{ {
int i; size_t i;
for (i = 0; i < s->user_mode_len; i++) { for (i = 0; i < s->user_mode_len; i++) {
if (s->user_mode[i] == c) if (s->user_mode[i] == c)
return; return;
@ -1634,7 +1634,7 @@ static void mode_add_letter_uniq(struct link_server *s, char c)
static void mode_remove_letter(struct link_server *s, char c) static void mode_remove_letter(struct link_server *s, char c)
{ {
int i; size_t i;
for (i = 0; i < s->user_mode_len; i++) { for (i = 0; i < s->user_mode_len; i++) {
if (s->user_mode[i] == c) { if (s->user_mode[i] == c) {
for (; i < s->user_mode_len - 1; i++) for (; i < s->user_mode_len - 1; i++)
@ -1672,7 +1672,7 @@ static int irc_mode(struct link_server *server, struct line *line)
struct channel *channel; struct channel *channel;
const char *mode; const char *mode;
int add = 1; int add = 1;
unsigned cur_arg = 0; int cur_arg = 0;
array_t *mode_args = NULL; array_t *mode_args = NULL;
int ret; int ret;
@ -2348,7 +2348,7 @@ connection_t *irc_server_connect(struct link *link)
#else #else
0, NULL, 0, NULL, NULL, 0, NULL, 0, NULL, NULL,
#endif #endif
CONNECT_TIMEOUT); (time_t)CONNECT_TIMEOUT);
assert(conn); assert(conn);
if (conn->handle == -1) { if (conn->handle == -1) {
mylog(LOG_INFO, "[%s] Cannot connect.", link->name); mylog(LOG_INFO, "[%s] Cannot connect.", link->name);
@ -2698,7 +2698,7 @@ prot_err:
*/ */
void irc_main(bip_t *bip) void irc_main(bip_t *bip)
{ {
int timeleft = 1000; time_t timeleft = 1000;
if (bip->reloading_client) { if (bip->reloading_client) {
char *l; char *l;
@ -2835,7 +2835,8 @@ static void server_set_chanmodes(struct link_server *l, const char *modes)
if (cur || modes) { if (cur || modes) {
size_t len; size_t len;
if (cur) if (cur)
len = cur - modes; // cur can't be lower than modes if !NULL
len = (size_t)(cur - modes);
else else
len = strlen(modes); // last piece len = strlen(modes); // last piece
dup = bip_malloc(len + 1); dup = bip_malloc(len + 1);
@ -2855,7 +2856,7 @@ static void server_set_chanmodes(struct link_server *l, const char *modes)
static void server_set_prefix(struct link_server *s, const char *modes) static void server_set_prefix(struct link_server *s, const char *modes)
{ {
char * end_mode; char * end_mode;
unsigned int len; size_t len;
mylog(LOG_DEBUG, "[%s] Set user modes", LINK(s)->name); mylog(LOG_DEBUG, "[%s] Set user modes", LINK(s)->name);
@ -2867,7 +2868,8 @@ static void server_set_prefix(struct link_server *s, const char *modes)
return; return;
} }
len = end_mode - modes - 1; // len of mode without '(' // end_mode can't be lower than (modes + 1)
len = (size_t)(end_mode - modes - 1); // len of mode without '('
if (len * 2 + 2 != strlen(modes)) { if (len * 2 + 2 != strlen(modes)) {
mylog(LOG_WARN, "[%s] Unable to parse PREFIX parameter", LINK(s)->name); mylog(LOG_WARN, "[%s] Unable to parse PREFIX parameter", LINK(s)->name);
return; return;
@ -2895,9 +2897,9 @@ static int bip_get_index(const char* str, char car)
return 0; return 0;
} }
static int bip_fls(int v) static int bip_fls(long v)
{ {
unsigned int r = 0; int r = 0;
while (v >>= 1) while (v >>= 1)
r++; r++;

View File

@ -236,7 +236,7 @@ struct link_server {
hash_t channels; hash_t channels;
char *user_mode; char *user_mode;
int user_mode_len; size_t user_mode_len;
/* init stuff */ /* init stuff */
int lag; int lag;

View File

@ -2,7 +2,8 @@
* $Id$ * $Id$
* *
* This file is part of the bip project * This file is part of the bip project
* Copyright (C) 2004 2005 Arnaud Cornet and Loïc Gomez * Copyright (C) 2004,2005 Arnaud Cornet
* Copyright (C) 2004,2005,2022 Loïc Gomez
* *
* This program is free software; you can redistribute it and/or modify * This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by * it under the terms of the GNU General Public License as published by
@ -164,7 +165,8 @@ struct line *irc_line_new_from_string(char *str)
irc_line_free(line); irc_line_free(line);
return NULL; return NULL;
} }
len = space - str - 1; /* leading ':' */ // space is at least str + 1, len >= 0
len = (size_t)(space - str - 1); /* leading ':' */
line->origin = bip_malloc(len + 1); line->origin = bip_malloc(len + 1);
memcpy(line->origin, str + 1, len); memcpy(line->origin, str + 1, len);
line->origin[len] = 0; line->origin[len] = 0;
@ -187,7 +189,9 @@ struct line *irc_line_new_from_string(char *str)
while (*space && *space != ' ') while (*space && *space != ' ')
space++; space++;
} }
len = space - str; // str is the start of string
// space is the end of string or end of word
len = (size_t)(space - str);
tmp = bip_malloc(len + 1); tmp = bip_malloc(len + 1);
memcpy(tmp, str, len); memcpy(tmp, str, len);
tmp[len] = 0; tmp[len] = 0;

View File

@ -28,7 +28,7 @@ extern int conf_log;
extern FILE *conf_global_log_file; extern FILE *conf_global_log_file;
static int _log_write(log_t *logdata, logstore_t *lf, const char *d, static size_t _log_write(log_t *logdata, logstore_t *lf, const char *d,
const char *str); const char *str);
static char *_log_wrap(const char *dest, const char *line); static char *_log_wrap(const char *dest, const char *line);
void logfile_free(logfile_t *lf); void logfile_free(logfile_t *lf);
@ -73,9 +73,10 @@ int check_dir(char *filename, int is_fatal)
int check_dir_r(char *dirname) int check_dir_r(char *dirname)
{ {
int pos, count = 0; size_t count = 0;
char *dir, *tmp; char *dir, *tmp;
int len = strlen(dirname); size_t len = strlen(dirname);
size_t pos;
mylog(LOG_DEBUGVERB, "Recursive check of %s engaged", dirname); mylog(LOG_DEBUGVERB, "Recursive check of %s engaged", dirname);
tmp = dirname; tmp = dirname;
@ -110,7 +111,7 @@ void strtolower(char *str)
char *c; char *c;
for (c = str; *c != '\0'; c++) for (c = str; *c != '\0'; c++)
*c = tolower(*c); *c = (char)tolower(*c); // tolower()->int but should be safe to cast
} }
/* /*
@ -121,8 +122,8 @@ void strtolower(char *str)
void replace_var(char *str, char *var, char *value, unsigned int max) void replace_var(char *str, char *var, char *value, unsigned int max)
{ {
char *pos; char *pos;
unsigned int lenvar = strlen(var); size_t lenvar = strlen(var);
unsigned int lenval = strlen(value); size_t lenval = strlen(value);
while((pos = strstr(str, var))) { while((pos = strstr(str, var))) {
/* Make room */ /* Make room */
@ -852,14 +853,18 @@ char *log_beautify(log_t *logdata, const char *buf, const char *storename,
assert(buf); assert(buf);
p = strchr(buf, ' '); p = strchr(buf, ' ');
// buf...p
if (!p || !p[0] || !p[1]) if (!p || !p[0] || !p[1])
return _log_wrap(dest, buf); return _log_wrap(dest, buf);
p++; p++;
// buf...p
sots = logdata->user->backlog_timestamp == BLTSDateTime ? buf : p; sots = logdata->user->backlog_timestamp == BLTSDateTime ? buf : p;
// buf...sots=p OR sots=buf...p
p = strchr(p, ' '); p = strchr(p, ' ');
// buf...sots...p OR sots=buf...p
if (!p || !p[0] || !p[1]) if (!p || !p[0] || !p[1])
return _log_wrap(dest, buf); return _log_wrap(dest, buf);
lots = p - sots; lots = (size_t)(p - sots);
p++; p++;
if (strncmp(p, "-!-", (size_t)3) == 0) { if (strncmp(p, "-!-", (size_t)3) == 0) {
@ -892,7 +897,7 @@ char *log_beautify(log_t *logdata, const char *buf, const char *storename,
p++; p++;
if (!p[0]) if (!p[0])
return _log_wrap(dest, buf); return _log_wrap(dest, buf);
lon = p - son; lon = (size_t)(p - son);
p = strchr(p, ' '); p = strchr(p, ' ');
if (!p || !p[0] || !p[1]) if (!p || !p[0] || !p[1])
@ -909,10 +914,11 @@ char *log_beautify(log_t *logdata, const char *buf, const char *storename,
if (lom == 0) if (lom == 0)
return _log_wrap(dest, buf); return _log_wrap(dest, buf);
// action and out are 0 or 1, safe to cast
p = ret = (char *)bip_malloc( p = ret = (char *)bip_malloc(
1 + lon + strlen(LAMESTRING) + strlen(dest) + 2 + lots + 2 + 1 + lon + strlen(LAMESTRING) + strlen(dest) + 2 + lots + 2 +
lom + 3 + action * (2 + strlen("ACTION ")) + lom + 3 + (size_t)action * (2 + strlen("ACTION ")) +
out * strlen(PMSG_ARROW)); (size_t)out * strlen(PMSG_ARROW));
*p++ = ':'; *p++ = ':';
@ -1025,7 +1031,9 @@ static int log_backread_file(log_t *log, logstore_t *store, logfile_t *lf,
/* error or oef */ /* error or oef */
break; break;
} }
int slen = strlen(buf); size_t slen = strlen(buf);
if (slen == 0)
break; // should not happen, per previous fgets block
if (buf[slen - 1] == '\n') if (buf[slen - 1] == '\n')
buf[slen - 1] = 0; buf[slen - 1] = 0;
if (slen >= 2 && buf[slen] == '\r') if (slen >= 2 && buf[slen] == '\r')
@ -1122,7 +1130,7 @@ static char *_log_wrap(const char *dest, const char *line)
return buf; return buf;
} }
static int _log_write(log_t *logdata, logstore_t *store, static size_t _log_write(log_t *logdata, logstore_t *store,
const char *destination, const char *str) const char *destination, const char *str)
{ {
size_t nbwrite; size_t nbwrite;
@ -1247,13 +1255,13 @@ array_t *str_split(const char *str, const char *splt)
{ {
const char *p = str; const char *p = str;
const char *start = str; const char *start = str;
int len; size_t len;
char *extracted; char *extracted;
array_t *array = array_new();; array_t *array = array_new();;
do { do {
if (!*p || strchr(splt, *p)) { if (!*p || strchr(splt, *p)) {
len = p - start; len = (size_t)(p - start);
extracted = bip_malloc(len + 1); extracted = bip_malloc(len + 1);
memcpy(extracted, start, len); memcpy(extracted, start, len);
extracted[len] = 0; extracted[len] = 0;

View File

@ -286,7 +286,7 @@ void mylog(int level, char *fmt, ...)
void dump_trace(void) void dump_trace(void)
{ {
void *array[32]; void *array[32];
size_t size; int size;
size = backtrace(array, 32); size = backtrace(array, 32);
fflush(conf_global_log_file); fflush(conf_global_log_file);
@ -630,8 +630,9 @@ static unsigned char hash_func(const char *pkey)
char c; char c;
unsigned long hash = 5381; /* 5381 & 0xff makes more sense */ unsigned long hash = 5381; /* 5381 & 0xff makes more sense */
// toupper should not return negative values (only char compatible int)
while ((c = *pkey++)) while ((c = *pkey++))
hash = ((hash << 5) + hash) ^ toupper(c); hash = ((hash << 5) + hash) ^ (long unsigned)toupper(c);
return (unsigned char)hash; return (unsigned char)hash;
} }
@ -810,7 +811,7 @@ char *bip_strmaydup(char *s)
void strucase(char *s) void strucase(char *s)
{ {
while (*s) { while (*s) {
*s = toupper(*s); *s = (char)toupper(*s); // toupper, safe to cast to char
s++; s++;
} }
} }
@ -840,8 +841,10 @@ void array_ensure(array_t *a, int index)
if (array_includes(a, index)) if (array_includes(a, index))
return; return;
a->elemv = bip_realloc(a->elemv, sizeof(void *) * (index + 1)); a->elemv = bip_realloc(a->elemv, sizeof(void *) * (size_t)(index + 1));
memset(a->elemv + a->elemc, 0, sizeof(void *) * (index + 1 - a->elemc)); // a->elemc should be lower than index + 1
memset(a->elemv + a->elemc, 0,
sizeof(void *) * (size_t)(index + 1 - a->elemc));
a->elemc = index + 1; a->elemc = index + 1;
} }