From: Dan Turner Date: Thu, 25 Jun 2015 15:05:51 +0000 (+0100) Subject: Removed null-pointer dereference in low mem. X-Git-Url: http://git.code-monkey.de/?p=umurmur.git;a=commitdiff_plain;h=a1a0ba19a94a1bfe3c40629aa9c5f3a4b23db3ee Removed null-pointer dereference in low mem. --- diff --git a/src/ban.c b/src/ban.c index 47f02ed..7028ea6 100644 --- a/src/ban.c +++ b/src/ban.c @@ -33,6 +33,7 @@ #include #include #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); @@ -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); - 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) { @@ -354,9 +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"); + 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) { diff --git a/src/channel.c b/src/channel.c index fdf4e82..da15a52 100644 --- a/src/channel.c +++ b/src/channel.c @@ -32,6 +32,7 @@ #include #include #include "log.h" +#include "memory.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; - 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); @@ -222,9 +221,7 @@ void Chan_init() 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); @@ -382,9 +379,7 @@ void Chan_buildTreeList(channel_t *ch, struct dlist *head) 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); diff --git a/src/client.c b/src/client.c index 1c5972b..579a1b6 100644 --- a/src/client.c +++ b/src/client.c @@ -36,6 +36,7 @@ #include #include #include "log.h" +#include "memory.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) { - 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); @@ -157,9 +156,7 @@ void Client_token_add(client_t *client, char *token_string) 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) @@ -227,9 +224,7 @@ void recheckCodecVersions(client_t *connectingClient) } } 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; @@ -332,8 +327,7 @@ int Client_add(int fd, struct sockaddr_storage *remote) 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)); @@ -675,12 +669,8 @@ void Client_textmessage(client_t *client, char *text) 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; @@ -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__) - buf = mbuf = malloc(len + 4 + 16); + buf = mbuf = Memory_safeMalloc(1, len + 4 + 16); buf += 4; #else - mbuf = buf = malloc(len + 4); + mbuf = buf = Memory_safeMalloc(1, len + 4); #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__) diff --git a/src/memory.c b/src/memory.c new file mode 100644 index 0000000..1a0ed18 --- /dev/null +++ b/src/memory.c @@ -0,0 +1,61 @@ +/* Copyright (C) 2009-2014, Martin Johansson + Copyright (C) 2005-2014, Thorvald Natvig + + 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 +#include + +#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 index 0000000..39450b0 --- /dev/null +++ b/src/memory.h @@ -0,0 +1,33 @@ +/* Copyright (C) 2009-2014, Martin Johansson + Copyright (C) 2005-2014, Thorvald Natvig + + 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); diff --git a/src/messagehandler.c b/src/messagehandler.c index e93afb9..a847287 100644 --- a/src/messagehandler.c +++ b/src/messagehandler.c @@ -32,6 +32,7 @@ #include #include "log.h" +#include "memory.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; - 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; @@ -498,12 +498,8 @@ void Mh_handle_message(client_t *client, message_t *msg) 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; @@ -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); - 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); @@ -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->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; @@ -878,9 +871,8 @@ void Mh_handle_message(client_t *client, message_t *msg) /* 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 */ diff --git a/src/messages.c b/src/messages.c index 704f20d..d661c9e 100644 --- a/src/messages.c +++ b/src/messages.c @@ -38,6 +38,7 @@ #include "client.h" #include "pds.h" #include "log.h" +#include "memory.h" #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) { - 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; @@ -284,92 +283,89 @@ message_t *Msg_create(messageType_t messageType) 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: - 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: - 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: - 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: - 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: - 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: - 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: - 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: - 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: - 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: - 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: - 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: - 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: - 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: - 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: - 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: - 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: - msg->payload.userStats = malloc(sizeof(MumbleProto__UserStats)); + msg->payload.userStats = Memory_safeMalloc(1, sizeof(MumbleProto__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); - 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); - 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); - 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: - 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; @@ -386,19 +382,13 @@ message_t *Msg_banList_create(int n_bans) 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; - 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++) { - 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]); } @@ -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]; - 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; @@ -674,12 +662,8 @@ message_t *Msg_CreateVoiceMsg(uint8_t *data, int size) 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; diff --git a/src/pds.c b/src/pds.c index 066666f..88339d0 100644 --- a/src/pds.c +++ b/src/pds.c @@ -32,6 +32,7 @@ #include #include "pds.h" #include "log.h" +#include "memory.h" /* * 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 = 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; diff --git a/src/server.c b/src/server.c index 8aa356c..a56f7b8 100644 --- a/src/server.c +++ b/src/server.c @@ -44,6 +44,7 @@ #include "client.h" #include "conf.h" #include "log.h" +#include "memory.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() { - 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; - - 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__) @@ -268,8 +262,7 @@ void Server_setupUDPSockets(struct sockaddr_storage* addresses[2], struct pollfd 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); @@ -322,8 +315,7 @@ void Server_run() 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(); diff --git a/src/ssli_gnutls.c b/src/ssli_gnutls.c index ac5eb90..632ec57 100644 --- a/src/ssli_gnutls.c +++ b/src/ssli_gnutls.c @@ -31,6 +31,7 @@ #include "ssl.h" #include "conf.h" #include "log.h" +#include "memory.h" #include @@ -97,7 +98,8 @@ void SSLi_deinit() 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); diff --git a/src/ssli_openssl.c b/src/ssli_openssl.c index 65a21de..ee839b8 100644 --- a/src/ssli_openssl.c +++ b/src/ssli_openssl.c @@ -33,6 +33,7 @@ #include "conf.h" #include "log.h" +#include "memory.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; } - 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)); } @@ -328,10 +327,7 @@ bool_t SSLi_getSHA1Hash(SSL_handle_t *ssl, uint8_t *hash) } 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); diff --git a/src/ssli_polarssl.c b/src/ssli_polarssl.c index 167637b..f5eef21 100644 --- a/src/ssli_polarssl.c +++ b/src/ssli_polarssl.c @@ -30,6 +30,7 @@ */ #include "conf.h" #include "log.h" +#include "memory.h" #include "ssl.h" #include @@ -269,10 +270,8 @@ SSL_handle_t *SSLi_newconnection(int *fd, bool_t *SSLready) 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 ) diff --git a/src/util.c b/src/util.c index 29792e4..c933ab9 100644 --- a/src/util.c +++ b/src/util.c @@ -28,16 +28,17 @@ 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) { - 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) { - addressString = malloc(INET6_ADDRSTRLEN); + addressString = Memory_safeMalloc(1, INET6_ADDRSTRLEN); inet_ntop(AF_INET6, &((struct sockaddr_in6 *)address)->sin6_addr, addressString, INET6_ADDRSTRLEN); } diff --git a/src/voicetarget.c b/src/voicetarget.c index e2ed569..66de959 100644 --- a/src/voicetarget.c +++ b/src/voicetarget.c @@ -33,6 +33,7 @@ #include #include "voicetarget.h" #include "log.h" +#include "memory.h" 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); - 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++)