Skip to content

Commit

Permalink
cppcheck
Browse files Browse the repository at this point in the history
  • Loading branch information
iphydf committed Feb 7, 2024
1 parent d3d92fd commit 7f9483e
Show file tree
Hide file tree
Showing 9 changed files with 39 additions and 41 deletions.
14 changes: 9 additions & 5 deletions auto_tests/toxav_many_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -168,29 +168,33 @@ static void test_av_three_calls(void)
Time_Data time_data;
pthread_mutex_init(&time_data.lock, nullptr);
{
Tox_Options *opts = tox_options_new(nullptr);
ck_assert(opts != nullptr);
tox_options_set_experimental_thread_safety(opts, true);
Tox_Err_New error;

bootstrap = tox_new_log(nullptr, &error, &index[0]);
bootstrap = tox_new_log(opts, &error, &index[0]);
ck_assert(error == TOX_ERR_NEW_OK);

time_data.clock = current_time_monotonic(bootstrap->mono_time);
set_current_time_callback(bootstrap, &time_data);

alice = tox_new_log(nullptr, &error, &index[1]);
alice = tox_new_log(opts, &error, &index[1]);
ck_assert(error == TOX_ERR_NEW_OK);
set_current_time_callback(alice, &time_data);

bobs[0] = tox_new_log(nullptr, &error, &index[2]);
bobs[0] = tox_new_log(opts, &error, &index[2]);
ck_assert(error == TOX_ERR_NEW_OK);
set_current_time_callback(bobs[0], &time_data);

bobs[1] = tox_new_log(nullptr, &error, &index[3]);
bobs[1] = tox_new_log(opts, &error, &index[3]);
ck_assert(error == TOX_ERR_NEW_OK);
set_current_time_callback(bobs[1], &time_data);

bobs[2] = tox_new_log(nullptr, &error, &index[4]);
bobs[2] = tox_new_log(opts, &error, &index[4]);
ck_assert(error == TOX_ERR_NEW_OK);
set_current_time_callback(bobs[2], &time_data);
tox_options_free(opts);
}

printf("Created 5 instances of Tox\n");
Expand Down
34 changes: 16 additions & 18 deletions toxav/rtp.c
Original file line number Diff line number Diff line change
Expand Up @@ -428,11 +428,8 @@ static int handle_video_packet(const Logger *log, RTPSession *session, const str
/**
* receive custom lossypackets and process them. they can be incoming audio or video packets
*/
void handle_rtp_packet(Tox *tox, uint32_t friend_number, const uint8_t *data, size_t pkt_length, void *object)
void handle_rtp_packet(Tox *tox, uint32_t friend_number, const uint8_t *data, size_t length, void *user_data)
{
// TODO(Zoff): is this ok?
uint16_t length = (uint16_t)pkt_length;

ToxAV *toxav = (ToxAV *)tox_get_av_object(tox);

if (toxav == nullptr) {
Expand Down Expand Up @@ -468,12 +465,13 @@ void handle_rtp_packet(Tox *tox, uint32_t friend_number, const uint8_t *data, si

// Get the packet type.
const uint8_t packet_type = data[0];
++data;
--length;
const uint8_t *payload = &data[1];
// TODO(Zoff): is this ok?
const uint16_t payload_size = (uint16_t)length - 1;

// Unpack the header.
struct RTPHeader header;
rtp_header_unpack(data, &header);
rtp_header_unpack(payload, &header);

if (header.pt != packet_type % 128) {
LOGGER_WARNING(log, "RTPHeader packet type and Tox protocol packet type did not agree: %d != %d",
Expand Down Expand Up @@ -504,19 +502,19 @@ void handle_rtp_packet(Tox *tox, uint32_t friend_number, const uint8_t *data, si
// The sender uses the new large-frame capable protocol and is sending a
// video packet.
if ((header.flags & RTP_LARGE_FRAME) != 0 && header.pt == (RTP_TYPE_VIDEO % 128)) {
handle_video_packet(log, session, &header, data + RTP_HEADER_SIZE, length - RTP_HEADER_SIZE);
handle_video_packet(log, session, &header, &payload[RTP_HEADER_SIZE], payload_size - RTP_HEADER_SIZE);
return;
}

// everything below here is for the old 16 bit protocol ------------------

if (header.data_length_lower == length - RTP_HEADER_SIZE) {
if (header.data_length_lower == payload_size - RTP_HEADER_SIZE) {
/* The message is sent in single part */

/* Message is not late; pick up the latest parameters */
session->rsequnum = header.sequnum;
session->rtimestamp = header.timestamp;
bwc_add_recv(session->bwc, length);
bwc_add_recv(session->bwc, payload_size);

/* Invoke processing of active multiparted message */
if (session->mp != nullptr) {
Expand All @@ -529,7 +527,7 @@ void handle_rtp_packet(Tox *tox, uint32_t friend_number, const uint8_t *data, si
/* The message came in the allowed time;
*/

session->mp = new_message(log, &header, length - RTP_HEADER_SIZE, data + RTP_HEADER_SIZE, length - RTP_HEADER_SIZE);
session->mp = new_message(log, &header, payload_size - RTP_HEADER_SIZE, &payload[RTP_HEADER_SIZE], payload_size - RTP_HEADER_SIZE);
Mono_Time *mt = toxav_get_av_mono_time(session->toxav);
assert(mt != nullptr);
session->mcb(mt, session->cs, session->mp);
Expand All @@ -552,18 +550,18 @@ void handle_rtp_packet(Tox *tox, uint32_t friend_number, const uint8_t *data, si
/* First case */

/* Make sure we have enough allocated memory */
if (session->mp->header.data_length_lower - session->mp->len < length - RTP_HEADER_SIZE ||
if (session->mp->header.data_length_lower - session->mp->len < payload_size - RTP_HEADER_SIZE ||
session->mp->header.data_length_lower <= header.offset_lower) {
/* There happened to be some corruption on the stream;
* continue wihtout this part
*/
return;
}

memcpy(session->mp->data + header.offset_lower, data + RTP_HEADER_SIZE,
length - RTP_HEADER_SIZE);
session->mp->len += length - RTP_HEADER_SIZE;
bwc_add_recv(session->bwc, length);
memcpy(session->mp->data + header.offset_lower, &payload[RTP_HEADER_SIZE],
payload_size - RTP_HEADER_SIZE);
session->mp->len += payload_size - RTP_HEADER_SIZE;
bwc_add_recv(session->bwc, payload_size);

if (session->mp->len == session->mp->header.data_length_lower) {
/* Received a full message; now push it for the further
Expand Down Expand Up @@ -600,11 +598,11 @@ void handle_rtp_packet(Tox *tox, uint32_t friend_number, const uint8_t *data, si
/* Message is not late; pick up the latest parameters */
session->rsequnum = header.sequnum;
session->rtimestamp = header.timestamp;
bwc_add_recv(session->bwc, length);
bwc_add_recv(session->bwc, payload_size);

/* Store message.
*/
session->mp = new_message(log, &header, header.data_length_lower, data + RTP_HEADER_SIZE, length - RTP_HEADER_SIZE);
session->mp = new_message(log, &header, header.data_length_lower, &payload[RTP_HEADER_SIZE], payload_size - RTP_HEADER_SIZE);

if (session->mp != nullptr) {
memmove(session->mp->data + header.offset_lower, session->mp->data, session->mp->len);
Expand Down
2 changes: 1 addition & 1 deletion toxav/rtp.h
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ typedef struct RTPSession {
} RTPSession;


void handle_rtp_packet(Tox *tox, uint32_t friendnumber, const uint8_t *data, size_t pkt_length, void *object);
void handle_rtp_packet(Tox *tox, uint32_t friend_number, const uint8_t *data, size_t length, void *user_data);

/**
* Serialise an RTPHeader to bytes to be sent over the network.
Expand Down
4 changes: 2 additions & 2 deletions toxcore/Messenger.c
Original file line number Diff line number Diff line change
Expand Up @@ -1839,7 +1839,7 @@ static int handle_filecontrol(Messenger *m, int32_t friendnumber, bool outbound,
}
}

static int m_handle_lossy_packet(void *object, int friendcon_id, const uint8_t *packet, uint16_t length,
static int m_handle_lossy_packet(void *object, int friendcon_id, const uint8_t *data, uint16_t length,
void *userdata)
{
Messenger *m = (Messenger *)object;
Expand All @@ -1849,7 +1849,7 @@ static int m_handle_lossy_packet(void *object, int friendcon_id, const uint8_t *
}

if (m->lossy_packethandler != nullptr) {
m->lossy_packethandler(m, friendcon_id, packet[0], packet, length, userdata);
m->lossy_packethandler(m, friendcon_id, data[0], data, length, userdata);
}

return 1;
Expand Down
1 change: 0 additions & 1 deletion toxcore/logger.c
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>

#include "attributes.h"
#include "ccompat.h"
Expand Down
2 changes: 0 additions & 2 deletions toxcore/logger.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@
#define C_TOXCORE_TOXCORE_LOGGER_H

#include <stdint.h>
#include <stdarg.h>
#include <pthread.h>

#include "attributes.h"

Expand Down
10 changes: 0 additions & 10 deletions toxcore/tox.h
Original file line number Diff line number Diff line change
Expand Up @@ -894,16 +894,6 @@ Tox *tox_new(const Tox_Options *options, Tox_Err_New *error);
*/
void tox_kill(Tox *tox);

/**
* @brief Get a Tox_Options similar to the one used to create the Tox.
*
* Initialises the `options` object such that `tox_new` called with the passed
* options will recreate the current @ref Tox instance.
*
* @param options the options object we want to initialise.
*/
void tox_get_options(Tox *tox, struct Tox_Options *options);

/**
* @brief Calculates the number of bytes required to store the tox instance with
* tox_get_savedata.
Expand Down
3 changes: 1 addition & 2 deletions toxcore/tox_api.c
Original file line number Diff line number Diff line change
Expand Up @@ -274,7 +274,6 @@ void tox_options_set_operating_system(Tox_Options *options, const Tox_System *op
options->operating_system = operating_system;
}


const uint8_t *tox_options_get_savedata_data(const Tox_Options *options)
{
return options->savedata_data;
Expand All @@ -297,7 +296,7 @@ void tox_options_default(Tox_Options *options)
tox_options_set_hole_punching_enabled(options, true);
tox_options_set_local_discovery_enabled(options, true);
tox_options_set_dht_announcements_enabled(options, true);
tox_options_set_experimental_thread_safety(options, true);
tox_options_set_experimental_thread_safety(options, false);
}
}

Expand Down
10 changes: 10 additions & 0 deletions toxcore/tox_private.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,16 @@ void tox_unlock(const Tox *tox);

const Tox_System *tox_get_system(Tox *tox);

/**
* @brief Get a Tox_Options similar to the one used to create the Tox.
*
* Initialises the `options` object such that `tox_new` called with the passed
* options will recreate the current @ref Tox instance.
*
* @param options the options object we want to initialise.
*/
void tox_get_options(Tox *tox, struct Tox_Options *options);

/**
* Set the callback for the `friend_lossy_packet` event for a specific packet ID.
* Pass NULL to unset.
Expand Down

0 comments on commit 7f9483e

Please sign in to comment.