Use Memory_safeCalloc() to allocate zeroed memory.
[umurmur.git] / src / ban.c
index c8b868809f0090a78465785ad6cdf2e097f523bc..84e95dc174e7a3ed881b616a70f4a75d39f573b6 100644 (file)
--- a/src/ban.c
+++ b/src/ban.c
@@ -33,6 +33,7 @@
 #include <time.h>
 #include <string.h>
 #include "log.h"
+#include "memory.h"
 #include "list.h"
 #include "ban.h"
 #include "conf.h"
@@ -70,9 +71,7 @@ void Ban_UserBan(client_t *client, char *reason)
        ban_t *ban;
        char hexhash[41];
 
-       ban = calloc(1, sizeof(ban_t));
-       if (ban == NULL)
-               Log_fatal("Out of memory");
+       ban = Memory_safeCalloc(1, sizeof(ban_t));
 
        memcpy(ban->hash, client->hash, 20);
 
@@ -90,8 +89,12 @@ void Ban_UserBan(client_t *client, char *reason)
 
        SSLi_hash2hex(ban->hash, hexhash);
 
+       char *clientAddressString = Util_clientAddressToString(client);
+
        Log_info_client(client, "User kickbanned. Reason: '%s' Hash: %s IP: %s Banned for: %d seconds",
-               ban->reason, hexhash, Util_clientAddressToString(client), ban->duration);
+               ban->reason, hexhash, clientAddressString, ban->duration);
+
+       free(clientAddressString);
 }
 
 
@@ -103,10 +106,13 @@ void Ban_pruneBanned()
        list_iterate(itr, &banlist) {
                ban = list_get_entry(itr, ban_t, node);
 #ifdef DEBUG
+               char hexhash[41];
                SSLi_hash2hex(ban->hash, hexhash);
+               char *addressString = Util_addressToString(&ban->address);
                Log_debug("BL: User %s Reason: '%s' Hash: %s IP: %s Time left: %d",
-                       ban->name, ban->reason, hexhash, Util_addressToString(&ban->address)),
+                       ban->name, ban->reason, hexhash, addressString,
                        ban->time + ban->duration - time(NULL));
+               free(addressString);
 #endif
                /* Duration of 0 = forever */
                if (ban->duration != 0 && ban->time + ban->duration - time(NULL) <= 0) {
@@ -238,9 +244,7 @@ void Ban_putBanList(message_t *msg, int n_bans)
 
        for (i = 0; i < n_bans; i++) {
                Msg_banList_getEntry(msg, i, &address, &mask, &name, &hexhash, &reason, &start, &duration);
-               ban = malloc(sizeof(ban_t));
-               if (ban == NULL)
-                       Log_fatal("Out of memory");
+               ban = Memory_safeMalloc(1, sizeof(ban_t));
                SSLi_hex2hash(hexhash, ban->hash);
 
                if(memcmp(address, mappedBytes, 12) == 0) {
@@ -302,7 +306,9 @@ static void Ban_saveBanFile(void)
                ban = list_get_entry(itr, ban_t, node);
                SSLi_hash2hex(ban->hash, hexhash);
 
-               fprintf(file, "%s,%s,%d,%ld,%d,%s,%s\n", hexhash, Util_addressToString(&ban->address),ban->mask, (long int)ban->time, ban->duration, ban->name, ban->reason);
+               char *addressString = Util_addressToString(&ban->address);
+               fprintf(file, "%s,%s,%d,%ld,%d,%s,%s\n", hexhash, addressString,ban->mask, (long int)ban->time, ban->duration, ban->name, ban->reason);
+               free(addressString);
        }
        fclose(file);
        banlist_changed = false;
@@ -345,10 +351,7 @@ static void Ban_readBanFile(void)
                if (p == NULL) break;
                reason = p;
 
-               ban = malloc(sizeof(ban_t));
-               if (ban == NULL)
-                       Log_fatal("Out of memory");
-               memset(ban, 0, sizeof(ban_t));
+               ban = Memory_safeCalloc(1, sizeof(ban_t));
                SSLi_hex2hash(hexhash, ban->hash);
                if (inet_pton(AF_INET, address, &ban->address) == 0) {
                        if (inet_pton(AF_INET6, address, &ban->address) == 0) {