Skip to content

Commit

Permalink
cleanup: Small improvements found by PVS Studio.
Browse files Browse the repository at this point in the history
  • Loading branch information
iphydf committed Feb 9, 2024
1 parent 9fe18b1 commit 49068b0
Show file tree
Hide file tree
Showing 6 changed files with 17 additions and 42 deletions.
8 changes: 2 additions & 6 deletions toxcore/announce.c
Original file line number Diff line number Diff line change
Expand Up @@ -239,9 +239,7 @@ bool announce_store_data(Announcements *announce, const uint8_t *data_public_key
if (length > 0) {
assert(data != nullptr);

if (entry->data != nullptr) {
free(entry->data);
}
free(entry->data);

uint8_t *entry_data = (uint8_t *)malloc(length);

Expand Down Expand Up @@ -702,9 +700,7 @@ void kill_announcements(Announcements *announce)
shared_key_cache_free(announce->shared_keys);

for (uint32_t i = 0; i < ANNOUNCE_BUCKETS * ANNOUNCE_BUCKET_SIZE; ++i) {
if (announce->entries[i].data != nullptr) {
free(announce->entries[i].data);
}
free(announce->entries[i].data);
}

free(announce);
Expand Down
28 changes: 7 additions & 21 deletions toxcore/group_chats.c
Original file line number Diff line number Diff line change
Expand Up @@ -2597,19 +2597,15 @@ void gc_get_chat_id(const GC_Chat *chat, uint8_t *dest)
non_null()
static bool send_self_to_peer(const GC_Chat *chat, GC_Connection *gconn)
{
GC_Peer *self = (GC_Peer *)calloc(1, sizeof(GC_Peer));
static_assert(sizeof(GC_Peer) < 2048, "GC_Peer is too large for stack allocation");
GC_Peer self = {0};

if (self == nullptr) {
return false;
}

copy_self(chat, self);
copy_self(chat, &self);

const uint16_t data_size = PACKED_GC_PEER_SIZE + sizeof(uint16_t) + MAX_GC_PASSWORD_SIZE;
uint8_t *data = (uint8_t *)malloc(data_size);

if (data == nullptr) {
free(self);
return false;
}

Expand All @@ -2623,11 +2619,9 @@ static bool send_self_to_peer(const GC_Chat *chat, GC_Connection *gconn)
length += MAX_GC_PASSWORD_SIZE;
}

const int packed_len = pack_gc_peer(data + length, data_size - length, self);
const int packed_len = pack_gc_peer(data + length, data_size - length, &self);
length += packed_len;

free(self);

if (packed_len <= 0) {
LOGGER_DEBUG(chat->log, "pack_gc_peer failed in handle_gc_peer_info_request_request %d", packed_len);
free(data);
Expand Down Expand Up @@ -2747,26 +2741,18 @@ static int handle_gc_peer_info_response(const GC_Session *c, GC_Chat *chat, uint
return -1;
}

GC_Peer *peer_info = (GC_Peer *)calloc(1, sizeof(GC_Peer));
GC_Peer peer_info = {0};

if (peer_info == nullptr) {
return -8;
}

if (unpack_gc_peer(peer_info, data + unpacked_len, length - unpacked_len) == -1) {
if (unpack_gc_peer(&peer_info, data + unpacked_len, length - unpacked_len) == -1) {
LOGGER_ERROR(chat->log, "unpack_gc_peer() failed");
free(peer_info);
return -6;
}

if (peer_update(chat, peer_info, peer_number) == -1) {
if (peer_update(chat, &peer_info, peer_number) == -1) {
LOGGER_WARNING(chat->log, "peer_update() failed");
free(peer_info);
return -6;
}

free(peer_info);

const bool was_confirmed = gconn->confirmed;
gconn->confirmed = true;

Expand Down
4 changes: 1 addition & 3 deletions toxcore/group_connection.c
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,7 @@ static bool array_entry_is_empty(const GC_Message_Array_Entry *array_entry)
non_null()
static void clear_array_entry(GC_Message_Array_Entry *const array_entry)
{
if (array_entry->data != nullptr) {
free(array_entry->data);
}
free(array_entry->data);

*array_entry = (GC_Message_Array_Entry) {
nullptr
Expand Down
4 changes: 1 addition & 3 deletions toxcore/group_moderation.c
Original file line number Diff line number Diff line change
Expand Up @@ -864,9 +864,7 @@ uint16_t sanctions_list_replace_sig(Moderation *moderation, const uint8_t *publi

void sanctions_list_cleanup(Moderation *moderation)
{
if (moderation->sanctions != nullptr) {
free(moderation->sanctions);
}
free(moderation->sanctions);

moderation->sanctions = nullptr;
moderation->num_sanctions = 0;
Expand Down
9 changes: 3 additions & 6 deletions toxcore/network_test_util.cc
Original file line number Diff line number Diff line change
Expand Up @@ -106,14 +106,11 @@ IP_Port random_ip_port(const Random *rng)
return ip_port;
}

bool operator==(Family const &a, Family const &b) { return a.value == b.value; }
bool operator==(Family a, Family b) { return a.value == b.value; }

bool operator==(IP4 const &a, IP4 const &b) { return a.uint32 == b.uint32; }
bool operator==(IP4 a, IP4 b) { return a.uint32 == b.uint32; }

bool operator==(IP6 const &a, IP6 const &b)
{
return a.uint64[0] == b.uint64[0] && a.uint64[1] == b.uint64[1];
}
bool operator==(IP6 a, IP6 b) { return a.uint64[0] == b.uint64[0] && a.uint64[1] == b.uint64[1]; }

bool operator==(IP const &a, IP const &b)
{
Expand Down
6 changes: 3 additions & 3 deletions toxcore/network_test_util.hh
Original file line number Diff line number Diff line change
Expand Up @@ -83,10 +83,10 @@ public:
IP_Port operator()();
};

bool operator==(Family const &a, Family const &b);
bool operator==(Family a, Family b);

bool operator==(IP4 const &a, IP4 const &b);
bool operator==(IP6 const &a, IP6 const &b);
bool operator==(IP4 a, IP4 b);
bool operator==(IP6 a, IP6 b);
bool operator==(IP const &a, IP const &b);
bool operator==(IP_Port const &a, IP_Port const &b);

Expand Down

0 comments on commit 49068b0

Please sign in to comment.