Project

General

Profile

Enhancement #750 » 0010-sanitize-use-proper-types-safe-casts-mostly-size_t.patch

Loïc Gomez, 2022-01-10 19:27

View differences:

src/bip.c
void conf_die(bip_t *bip, char *fmt, ...)
{
va_list ap;
int size = ERRBUFSZ;
size_t size = ERRBUFSZ;
int n;
char *error = bip_malloc(size);
......
va_start(ap, fmt);
n = vsnprintf(error, size, fmt, ap);
va_end(ap);
if (n > -1 && n < size) {
if (n > -1 && (unsigned int) n < size) {
list_add_last(&bip->errors, error);
break;
}
if (n > -1)
size = n + 1;
size = (unsigned int) n + 1;
else
size *= 2;
error = bip_realloc(error, size);
......
FILE *f;
int fd;
// 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);
try_again:
......
#define ON_CONNECT_MAX_STRSIZE 1024
void adm_on_connect_send(struct link_client *ic, struct line *line,
unsigned int privmsg)
int privmsg)
{
size_t remaining = ON_CONNECT_MAX_STRSIZE;
char buf[ON_CONNECT_MAX_STRSIZE];
......
for (i = privmsg + 2; i < irc_line_count(line); i++) {
mylog(LOG_DEBUG, "[%s] processing item %d, remaining %ld, %s",
LINK(ic)->user->name, i, remaining, buf);
if ((unsigned int)i > privmsg + 2)
if (i > privmsg + 2)
bufpos = bip_strcatf_fit(&remaining, bufpos, " %s",
irc_line_elem(line, i));
else
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",
LINK(ic)->user->name, i, remaining, buf);
if (!bufpos) {
......
if (privmsg) {
char *linestr, *elemstr;
char *ptr, *eptr;
int slen;
size_t slen;
if (irc_line_count(line) != 3)
return OK_FORGET;
......
elemstr = bip_malloc(strlen(linestr) + 1);
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) {
ptr++;
continue;
......
irc_line_append(line, elemstr);
ptr = eptr + 1;
}
eptr = ptr + strlen(ptr);
slen = eptr - ptr;
slen = strlen(ptr);
eptr = ptr + slen;
if (slen != 0) {
memcpy(elemstr, ptr, slen);
elemstr[slen] = 0;
src/connection.c
static int _write_socket_SSL(connection_t *cn, char* message)
{
int count, size;
int count;
size_t size;
size = sizeof(char)*strlen(message);
......
count = write(cn->handle, ((const char *)message) + tcount,
size - tcount);
if (count > 0) {
tcount += count;
tcount += (size_t)count;
if (tcount == size)
return WRITE_OK;
}
......
* cn->lasttoken is the number of milliseconds when we
* last added a token to the bucket */
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 */
now -= now % TOKEN_INTERVAL;
if (now < cn->lasttoken) {
......
cn->token = 1;
cn->lasttoken = now;
} else if (now > cn->lasttoken + TOKEN_INTERVAL) {
cn->token += (now - cn->lasttoken) /
TOKEN_INTERVAL;
cn->token += (unsigned)((now - cn->lasttoken) /
TOKEN_INTERVAL);
if (cn->token > TOKEN_MAX)
cn->token = TOKEN_MAX;
if (!cn->token)
......
}
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;
int maxfd = -1, err, errtime;
......
if (setsockopt(cn->handle, SOL_SOCKET, SO_REUSEADDR,
(char *)&multi_client,
sizeof(multi_client)) < 0) {
(socklen_t)sizeof(multi_client)) < 0) {
mylog(LOG_WARN, "setsockopt() : %s", strerror(errno));
close(cn->handle);
cn->handle = -1;
......
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)
{
connection_t *conn;
......
/* Return ephemeral DH parameters. */
DH *dh = NULL;
FILE *f;
int ret;
long ret;
if ((f = fopen(conf_client_dh_file, "r")) == NULL) {
mylog(LOG_ERROR, "Unable to open DH parameters (%s): %s",
......
* SSL flag is only here to tell program to convert socket to SSL after
* 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);
return conn;
}
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;
......
#ifdef HAVE_LIBSSL
static SSL_CTX *SSL_init_context(char *ciphers)
{
int fd, flags, ret, rng;
int fd, flags, rng;
ssize_t ret;
char buf[1025];
SSL_CTX *ctx;
......
}
mylog(LOG_DEBUG, "PRNG seeded with %d /dev/random "
"bytes", ret);
RAND_seed(buf, ret);
RAND_seed(buf, (int)ret);
} while (!(rng = RAND_status()));
prng_end:
......
if (!result) {
/* We have a verify error! Log it */
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;
......
static int SSLize(connection_t *cn, int *nc)
{
int err, err2;
long errl;
if (cn == NULL)
return 1;
......
if (err2 == SSL_ERROR_NONE) {
const SSL_CIPHER *cipher;
char buf[128];
int len;
size_t len;
cipher = SSL_get_current_cipher(cn->ssl_h);
SSL_CIPHER_description(cipher, buf, 128);
......
case SSL_CHECK_NONE:
break;
case SSL_CHECK_BASIC:
if((err = SSL_get_verify_result(cn->ssl_h)) != X509_V_OK) {
mylog(LOG_ERROR, "Certificate check failed: %s (%d)!",
X509_verify_cert_error_string(err), err);
if((errl = SSL_get_verify_result(cn->ssl_h)) != X509_V_OK) {
mylog(LOG_ERROR, "Certificate check failed: %s (%ld)!",
X509_verify_cert_error_string(errl), errl);
cn->connected = CONN_UNTRUSTED;
return 1;
}
break;
case SSL_CHECK_CA:
if((err = SSL_get_verify_result(cn->ssl_h)) != X509_V_OK) {
mylog(LOG_ERROR, "Certificate check failed: %s (%d)!",
X509_verify_cert_error_string(err), err);
if((errl = SSL_get_verify_result(cn->ssl_h)) != X509_V_OK) {
mylog(LOG_ERROR, "Certificate check failed: %s (%ld)!",
X509_verify_cert_error_string(errl), errl);
cn->connected = CONN_UNTRUSTED;
return 1;
}
......
static connection_t *_connection_new_SSL(char *dsthostname, char *dstport,
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 *connection_new(char *dsthostname, int dstport, char *srchostname,
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;
#ifndef HAVE_LIBSSL
......
fprintf(stderr,"Usage: %s host port\n",argv[0]);
exit(1);
}
conn = connection_init(0, 0, 0, 1);
conn = connection_init(0, 0, (time_t)0, 1);
conn->connect_time = time(NULL);
create_listening_socket(argv[1],argv[2],&conn);
if (s == -1) {
......
}
#endif
int connection_localport(connection_t *cn)
uint16_t connection_localport(connection_t *cn)
{
struct sockaddr_in addr;
int err;
socklen_t addrlen;
if (cn->handle <= 0)
return -1;
return 0;
addrlen = sizeof(addr);
err = getsockname(cn->handle, (struct sockaddr *)&addr, &addrlen);
if (err != 0) {
mylog(LOG_ERROR, "in getsockname(%d): %s", cn->handle,
strerror(errno));
return -1;
return 0;
}
return ntohs(addr.sin_port);
}
int connection_remoteport(connection_t *cn)
uint16_t connection_remoteport(connection_t *cn)
{
struct sockaddr_in addr;
int err;
socklen_t addrlen;
if (cn->handle <= 0)
return -1;
return 0;
addrlen = sizeof(addr);
err = getpeername(cn->handle, (struct sockaddr *)&addr, &addrlen);
if (err != 0) {
mylog(LOG_ERROR, "in getpeername(%d): %s", cn->handle,
strerror(errno));
return -1;
return 0;
}
return ntohs(addr.sin_port);
src/connection.h
connection_t *connection_new(char *dsthostname, int dstport, char *srchostname,
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 *accept_new(connection_t *cn);
void connection_free(connection_t *cn);
......
void write_lines(connection_t *cn, list_t *lines);
void write_line_fast(connection_t *cn, char *line);
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_listening(connection_t *cn);
int connection_localport(connection_t *cn);
int connection_remoteport(connection_t *cn);
uint16_t connection_localport(connection_t *cn);
uint16_t connection_remoteport(connection_t *cn);
char *connection_localip(connection_t *cn);
char *connection_remoteip(connection_t *cn);
#endif
src/irc.c
static void server_set_prefix(struct link_server *l, const char *prefix);
static void server_init_modes(struct link_server *s);
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);
......
nick++;
if (!*nick)
return bip_strdup(mask);
len = nick - mask;
len = (size_t)(nick - mask); // cannot be < 0
ret = bip_malloc(len + 1);
memcpy(ret, mask, len);
ret[len] = 0;
......
newnick[7] = '`';
}
} else {
newnick[8] = rand() *
('z' - 'a') / RAND_MAX + 'a';
newnick[8] = (char)('a' + ('z' - 'a') *
rand() / RAND_MAX);
}
newnick[9] = 0;
}
......
ks = NULL;
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);
size_t klen;
char *kp = NULL;
......
ke = strchr(ks, ',');
if (!ke)
ke = ks + strlen(ks);
klen = ke - ks;
klen = (size_t)(ke - ks);
kp = bip_malloc(klen + 1);
memcpy(kp, ks, klen);
kp[klen] = 0;
......
while (*eon && *eon != ' ')
eon++;
len = eon - names;
len = (size_t)(eon - names); // cannot be < 0
nick = bip_malloc(len + 1);
memcpy(nick, names, len);
nick[len] = 0;
......
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++) {
if (s->user_mode[i] == c)
return;
......
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++) {
if (s->user_mode[i] == c) {
for (; i < s->user_mode_len - 1; i++)
......
struct channel *channel;
const char *mode;
int add = 1;
unsigned cur_arg = 0;
int cur_arg = 0;
array_t *mode_args = NULL;
int ret;
......
#else
0, NULL, 0, NULL, NULL,
#endif
CONNECT_TIMEOUT);
(time_t)CONNECT_TIMEOUT);
assert(conn);
if (conn->handle == -1) {
mylog(LOG_INFO, "[%s] Cannot connect.", link->name);
......
*/
void irc_main(bip_t *bip)
{
int timeleft = 1000;
time_t timeleft = 1000;
if (bip->reloading_client) {
char *l;
......
if (cur || modes) {
size_t len;
if (cur)
len = cur - modes;
// cur can't be lower than modes if !NULL
len = (size_t)(cur - modes);
else
len = strlen(modes); // last piece
dup = bip_malloc(len + 1);
......
static void server_set_prefix(struct link_server *s, const char *modes)
{
char * end_mode;
unsigned int len;
size_t len;
mylog(LOG_DEBUG, "[%s] Set user modes", LINK(s)->name);
......
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)) {
mylog(LOG_WARN, "[%s] Unable to parse PREFIX parameter", LINK(s)->name);
return;
......
return 0;
}
static int bip_fls(int v)
static int bip_fls(long v)
{
unsigned int r = 0;
int r = 0;
while (v >>= 1)
r++;
src/irc.h
hash_t channels;
char *user_mode;
int user_mode_len;
size_t user_mode_len;
/* init stuff */
int lag;
src/line.c
irc_line_free(line);
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);
memcpy(line->origin, str + 1, len);
line->origin[len] = 0;
......
while (*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);
memcpy(tmp, str, len);
tmp[len] = 0;
src/log.c
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);
static char *_log_wrap(const char *dest, const char *line);
void logfile_free(logfile_t *lf);
......
int check_dir_r(char *dirname)
{
int pos, count = 0;
size_t count = 0;
char *dir, *tmp;
int len = strlen(dirname);
size_t len = strlen(dirname);
size_t pos;
mylog(LOG_DEBUGVERB, "Recursive check of %s engaged", dirname);
tmp = dirname;
......
char *c;
for (c = str; *c != '\0'; c++)
*c = tolower(*c);
*c = (char)tolower(*c); // tolower()->int but should be safe to cast
}
/*
......
void replace_var(char *str, char *var, char *value, unsigned int max)
{
char *pos;
unsigned int lenvar = strlen(var);
unsigned int lenval = strlen(value);
size_t lenvar = strlen(var);
size_t lenval = strlen(value);
while((pos = strstr(str, var))) {
/* Make room */
......
assert(buf);
p = strchr(buf, ' ');
// buf...p
if (!p || !p[0] || !p[1])
return _log_wrap(dest, buf);
p++;
// buf...p
sots = logdata->user->backlog_timestamp == BLTSDateTime ? buf : p;
// buf...sots=p OR sots=buf...p
p = strchr(p, ' ');
// buf...sots...p OR sots=buf...p
if (!p || !p[0] || !p[1])
return _log_wrap(dest, buf);
lots = p - sots;
lots = (size_t)(p - sots);
p++;
if (strncmp(p, "-!-", (size_t)3) == 0) {
......
p++;
if (!p[0])
return _log_wrap(dest, buf);
lon = p - son;
lon = (size_t)(p - son);
p = strchr(p, ' ');
if (!p || !p[0] || !p[1])
......
if (lom == 0)
return _log_wrap(dest, buf);
// action and out are 0 or 1, safe to cast
p = ret = (char *)bip_malloc(
1 + lon + strlen(LAMESTRING) + strlen(dest) + 2 + lots + 2 +
lom + 3 + action * (2 + strlen("ACTION ")) +
out * strlen(PMSG_ARROW));
lom + 3 + (size_t)action * (2 + strlen("ACTION ")) +
(size_t)out * strlen(PMSG_ARROW));
*p++ = ':';
......
/* error or oef */
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')
buf[slen - 1] = 0;
if (slen >= 2 && buf[slen] == '\r')
......
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)
{
size_t nbwrite;
......
{
const char *p = str;
const char *start = str;
int len;
size_t len;
char *extracted;
array_t *array = array_new();;
do {
if (!*p || strchr(splt, *p)) {
len = p - start;
len = (size_t)(p - start);
extracted = bip_malloc(len + 1);
memcpy(extracted, start, len);
extracted[len] = 0;
src/util.c
void dump_trace(void)
{
void *array[32];
size_t size;
int size;
size = backtrace(array, 32);
fflush(conf_global_log_file);
......
char c;
unsigned long hash = 5381; /* 5381 & 0xff makes more sense */
// toupper should not return negative values (only char compatible int)
while ((c = *pkey++))
hash = ((hash << 5) + hash) ^ toupper(c);
hash = ((hash << 5) + hash) ^ (long unsigned)toupper(c);
return (unsigned char)hash;
}
......
void strucase(char *s)
{
while (*s) {
*s = toupper(*s);
*s = (char)toupper(*s); // toupper, safe to cast to char
s++;
}
}
......
if (array_includes(a, index))
return;
a->elemv = bip_realloc(a->elemv, sizeof(void *) * (index + 1));
memset(a->elemv + a->elemc, 0, sizeof(void *) * (index + 1 - a->elemc));
a->elemv = bip_realloc(a->elemv, sizeof(void *) * (size_t)(index + 1));
// 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;
}
(9-9/24)