Removed null-pointer dereference in low mem.
authorDan Turner <dan.turner@york.ac.uk>
Thu, 25 Jun 2015 15:05:51 +0000 (16:05 +0100)
committerFelix Morgner <felix.morgner@gmail.com>
Mon, 13 Jul 2015 08:12:49 +0000 (10:12 +0200)
14 files changed:
src/ban.c
src/channel.c
src/client.c
src/memory.c [new file with mode: 0644]
src/memory.h [new file with mode: 0644]
src/messagehandler.c
src/messages.c
src/pds.c
src/server.c
src/ssli_gnutls.c
src/ssli_openssl.c
src/ssli_polarssl.c
src/util.c
src/voicetarget.c

index 47f02edab1c1d3ae3a9e277918499b67b41cb415..7028ea6f5dd597bd60f3c3e27d7293202d87e3f8 100644 (file)
--- a/src/ban.c
+++ b/src/ban.c
@@ -33,6 +33,7 @@
 #include <time.h>
 #include <string.h>
 #include "log.h"
 #include <time.h>
 #include <string.h>
 #include "log.h"
+#include "memory.h"
 #include "list.h"
 #include "ban.h"
 #include "conf.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_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);
 
 
        memcpy(ban->hash, client->hash, 20);
 
@@ -245,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);
 
        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) {
                SSLi_hex2hash(hexhash, ban->hash);
 
                if(memcmp(address, mappedBytes, 12) == 0) {
@@ -354,9 +351,7 @@ static void Ban_readBanFile(void)
                if (p == NULL) break;
                reason = p;
 
                if (p == NULL) break;
                reason = p;
 
-               ban = malloc(sizeof(ban_t));
-               if (ban == NULL)
-                       Log_fatal("Out of memory");
+               ban = Memory_safeMalloc(1, sizeof(ban_t));
                memset(ban, 0, sizeof(ban_t));
                SSLi_hex2hash(hexhash, ban->hash);
                if (inet_pton(AF_INET, address, &ban->address) == 0) {
                memset(ban, 0, sizeof(ban_t));
                SSLi_hex2hash(hexhash, ban->hash);
                if (inet_pton(AF_INET, address, &ban->address) == 0) {
index fdf4e82df9acab3a9597b56b5449d13508068b23..da15a52f175e602bc3c0a633b472862f4684e013 100644 (file)
@@ -32,6 +32,7 @@
 #include <stdlib.h>
 #include <string.h>
 #include "log.h"
 #include <stdlib.h>
 #include <string.h>
 #include "log.h"
+#include "memory.h"
 #include "list.h"
 #include "client.h"
 #include "channel.h"
 #include "list.h"
 #include "client.h"
 #include "channel.h"
@@ -46,9 +47,7 @@ static channel_t *createChannel(int id, const char *name, const char *desc)
 {
        channel_t *ch;
 
 {
        channel_t *ch;
 
-       ch = malloc(sizeof(channel_t));
-       if (ch == NULL)
-               Log_fatal("out of memory");
+       ch = Memory_safeMalloc(1, sizeof(channel_t));
        memset(ch, 0, sizeof(channel_t));
        ch->id = id;
        ch->name = strdup(name);
        memset(ch, 0, sizeof(channel_t));
        ch->id = id;
        ch->name = strdup(name);
@@ -222,9 +221,7 @@ void Chan_init()
                else
                        ch_dst = ch_itr;
 
                else
                        ch_dst = ch_itr;
 
-               chl = malloc(sizeof(channellist_t));
-               if(!chl)
-                       Log_fatal("Out of memory");
+               chl = Memory_safeMalloc(1, sizeof(channellist_t));
                chl->chan = ch_dst;
                init_list_entry(&chl->node);
                list_add_tail(&chl->node, &ch_src->channel_links);
                chl->chan = ch_dst;
                init_list_entry(&chl->node);
                list_add_tail(&chl->node, &ch_src->channel_links);
@@ -382,9 +379,7 @@ void Chan_buildTreeList(channel_t *ch, struct dlist *head)
        struct dlist *itr;
        channel_t *sub;
 
        struct dlist *itr;
        channel_t *sub;
 
-       chl = malloc(sizeof(channellist_t));
-       if(!chl)
-               Log_fatal("Out of memory");
+       chl = Memory_safeMalloc(1, sizeof(channellist_t));
        chl->chan = ch;
        init_list_entry(&chl->node);
        list_add_tail(&chl->node, head);
        chl->chan = ch;
        init_list_entry(&chl->node);
        list_add_tail(&chl->node, head);
index 1c5972b688e6e741c07be4161c85dcf8289a9656..579a1b66ed1f694223f4805cbd897e4cc99791ac 100644 (file)
@@ -36,6 +36,7 @@
 #include <stdlib.h>
 #include <string.h>
 #include "log.h"
 #include <stdlib.h>
 #include <string.h>
 #include "log.h"
+#include "memory.h"
 #include "list.h"
 #include "client.h"
 #include "ssl.h"
 #include "list.h"
 #include "client.h"
 #include "ssl.h"
@@ -115,9 +116,7 @@ void Client_janitor()
 
 void Client_codec_add(client_t *client, int codec)
 {
 
 void Client_codec_add(client_t *client, int codec)
 {
-       codec_t *cd = malloc(sizeof(codec_t));
-       if (cd == NULL)
-               Log_fatal("Out of memory");
+       codec_t *cd = Memory_safeMalloc(1, sizeof(codec_t));
        init_list_entry(&cd->node);
        cd->codec = codec;
        list_add_tail(&cd->node, &client->codecs);
        init_list_entry(&cd->node);
        cd->codec = codec;
        list_add_tail(&cd->node, &client->codecs);
@@ -157,9 +156,7 @@ void Client_token_add(client_t *client, char *token_string)
 
        if (client->tokencount >= MAX_TOKENS)
                return;
 
        if (client->tokencount >= MAX_TOKENS)
                return;
-       token = malloc(sizeof(token_t));
-       if (token == NULL)
-               Log_fatal("Out of memory");
+       token = Memory_safeMalloc(1, sizeof(token_t));
        init_list_entry(&token->node);
        token->token = strdup(token_string);
        if (token->token == NULL)
        init_list_entry(&token->node);
        token->token = strdup(token_string);
        if (token->token == NULL)
@@ -227,9 +224,7 @@ void recheckCodecVersions(client_t *connectingClient)
                                }
                        }
                        if (!found) {
                                }
                        }
                        if (!found) {
-                               cd = malloc(sizeof(codec_t));
-                               if (!cd)
-                                       Log_fatal("Out of memory");
+                               cd = Memory_safeMalloc(1, sizeof(codec_t));
                                memset(cd, 0, sizeof(codec_t));
                                init_list_entry(&cd->node);
                                cd->codec = codec_itr->codec;
                                memset(cd, 0, sizeof(codec_t));
                                init_list_entry(&cd->node);
                                cd->codec = codec_itr->codec;
@@ -332,8 +327,7 @@ int Client_add(int fd, struct sockaddr_storage *remote)
                return -1;
        }
 
                return -1;
        }
 
-       if ((newclient = calloc(1, sizeof(client_t))) == NULL)
-               Log_fatal("(%s:%s): Out of memory while allocating %d bytes.", __FILE__, __LINE__, sizeof(client_t));
+       newclient = Memory_safeCalloc(1, sizeof(client_t));
 
        newclient->tcpfd = fd;
        memcpy(&newclient->remote_tcp, remote, sizeof(struct sockaddr_storage));
 
        newclient->tcpfd = fd;
        memcpy(&newclient->remote_tcp, remote, sizeof(struct sockaddr_storage));
@@ -675,12 +669,8 @@ void Client_textmessage(client_t *client, char *text)
        uint32_t *tree_id;
        message_t *sendmsg = NULL;
 
        uint32_t *tree_id;
        message_t *sendmsg = NULL;
 
-       message = malloc(strlen(text) + 1);
-       if (!message)
-               Log_fatal("Out of memory");
-       tree_id = malloc(sizeof(uint32_t));
-       if (!tree_id)
-               Log_fatal("Out of memory");
+       message = Memory_safeMalloc(1, strlen(text) + 1);
+       tree_id = Memory_safeMalloc(1, sizeof(uint32_t));
        *tree_id = 0;
        sendmsg = Msg_create(TextMessage);
        sendmsg->payload.textMessage->message = message;
        *tree_id = 0;
        sendmsg = Msg_create(TextMessage);
        sendmsg->payload.textMessage->message = message;
@@ -1029,14 +1019,11 @@ static int Client_send_udp(client_t *client, uint8_t *data, int len)
        if (Util_clientAddressToPortUDP(client) != 0 && CryptState_isValid(&client->cryptState) &&
                client->bUDP) {
 #if defined(__LP64__)
        if (Util_clientAddressToPortUDP(client) != 0 && CryptState_isValid(&client->cryptState) &&
                client->bUDP) {
 #if defined(__LP64__)
-               buf = mbuf = malloc(len + 4 + 16);
+               buf = mbuf = Memory_safeMalloc(1, len + 4 + 16);
                buf += 4;
 #else
                buf += 4;
 #else
-               mbuf = buf = malloc(len + 4);
+               mbuf = buf = Memory_safeMalloc(1, len + 4);
 #endif
 #endif
-               if (mbuf == NULL)
-                       Log_fatal("Out of memory");
-
                CryptState_encrypt(&client->cryptState, data, buf, len);
 
 #if defined(__NetBSD__) || defined(__FreeBSD__) || defined(__OpenBSD__) || defined(__APPLE__)
                CryptState_encrypt(&client->cryptState, data, buf, len);
 
 #if defined(__NetBSD__) || defined(__FreeBSD__) || defined(__OpenBSD__) || defined(__APPLE__)
diff --git a/src/memory.c b/src/memory.c
new file mode 100644 (file)
index 0000000..1a0ed18
--- /dev/null
@@ -0,0 +1,61 @@
+/* Copyright (C) 2009-2014, Martin Johansson <martin@fatbob.nu>
+   Copyright (C) 2005-2014, Thorvald Natvig <thorvald@natvig.com>
+
+   All rights reserved.
+
+   Redistribution and use in source and binary forms, with or without
+   modification, are permitted provided that the following conditions
+   are met:
+
+   - Redistributions of source code must retain the above copyright notice,
+     this list of conditions and the following disclaimer.
+   - Redistributions in binary form must reproduce the above copyright notice,
+     this list of conditions and the following disclaimer in the documentation
+     and/or other materials provided with the distribution.
+   - Neither the name of the Developers nor the names of its contributors may
+     be used to endorse or promote products derived from this software without
+     specific prior written permission.
+
+   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+   ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+   A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR
+   CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
+   EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
+   PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
+   PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
+   LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
+   NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
+   SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+*/
+#include <stdint.h>
+#include <unistd.h>
+
+#include "log.h"
+#include "memory.h"
+
+void * Memory_safeMalloc(size_t nmem, size_t size) {
+       // Check if we're going to overflow.
+       if (size && num > SIZE_MAX / size)
+               Log_fatal("Request for memory would've overflowed.");
+
+       // Allocate the memory requested.
+       void * retPtr = malloc(nmem * size);
+
+       // Check if we had an error.
+       if (retPtr == NULL)
+               Log_fatal("Out of memory.");
+
+       return retPtr;
+}
+
+void * Memory_safeCalloc(size_t nmem, size_t size) {
+       // Allocate the memory requested.
+       void * retPtr = calloc(nmem, size);
+
+       // Check if we had an error.
+       if (retPtr == NULL)
+               Log_fatal("Out of memory.");
+
+       return retPtr;
+}
diff --git a/src/memory.h b/src/memory.h
new file mode 100644 (file)
index 0000000..39450b0
--- /dev/null
@@ -0,0 +1,33 @@
+/* Copyright (C) 2009-2014, Martin Johansson <martin@fatbob.nu>
+   Copyright (C) 2005-2014, Thorvald Natvig <thorvald@natvig.com>
+
+   All rights reserved.
+
+   Redistribution and use in source and binary forms, with or without
+   modification, are permitted provided that the following conditions
+   are met:
+
+   - Redistributions of source code must retain the above copyright notice,
+     this list of conditions and the following disclaimer.
+   - Redistributions in binary form must reproduce the above copyright notice,
+     this list of conditions and the following disclaimer in the documentation
+     and/or other materials provided with the distribution.
+   - Neither the name of the Developers nor the names of its contributors may
+     be used to endorse or promote products derived from this software without
+     specific prior written permission.
+
+   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+   ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+   A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR
+   CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
+   EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
+   PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
+   PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
+   LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
+   NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
+   SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+*/
+
+void * Memory_safeMalloc(size_t nmem, size_t size);
+void * Memory_safeCalloc(size_t nmem, size_t size);
index e93afb99ab488ad894dfd45fe8dc3a26f300455b..a8472871ae86f38ad5c391266eedb2053c11e974 100644 (file)
@@ -32,6 +32,7 @@
 #include <stdlib.h>
 
 #include "log.h"
 #include <stdlib.h>
 
 #include "log.h"
+#include "memory.h"
 #include "list.h"
 #include "client.h"
 #include "messages.h"
 #include "list.h"
 #include "client.h"
 #include "messages.h"
@@ -284,10 +285,9 @@ void Mh_handle_message(client_t *client, message_t *msg)
                                sendmsg->payload.channelState->channel_id = ch_itr->id;
                                sendmsg->payload.channelState->n_links = ch_itr->linkcount;
 
                                sendmsg->payload.channelState->channel_id = ch_itr->id;
                                sendmsg->payload.channelState->n_links = ch_itr->linkcount;
 
-                               links = (uint32_t *)malloc(ch_itr->linkcount * sizeof(uint32_t));
-                               if(!links)
-                                       Log_fatal("Out of memory");
-
+                               links = (uint32_t*)Memory_safeMalloc(
+                                       ch_itr->linkcount,
+                                       sizeof(uint32_t));
                                list_iterate(itr, &ch_itr->channel_links) { /* Iterate links */
                                        channellist_t *chl;
                                        channel_t *ch;
                                list_iterate(itr, &ch_itr->channel_links) { /* Iterate links */
                                        channellist_t *chl;
                                        channel_t *ch;
@@ -498,12 +498,8 @@ void Mh_handle_message(client_t *client, message_t *msg)
                        char *message;
                        uint32_t *tree_id;
 
                        char *message;
                        uint32_t *tree_id;
 
-                       message = malloc(strlen(client->username) + 32);
-                       if (!message)
-                               Log_fatal("Out of memory");
-                       tree_id = malloc(sizeof(uint32_t));
-                       if (!tree_id)
-                               Log_fatal("Out of memory");
+                       message = Memory_safeMalloc(1, strlen(client->username) + 32);
+                       tree_id = Memory_safeMalloc(1, sizeof(uint32_t));
                        *tree_id = 0;
                        sendmsg = Msg_create(TextMessage);
                        sendmsg->payload.textMessage->message = message;
                        *tree_id = 0;
                        sendmsg = Msg_create(TextMessage);
                        sendmsg->payload.textMessage->message = message;
@@ -560,9 +556,7 @@ void Mh_handle_message(client_t *client, message_t *msg)
                if (msg->payload.userState->has_plugin_context) {
                        if (client->context)
                                free(client->context);
                if (msg->payload.userState->has_plugin_context) {
                        if (client->context)
                                free(client->context);
-                       client->context = malloc(msg->payload.userState->plugin_context.len);
-                       if (client->context == NULL)
-                               Log_fatal("Out of memory");
+                       client->context = Memory_safeMalloc(1, msg->payload.userState->plugin_context.len);
                        memcpy(client->context, msg->payload.userState->plugin_context.data,
                                   msg->payload.userState->plugin_context.len);
 
                        memcpy(client->context, msg->payload.userState->plugin_context.data,
                                   msg->payload.userState->plugin_context.len);
 
@@ -866,9 +860,8 @@ void Mh_handle_message(client_t *client, message_t *msg)
                                sendmsg->payload.userStats->version->os_version = strdup(target->os_version);
 
                        sendmsg->payload.userStats->n_celt_versions = target->codec_count;
                                sendmsg->payload.userStats->version->os_version = strdup(target->os_version);
 
                        sendmsg->payload.userStats->n_celt_versions = target->codec_count;
-                       sendmsg->payload.userStats->celt_versions = malloc(sizeof(int32_t) * target->codec_count);
-                       if (!sendmsg->payload.userStats->celt_versions)
-                               Log_fatal("Out of memory");
+                       sendmsg->payload.userStats->celt_versions
+                               = Memory_safeMalloc(target->codec_count, sizeof(int32_t));
                        i = 0;
                        while (Client_codec_iterate(target, &codec_itr) != NULL)
                                sendmsg->payload.userStats->celt_versions[i++] = codec_itr->codec;
                        i = 0;
                        while (Client_codec_iterate(target, &codec_itr) != NULL)
                                sendmsg->payload.userStats->celt_versions[i++] = codec_itr->codec;
@@ -878,9 +871,8 @@ void Mh_handle_message(client_t *client, message_t *msg)
 
                        /* Address */
                        sendmsg->payload.userStats->has_address = true;
 
                        /* Address */
                        sendmsg->payload.userStats->has_address = true;
-                       sendmsg->payload.userStats->address.data = malloc(sizeof(uint8_t) * 16);
-                       if (!sendmsg->payload.userStats->address.data)
-                               Log_fatal("Out of memory");
+                       sendmsg->payload.userStats->address.data
+                               = Memory_safeMalloc(16, sizeof(uint8_t));
                        memset(sendmsg->payload.userStats->address.data, 0, 16);
                        /* ipv4 representation as ipv6 address. Supposedly correct. */
                        memset(&sendmsg->payload.userStats->address.data[10], 0xff, 2); /* IPv4 */
                        memset(sendmsg->payload.userStats->address.data, 0, 16);
                        /* ipv4 representation as ipv6 address. Supposedly correct. */
                        memset(&sendmsg->payload.userStats->address.data[10], 0xff, 2); /* IPv4 */
index 704f20dcb72d80ea160dad76c6f0d08c10663fd4..d661c9e356e674cdc1a69592179131c82b847ee6 100644 (file)
@@ -38,6 +38,7 @@
 #include "client.h"
 #include "pds.h"
 #include "log.h"
 #include "client.h"
 #include "pds.h"
 #include "log.h"
+#include "memory.h"
 
 #define PREAMBLE_SIZE 6
 
 
 #define PREAMBLE_SIZE 6
 
@@ -266,10 +267,8 @@ int Msg_messageToNetwork(message_t *msg, uint8_t *buffer)
 
 static message_t *Msg_create_nopayload(messageType_t messageType)
 {
 
 static message_t *Msg_create_nopayload(messageType_t messageType)
 {
-       message_t *msg = malloc(sizeof(message_t));
+       message_t *msg = Memory_safeMalloc(1, sizeof(message_t));
 
 
-       if (msg == NULL)
-               Log_fatal("Out of memory");
        memset(msg, 0, sizeof(message_t));
        msg->refcount = 1;
        msg->messageType = messageType;
        memset(msg, 0, sizeof(message_t));
        msg->refcount = 1;
        msg->messageType = messageType;
@@ -284,92 +283,89 @@ message_t *Msg_create(messageType_t messageType)
 
        switch (messageType) {
        case Version:
 
        switch (messageType) {
        case Version:
-               msg->payload.version = malloc(sizeof(MumbleProto__Version));
+               msg->payload.version = Memory_safeMalloc(1, sizeof(MumbleProto__Version));
                mumble_proto__version__init(msg->payload.version);
                break;
        case UDPTunnel:
                mumble_proto__version__init(msg->payload.version);
                break;
        case UDPTunnel:
-               msg->payload.UDPTunnel = malloc(sizeof(MumbleProto__UDPTunnel));
+               msg->payload.UDPTunnel = Memory_safeMalloc(1, sizeof(MumbleProto__UDPTunnel));
                mumble_proto__udptunnel__init(msg->payload.UDPTunnel);
                break;
        case Authenticate:
                mumble_proto__udptunnel__init(msg->payload.UDPTunnel);
                break;
        case Authenticate:
-               msg->payload.authenticate = malloc(sizeof(MumbleProto__Authenticate));
+               msg->payload.authenticate = Memory_safeMalloc(1, sizeof(MumbleProto__Authenticate));
                mumble_proto__authenticate__init(msg->payload.authenticate);
                break;
        case Ping:
                mumble_proto__authenticate__init(msg->payload.authenticate);
                break;
        case Ping:
-               msg->payload.ping = malloc(sizeof(MumbleProto__Ping));
+               msg->payload.ping = Memory_safeMalloc(1, sizeof(MumbleProto__Ping));
                mumble_proto__ping__init(msg->payload.ping);
                break;
        case Reject:
                mumble_proto__ping__init(msg->payload.ping);
                break;
        case Reject:
-               msg->payload.reject = malloc(sizeof(MumbleProto__Reject));
+               msg->payload.reject = Memory_safeMalloc(1, sizeof(MumbleProto__Reject));
                mumble_proto__reject__init(msg->payload.reject);
                break;
        case ServerSync:
                mumble_proto__reject__init(msg->payload.reject);
                break;
        case ServerSync:
-               msg->payload.serverSync = malloc(sizeof(MumbleProto__ServerSync));
+               msg->payload.serverSync = Memory_safeMalloc(1, sizeof(MumbleProto__ServerSync));
                mumble_proto__server_sync__init(msg->payload.serverSync);
                break;
        case TextMessage:
                mumble_proto__server_sync__init(msg->payload.serverSync);
                break;
        case TextMessage:
-               msg->payload.textMessage = malloc(sizeof(MumbleProto__TextMessage));
+               msg->payload.textMessage = Memory_safeMalloc(1, sizeof(MumbleProto__TextMessage));
                mumble_proto__text_message__init(msg->payload.textMessage);
                break;
        case PermissionDenied:
                mumble_proto__text_message__init(msg->payload.textMessage);
                break;
        case PermissionDenied:
-               msg->payload.permissionDenied = malloc(sizeof(MumbleProto__PermissionDenied));
+               msg->payload.permissionDenied = Memory_safeMalloc(1, sizeof(MumbleProto__PermissionDenied));
                mumble_proto__permission_denied__init(msg->payload.permissionDenied);
                break;
        case CryptSetup:
                mumble_proto__permission_denied__init(msg->payload.permissionDenied);
                break;
        case CryptSetup:
-               msg->payload.cryptSetup = malloc(sizeof(MumbleProto__CryptSetup));
+               msg->payload.cryptSetup = Memory_safeMalloc(1, sizeof(MumbleProto__CryptSetup));
                mumble_proto__crypt_setup__init(msg->payload.cryptSetup);
                break;
        case UserList:
                mumble_proto__crypt_setup__init(msg->payload.cryptSetup);
                break;
        case UserList:
-               msg->payload.userList = malloc(sizeof(MumbleProto__UserList));
+               msg->payload.userList = Memory_safeMalloc(1, sizeof(MumbleProto__UserList));
                mumble_proto__user_list__init(msg->payload.userList);
                break;
        case UserState:
                mumble_proto__user_list__init(msg->payload.userList);
                break;
        case UserState:
-               msg->payload.userState = malloc(sizeof(MumbleProto__UserState));
+               msg->payload.userState = Memory_safeMalloc(1, sizeof(MumbleProto__UserState));
                mumble_proto__user_state__init(msg->payload.userState);
                break;
        case ChannelState:
                mumble_proto__user_state__init(msg->payload.userState);
                break;
        case ChannelState:
-               msg->payload.channelState = malloc(sizeof(MumbleProto__ChannelState));
+               msg->payload.channelState = Memory_safeMalloc(1, sizeof(MumbleProto__ChannelState));
                mumble_proto__channel_state__init(msg->payload.channelState);
                break;
        case UserRemove:
                mumble_proto__channel_state__init(msg->payload.channelState);
                break;
        case UserRemove:
-               msg->payload.userRemove = malloc(sizeof(MumbleProto__UserRemove));
+               msg->payload.userRemove = Memory_safeMalloc(1, sizeof(MumbleProto__UserRemove));
                mumble_proto__user_remove__init(msg->payload.userRemove);
                break;
        case VoiceTarget:
                mumble_proto__user_remove__init(msg->payload.userRemove);
                break;
        case VoiceTarget:
-               msg->payload.voiceTarget = malloc(sizeof(MumbleProto__VoiceTarget));
+               msg->payload.voiceTarget = Memory_safeMalloc(1, sizeof(MumbleProto__VoiceTarget));
                mumble_proto__voice_target__init(msg->payload.voiceTarget);
                break;
        case CodecVersion:
                mumble_proto__voice_target__init(msg->payload.voiceTarget);
                break;
        case CodecVersion:
-               msg->payload.codecVersion = malloc(sizeof(MumbleProto__CodecVersion));
+               msg->payload.codecVersion = Memory_safeMalloc(1, sizeof(MumbleProto__CodecVersion));
                mumble_proto__codec_version__init(msg->payload.codecVersion);
                break;
        case PermissionQuery:
                mumble_proto__codec_version__init(msg->payload.codecVersion);
                break;
        case PermissionQuery:
-               msg->payload.permissionQuery = malloc(sizeof(MumbleProto__PermissionQuery));
+               msg->payload.permissionQuery = Memory_safeMalloc(1, sizeof(MumbleProto__PermissionQuery));
                mumble_proto__permission_query__init(msg->payload.permissionQuery);
                break;
        case ChannelRemove:
                mumble_proto__permission_query__init(msg->payload.permissionQuery);
                break;
        case ChannelRemove:
-               msg->payload.channelRemove = malloc(sizeof(MumbleProto__ChannelRemove));
+               msg->payload.channelRemove = Memory_safeMalloc(1, sizeof(MumbleProto__ChannelRemove));
                mumble_proto__channel_remove__init(msg->payload.channelRemove);
                break;
        case UserStats:
                mumble_proto__channel_remove__init(msg->payload.channelRemove);
                break;
        case UserStats:
-               msg->payload.userStats = malloc(sizeof(MumbleProto__UserStats));
+               msg->payload.userStats = Memory_safeMalloc(1, sizeof(MumbleProto__UserStats));
                mumble_proto__user_stats__init(msg->payload.userStats);
 
                mumble_proto__user_stats__init(msg->payload.userStats);
 
-               msg->payload.userStats->from_client = malloc(sizeof(MumbleProto__UserStats__Stats));
+               msg->payload.userStats->from_client = Memory_safeMalloc(1, sizeof(MumbleProto__UserStats__Stats));
                mumble_proto__user_stats__stats__init(msg->payload.userStats->from_client);
 
                mumble_proto__user_stats__stats__init(msg->payload.userStats->from_client);
 
-               msg->payload.userStats->from_server = malloc(sizeof(MumbleProto__UserStats__Stats));
+               msg->payload.userStats->from_server = Memory_safeMalloc(1, sizeof(MumbleProto__UserStats__Stats));
                mumble_proto__user_stats__stats__init(msg->payload.userStats->from_server);
 
                mumble_proto__user_stats__stats__init(msg->payload.userStats->from_server);
 
-               msg->payload.userStats->version = malloc(sizeof(MumbleProto__Version));
+               msg->payload.userStats->version = Memory_safeMalloc(1, sizeof(MumbleProto__Version));
                mumble_proto__version__init(msg->payload.userStats->version);
 
                mumble_proto__version__init(msg->payload.userStats->version);
 
-               if (!msg->payload.userStats || !msg->payload.userStats->from_client ||
-                       !msg->payload.userStats->from_server || !msg->payload.userStats->version)
-                       Log_fatal("Out of memory");
                break;
        case ServerConfig:
                break;
        case ServerConfig:
-               msg->payload.serverConfig = malloc(sizeof(MumbleProto__ServerConfig));
+               msg->payload.serverConfig = Memory_safeMalloc(1, sizeof(MumbleProto__ServerConfig));
                mumble_proto__server_config__init(msg->payload.serverConfig);
                break;
 
                mumble_proto__server_config__init(msg->payload.serverConfig);
                break;
 
@@ -386,19 +382,13 @@ message_t *Msg_banList_create(int n_bans)
        message_t *msg = Msg_create_nopayload(BanList);
        int i;
 
        message_t *msg = Msg_create_nopayload(BanList);
        int i;
 
-       msg->payload.banList = malloc(sizeof(MumbleProto__BanList));
-       if (msg->payload.banList == NULL)
-               Log_fatal("Out of memory");
+       msg->payload.banList = Memory_safeMalloc(1, sizeof(MumbleProto__BanList));
        memset(msg->payload.banList, 0, sizeof(MumbleProto__BanList));
        mumble_proto__ban_list__init(msg->payload.banList);
        msg->payload.banList->n_bans = n_bans;
        memset(msg->payload.banList, 0, sizeof(MumbleProto__BanList));
        mumble_proto__ban_list__init(msg->payload.banList);
        msg->payload.banList->n_bans = n_bans;
-       msg->payload.banList->bans = malloc(sizeof(MumbleProto__BanList__BanEntry *) * n_bans);
-       if (msg->payload.banList->bans == NULL)
-               Log_fatal("Out of memory");
+       msg->payload.banList->bans = Memory_safeMalloc(1, sizeof(MumbleProto__BanList__BanEntry *) * n_bans);
        for (i = 0; i < n_bans; i++) {
        for (i = 0; i < n_bans; i++) {
-               msg->payload.banList->bans[i] = malloc(sizeof(MumbleProto__BanList__BanEntry));
-               if (msg->payload.banList->bans[i] == NULL)
-                       Log_fatal("Out of memory");
+               msg->payload.banList->bans[i] = Memory_safeMalloc(1, sizeof(MumbleProto__BanList__BanEntry));
                memset(msg->payload.banList->bans[i], 0, sizeof(MumbleProto__BanList__BanEntry));
                mumble_proto__ban_list__ban_entry__init(msg->payload.banList->bans[i]);
        }
                memset(msg->payload.banList->bans[i], 0, sizeof(MumbleProto__BanList__BanEntry));
                mumble_proto__ban_list__ban_entry__init(msg->payload.banList->bans[i]);
        }
@@ -410,9 +400,7 @@ void Msg_banList_addEntry(message_t *msg, int index, uint8_t *address, uint32_t
 {
        MumbleProto__BanList__BanEntry *entry = msg->payload.banList->bans[index];
 
 {
        MumbleProto__BanList__BanEntry *entry = msg->payload.banList->bans[index];
 
-       entry->address.data = malloc(16);
-       if (!entry->address.data)
-               Log_fatal("Out of memory");
+       entry->address.data = Memory_safeMalloc(1, 16);
        memcpy(entry->address.data, address, 16);
        entry->address.len = 16;
        entry->mask = mask;
        memcpy(entry->address.data, address, 16);
        entry->address.len = 16;
        entry->mask = mask;
@@ -674,12 +662,8 @@ message_t *Msg_CreateVoiceMsg(uint8_t *data, int size)
 
        msg = Msg_create_nopayload(UDPTunnel);
        msg->unpacked = false;
 
        msg = Msg_create_nopayload(UDPTunnel);
        msg->unpacked = false;
-       msg->payload.UDPTunnel = malloc(sizeof(struct _MumbleProto__UDPTunnel));
-       if (msg->payload.UDPTunnel == NULL)
-               Log_fatal("Out of memory");
-       msg->payload.UDPTunnel->packet.data = malloc(size);
-       if (msg->payload.UDPTunnel->packet.data == NULL)
-               Log_fatal("Out of memory");
+       msg->payload.UDPTunnel = Memory_safeMalloc(1, sizeof(struct _MumbleProto__UDPTunnel));
+       msg->payload.UDPTunnel->packet.data = Memory_safeMalloc(1, size);
        memcpy(msg->payload.UDPTunnel->packet.data, data, size);
        msg->payload.UDPTunnel->packet.len = size;
        return msg;
        memcpy(msg->payload.UDPTunnel->packet.data, data, size);
        msg->payload.UDPTunnel->packet.len = size;
        return msg;
index 066666f13b6c402ac4c684e3e321907246d9b3a6..88339d0bd907ec2c81e6f3efef75bb055818dd38 100644 (file)
--- a/src/pds.c
+++ b/src/pds.c
@@ -32,6 +32,7 @@
 #include <stdlib.h>
 #include "pds.h"
 #include "log.h"
 #include <stdlib.h>
 #include "pds.h"
 #include "log.h"
+#include "memory.h"
 
 /*
  * Data serialization functions below
 
 /*
  * Data serialization functions below
@@ -117,9 +118,7 @@ static inline uint64_t next(pds_t *pds)
 
 pds_t *Pds_create(uint8_t *buf, int size)
 {
 
 pds_t *Pds_create(uint8_t *buf, int size)
 {
-       pds_t *pds = malloc(sizeof(pds_t));
-       if (pds == NULL)
-               Log_fatal("Out of memory");
+       pds_t *pds = Memory_safeMalloc(1, sizeof(pds_t));
        pds->data = buf;
        pds->offset = pds->overshoot = 0;
        pds->maxsize = size;
        pds->data = buf;
        pds->offset = pds->overshoot = 0;
        pds->maxsize = size;
index 8aa356c58c2f0df6e69f580f947e8137dd4183c7..a56f7b857bf7236cb1e1bbc6a63551b4727799d2 100644 (file)
@@ -44,6 +44,7 @@
 #include "client.h"
 #include "conf.h"
 #include "log.h"
 #include "client.h"
 #include "conf.h"
 #include "log.h"
+#include "memory.h"
 #include "timer.h"
 #include "version.h"
 #include "util.h"
 #include "timer.h"
 #include "version.h"
 #include "util.h"
@@ -94,18 +95,11 @@ void checkIPversions()
 /* Initialize the address structures for IPv4 and IPv6 */
 struct sockaddr_storage** Server_setupAddressesAndPorts()
 {
 /* Initialize the address structures for IPv4 and IPv6 */
 struct sockaddr_storage** Server_setupAddressesAndPorts()
 {
-       struct sockaddr_storage** addresses = calloc(2, sizeof(void*));
-       if(!addresses)
-               Log_fatal("Not enough memory to allocate addresses");
+       struct sockaddr_storage** addresses = Memory_safeCalloc(2, sizeof(void*));
 
 
-       struct sockaddr_storage* v4address = calloc(1, sizeof(struct sockaddr_storage));
-       if(!v4address)
-               Log_fatal("Not enough memory to allocate IPv4 address");
+       struct sockaddr_storage* v4address = Memory_safeCalloc(1, sizeof(struct sockaddr_storage));
        v4address->ss_family = AF_INET;
        v4address->ss_family = AF_INET;
-
-       struct sockaddr_storage* v6address = calloc(1, sizeof(struct sockaddr_storage));
-       if(!v6address)
-               Log_fatal("Not enough memory to allocate IPv6 address");
+       struct sockaddr_storage* v6address = Memory_safeCalloc(1, sizeof(struct sockaddr_storage));
        v6address->ss_family = AF_INET6;
 
 #if defined(__NetBSD__) || defined(__FreeBSD__) || defined(__OpenBSD__) || defined(__APPLE__)
        v6address->ss_family = AF_INET6;
 
 #if defined(__NetBSD__) || defined(__FreeBSD__) || defined(__OpenBSD__) || defined(__APPLE__)
@@ -268,8 +262,7 @@ void Server_setupUDPSockets(struct sockaddr_storage* addresses[2], struct pollfd
        int val = 0;
        int sockets[2] = {-1, -1};
 
        int val = 0;
        int sockets[2] = {-1, -1};
 
-       if((udpsocks = calloc(nofServerSocks / 2, sizeof(int))) == NULL)
-               Log_fatal("Out of memory (%s:%s)", __FILE__, __LINE__);
+       udpsocks = Memory_safeCalloc(nofServerSocks / 2, sizeof(int))
 
        if (hasv4) {
                sockets[0] = socket(PF_INET, SOCK_DGRAM, 0);
 
        if (hasv4) {
                sockets[0] = socket(PF_INET, SOCK_DGRAM, 0);
@@ -322,8 +315,7 @@ void Server_run()
        checkIPversions();
 
        /* max clients + server sokets + client connecting that will be disconnected */
        checkIPversions();
 
        /* max clients + server sokets + client connecting that will be disconnected */
-       if ((pollfds = calloc((getIntConf(MAX_CLIENTS) + nofServerSocks + 1) , sizeof(struct pollfd))) == NULL)
-               Log_fatal("out of memory");
+       pollfds = Memory_safeCalloc((getIntConf(MAX_CLIENTS) + nofServerSocks + 1) , sizeof(struct pollfd))
 
        /* Figure out bind address and port */
        struct sockaddr_storage** addresses = Server_setupAddressesAndPorts();
 
        /* Figure out bind address and port */
        struct sockaddr_storage** addresses = Server_setupAddressesAndPorts();
index ac5eb90eca12c404fee3e3b2967fa0b56e80c70a..632ec57e2570980d4e4fcf751cc4ff99a5869e5d 100644 (file)
@@ -31,6 +31,7 @@
 #include "ssl.h"
 #include "conf.h"
 #include "log.h"
 #include "ssl.h"
 #include "conf.h"
 #include "log.h"
+#include "memory.h"
 
 #include <stdlib.h>
 
 
 #include <stdlib.h>
 
@@ -97,7 +98,8 @@ void SSLi_deinit()
 
 SSL_handle_t * SSLi_newconnection( int * fileDescriptor, bool_t * isSSLReady )
 {
 
 SSL_handle_t * SSLi_newconnection( int * fileDescriptor, bool_t * isSSLReady )
 {
-       gnutls_session_t * session = calloc(1, sizeof(gnutls_session_t));
+       gnutls_session_t * session
+               = Memory_safeCalloc(1, sizeof(gnutls_session_t));
 
        gnutls_init(session, GNUTLS_SERVER);
        gnutls_priority_set(*session, cipherCache);
 
        gnutls_init(session, GNUTLS_SERVER);
        gnutls_priority_set(*session, cipherCache);
index 65a21deab05b09296e4f6cc1b03e701a234c4259..ee839b8e39412fb084e7f4e1d02e75367ffb3449 100644 (file)
@@ -33,6 +33,7 @@
 
 #include "conf.h"
 #include "log.h"
 
 #include "conf.h"
 #include "log.h"
+#include "memory.h"
 #include "ssl.h"
 
 /*
 #include "ssl.h"
 
 /*
@@ -250,9 +251,7 @@ void SSLi_init(void)
                        Log_debug("%s", SSL_CIPHER_get_name(cipher));
                        cipherstringlen += strlen(SSL_CIPHER_get_name(cipher)) + 1;
                }
                        Log_debug("%s", SSL_CIPHER_get_name(cipher));
                        cipherstringlen += strlen(SSL_CIPHER_get_name(cipher)) + 1;
                }
-               cipherstring = malloc(cipherstringlen + 1);
-               if (cipherstring == NULL)
-                       Log_fatal("Out of memory");
+               cipherstring = Memory_safeMalloc(1, cipherstringlen + 1);
                for (i = 0; (cipher = sk_SSL_CIPHER_value(cipherlist_new, i)) != NULL; i++) {
                        offset += sprintf(cipherstring + offset, "%s:", SSL_CIPHER_get_name(cipher));
                }
                for (i = 0; (cipher = sk_SSL_CIPHER_value(cipherlist_new, i)) != NULL; i++) {
                        offset += sprintf(cipherstring + offset, "%s:", SSL_CIPHER_get_name(cipher));
                }
@@ -328,10 +327,7 @@ bool_t SSLi_getSHA1Hash(SSL_handle_t *ssl, uint8_t *hash)
        }
 
        len = i2d_X509(x509, NULL);
        }
 
        len = i2d_X509(x509, NULL);
-       buf = malloc(len);
-       if (buf == NULL) {
-               Log_fatal("malloc");
-       }
+       buf = Memory_safeMalloc(1, len);
 
        p = buf;
        i2d_X509(x509, &p);
 
        p = buf;
        i2d_X509(x509, &p);
index 167637b9d71fed4ad83787bfba2622f5bd5d7c7f..f5eef213dc107afe45867589e5ce35c964626337 100644 (file)
@@ -30,6 +30,7 @@
 */
 #include "conf.h"
 #include "log.h"
 */
 #include "conf.h"
 #include "log.h"
+#include "memory.h"
 #include "ssl.h"
 
 #include <stdlib.h>
 #include "ssl.h"
 
 #include <stdlib.h>
@@ -269,10 +270,8 @@ SSL_handle_t *SSLi_newconnection(int *fd, bool_t *SSLready)
        ssl_session *ssn;
        int rc;
 
        ssl_session *ssn;
        int rc;
 
-       ssl = calloc(1, sizeof(ssl_context));
-       ssn = calloc(1, sizeof(ssl_session));
-       if (!ssl || !ssn)
-               Log_fatal("Out of memory");
+       ssl = Memory_safeCalloc(1, sizeof(ssl_context));
+       ssn = Memory_safeCalloc(1, sizeof(ssl_session));
 
        rc = ssl_init(ssl);
        if (rc != 0 )
 
        rc = ssl_init(ssl);
        if (rc != 0 )
index 29792e44c27d72180c737ea076679c36c53a03c0..c933ab9361e5d5c3c72044667f737467bf65feee 100644 (file)
    SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */
 #include "util.h"
    SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */
 #include "util.h"
+#include "memory.h"
 
 char* Util_addressToString(struct sockaddr_storage *address)
 {
        char* addressString = NULL;
 
        if (address->ss_family == AF_INET) {
 
 char* Util_addressToString(struct sockaddr_storage *address)
 {
        char* addressString = NULL;
 
        if (address->ss_family == AF_INET) {
-               addressString = malloc(INET_ADDRSTRLEN);
+               addressString = Memory_safeMalloc(1, INET_ADDRSTRLEN);
                inet_ntop(AF_INET, &((struct sockaddr_in *)address)->sin_addr, addressString, INET_ADDRSTRLEN);
        } else if(address->ss_family == AF_INET6) {
                inet_ntop(AF_INET, &((struct sockaddr_in *)address)->sin_addr, addressString, INET_ADDRSTRLEN);
        } else if(address->ss_family == AF_INET6) {
-               addressString = malloc(INET6_ADDRSTRLEN);
+               addressString = Memory_safeMalloc(1, INET6_ADDRSTRLEN);
                inet_ntop(AF_INET6, &((struct sockaddr_in6 *)address)->sin6_addr, addressString, INET6_ADDRSTRLEN);
        }
 
                inet_ntop(AF_INET6, &((struct sockaddr_in6 *)address)->sin6_addr, addressString, INET6_ADDRSTRLEN);
        }
 
index e2ed569c8f2db196c6d129404503a93355848e7d..66de959ce30dbead14633b58b1c922984b6a72a2 100644 (file)
@@ -33,6 +33,7 @@
 #include <string.h>
 #include "voicetarget.h"
 #include "log.h"
 #include <string.h>
 #include "voicetarget.h"
 #include "log.h"
+#include "memory.h"
 
 void Voicetarget_add_session(client_t *client, int targetId, int sessionId)
 {
 
 void Voicetarget_add_session(client_t *client, int targetId, int sessionId)
 {
@@ -83,10 +84,7 @@ void Voicetarget_add_id(client_t *client, int targetId)
        int i;
 
        Voicetarget_del_id(client, targetId);
        int i;
 
        Voicetarget_del_id(client, targetId);
-       newtarget = malloc(sizeof(voicetarget_t));
-       if (!newtarget)
-               Log_fatal("Out of memory");
-       memset(newtarget, 0, sizeof(voicetarget_t));
+       newtarget = Memory_safeCalloc(1, sizeof(voicetarget_t));
        for (i = 0; i < TARGET_MAX_CHANNELS; i++)
                newtarget->channels[i].channel = -1;
        for (i = 0; i < TARGET_MAX_SESSIONS; i++)
        for (i = 0; i < TARGET_MAX_CHANNELS; i++)
                newtarget->channels[i].channel = -1;
        for (i = 0; i < TARGET_MAX_SESSIONS; i++)