Skip to content

Commit 2760966

Browse files
committed
cleanup: Avoid casting function pointers.
Also added declarations using the `_cb` type for each of the callback handlers in tox.h. This forces cppcheck to check whether the parameter names in the definitions agree with the `_cb` parameter names.
1 parent f98137d commit 2760966

File tree

3 files changed

+51
-11
lines changed

3 files changed

+51
-11
lines changed

toxcore/Messenger.c

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -893,10 +893,7 @@ static void set_friend_typing(const Messenger *m, int32_t friendnumber, uint8_t
893893
/** Set the function that will be executed when a friend request is received. */
894894
void m_callback_friendrequest(Messenger *m, m_friend_request_cb *function)
895895
{
896-
/* TODO(iphydf): Don't cast function pointers. */
897-
//!TOKSTYLE-
898-
callback_friendrequest(m->fr, (fr_friend_request_cb *)function, m);
899-
//!TOKSTYLE+
896+
m->friend_request = function;
900897
}
901898

902899
/** Set the function that will be executed when a message from a friend is received. */
@@ -3167,6 +3164,16 @@ uint32_t copy_friendlist(Messenger const *m, uint32_t *out_list, uint32_t list_s
31673164
return ret;
31683165
}
31693166

3167+
static fr_friend_request_cb m_handle_friend_request;
3168+
non_null(1, 2, 3) nullable(5)
3169+
static void m_handle_friend_request(
3170+
void *object, const uint8_t *public_key, const uint8_t *message, size_t length, void *user_data)
3171+
{
3172+
Messenger *m = (Messenger *)object;
3173+
assert(m != nullptr);
3174+
m->friend_request(m, public_key, message, length, user_data);
3175+
}
3176+
31703177
/** Run this at startup.
31713178
* return allocated instance of Messenger on success.
31723179
* return 0 if there are problems.
@@ -3309,6 +3316,7 @@ Messenger *new_messenger(Mono_Time *mono_time, Messenger_Options *options, Messe
33093316
m->is_receiving_file = 0;
33103317

33113318
m_register_default_plugins(m);
3319+
callback_friendrequest(m->fr, m_handle_friend_request, m);
33123320

33133321
if (error != nullptr) {
33143322
*error = MESSENGER_ERROR_NONE;

toxcore/Messenger.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -266,6 +266,7 @@ struct Messenger {
266266
uint16_t num_loaded_relays;
267267
Node_format loaded_relays[NUM_SAVED_TCP_RELAYS]; // Relays loaded from config
268268

269+
m_friend_request_cb *friend_request;
269270
m_friend_message_cb *friend_message;
270271
m_friend_name_cb *friend_namechange;
271272
m_friend_status_message_cb *friend_statusmessagechange;

toxcore/tox.c

Lines changed: 38 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ struct Tox {
6060
Mono_Time *mono_time;
6161
pthread_mutex_t *mutex;
6262

63+
tox_log_cb *log_callback;
6364
tox_self_connection_status_cb *self_connection_status_callback;
6465
tox_friend_name_cb *friend_name_callback;
6566
tox_friend_status_message_cb *friend_status_message_callback;
@@ -107,6 +108,17 @@ struct Tox_Userdata {
107108
void *user_data;
108109
};
109110

111+
static logger_cb tox_log_handler;
112+
non_null(1, 3, 5, 6) nullable(7)
113+
static void tox_log_handler(void *context, Logger_Level level, const char *file, int line, const char *func,
114+
const char *message, void *userdata)
115+
{
116+
Tox *tox = (Tox *)context;
117+
assert(tox != nullptr);
118+
tox->log_callback(tox, (Tox_Log_Level)level, file, line, func, message, userdata);
119+
}
120+
121+
static m_self_connection_status_cb tox_self_connection_status_handler;
110122
non_null(1) nullable(3)
111123
static void tox_self_connection_status_handler(Messenger *m, Onion_Connection_Status connection_status, void *user_data)
112124
{
@@ -117,6 +129,7 @@ static void tox_self_connection_status_handler(Messenger *m, Onion_Connection_St
117129
}
118130
}
119131

132+
static m_friend_name_cb tox_friend_name_handler;
120133
non_null(1, 3) nullable(5)
121134
static void tox_friend_name_handler(Messenger *m, uint32_t friend_number, const uint8_t *name, size_t length,
122135
void *user_data)
@@ -128,6 +141,7 @@ static void tox_friend_name_handler(Messenger *m, uint32_t friend_number, const
128141
}
129142
}
130143

144+
static m_friend_status_message_cb tox_friend_status_message_handler;
131145
non_null(1, 3) nullable(5)
132146
static void tox_friend_status_message_handler(Messenger *m, uint32_t friend_number, const uint8_t *message,
133147
size_t length, void *user_data)
@@ -139,6 +153,7 @@ static void tox_friend_status_message_handler(Messenger *m, uint32_t friend_numb
139153
}
140154
}
141155

156+
static m_friend_status_cb tox_friend_status_handler;
142157
non_null(1) nullable(4)
143158
static void tox_friend_status_handler(Messenger *m, uint32_t friend_number, unsigned int status, void *user_data)
144159
{
@@ -149,6 +164,7 @@ static void tox_friend_status_handler(Messenger *m, uint32_t friend_number, unsi
149164
}
150165
}
151166

167+
static m_friend_connection_status_cb tox_friend_connection_status_handler;
152168
non_null(1) nullable(4)
153169
static void tox_friend_connection_status_handler(Messenger *m, uint32_t friend_number, unsigned int connection_status,
154170
void *user_data)
@@ -161,6 +177,7 @@ static void tox_friend_connection_status_handler(Messenger *m, uint32_t friend_n
161177
}
162178
}
163179

180+
static m_friend_typing_cb tox_friend_typing_handler;
164181
non_null(1) nullable(4)
165182
static void tox_friend_typing_handler(Messenger *m, uint32_t friend_number, bool is_typing, void *user_data)
166183
{
@@ -171,6 +188,7 @@ static void tox_friend_typing_handler(Messenger *m, uint32_t friend_number, bool
171188
}
172189
}
173190

191+
static m_friend_read_receipt_cb tox_friend_read_receipt_handler;
174192
non_null(1) nullable(4)
175193
static void tox_friend_read_receipt_handler(Messenger *m, uint32_t friend_number, uint32_t message_id, void *user_data)
176194
{
@@ -181,6 +199,7 @@ static void tox_friend_read_receipt_handler(Messenger *m, uint32_t friend_number
181199
}
182200
}
183201

202+
static m_friend_request_cb tox_friend_request_handler;
184203
non_null(1, 2, 3) nullable(5)
185204
static void tox_friend_request_handler(Messenger *m, const uint8_t *public_key, const uint8_t *message, size_t length,
186205
void *user_data)
@@ -192,18 +211,20 @@ static void tox_friend_request_handler(Messenger *m, const uint8_t *public_key,
192211
}
193212
}
194213

214+
static m_friend_message_cb tox_friend_message_handler;
195215
non_null(1, 4) nullable(6)
196-
static void tox_friend_message_handler(Messenger *m, uint32_t friend_number, unsigned int type, const uint8_t *message,
197-
size_t length, void *user_data)
216+
static void tox_friend_message_handler(Messenger *m, uint32_t friend_number, unsigned int message_type,
217+
const uint8_t *message, size_t length, void *user_data)
198218
{
199219
struct Tox_Userdata *tox_data = (struct Tox_Userdata *)user_data;
200220

201221
if (tox_data->tox->friend_message_callback != nullptr) {
202-
tox_data->tox->friend_message_callback(tox_data->tox, friend_number, (Tox_Message_Type)type, message, length,
222+
tox_data->tox->friend_message_callback(tox_data->tox, friend_number, (Tox_Message_Type)message_type, message, length,
203223
tox_data->user_data);
204224
}
205225
}
206226

227+
static m_file_recv_control_cb tox_file_recv_control_handler;
207228
non_null(1) nullable(5)
208229
static void tox_file_recv_control_handler(Messenger *m, uint32_t friend_number, uint32_t file_number,
209230
unsigned int control, void *user_data)
@@ -216,6 +237,7 @@ static void tox_file_recv_control_handler(Messenger *m, uint32_t friend_number,
216237
}
217238
}
218239

240+
static m_file_chunk_request_cb tox_file_chunk_request_handler;
219241
non_null(1) nullable(6)
220242
static void tox_file_chunk_request_handler(Messenger *m, uint32_t friend_number, uint32_t file_number,
221243
uint64_t position, size_t length, void *user_data)
@@ -228,6 +250,7 @@ static void tox_file_chunk_request_handler(Messenger *m, uint32_t friend_number,
228250
}
229251
}
230252

253+
static m_file_recv_cb tox_file_recv_handler;
231254
non_null(1, 6) nullable(8)
232255
static void tox_file_recv_handler(Messenger *m, uint32_t friend_number, uint32_t file_number, uint32_t kind,
233256
uint64_t file_size, const uint8_t *filename, size_t filename_length, void *user_data)
@@ -240,6 +263,7 @@ static void tox_file_recv_handler(Messenger *m, uint32_t friend_number, uint32_t
240263
}
241264
}
242265

266+
static m_file_recv_chunk_cb tox_file_recv_chunk_handler;
243267
non_null(1, 5) nullable(7)
244268
static void tox_file_recv_chunk_handler(Messenger *m, uint32_t friend_number, uint32_t file_number, uint64_t position,
245269
const uint8_t *data, size_t length, void *user_data)
@@ -252,6 +276,7 @@ static void tox_file_recv_chunk_handler(Messenger *m, uint32_t friend_number, ui
252276
}
253277
}
254278

279+
static g_conference_invite_cb tox_conference_invite_handler;
255280
non_null(1, 4) nullable(6)
256281
static void tox_conference_invite_handler(Messenger *m, uint32_t friend_number, int type, const uint8_t *cookie,
257282
size_t length, void *user_data)
@@ -264,6 +289,7 @@ static void tox_conference_invite_handler(Messenger *m, uint32_t friend_number,
264289
}
265290
}
266291

292+
static g_conference_connected_cb tox_conference_connected_handler;
267293
non_null(1) nullable(3)
268294
static void tox_conference_connected_handler(Messenger *m, uint32_t conference_number, void *user_data)
269295
{
@@ -274,6 +300,7 @@ static void tox_conference_connected_handler(Messenger *m, uint32_t conference_n
274300
}
275301
}
276302

303+
static g_conference_message_cb tox_conference_message_handler;
277304
non_null(1, 5) nullable(7)
278305
static void tox_conference_message_handler(Messenger *m, uint32_t conference_number, uint32_t peer_number, int type,
279306
const uint8_t *message, size_t length, void *user_data)
@@ -286,6 +313,7 @@ static void tox_conference_message_handler(Messenger *m, uint32_t conference_num
286313
}
287314
}
288315

316+
static title_cb tox_conference_title_handler;
289317
non_null(1, 4) nullable(6)
290318
static void tox_conference_title_handler(Messenger *m, uint32_t conference_number, uint32_t peer_number,
291319
const uint8_t *title, size_t length, void *user_data)
@@ -298,6 +326,7 @@ static void tox_conference_title_handler(Messenger *m, uint32_t conference_numbe
298326
}
299327
}
300328

329+
static peer_name_cb tox_conference_peer_name_handler;
301330
non_null(1, 4) nullable(6)
302331
static void tox_conference_peer_name_handler(Messenger *m, uint32_t conference_number, uint32_t peer_number,
303332
const uint8_t *name, size_t length, void *user_data)
@@ -310,6 +339,7 @@ static void tox_conference_peer_name_handler(Messenger *m, uint32_t conference_n
310339
}
311340
}
312341

342+
static peer_list_changed_cb tox_conference_peer_list_changed_handler;
313343
non_null(1) nullable(3)
314344
static void tox_conference_peer_list_changed_handler(Messenger *m, uint32_t conference_number, void *user_data)
315345
{
@@ -320,6 +350,7 @@ static void tox_conference_peer_list_changed_handler(Messenger *m, uint32_t conf
320350
}
321351
}
322352

353+
static dht_get_nodes_response_cb tox_dht_get_nodes_response_handler;
323354
non_null(1, 2) nullable(3)
324355
static void tox_dht_get_nodes_response_handler(const DHT *dht, const Node_format *node, void *user_data)
325356
{
@@ -336,6 +367,7 @@ static void tox_dht_get_nodes_response_handler(const DHT *dht, const Node_format
336367
tox_data->user_data);
337368
}
338369

370+
static m_friend_lossy_packet_cb tox_friend_lossy_packet_handler;
339371
non_null(1, 4) nullable(6)
340372
static void tox_friend_lossy_packet_handler(Messenger *m, uint32_t friend_number, uint8_t packet_id,
341373
const uint8_t *data, size_t length, void *user_data)
@@ -351,6 +383,7 @@ static void tox_friend_lossy_packet_handler(Messenger *m, uint32_t friend_number
351383
}
352384
}
353385

386+
static m_friend_lossless_packet_cb tox_friend_lossless_packet_handler;
354387
non_null(1, 4) nullable(6)
355388
static void tox_friend_lossless_packet_handler(Messenger *m, uint32_t friend_number, uint8_t packet_id,
356389
const uint8_t *data, size_t length, void *user_data)
@@ -499,10 +532,8 @@ Tox *tox_new(const struct Tox_Options *options, Tox_Err_New *error)
499532
m_options.hole_punching_enabled = tox_options_get_hole_punching_enabled(opts);
500533
m_options.local_discovery_enabled = tox_options_get_local_discovery_enabled(opts);
501534

502-
// TODO(iphydf): Don't cast function pointers.
503-
//!TOKSTYLE-
504-
m_options.log_callback = (logger_cb *)tox_options_get_log_callback(opts);
505-
//!TOKSTYLE+
535+
tox->log_callback = tox_options_get_log_callback(opts);
536+
m_options.log_callback = tox_log_handler;
506537
m_options.log_context = tox;
507538
m_options.log_user_data = tox_options_get_log_user_data(opts);
508539

0 commit comments

Comments
 (0)