Skip to content

Commit 0f9961f

Browse files
committed
refactor: Add nullable annotations to struct members.
Also add a bunch of casts where needed. I've tried to model everything in such a way that it minimises casts. The casts *should* be safe, but it's not always obvious. In the obvious cases, we should have a linter that validates it. In the non-obvious cases, that linter should warn and require that we add a null check. I've added some null checks in some cases but not all. Also, refactored some of the constructor functions to never assign a maybe-null value to a non-null struct member, instead using a temporary local variable to check if construction/allocation succeeded.
1 parent e115b13 commit 0f9961f

File tree

89 files changed

+862
-703
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

89 files changed

+862
-703
lines changed

other/analysis/run-cppcheck

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ CPPCHECK+=("--error-exitcode=1")
1616
CPPCHECK+=("--suppress=unmatchedSuppression")
1717
# We don't cast function pointers, which cppcheck suggests here.
1818
CPPCHECK+=("--suppress=constParameterCallback")
19+
CPPCHECK+=("--suppress=constParameterPointer")
1920
# This disagrees with clang's warnings.
2021
CPPCHECK+=("--suppress=invalidPrintfArgType_uint")
2122
# False positives in switch statements.

other/docker/misra/Makefile

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,10 @@ SUPPRESSIONS += 10.8
4242
#
4343
# Reason: this is needed for generic callbacks to make any sense.
4444
SUPPRESSIONS += 11.5
45+
# A conversion shall not remove any const, volatile or _Atomic qualification from the type pointed to by a pointer.
46+
#
47+
# Reason: we need to cast from _Nullable to _Nonnull.
48+
SUPPRESSIONS += 11.8
4549
# The precedence of operators within expressions should be made explicit.
4650
#
4751
# Reason: this asks us to add a lot of extra parentheses that don't really help

other/event_tooling/generate_event_c.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -417,7 +417,7 @@ void generate_event_impl(const std::string& event_name, const std::vector<EventT
417417
// free
418418
f << "void tox_event_" << event_name_l << "_free(Tox_Event_" << event_name << " *" << event_name_l << ", const Memory *mem)\n{\n";
419419
f << " if (" << event_name_l << " != nullptr) {\n";
420-
f << " tox_event_" << event_name_l << "_destruct(" << event_name_l << ", mem);\n }\n";
420+
f << " tox_event_" << event_name_l << "_destruct((Tox_Event_" << event_name << " * _Nonnull)" << event_name_l << ", mem);\n }\n";
421421
f << " mem_delete(mem, " << event_name_l << ");\n}\n\n";
422422

423423
// add

testing/Messenger_test.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -105,10 +105,10 @@ int main(int argc, char *argv[])
105105
Messenger_Options options = {0};
106106
options.ipv6enabled = ipv6enabled;
107107
Messenger_Error err;
108-
m = new_messenger(mono_time, mem, os_random(), os_network(), &options, &err);
108+
m = new_messenger(mono_time, mem, (const Random * _Nonnull)os_random(), (const Network * _Nonnull)os_network(), &options, &err);
109109

110110
if (!m) {
111-
fprintf(stderr, "Failed to allocate messenger datastructure: %d\n", err);
111+
fprintf(stderr, "Failed to allocate messenger datastructure: %u\n", err);
112112
exit(0);
113113
}
114114

testing/fuzzing/protodump.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,7 @@ void RecordBootstrap(const char *init, const char *bootstrap)
188188
[](Tox *tox, Tox_Log_Level level, const char *file, uint32_t line, const char *func,
189189
const char *message, void *user_data) {
190190
// Log to stdout.
191-
std::printf("[%s] %c %s:%d(%s): %s\n", static_cast<Record_System *>(user_data)->name_,
191+
std::printf("[%s] %c %s:%u(%s): %s\n", static_cast<Record_System *>(user_data)->name_,
192192
tox_log_level_name(level), file, line, func, message);
193193
});
194194

testing/fuzzing/protodump_reduce.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,7 @@ void TestEndToEnd(Fuzz_Data &input)
152152
const char *message, void *user_data) {
153153
// Log to stdout.
154154
if (PROTODUMP_DEBUG) {
155-
std::printf("[tox1] %c %s:%d(%s): %s\n", tox_log_level_name(level), file, line,
155+
std::printf("[tox1] %c %s:%u(%s): %s\n", tox_log_level_name(level), file, line,
156156
func, message);
157157
}
158158
});

toxcore/DHT.c

Lines changed: 43 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -56,8 +56,8 @@
5656
#define KEYS_TIMEOUT 600
5757

5858
typedef struct DHT_Friend_Callback {
59-
dht_ip_cb *ip_callback;
60-
void *data;
59+
dht_ip_cb *_Nullable ip_callback;
60+
void *_Nullable data;
6161
int32_t number;
6262
} DHT_Friend_Callback;
6363

@@ -87,17 +87,17 @@ const Node_format empty_node_format = {{0}};
8787
static_assert(sizeof(empty_dht_friend.lock_flags) * 8 == DHT_FRIEND_MAX_LOCKS, "Bitfield size and number of locks don't match");
8888

8989
typedef struct Cryptopacket_Handler {
90-
cryptopacket_handler_cb *function;
91-
void *object;
90+
cryptopacket_handler_cb *_Nullable function;
91+
void *_Nullable object;
9292
} Cryptopacket_Handler;
9393

9494
struct DHT {
95-
const Logger *log;
96-
const Network *ns;
97-
Mono_Time *mono_time;
98-
const Memory *mem;
99-
const Random *rng;
100-
Networking_Core *net;
95+
const Logger *_Nonnull log;
96+
const Network *_Nonnull ns;
97+
Mono_Time *_Nonnull mono_time;
98+
const Memory *_Nonnull mem;
99+
const Random *_Nonnull rng;
100+
Networking_Core *_Nonnull net;
101101

102102
bool hole_punching_enabled;
103103
bool lan_discovery_enabled;
@@ -110,26 +110,26 @@ struct DHT {
110110
uint8_t self_public_key[CRYPTO_PUBLIC_KEY_SIZE];
111111
uint8_t self_secret_key[CRYPTO_SECRET_KEY_SIZE];
112112

113-
DHT_Friend *friends_list;
113+
DHT_Friend *_Nullable friends_list;
114114
uint16_t num_friends;
115115

116-
Node_format *loaded_nodes_list;
116+
Node_format *_Nullable loaded_nodes_list;
117117
uint32_t loaded_num_nodes;
118118
unsigned int loaded_nodes_index;
119119

120-
Shared_Key_Cache *shared_keys_recv;
121-
Shared_Key_Cache *shared_keys_sent;
120+
Shared_Key_Cache *_Nonnull shared_keys_recv;
121+
Shared_Key_Cache *_Nonnull shared_keys_sent;
122122

123-
struct Ping *ping;
124-
Ping_Array *dht_ping_array;
123+
struct Ping *_Nonnull ping;
124+
Ping_Array *_Nonnull dht_ping_array;
125125
uint64_t cur_time;
126126

127127
Cryptopacket_Handler cryptopackethandlers[256];
128128

129129
Node_format to_bootstrap[MAX_CLOSE_TO_BOOTSTRAP_NODES];
130130
unsigned int num_to_bootstrap;
131131

132-
dht_nodes_response_cb *nodes_response_callback;
132+
dht_nodes_response_cb *_Nullable nodes_response_callback;
133133
};
134134

135135
const uint8_t *dht_friend_public_key(const DHT_Friend *dht_friend)
@@ -732,7 +732,7 @@ int get_close_nodes(
732732
return get_somewhat_close_nodes(
733733
dht->cur_time, public_key, nodes_list,
734734
sa_family, dht->close_clientlist,
735-
dht->friends_list, dht->num_friends,
735+
(const DHT_Friend * _Nonnull)dht->friends_list, dht->num_friends,
736736
is_lan, want_announce);
737737
}
738738

@@ -847,9 +847,9 @@ static bool store_node_ok(const Client_data *_Nonnull client, uint64_t cur_time,
847847
}
848848

849849
typedef struct Client_data_Cmp {
850-
const Memory *mem;
850+
const Memory *_Nonnull mem;
851851
uint64_t cur_time;
852-
const uint8_t *comp_public_key;
852+
const uint8_t *_Nonnull comp_public_key;
853853
} Client_data_Cmp;
854854

855855
static int client_data_cmp(const Client_data_Cmp *_Nonnull cmp, const Client_data *_Nonnull entry1, const Client_data *_Nonnull entry2)
@@ -2507,14 +2507,16 @@ DHT *new_dht(const Logger *log, const Memory *mem, const Random *rng, const Netw
25072507
dht->hole_punching_enabled = hole_punching_enabled;
25082508
dht->lan_discovery_enabled = lan_discovery_enabled;
25092509

2510-
dht->ping = ping_new(mem, mono_time, rng, dht, net);
2510+
struct Ping *temp_ping = ping_new(mem, mono_time, rng, dht, net);
25112511

2512-
if (dht->ping == nullptr) {
2512+
if (temp_ping == nullptr) {
25132513
LOGGER_ERROR(log, "failed to initialise ping");
25142514
kill_dht(dht);
25152515
return nullptr;
25162516
}
25172517

2518+
dht->ping = temp_ping;
2519+
25182520
networking_registerhandler(dht->net, NET_PACKET_NODES_REQUEST, &handle_nodes_request, dht);
25192521
networking_registerhandler(dht->net, NET_PACKET_NODES_RESPONSE, &handle_nodes_response, dht);
25202522
networking_registerhandler(dht->net, NET_PACKET_CRYPTO, &cryptopacket_handle, dht);
@@ -2527,23 +2529,36 @@ DHT *new_dht(const Logger *log, const Memory *mem, const Random *rng, const Netw
25272529

25282530
crypto_new_keypair(rng, dht->self_public_key, dht->self_secret_key);
25292531

2530-
dht->shared_keys_recv = shared_key_cache_new(log, mono_time, mem, dht->self_secret_key, KEYS_TIMEOUT, MAX_KEYS_PER_SLOT);
2531-
dht->shared_keys_sent = shared_key_cache_new(log, mono_time, mem, dht->self_secret_key, KEYS_TIMEOUT, MAX_KEYS_PER_SLOT);
2532+
Shared_Key_Cache *const temp_shared_keys_recv = shared_key_cache_new(log, mono_time, mem, dht->self_secret_key, KEYS_TIMEOUT, MAX_KEYS_PER_SLOT);
2533+
2534+
if (temp_shared_keys_recv == nullptr) {
2535+
LOGGER_ERROR(log, "failed to initialise shared key cache");
2536+
kill_dht(dht);
2537+
return nullptr;
2538+
}
2539+
2540+
dht->shared_keys_recv = temp_shared_keys_recv;
2541+
2542+
Shared_Key_Cache *const temp_shared_keys_sent = shared_key_cache_new(log, mono_time, mem, dht->self_secret_key, KEYS_TIMEOUT, MAX_KEYS_PER_SLOT);
25322543

2533-
if (dht->shared_keys_recv == nullptr || dht->shared_keys_sent == nullptr) {
2544+
if (temp_shared_keys_sent == nullptr) {
25342545
LOGGER_ERROR(log, "failed to initialise shared key cache");
25352546
kill_dht(dht);
25362547
return nullptr;
25372548
}
25382549

2539-
dht->dht_ping_array = ping_array_new(mem, DHT_PING_ARRAY_SIZE, PING_TIMEOUT);
2550+
dht->shared_keys_sent = temp_shared_keys_sent;
25402551

2541-
if (dht->dht_ping_array == nullptr) {
2552+
Ping_Array *const temp_ping_array = ping_array_new(mem, DHT_PING_ARRAY_SIZE, PING_TIMEOUT);
2553+
2554+
if (temp_ping_array == nullptr) {
25422555
LOGGER_ERROR(log, "failed to initialise ping array");
25432556
kill_dht(dht);
25442557
return nullptr;
25452558
}
25462559

2560+
dht->dht_ping_array = temp_ping_array;
2561+
25472562
for (uint32_t i = 0; i < DHT_FAKE_FRIEND_NUMBER; ++i) {
25482563
uint8_t random_public_key_bytes[CRYPTO_PUBLIC_KEY_SIZE];
25492564
uint8_t random_secret_key_bytes[CRYPTO_SECRET_KEY_SIZE];
@@ -2598,7 +2613,7 @@ void kill_dht(DHT *dht)
25982613
networking_registerhandler(dht->net, NET_PACKET_NODES_RESPONSE, nullptr, nullptr);
25992614
networking_registerhandler(dht->net, NET_PACKET_CRYPTO, nullptr, nullptr);
26002615
networking_registerhandler(dht->net, NET_PACKET_LAN_DISCOVERY, nullptr, nullptr);
2601-
cryptopacket_registerhandler(dht, CRYPTO_PACKET_NAT_PING, nullptr, nullptr);
2616+
cryptopacket_registerhandler((DHT * _Nonnull)dht, CRYPTO_PACKET_NAT_PING, nullptr, nullptr);
26022617

26032618
shared_key_cache_free(dht->shared_keys_recv);
26042619
shared_key_cache_free(dht->shared_keys_sent);

toxcore/DHT.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -212,7 +212,7 @@ int unpack_nodes(Node_format *_Nonnull nodes, uint16_t max_num_nodes, uint16_t *
212212
uint16_t length, bool tcp_enabled);
213213
/*----------------------------------------------------------------------------------*/
214214

215-
typedef int cryptopacket_handler_cb(void *_Nonnull object, const IP_Port *_Nonnull source, const uint8_t *_Nonnull source_pubkey,
215+
typedef int cryptopacket_handler_cb(void *_Nullable object, const IP_Port *_Nonnull source, const uint8_t *_Nonnull source_pubkey,
216216
const uint8_t *_Nonnull packet, uint16_t length, void *_Nullable userdata);
217217

218218
typedef struct DHT DHT;

0 commit comments

Comments
 (0)