Skip to content

Commit 8f07755

Browse files
committed
cleanup: Use memzero(x, s) instead of memset(x, 0, s).
It's clearer and doesn't risk having a non-zero filler value.
1 parent a7258e4 commit 8f07755

File tree

20 files changed

+105
-66
lines changed

20 files changed

+105
-66
lines changed
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
9190d56ef3b346bc1632e6d7ee5fe5362be661bff58f2d6d88b5c1d1827394cd /usr/local/bin/tox-bootstrapd
1+
c028527fe5eecde7daa6ac9dacc47bd4bb765463e8e4b5af3881789797bf81a8 /usr/local/bin/tox-bootstrapd

other/docker/compcert/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
!/Makefile

other/docker/compcert/Dockerfile

Lines changed: 10 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,12 @@
11
FROM toxchat/compcert:latest
22

3+
RUN apt-get update && \
4+
DEBIAN_FRONTEND="noninteractive" apt-get install -y --no-install-recommends \
5+
gdb \
6+
make \
7+
&& apt-get clean \
8+
&& rm -rf /var/lib/apt/lists/*
9+
310
WORKDIR /work
411
COPY auto_tests/ /work/auto_tests/
512
COPY testing/ /work/testing/
@@ -10,21 +17,6 @@ COPY third_party/ /work/third_party/
1017

1118
SHELL ["/bin/bash", "-o", "pipefail", "-c"]
1219

13-
RUN ccomp \
14-
-o send_message_test \
15-
-Wall -Werror \
16-
-Wno-c11-extensions \
17-
-Wno-unknown-pragmas \
18-
-Wno-unused-variable \
19-
-fstruct-passing -fno-unprototyped -g \
20-
auto_tests/auto_test_support.c \
21-
auto_tests/send_message_test.c \
22-
testing/misc_tools.c \
23-
toxav/*.c \
24-
toxcore/*.c \
25-
toxcore/*/*.c \
26-
toxencryptsave/*.c \
27-
third_party/cmp/*.c \
28-
-D__COMPCERT__ -DDISABLE_VLA -Dinline= \
29-
-lpthread $(pkg-config --cflags --libs libsodium opus vpx) \
30-
&& ./send_message_test | grep 'tox clients connected'
20+
COPY other/docker/compcert/Makefile /work/
21+
RUN make "-j$(nproc)"
22+
RUN ./send_message_test #| grep 'tox clients connected'

other/docker/compcert/Makefile

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
SOURCES := $(wildcard auto_tests/auto_test_support.c \
2+
auto_tests/send_message_test.c \
3+
testing/misc_tools.c \
4+
toxav/*.c \
5+
toxcore/*.c \
6+
toxcore/*/*.c \
7+
toxencryptsave/*.c \
8+
third_party/cmp/*.c)
9+
10+
OBJECTS := $(SOURCES:.c=.o)
11+
12+
CC := ccomp
13+
CFLAGS := -Wall -Werror \
14+
-Wno-c11-extensions \
15+
-Wno-unknown-pragmas \
16+
-Wno-unused-variable \
17+
-fstruct-passing -fno-unprototyped -g \
18+
-D__COMPCERT__ \
19+
-DDISABLE_VLA \
20+
-DMIN_LOGGER_LEVEL=LOGGER_LEVEL_TRACE \
21+
-Dinline= \
22+
$(shell pkg-config --cflags libsodium opus vpx)
23+
LDFLAGS := -lpthread $(shell pkg-config --libs libsodium opus vpx)
24+
25+
send_message_test: $(OBJECTS)
26+
$(CC) -o $@ $+ $(LDFLAGS)

toxcore/BUILD.bazel

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,7 @@ cc_library(
167167
deps = [
168168
":attributes",
169169
":ccompat",
170+
":util",
170171
"@libsodium",
171172
],
172173
)

toxcore/DHT.c

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -853,12 +853,14 @@ static void get_close_nodes_inner(
853853
*/
854854
non_null()
855855
static int get_somewhat_close_nodes(
856-
uint64_t cur_time, const uint8_t *public_key, Node_format *nodes_list,
856+
uint64_t cur_time, const uint8_t *public_key, Node_format nodes_list[MAX_SENT_NODES],
857857
Family sa_family, const Client_data *close_clientlist,
858858
const DHT_Friend *friends_list, uint16_t friends_list_size,
859859
bool is_lan, bool want_announce)
860860
{
861-
memset(nodes_list, 0, MAX_SENT_NODES * sizeof(Node_format));
861+
for (uint16_t i = 0; i < MAX_SENT_NODES; ++i) {
862+
nodes_list[i] = empty_node_format;
863+
}
862864

863865
uint32_t num_nodes = 0;
864866
get_close_nodes_inner(
@@ -882,7 +884,7 @@ static int get_somewhat_close_nodes(
882884

883885
int get_close_nodes(
884886
const DHT *dht, const uint8_t *public_key,
885-
Node_format *nodes_list, Family sa_family,
887+
Node_format nodes_list[MAX_SENT_NODES], Family sa_family,
886888
bool is_lan, bool want_announce)
887889
{
888890
return get_somewhat_close_nodes(
@@ -1101,7 +1103,8 @@ static void update_client_with_reset(const Mono_Time *mono_time, Client_data *cl
11011103
ipptp_write->ret_ip_self = false;
11021104

11031105
/* zero out other address */
1104-
memset(ipptp_clear, 0, sizeof(*ipptp_clear));
1106+
const IPPTsPng empty_ipptp = {{{{0}}}};
1107+
*ipptp_clear = empty_ipptp;
11051108
}
11061109

11071110
/**

toxcore/DHT.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -396,7 +396,7 @@ void set_announce_node(DHT *dht, const uint8_t *public_key);
396396
non_null()
397397
int get_close_nodes(
398398
const DHT *dht, const uint8_t *public_key,
399-
Node_format *nodes_list, Family sa_family,
399+
Node_format nodes_list[MAX_SENT_NODES], Family sa_family,
400400
bool is_lan, bool want_announce);
401401

402402

toxcore/LAN_discovery.c

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
#include "LAN_discovery.h"
1010

1111
#include <stdlib.h>
12-
#include <string.h>
1312

1413
#if defined(_WIN32) || defined(__WIN32__) || defined(WIN32)
1514
// The mingw32/64 Windows library warns about including winsock2.h after
@@ -143,8 +142,7 @@ static Broadcast_Info *fetch_broadcast_info(const Network *ns)
143142
}
144143

145144
/* Configure ifconf for the ioctl call. */
146-
struct ifreq i_faces[MAX_INTERFACES];
147-
memset(i_faces, 0, sizeof(struct ifreq) * MAX_INTERFACES);
145+
struct ifreq i_faces[MAX_INTERFACES] = {{0}};
148146

149147
struct ifconf ifc;
150148
ifc.ifc_buf = (char *)i_faces;

toxcore/Messenger.c

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -857,7 +857,7 @@ int m_copy_statusmessage(const Messenger *m, int32_t friendnumber, uint8_t *buf,
857857
const uint32_t msglen = min_u32(maxlen, m->friendlist[friendnumber].statusmessage_length);
858858

859859
memcpy(buf, m->friendlist[friendnumber].statusmessage, msglen);
860-
memset(buf + msglen, 0, maxlen - msglen);
860+
memzero(buf + msglen, maxlen - msglen);
861861
return msglen;
862862
}
863863

@@ -2620,7 +2620,7 @@ static bool self_announce_group(const Messenger *m, GC_Chat *chat, Onion_Friend
26202620
if (tcp_num > 0) {
26212621
pk_copy(chat->announced_tcp_relay_pk, announce.base_announce.tcp_relays[0].public_key);
26222622
} else {
2623-
memset(chat->announced_tcp_relay_pk, 0, sizeof(chat->announced_tcp_relay_pk));
2623+
memzero(chat->announced_tcp_relay_pk, sizeof(chat->announced_tcp_relay_pk));
26242624
}
26252625

26262626
LOGGER_DEBUG(chat->log, "Published group announce. TCP relays: %d, UDP status: %d", tcp_num,
@@ -3416,10 +3416,9 @@ static uint32_t path_node_size(const Messenger *m)
34163416
non_null()
34173417
static uint8_t *save_path_nodes(const Messenger *m, uint8_t *data)
34183418
{
3419-
Node_format nodes[NUM_SAVED_PATH_NODES];
3419+
Node_format nodes[NUM_SAVED_PATH_NODES] = {{{0}}};
34203420
uint8_t *temp_data = data;
34213421
data = state_write_section_header(data, STATE_COOKIE_TYPE, 0, STATE_TYPE_PATH_NODE);
3422-
memset(nodes, 0, sizeof(nodes));
34233422
const unsigned int num = onion_backup_nodes(m->onion_c, nodes, NUM_SAVED_PATH_NODES);
34243423
const int l = pack_nodes(m->log, data, NUM_SAVED_PATH_NODES * packed_node_size(net_family_tcp_ipv6()), nodes, num);
34253424

toxcore/TCP_server.c

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,8 @@ typedef struct TCP_Secure_Connection {
6060
uint64_t ping_id;
6161
} TCP_Secure_Connection;
6262

63+
static const TCP_Secure_Connection empty_tcp_secure_connection = {{nullptr}};
64+
6365

6466
struct TCP_Server {
6567
const Logger *logger;
@@ -135,8 +137,9 @@ static int alloc_new_connections(TCP_Server *tcp_server, uint32_t num)
135137
}
136138

137139
const uint32_t old_size = tcp_server->size_accepted_connections;
138-
const uint32_t size_new_entries = num * sizeof(TCP_Secure_Connection);
139-
memset(new_connections + old_size, 0, size_new_entries);
140+
for (uint32_t i = 0; i < num; ++i) {
141+
new_connections[old_size + i] = empty_tcp_secure_connection;
142+
}
140143

141144
tcp_server->accepted_connection_array = new_connections;
142145
tcp_server->size_accepted_connections = new_size;

0 commit comments

Comments
 (0)