From 7b2f6c34c6708e0e9d3cfb6a4090acd26f4f4a3c Mon Sep 17 00:00:00 2001 From: iphydf Date: Thu, 17 Feb 2022 22:03:32 +0000 Subject: [PATCH] cleanup: Apply stronger type checks and fix errors. Found a bug, too: file recv and chunk events were intermittently converting to `uint32_t`, losing precision. --- auto_tests/messenger_test.c | 2 +- testing/Messenger_test.c | 2 +- toxav/audio.c | 22 +++++++++++----------- toxav/audio.h | 18 +++++++++--------- toxav/toxav.c | 2 +- toxcore/Messenger.c | 8 +++++--- toxcore/Messenger.h | 6 ++---- toxcore/TCP_client.c | 2 +- toxcore/events/file_recv.c | 2 +- toxcore/events/file_recv_chunk.c | 2 +- toxcore/onion_client.c | 2 +- toxcore/tox.c | 2 +- 12 files changed, 35 insertions(+), 35 deletions(-) diff --git a/auto_tests/messenger_test.c b/auto_tests/messenger_test.c index 1f408f9b..3219da96 100644 --- a/auto_tests/messenger_test.c +++ b/auto_tests/messenger_test.c @@ -286,7 +286,7 @@ int main(void) options.port_range[0] = 41234; options.port_range[1] = 44234; options.log_callback = (logger_cb *)print_debug_log; - unsigned int err; + Messenger_Error err; m = new_messenger(mono_time, &options, &err); ck_assert(err == MESSENGER_ERROR_NONE); diff --git a/testing/Messenger_test.c b/testing/Messenger_test.c index 140c84d6..0921e1f5 100644 --- a/testing/Messenger_test.c +++ b/testing/Messenger_test.c @@ -100,7 +100,7 @@ int main(int argc, char *argv[]) Messenger_Options options = {0}; options.ipv6enabled = ipv6enabled; - unsigned int err; + Messenger_Error err; m = new_messenger(mono_time, &options, &err); if (!m) { diff --git a/toxav/audio.c b/toxav/audio.c index 536f6916..93dce5ce 100644 --- a/toxav/audio.c +++ b/toxav/audio.c @@ -18,11 +18,11 @@ static void jbuf_clear(struct JitterBuffer *q); static void jbuf_free(struct JitterBuffer *q); static int jbuf_write(const Logger *log, struct JitterBuffer *q, struct RTPMessage *m); static struct RTPMessage *jbuf_read(struct JitterBuffer *q, int32_t *success); -static OpusEncoder *create_audio_encoder(const Logger *log, int32_t bit_rate, int32_t sampling_rate, - int32_t channel_count); -static bool reconfigure_audio_encoder(const Logger *log, OpusEncoder **e, int32_t new_br, int32_t new_sr, - uint8_t new_ch, int32_t *old_br, int32_t *old_sr, int32_t *old_ch); -static bool reconfigure_audio_decoder(ACSession *ac, int32_t sampling_rate, int8_t channels); +static OpusEncoder *create_audio_encoder(const Logger *log, uint32_t bit_rate, uint32_t sampling_rate, + uint8_t channel_count); +static bool reconfigure_audio_encoder(const Logger *log, OpusEncoder **e, uint32_t new_br, uint32_t new_sr, + uint8_t new_ch, uint32_t *old_br, uint32_t *old_sr, uint8_t *old_ch); +static bool reconfigure_audio_decoder(ACSession *ac, uint32_t sampling_rate, uint8_t channels); @@ -242,7 +242,7 @@ int ac_queue_message(Mono_Time *mono_time, void *acp, struct RTPMessage *msg) return 0; } -int ac_reconfigure_encoder(ACSession *ac, int32_t bit_rate, int32_t sampling_rate, uint8_t channels) +int ac_reconfigure_encoder(ACSession *ac, uint32_t bit_rate, uint32_t sampling_rate, uint8_t channels) { if (!ac || !reconfigure_audio_encoder(ac->log, &ac->encoder, bit_rate, sampling_rate, channels, @@ -362,8 +362,8 @@ static struct RTPMessage *jbuf_read(struct JitterBuffer *q, int32_t *success) *success = 0; return nullptr; } -static OpusEncoder *create_audio_encoder(const Logger *log, int32_t bit_rate, int32_t sampling_rate, - int32_t channel_count) +static OpusEncoder *create_audio_encoder(const Logger *log, uint32_t bit_rate, uint32_t sampling_rate, + uint8_t channel_count) { int status = OPUS_OK; /* @@ -456,8 +456,8 @@ FAILURE: return nullptr; } -static bool reconfigure_audio_encoder(const Logger *log, OpusEncoder **e, int32_t new_br, int32_t new_sr, - uint8_t new_ch, int32_t *old_br, int32_t *old_sr, int32_t *old_ch) +static bool reconfigure_audio_encoder(const Logger *log, OpusEncoder **e, uint32_t new_br, uint32_t new_sr, + uint8_t new_ch, uint32_t *old_br, uint32_t *old_sr, uint8_t *old_ch) { /* Values are checked in toxav.c */ if (*old_sr != new_sr || *old_ch != new_ch) { @@ -488,7 +488,7 @@ static bool reconfigure_audio_encoder(const Logger *log, OpusEncoder **e, int32_ return true; } -static bool reconfigure_audio_decoder(ACSession *ac, int32_t sampling_rate, int8_t channels) +static bool reconfigure_audio_decoder(ACSession *ac, uint32_t sampling_rate, uint8_t channels) { if (sampling_rate != ac->ld_sample_rate || channels != ac->ld_channel_count) { if (current_time_monotonic(ac->mono_time) - ac->ldrts < 500) { diff --git a/toxav/audio.h b/toxav/audio.h index 4f335c29..358fe4e8 100644 --- a/toxav/audio.h +++ b/toxav/audio.h @@ -40,17 +40,17 @@ typedef struct ACSession { /* encoding */ OpusEncoder *encoder; - int32_t le_sample_rate; /* Last encoder sample rate */ - int32_t le_channel_count; /* Last encoder channel count */ - int32_t le_bit_rate; /* Last encoder bit rate */ + uint32_t le_sample_rate; /* Last encoder sample rate */ + uint8_t le_channel_count; /* Last encoder channel count */ + uint32_t le_bit_rate; /* Last encoder bit rate */ /* decoding */ OpusDecoder *decoder; - int32_t lp_channel_count; /* Last packet channel count */ - int32_t lp_sampling_rate; /* Last packet sample rate */ - int32_t lp_frame_duration; /* Last packet frame duration */ - int32_t ld_sample_rate; /* Last decoder sample rate */ - int32_t ld_channel_count; /* Last decoder channel count */ + uint8_t lp_channel_count; /* Last packet channel count */ + uint32_t lp_sampling_rate; /* Last packet sample rate */ + uint32_t lp_frame_duration; /* Last packet frame duration */ + uint32_t ld_sample_rate; /* Last decoder sample rate */ + uint8_t ld_channel_count; /* Last decoder channel count */ uint64_t ldrts; /* Last decoder reconfiguration time stamp */ void *j_buf; @@ -68,6 +68,6 @@ ACSession *ac_new(Mono_Time *mono_time, const Logger *log, ToxAV *av, uint32_t f void ac_kill(ACSession *ac); void ac_iterate(ACSession *ac); int ac_queue_message(Mono_Time *mono_time, void *acp, struct RTPMessage *msg); -int ac_reconfigure_encoder(ACSession *ac, int32_t bit_rate, int32_t sampling_rate, uint8_t channels); +int ac_reconfigure_encoder(ACSession *ac, uint32_t bit_rate, uint32_t sampling_rate, uint8_t channels); #endif // C_TOXCORE_TOXAV_AUDIO_H diff --git a/toxav/toxav.c b/toxav/toxav.c index 946b4e2c..4febcde7 100644 --- a/toxav/toxav.c +++ b/toxav/toxav.c @@ -325,7 +325,7 @@ static void iterate_common(ToxAV *av, bool audio) if (i->msi_call->self_capabilities & MSI_CAP_R_VIDEO && i->msi_call->peer_capabilities & MSI_CAP_S_VIDEO) { pthread_mutex_lock(i->video->queue_mutex); - frame_time = min_u32(i->video->lcfd, frame_time); + frame_time = min_s32(i->video->lcfd, frame_time); pthread_mutex_unlock(i->video->queue_mutex); } } diff --git a/toxcore/Messenger.c b/toxcore/Messenger.c index ce2e1627..484fdb3d 100644 --- a/toxcore/Messenger.c +++ b/toxcore/Messenger.c @@ -2483,7 +2483,8 @@ void do_messenger(Messenger *m, void *userdata) for (uint32_t dhtfriend = 0; dhtfriend < dht_get_num_friends(m->dht); ++dhtfriend) { if (id_equal(m->friendlist[friend_idx].real_pk, dht_get_friend_public_key(m->dht, dhtfriend))) { - m2dht[friend_idx] = dhtfriend; + assert(dhtfriend < INT32_MAX); + m2dht[friend_idx] = (int32_t)dhtfriend; break; } } @@ -2491,7 +2492,8 @@ void do_messenger(Messenger *m, void *userdata) for (uint32_t friend_idx = 0; friend_idx < num_dhtfriends; ++friend_idx) { if (m2dht[friend_idx] >= 0) { - dht2m[m2dht[friend_idx]] = friend_idx; + assert(friend_idx < INT32_MAX); + dht2m[m2dht[friend_idx]] = (int32_t)friend_idx; } } @@ -3164,7 +3166,7 @@ uint32_t copy_friendlist(Messenger const *m, uint32_t *out_list, uint32_t list_s * * if error is not NULL it will be set to one of the values in the enum above. */ -Messenger *new_messenger(Mono_Time *mono_time, Messenger_Options *options, unsigned int *error) +Messenger *new_messenger(Mono_Time *mono_time, Messenger_Options *options, Messenger_Error *error) { if (!options) { return nullptr; diff --git a/toxcore/Messenger.h b/toxcore/Messenger.h index 08a1ffc0..3f2dd005 100644 --- a/toxcore/Messenger.h +++ b/toxcore/Messenger.h @@ -10,8 +10,6 @@ #ifndef C_TOXCORE_TOXCORE_MESSENGER_H #define C_TOXCORE_TOXCORE_MESSENGER_H -#include - #include "TCP_server.h" #include "friend_connection.h" #include "friend_requests.h" @@ -260,7 +258,7 @@ struct Messenger { Friend *friendlist; uint32_t numfriends; - time_t lastdump; + uint64_t lastdump; uint8_t is_receiving_file; bool has_added_relays; // If the first connection has occurred in do_messenger @@ -754,7 +752,7 @@ typedef enum Messenger_Error { * if error is not NULL it will be set to one of the values in the enum above. */ non_null() -Messenger *new_messenger(Mono_Time *mono_time, Messenger_Options *options, unsigned int *error); +Messenger *new_messenger(Mono_Time *mono_time, Messenger_Options *options, Messenger_Error *error); /** Run this before closing shop * Free all datastructures. diff --git a/toxcore/TCP_client.c b/toxcore/TCP_client.c index fc67ddb7..3d928f70 100644 --- a/toxcore/TCP_client.c +++ b/toxcore/TCP_client.c @@ -887,7 +887,7 @@ void do_TCP_connection(const Logger *logger, const Mono_Time *mono_time, if (sizeof(data) == len) { if (handle_handshake(tcp_connection, data) == 0) { - tcp_connection->kill_at = -1; + tcp_connection->kill_at = UINT64_MAX; tcp_connection->status = TCP_CLIENT_CONFIRMED; } else { tcp_connection->kill_at = 0; diff --git a/toxcore/events/file_recv.c b/toxcore/events/file_recv.c index 9e9440c4..71a6c97c 100644 --- a/toxcore/events/file_recv.c +++ b/toxcore/events/file_recv.c @@ -85,7 +85,7 @@ uint32_t tox_event_file_recv_get_kind(const Tox_Event_File_Recv *file_recv) non_null() static void tox_event_file_recv_set_file_size(Tox_Event_File_Recv *file_recv, - uint32_t file_size) + uint64_t file_size) { assert(file_recv != nullptr); file_recv->file_size = file_size; diff --git a/toxcore/events/file_recv_chunk.c b/toxcore/events/file_recv_chunk.c index d505bc3d..d6c40dd0 100644 --- a/toxcore/events/file_recv_chunk.c +++ b/toxcore/events/file_recv_chunk.c @@ -71,7 +71,7 @@ uint32_t tox_event_file_recv_chunk_get_file_number(const Tox_Event_File_Recv_Chu non_null() static void tox_event_file_recv_chunk_set_position(Tox_Event_File_Recv_Chunk *file_recv_chunk, - uint32_t position) + uint64_t position) { assert(file_recv_chunk != nullptr); file_recv_chunk->position = position; diff --git a/toxcore/onion_client.c b/toxcore/onion_client.c index b4599d8f..17bf576a 100644 --- a/toxcore/onion_client.c +++ b/toxcore/onion_client.c @@ -1549,7 +1549,7 @@ static void populate_path_nodes_tcp(Onion_Client *onion_c) #define RUN_COUNT_FRIEND_ANNOUNCE_BEGINNING 17 #define ONION_FRIEND_BACKOFF_FACTOR 4 -#define ONION_FRIEND_MAX_PING_INTERVAL (5*60*MAX_ONION_CLIENTS) +#define ONION_FRIEND_MAX_PING_INTERVAL (uint64_t)(5*60*MAX_ONION_CLIENTS) non_null() static void do_friend(Onion_Client *onion_c, uint16_t friendnum) diff --git a/toxcore/tox.c b/toxcore/tox.c index 482ac8c7..60e953ce 100644 --- a/toxcore/tox.c +++ b/toxcore/tox.c @@ -586,7 +586,7 @@ Tox *tox_new(const struct Tox_Options *options, Tox_Err_New *error) lock(tox); - unsigned int m_error; + Messenger_Error m_error; tox->m = new_messenger(tox->mono_time, &m_options, &m_error); // TODO(iphydf): Clarify this code, check for NULL before new_groupchats, so