From d8f0b5d7635a160150cdd8d306e9f3a99dd0b7d2 Mon Sep 17 00:00:00 2001 From: Simon Arlott Date: Fri, 28 Oct 2016 19:23:21 +0100 Subject: [PATCH] cppcheck: fix various warnings/errors [ircd/match.c:316]: (error) Shifting a negative value is undefined behaviour [librb/src/patricia.c:55]: (error) Shifting a negative value is undefined behaviour [modules/m_alias.c:64]: (portability) '(void*)message' is of type 'void *'. When using void pointers in calculations, the behaviour is undefined. [modules/m_time.c:111]: (warning) %u in format string (no. 9) requires 'unsigned int' but the argument type is 'signed int'. [modules/m_time.c:111]: (warning) %u in format string (no. 10) requires 'unsigned int' but the argument type is 'signed int'. [librb/src/dictionary.c:819]: (warning) %d in format string (no. 3) requires 'int' but the argument type is 'unsigned int'. [librb/src/radixtree.c:1080]: (warning) %d in format string (no. 3) requires 'int' but the argument type is 'unsigned int'. [ircd/s_user.c:351] -> [ircd/s_user.c:357]: (warning) Either the condition '0!=source_p' is redundant or there is possible null pointer dereference: source_p. [extensions/ip_cloaking_3.0.c:109]: (warning, inconclusive) The buffer 'buf' may not be null-terminated after the call to strncpy(). [ircd/chmode.c:256]: (style) Clarify calculation precedence for '&' and '?'. [modules/m_help.c:100]: (style) Clarify calculation precedence for '&' and '?'. [modules/m_knock.c:169]: (style) Clarify calculation precedence for '&' and '?'. [modules/m_stats.c:628]: (style) Clarify calculation precedence for '&' and '?'. [modules/m_stats.c:727]: (style) Clarify calculation precedence for '&' and '?'. [librb/src/radixtree.c:601]: (style) Clarify calculation precedence for '&' and '?'. [librb/src/radixtree.c:704]: (style) Clarify calculation precedence for '&' and '?'. [librb/src/radixtree.c:739]: (style) Clarify calculation precedence for '&' and '?'. [librb/src/radixtree.c:763]: (style) Clarify calculation precedence for '&' and '?'. [librb/src/radixtree.c:768]: (style) Clarify calculation precedence for '&' and '?'. [librb/src/radixtree.c:774]: (style) Clarify calculation precedence for '&' and '?'. [librb/src/radixtree.c:781]: (style) Clarify calculation precedence for '&' and '?'. [librb/src/radixtree.c:786]: (style) Clarify calculation precedence for '&' and '?'. [librb/src/radixtree.c:791]: (style) Clarify calculation precedence for '&' and '?'. [librb/src/radixtree.c:804]: (style) Clarify calculation precedence for '&' and '?'. [ircd/wsproc.c:372]: (style) Unused variable: len [modules/core/m_modules.c:382]: (style) Unused variable: i [modules/m_stats.c:741]: (style) Unused variable: amsg [ircd/authproc.c:390]: (style) Unused variable: iter [ircd/authproc.c:391]: (style) Unused variable: client_p --- extensions/ip_cloaking_3.0.c | 4 ++-- extensions/ip_cloaking_old.c | 2 +- ircd/authproc.c | 3 --- ircd/chmode.c | 2 +- ircd/match.c | 2 +- ircd/s_user.c | 5 ++--- ircd/wsproc.c | 1 - librb/src/dictionary.c | 2 +- librb/src/patricia.c | 2 +- librb/src/radixtree.c | 4 ++-- modules/core/m_modules.c | 1 - modules/m_alias.c | 2 +- modules/m_help.c | 2 +- modules/m_knock.c | 2 +- modules/m_stats.c | 5 ++--- modules/m_time.c | 2 +- 16 files changed, 17 insertions(+), 24 deletions(-) diff --git a/extensions/ip_cloaking_3.0.c b/extensions/ip_cloaking_3.0.c index a1d16ca5..e648fc1b 100644 --- a/extensions/ip_cloaking_3.0.c +++ b/extensions/ip_cloaking_3.0.c @@ -103,7 +103,7 @@ do_host_cloak_ip(const char *inbuf, char *outbuf) { char *tptr; unsigned int accum = get_string_weighted_entropy(inbuf); - char buf[HOSTLEN]; + char buf[HOSTLEN + 1] = { 0 }; int ipv6 = 0; strncpy(buf, inbuf, HOSTLEN); @@ -216,7 +216,7 @@ check_new_user(void *vdata) source_p->umodes &= ~user_modes['h']; return; } - source_p->localClient->mangledhost = rb_malloc(HOSTLEN); + source_p->localClient->mangledhost = rb_malloc(HOSTLEN + 1); if (!irccmp(source_p->orighost, source_p->sockhost)) do_host_cloak_ip(source_p->orighost, source_p->localClient->mangledhost); else diff --git a/extensions/ip_cloaking_old.c b/extensions/ip_cloaking_old.c index ca4f9111..8333e362 100644 --- a/extensions/ip_cloaking_old.c +++ b/extensions/ip_cloaking_old.c @@ -158,7 +158,7 @@ check_new_user(void *vdata) source_p->umodes &= ~user_modes['h']; return; } - source_p->localClient->mangledhost = rb_malloc(HOSTLEN); + source_p->localClient->mangledhost = rb_malloc(HOSTLEN + 1); if (!irccmp(source_p->orighost, source_p->sockhost)) do_host_cloak(source_p->orighost, source_p->localClient->mangledhost, 1); else diff --git a/ircd/authproc.c b/ircd/authproc.c index 355edafd..26033b09 100644 --- a/ircd/authproc.c +++ b/ircd/authproc.c @@ -387,9 +387,6 @@ authd_abort_client(struct Client *client_p) static void restart_authd_cb(rb_helper * helper) { - rb_dictionary_iter iter; - struct Client *client_p; - iwarn("authd: restart_authd_cb called, authd died?"); sendto_realops_snomask(SNO_GENERAL, L_ALL, "authd: restart_authd_cb called, authd died?"); diff --git a/ircd/chmode.c b/ircd/chmode.c index d60dfddf..ec826538 100644 --- a/ircd/chmode.c +++ b/ircd/chmode.c @@ -253,7 +253,7 @@ add_id(struct Client *source_p, struct Channel *chptr, const char *banid, const */ if(MyClient(source_p)) { - if((rb_dlink_list_length(&chptr->banlist) + rb_dlink_list_length(&chptr->exceptlist) + rb_dlink_list_length(&chptr->invexlist) + rb_dlink_list_length(&chptr->quietlist)) >= (unsigned long)(chptr->mode.mode & MODE_EXLIMIT ? ConfigChannel.max_bans_large : ConfigChannel.max_bans)) + if((rb_dlink_list_length(&chptr->banlist) + rb_dlink_list_length(&chptr->exceptlist) + rb_dlink_list_length(&chptr->invexlist) + rb_dlink_list_length(&chptr->quietlist)) >= (unsigned long)((chptr->mode.mode & MODE_EXLIMIT) ? ConfigChannel.max_bans_large : ConfigChannel.max_bans)) { sendto_one(source_p, form_str(ERR_BANLISTFULL), me.name, source_p->name, chptr->chname, realban); diff --git a/ircd/match.c b/ircd/match.c index 64389232..f1c86d2d 100644 --- a/ircd/match.c +++ b/ircd/match.c @@ -313,7 +313,7 @@ int comp_with_mask(void *addr, void *dest, unsigned int mask) if (memcmp(addr, dest, mask / 8) == 0) { int n = mask / 8; - int m = ((-1) << (8 - (mask % 8))); + unsigned char m = (0xFF << (8 - (mask % 8))); if (mask % 8 == 0 || (((unsigned char *) addr)[n] & m) == (((unsigned char *) dest)[n] & m)) { return (1); diff --git a/ircd/s_user.c b/ircd/s_user.c index 625cfcbd..c7e64161 100644 --- a/ircd/s_user.c +++ b/ircd/s_user.c @@ -348,7 +348,6 @@ int register_local_user(struct Client *client_p, struct Client *source_p) { struct ConfItem *aconf, *xconf; - struct User *user = source_p->user; char tmpstr2[BUFSIZE]; char ipaddr[HOSTIPLEN]; char myusername[USERLEN+1]; @@ -480,7 +479,7 @@ register_local_user(struct Client *client_p, struct Client *source_p) } } - if(IsNeedSasl(aconf) && !*user->suser) + if(IsNeedSasl(aconf) && !*source_p->user->suser) { ServerStats.is_ref++; sendto_one_notice(source_p, ":*** Notice -- You need to identify via SASL to use this server"); @@ -665,7 +664,7 @@ register_local_user(struct Client *client_p, struct Client *source_p) free_pre_client(source_p); - introduce_client(client_p, source_p, user, source_p->name, 1); + introduce_client(client_p, source_p, source_p->user, source_p->name, 1); return 0; } diff --git a/ircd/wsproc.c b/ircd/wsproc.c index 2a0d34ac..61c27aaf 100644 --- a/ircd/wsproc.c +++ b/ircd/wsproc.c @@ -369,7 +369,6 @@ ws_process_cmd_recv(ws_ctl_t * ctl) { rb_dlink_node *ptr, *next; ws_ctl_buf_t *ctl_buf; - unsigned long len; if(ctl->dead) return; diff --git a/librb/src/dictionary.c b/librb/src/dictionary.c index f72ceca9..124edbbe 100644 --- a/librb/src/dictionary.c +++ b/librb/src/dictionary.c @@ -816,7 +816,7 @@ void rb_dictionary_stats(rb_dictionary *dict, void (*cb)(const char *line, void { maxdepth = 0; sum = stats_recurse(dict->root, 0, &maxdepth); - snprintf(str, sizeof str, "%-30s %-15s %-10d %-10d %-10d %-10d", dict->id, "DICT", dict->count, sum, sum / dict->count, maxdepth); + snprintf(str, sizeof str, "%-30s %-15s %-10u %-10d %-10d %-10d", dict->id, "DICT", dict->count, sum, sum / dict->count, maxdepth); } else { diff --git a/librb/src/patricia.c b/librb/src/patricia.c index fe02c73c..d04e8865 100644 --- a/librb/src/patricia.c +++ b/librb/src/patricia.c @@ -52,7 +52,7 @@ comp_with_mask(void *addr, void *dest, unsigned int mask) if( /* mask/8 == 0 || */ memcmp(addr, dest, mask / 8) == 0) { int n = mask / 8; - int m = ((-1) << (8 - (mask % 8))); + uint8_t m = (0xFF << (8 - (mask % 8))); if(mask % 8 == 0 || (((uint8_t *)addr)[n] & m) == (((uint8_t *)dest)[n] & m)) return (1); diff --git a/librb/src/radixtree.c b/librb/src/radixtree.c index abc153fa..581eba04 100644 --- a/librb/src/radixtree.c +++ b/librb/src/radixtree.c @@ -70,7 +70,7 @@ struct rb_radixtree }; #define POINTERS_PER_NODE 16 -#define NIBBLE_VAL(key, nibnum) (((key)[(nibnum) / 2] >> ((nibnum) & 1 ? 0 : 4)) & 0xF) +#define NIBBLE_VAL(key, nibnum) (((key)[(nibnum) / 2] >> (((nibnum) & 1) ? 0 : 4)) & 0xF) struct rb_radixtree_node { @@ -1077,7 +1077,7 @@ rb_radixtree_stats(rb_radixtree *dict, void (*cb)(const char *line, void *privda if (dict->count > 0) { sum = stats_recurse(dict->root, 0, &maxdepth); - snprintf(str, sizeof str, "%-30s %-15s %-10d %-10d %-10d %-10d", dict->id, "RADIX", dict->count, sum, sum / dict->count, maxdepth); + snprintf(str, sizeof str, "%-30s %-15s %-10u %-10d %-10d %-10d", dict->id, "RADIX", dict->count, sum, sum / dict->count, maxdepth); } else { diff --git a/modules/core/m_modules.c b/modules/core/m_modules.c index 813a444b..67944bcc 100644 --- a/modules/core/m_modules.c +++ b/modules/core/m_modules.c @@ -379,7 +379,6 @@ static void do_modlist(struct Client *source_p, const char *pattern) { rb_dlink_node *ptr; - int i; RB_DLINK_FOREACH(ptr, module_list.head) { diff --git a/modules/m_alias.c b/modules/m_alias.c index ebff11e2..f59ca2a6 100644 --- a/modules/m_alias.c +++ b/modules/m_alias.c @@ -61,7 +61,7 @@ create_aliases(void) RB_DICTIONARY_FOREACH(alias, &iter, alias_dict) { struct Message *message = rb_malloc(sizeof(*message) + strlen(alias->name) + 1); - char *cmd = (void*)message + sizeof(*message); + char *cmd = (char*)message + sizeof(*message); /* copy the alias name as it will be freed early on a rehash */ strcpy(cmd, alias->name); diff --git a/modules/m_help.c b/modules/m_help.c index 251b20ca..9e56c861 100644 --- a/modules/m_help.c +++ b/modules/m_help.c @@ -97,7 +97,7 @@ dohelp(struct Client *source_p, int flags, const char *topic) if(EmptyString(topic)) topic = ntopic; - hptr = rb_dictionary_retrieve(flags & HELP_OPER ? help_dict_oper : help_dict_user, topic); + hptr = rb_dictionary_retrieve((flags & HELP_OPER) ? help_dict_oper : help_dict_user, topic); if(hptr == NULL || !(hptr->flags & flags)) { diff --git a/modules/m_knock.c b/modules/m_knock.c index 756a523e..da3d0c84 100644 --- a/modules/m_knock.c +++ b/modules/m_knock.c @@ -166,7 +166,7 @@ m_knock(struct MsgBuf *msgbuf_p, struct Client *client_p, struct Client *source_ chptr->last_knock = rb_current_time(); if(ConfigChannel.use_knock) - sendto_channel_local(chptr->mode.mode & MODE_FREEINVITE ? ALL_MEMBERS : ONLY_CHANOPS, + sendto_channel_local((chptr->mode.mode & MODE_FREEINVITE) ? ALL_MEMBERS : ONLY_CHANOPS, chptr, form_str(RPL_KNOCK), me.name, name, name, source_p->name, source_p->username, source_p->host); diff --git a/modules/m_stats.c b/modules/m_stats.c index 997019d7..94e9cff2 100644 --- a/modules/m_stats.c +++ b/modules/m_stats.c @@ -625,7 +625,7 @@ stats_tklines(struct Client *source_p) get_printable_kline(source_p, aconf, &host, &pass, &user, &oper_reason); sendto_one_numeric(source_p, RPL_STATSKLINE, - form_str(RPL_STATSKLINE), aconf->flags & CONF_FLAGS_TEMPORARY ? 'k' : 'K', + form_str(RPL_STATSKLINE), (aconf->flags & CONF_FLAGS_TEMPORARY) ? 'k' : 'K', host, user, pass, oper_reason ? "|" : "", oper_reason ? oper_reason : ""); } @@ -724,7 +724,7 @@ stats_klines(struct Client *source_p) get_printable_kline(source_p, aconf, &host, &pass, &user, &oper_reason); sendto_one_numeric(source_p, RPL_STATSKLINE, form_str(RPL_STATSKLINE), - aconf->flags & CONF_FLAGS_TEMPORARY ? 'k' : 'K', + (aconf->flags & CONF_FLAGS_TEMPORARY) ? 'k' : 'K', host, user, pass, oper_reason ? "|" : "", oper_reason ? oper_reason : ""); } @@ -738,7 +738,6 @@ stats_messages(struct Client *source_p) { rb_dictionary_iter iter; struct Message *msg; - struct alias_entry *amsg; RB_DICTIONARY_FOREACH(msg, &iter, cmd_dict) { diff --git a/modules/m_time.c b/modules/m_time.c index 4f4ae72a..268e40f6 100644 --- a/modules/m_time.c +++ b/modules/m_time.c @@ -111,7 +111,7 @@ date(void) sprintf(buf, "%s %s %d %d -- %02u:%02u:%02u %c%02u:%02u", weekdays[lt->tm_wday], months[lt->tm_mon], lt->tm_mday, lt->tm_year + 1900, lt->tm_hour, lt->tm_min, lt->tm_sec, - plus, minswest / 60, minswest % 60); + plus, (unsigned int)minswest / 60, (unsigned int)minswest % 60); return buf; }