Skip to content

Commit

Permalink
global: use c_mem*() over mem*()
Browse files Browse the repository at this point in the history
Use the new c_mem*() functions rather than mem*() so we protect against
NULL pointers in empty areas, which are UB with the classic mem*()
functions.

Signed-off-by: David Rheinsberg <[email protected]>
  • Loading branch information
dvdhrm committed May 12, 2022
1 parent 6d9b817 commit 701759a
Show file tree
Hide file tree
Showing 16 changed files with 44 additions and 44 deletions.
4 changes: 2 additions & 2 deletions src/broker/controller.c
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ static int controller_name_new(ControllerName **namep, Controller *controller, c
name->controller = controller;
name->controller_node = (CRBNode)C_RBNODE_INIT(name->controller_node);
name->activation = (Activation)ACTIVATION_NULL(name->activation);
memcpy(name->path, path, n_path + 1);
c_memcpy(name->path, path, n_path + 1);

c_rbtree_add(&controller->name_tree, parent, slot, &name->controller_node);
*namep = name;
Expand Down Expand Up @@ -192,7 +192,7 @@ static int controller_listener_new(ControllerListener **listenerp, Controller *c

listener->controller = controller;
listener->controller_node = (CRBNode)C_RBNODE_INIT(listener->controller_node);
memcpy(listener->path, path, n_path + 1);
c_memcpy(listener->path, path, n_path + 1);

c_rbtree_add(&controller->listener_tree, parent, slot, &listener->controller_node);
*listenerp = listener;
Expand Down
4 changes: 2 additions & 2 deletions src/bus/bus.c
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,11 @@ int bus_init(Bus *bus,
*bus = (Bus)BUS_NULL(*bus);
bus->log = log;

memcpy(bus->machine_id, machine_id, sizeof(bus->machine_id));
c_memcpy(bus->machine_id, machine_id, sizeof(bus->machine_id));

random = (void *)getauxval(AT_RANDOM);
c_assert(random);
memcpy(bus->guid, random, sizeof(bus->guid));
c_memcpy(bus->guid, random, sizeof(bus->guid));

r = user_registry_init(&bus->users, log, _USER_SLOT_N, maxima);
if (r)
Expand Down
2 changes: 1 addition & 1 deletion src/bus/name.c
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,7 @@ static int name_new(Name **namep, NameRegistry *registry, const char *name_str)

*name = (Name)NAME_INIT(*name);
name->registry = registry;
memcpy(name->name, name_str, n_name + 1);
c_memcpy(name->name, name_str, n_name + 1);

*namep = name;
name = NULL;
Expand Down
10 changes: 5 additions & 5 deletions src/dbus/message.c
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ int message_new_incoming(Message **messagep, MessageHeader header) {
message->vecs[3] = (struct iovec){ message->body, n_body };

message->n_copied += sizeof(header);
memcpy(message->data, &header, sizeof(header));
c_memcpy(message->data, &header, sizeof(header));

*messagep = message;
message = NULL;
Expand Down Expand Up @@ -616,11 +616,11 @@ void message_stitch_sender(Message *message, uint64_t sender_id) {
message->patch[2] = 's';
message->patch[3] = 0;
if (message->big_endian)
memcpy(message->patch + 4, (uint32_t[1]){ htobe32(n_sender) }, sizeof(uint32_t));
c_memcpy(message->patch + 4, (uint32_t[1]){ htobe32(n_sender) }, sizeof(uint32_t));
else
memcpy(message->patch + 4, (uint32_t[1]){ htole32(n_sender) }, sizeof(uint32_t));
memcpy(message->patch + 8, sender, n_sender + 1);
memset(message->patch + 8 + n_sender + 1, 0, n_stitch - n_field);
c_memcpy(message->patch + 4, (uint32_t[1]){ htole32(n_sender) }, sizeof(uint32_t));
c_memcpy(message->patch + 8, sender, n_sender + 1);
c_memset(message->patch + 8 + n_sender + 1, 0, n_stitch - n_field);

/*
* After we cut the previous sender field and inserted the new, adjust
Expand Down
10 changes: 5 additions & 5 deletions src/dbus/queue.c
Original file line number Diff line number Diff line change
Expand Up @@ -170,14 +170,14 @@ int iqueue_get_cursor(IQueue *iq,
c_assert(!iq->data_start);
c_assert(iq->data == iq->buffer);

memcpy(p, iq->data, iq->data_end);
c_memcpy(p, iq->data, iq->data_end);
iq->data = p;
iq->data_size = IQUEUE_LINE_MAX;
} else if (_c_unlikely_(iq->data != iq->buffer && iq->pending.data)) {
c_assert(!iq->data_start);
c_assert(iq->data_end <= sizeof(iq->buffer));

memcpy(iq->buffer, iq->data, iq->data_end);
c_memcpy(iq->buffer, iq->data, iq->data_end);
free(iq->data);
user_charge_deinit(&iq->charge_data);
iq->data = iq->buffer;
Expand Down Expand Up @@ -314,9 +314,9 @@ int iqueue_pop_data(IQueue *iq, FDList **fdsp) {
if (n_data > 0) {
n = c_min(n_data, iq->pending.n_data - iq->pending.n_copied);

memcpy(iq->pending.data + iq->pending.n_copied,
iq->data + iq->data_start,
n);
c_memcpy(iq->pending.data + iq->pending.n_copied,
iq->data + iq->data_start,
n);

n_data -= n;
iq->data_start += n;
Expand Down
6 changes: 3 additions & 3 deletions src/dbus/socket.c
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ static int socket_buffer_new_message(SocketBuffer **bufferp,
return error_trace(r);

buffer->message = message_ref(message);
memcpy(buffer->vecs, message->vecs, sizeof(message->vecs));
c_memcpy(buffer->vecs, message->vecs, sizeof(message->vecs));

r = user_charge(socket->user,
&buffer->charges[0],
Expand Down Expand Up @@ -472,11 +472,11 @@ int socket_queue_line(Socket *socket, User *user, const char *line_in, size_t n)

socket_buffer_get_line_cursor(buffer, &line_out, &pos);

memcpy(line_out, line_in, n);
c_memcpy(line_out, line_in, n);
line_out += n;
*pos += n;

memcpy(line_out, "\r\n", strlen("\r\n"));
c_memcpy(line_out, "\r\n", strlen("\r\n"));
*pos += strlen("\r\n");

return 0;
Expand Down
10 changes: 5 additions & 5 deletions src/dbus/test-queue.c
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ static void test_in_special(void) {
if (!n)
break;

memcpy(buffer + *from, blob, n);
c_memcpy(buffer + *from, blob, n);
*from += n;
total += n;

Expand Down Expand Up @@ -110,7 +110,7 @@ static void test_in_special(void) {
c_assert(!r);
c_assert(to - *from >= 128);

memcpy(buffer + *from, (char [1]){}, 1);
c_memcpy(buffer + *from, (char [1]){}, 1);
*from += 1;
r = fdlist_new_with_fds(fds, (int [1]){}, 1);
c_assert(!r);
Expand Down Expand Up @@ -149,7 +149,7 @@ static void test_in_special(void) {
c_assert(!r);
c_assert(to - *from >= 128);

memcpy(buffer + *from, (char [2]){}, 2);
c_memcpy(buffer + *from, (char [2]){}, 2);
*from += 2;
r = fdlist_new_with_fds(fds, (int [1]){ 1 }, 1);
c_assert(!r);
Expand Down Expand Up @@ -199,7 +199,7 @@ static void test_in_special(void) {
c_assert(!r);
c_assert(to - *from >= 128);

memcpy(buffer + *from, (char [1]){}, 1);
c_memcpy(buffer + *from, (char [1]){}, 1);
*from += 1;
r = fdlist_new_with_fds(fds, (int [1]){}, 1);
c_assert(!r);
Expand Down Expand Up @@ -307,7 +307,7 @@ static void test_in_lines(void) {
n = rand() % c_min(n, strlen(send) - i_send);
++n;

memcpy(buffer + *from, send + i_send, n);
c_memcpy(buffer + *from, send + i_send, n);
i_send += n;
*from += n;
}
Expand Down
10 changes: 5 additions & 5 deletions src/dbus/test-stitching.c
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ static Message *test_new_message(size_t before, const char *sender_early, size_t
if (before) {
p = malloc(before + 1);
c_assert(p);
memset(p, 'a', before);
c_memset(p, 'a', before);
p[before] = 0;

c_dvar_write(&v, "(y<s>)",
Expand All @@ -82,7 +82,7 @@ static Message *test_new_message(size_t before, const char *sender_early, size_t
if (after) {
p = malloc(after + 1);
c_assert(p);
memset(p, 'a', after);
c_memset(p, 'a', after);
p[0] = '/';
p[after] = 0;

Expand Down Expand Up @@ -127,7 +127,7 @@ static void test_assert_message(Message *message, size_t before, const char *sen
c_assert(p);

for (n = 0, i = 0; i < C_ARRAY_SIZE(message->vecs); ++i) {
memcpy(p + n, message->vecs[i].iov_base, message->vecs[i].iov_len);
c_memcpy(p + n, message->vecs[i].iov_base, message->vecs[i].iov_len);
n += message->vecs[i].iov_len;
}

Expand Down Expand Up @@ -155,7 +155,7 @@ static void test_stitching(void) {
n = 8 + i % 8;
from = malloc(n + 1);
c_assert(from);
memset(from, '1', n);
c_memset(from, '1', n);
from[0] = ':';
from[1] = '1';
from[2] = '.';
Expand All @@ -164,7 +164,7 @@ static void test_stitching(void) {
n = 8 + i % 11;
to = malloc(n + 1);
c_assert(to);
memset(to, '2', n);
c_memset(to, '2', n);
to[0] = ':';
to[1] = '1';
to[2] = '.';
Expand Down
12 changes: 6 additions & 6 deletions src/launch/config.c
Original file line number Diff line number Diff line change
Expand Up @@ -64,11 +64,11 @@ int config_path_new(ConfigPath **filep, ConfigPath *parent, const char *prefix,
file->parent = config_path_ref(parent);

if (n_prefix) {
memcpy(file->path, prefix, n_prefix);
c_memcpy(file->path, prefix, n_prefix);
file->path[n_prefix] = '/';
memcpy(file->path + n_prefix + 1, path, n_path + 1);
c_memcpy(file->path + n_prefix + 1, path, n_path + 1);
} else {
memcpy(file->path, path, n_path + 1);
c_memcpy(file->path, path, n_path + 1);
}

*filep = file;
Expand Down Expand Up @@ -1204,8 +1204,8 @@ static void config_parser_blob_fn(void *userdata, const XML_Char *data, int n_da
return;
}

memcpy(t, state->current->cdata, state->current->n_cdata);
memcpy(t + state->current->n_cdata, data, n_data);
c_memcpy(t, state->current->cdata, state->current->n_cdata);
c_memcpy(t + state->current->n_cdata, data, n_data);
t[state->current->n_cdata + n_data] = 0;
free(state->current->cdata);
state->current->cdata = t;
Expand Down Expand Up @@ -1241,7 +1241,7 @@ static int config_parser_include(ConfigParser *parser, ConfigRoot *root, ConfigN
c_assert(node->type == CONFIG_NODE_INCLUDE);
c_assert(node->include.file);

memset(&parser->state, 0, sizeof(parser->state));
c_memset(&parser->state, 0, sizeof(parser->state));
parser->state.nss = nss_cache;
parser->state.dirwatch = dirwatch;
parser->state.file = node->include.file;
Expand Down
4 changes: 2 additions & 2 deletions src/launch/nss-cache.c
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,7 @@ static int nss_cache_add_user(NSSCache *cache, struct passwd *pw) {
return error_trace(r);

if (node) {
memset(&node->pw, 0, sizeof(node->pw));
c_memset(&node->pw, 0, sizeof(node->pw));
node->pw.pw_name = node->name;
node->pw.pw_uid = pw->pw_uid;
node->pw.pw_gid = pw->pw_gid;
Expand All @@ -235,7 +235,7 @@ static int nss_cache_add_group(NSSCache *cache, struct group *gr) {
return error_trace(r);

if (node) {
memset(&node->gr, 0, sizeof(node->gr));
c_memset(&node->gr, 0, sizeof(node->gr));
node->gr.gr_name = node->name;
node->gr.gr_gid = gr->gr_gid;
}
Expand Down
2 changes: 1 addition & 1 deletion src/util/fdlist.c
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ int fdlist_new_with_fds(FDList **listp, const int *fds, size_t n_fds) {
list->cmsg->cmsg_len = CMSG_LEN(n_fds * sizeof(int));
list->cmsg->cmsg_level = SOL_SOCKET;
list->cmsg->cmsg_type = SCM_RIGHTS;
memcpy(fdlist_data(list), fds, n_fds * sizeof(int));
c_memcpy(fdlist_data(list), fds, n_fds * sizeof(int));

*listp = list;
return 0;
Expand Down
4 changes: 2 additions & 2 deletions src/util/log.c
Original file line number Diff line number Diff line change
Expand Up @@ -278,7 +278,7 @@ static int log_fd_send(int destination_fd, int payload_fd) {
control.cmsg.cmsg_len = CMSG_LEN(sizeof(int));
control.cmsg.cmsg_level = SOL_SOCKET;
control.cmsg.cmsg_type = SCM_RIGHTS;
memcpy(CMSG_DATA(&control.cmsg), &payload_fd, sizeof(int));
c_memcpy(CMSG_DATA(&control.cmsg), &payload_fd, sizeof(int));

msg = (struct msghdr){
.msg_control = &control.cmsg,
Expand Down Expand Up @@ -590,7 +590,7 @@ void log_append(Log *log, const void *data, size_t n_data) {
return;
}

memcpy(log->map + log->offset, data, n_data);
c_memcpy(log->map + log->offset, data, n_data);
log->offset += n_data;
}

Expand Down
4 changes: 2 additions & 2 deletions src/util/selinux.c
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ static int bus_selinux_name_new(const char *name, const char *context, CRBTree *
return error_origin(-ENOMEM);
selinux_name->rb = (CRBNode)C_RBNODE_INIT(selinux_name->rb);
selinux_name->context = NULL;
memcpy(selinux_name->name, name, n_name);
c_memcpy(selinux_name->name, name, n_name);

selinux_name->context = strdup(context);
if (!selinux_name->context)
Expand Down Expand Up @@ -105,7 +105,7 @@ int bus_selinux_registry_new(BusSELinuxRegistry **registryp, const char *fallbac
registry->n_refs = REF_INIT;
registry->fallback_context = (const char *)(registry + 1);
registry->names = (CRBTree)C_RBTREE_INIT;
memcpy((char *)registry->fallback_context, fallback_context, n_fallback_context);
c_memcpy((char *)registry->fallback_context, fallback_context, n_fallback_context);

*registryp = registry;
registry = NULL;
Expand Down
2 changes: 1 addition & 1 deletion src/util/user.c
Original file line number Diff line number Diff line change
Expand Up @@ -389,7 +389,7 @@ int user_registry_init(UserRegistry *registry,

registry->log = log;
registry->n_slots = n_slots;
memcpy(registry->maxima, maxima, n_slots * sizeof(*registry->maxima));
c_memcpy(registry->maxima, maxima, n_slots * sizeof(*registry->maxima));

return 0;
}
Expand Down
2 changes: 1 addition & 1 deletion test/dbus/test-fdstream.c
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ static void test_fd_stream_send(Connection *c, unsigned int unix_fds, unsigned i
if (n_fds > 0) {
int fds[n_fds];

memset(fds, 0, sizeof(fds));
c_memset(fds, 0, sizeof(fds));
r = fdlist_new_with_fds(&m->fds, fds, n_fds);
c_assert(!r);
}
Expand Down
2 changes: 1 addition & 1 deletion test/dbus/util-message.c
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ static void test_message_append(void **buf, size_t *n_buf, const void *data, siz
p = realloc(*buf, *n_buf + n_data);
c_assert(p);

memcpy(p + *n_buf, data, n_data);
c_memcpy(p + *n_buf, data, n_data);

*buf = p;
*n_buf += n_data;
Expand Down

0 comments on commit 701759a

Please sign in to comment.