Skip to content
This repository was archived by the owner on Aug 5, 2022. It is now read-only.

Commit cbe7e8f

Browse files
committed
[general] Revisit usage of memcpy everywhere
For copying structs, direct assignment is preferable to memcpy because it is easier to read and gives more freedom to the compiler to be smart. Using memcpy with strlen() as the size is the same as strcpy but with an extra function call. And sprintf is preferable to multiple strcats and memcpys to construct a complex string. Also made a few other simplifications along the way. Signed-off-by: Geoff Gustafson <[email protected]>
1 parent 6f01734 commit cbe7e8f

File tree

14 files changed

+95
-99
lines changed

14 files changed

+95
-99
lines changed

arc/src/arc_sensor.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ static void send_sensor_data(enum sensor_channel channel,
8181
msg.user_data = NULL;
8282
msg.error_code = ERROR_IPM_NONE;
8383
msg.data.sensor.channel = channel;
84-
memcpy(&msg.data.sensor.reading, &reading, sizeof(union sensor_reading));
84+
msg.data.sensor.reading = reading;
8585
ipm_send_msg(&msg);
8686
}
8787

arc/src/main.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ static void queue_message(struct zjs_ipm_message *incoming_msg)
7070

7171
if (msg != end_of_queue_ptr) {
7272
// copy the message into our queue to be process in the mainloop
73-
memcpy(msg, incoming_msg, sizeof(struct zjs_ipm_message));
73+
*msg = *incoming_msg;
7474
} else {
7575
// running out of space, disregard message
7676
ERR_PRINT("skipping incoming message\n");

src/ashell/comms-uart.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -532,5 +532,5 @@ void zjs_ashell_init()
532532

533533
void comms_uart_set_config(struct comms_cfg_data *config)
534534
{
535-
memcpy(&comms_config, config, sizeof(struct comms_cfg_data));
535+
comms_config = *config;
536536
}

src/zjs_aio.c

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -127,9 +127,11 @@ static void ipm_msg_receive_callback(void *context, u32_t id,
127127

128128
if (msg->flags & MSG_SYNC_FLAG) {
129129
zjs_ipm_message_t *result = (zjs_ipm_message_t *)msg->user_data;
130+
130131
// synchronous ipm, copy the results
131-
if (result)
132-
memcpy(result, msg, sizeof(zjs_ipm_message_t));
132+
if (result) {
133+
*result = *msg;
134+
}
133135

134136
// un-block sync api
135137
k_sem_give(&aio_sem);

src/zjs_fs.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,7 @@ static jerry_value_t create_stats_obj(struct fs_dirent *entry)
159159
if (!new_entry) {
160160
return zjs_error_context("malloc failed", 0, 0);
161161
}
162-
memcpy(new_entry, entry, sizeof(struct fs_dirent));
162+
*new_entry = *entry;
163163

164164
jerry_set_object_native_pointer(stats_obj, new_entry, &stats_type_info);
165165

src/zjs_grove_lcd_ipm.c

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -92,15 +92,17 @@ static void ipm_msg_receive_callback(void *context, u32_t id,
9292

9393
if ((msg->flags & MSG_SYNC_FLAG) == MSG_SYNC_FLAG) {
9494
zjs_ipm_message_t *result = (zjs_ipm_message_t *)msg->user_data;
95-
// synchrounus ipm, copy the results
96-
if (result)
97-
memcpy(result, msg, sizeof(zjs_ipm_message_t));
95+
96+
// synchronous ipm, copy the results
97+
if (result) {
98+
*result = *msg;
99+
}
98100

99101
// un-block sync api
100102
k_sem_give(&glcd_sem);
101103
} else {
102104
// asynchronous ipm, should not get here
103-
ERR_PRINT("async message received\n");
105+
ZJS_ASSERT(false, "async message received");
104106
}
105107
}
106108

src/zjs_i2c_ipm_handler.c

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,15 +43,17 @@ static void ipm_msg_receive_callback(void *context, u32_t id,
4343

4444
if (msg->flags & MSG_SYNC_FLAG) {
4545
zjs_ipm_message_t *result = (zjs_ipm_message_t *)msg->user_data;
46+
4647
// synchronous ipm, copy the results
47-
if (result)
48-
memcpy(result, msg, sizeof(zjs_ipm_message_t));
48+
if (result) {
49+
*result = *msg;
50+
}
4951

5052
// un-block sync api
5153
k_sem_give(&i2c_sem);
5254
} else {
5355
// asynchronous ipm, should not get here
54-
ERR_PRINT("async message received\n");
56+
ZJS_ASSERT(false, "async message received");
5557
}
5658
}
5759

src/zjs_net.c

Lines changed: 36 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,29 @@ static struct k_mutex socket_mutex;
184184
#define NET_HOSTNAME_MAX 32
185185
#define SOCK_READ_BUF_SIZE 128
186186

187+
// TODO: this could perhaps be reused in dgram/ws etc
188+
static void zjs_copy_sockaddr(struct sockaddr *dst, struct sockaddr *src,
189+
socklen_t len)
190+
{
191+
// requires: dst contains at least sizeof(sockaddr) bytes, src points to
192+
// a valid sockaddr struct (sockaddr_in or sockaddr_in6),
193+
// len is the address length if known, or 0
194+
// effects: copies src to dest, but only the number of bytes required by
195+
// the underlying address family; if len is given, asserts
196+
// that it matches the expected size
197+
if (src->family == AF_INET) {
198+
ZJS_ASSERT(!len || len == sizeof(sockaddr_in), "expected IPv4 length");
199+
*(struct sockaddr_in *)dst = *(struct sockaddr_in *)src;
200+
}
201+
else if (src->family == AF_INET6) {
202+
ZJS_ASSERT(!len || len == sizeof(sockaddr_in6), "expected IPv6 length");
203+
*(struct sockaddr_in6 *)dst = *(struct sockaddr_in6 *)src;
204+
}
205+
else {
206+
ZJS_ASSERT(false, "invalid sockaddr struct");
207+
}
208+
}
209+
187210
static int match_timer(sock_handle_t *sock, struct k_timer *timer)
188211
{
189212
FTRACE("sock = %p, timer = %p\n", sock, timer);
@@ -891,7 +914,7 @@ static void tcp_accepted(struct net_context *context,
891914
accept.context = context;
892915
accept.server_h = server_h;
893916
memset(&accept.addr, 0, sizeof(struct sockaddr));
894-
memcpy(&accept.addr, addr, addrlen);
917+
zjs_copy_sockaddr(&accept.addr, addr, addrlen);
895918

896919
zjs_defer_work(accept_connection, &accept, sizeof(accept));
897920
}
@@ -1017,12 +1040,12 @@ static ZJS_DECL_FUNC(server_listen)
10171040
double backlog = 0;
10181041
u32_t size = NET_HOSTNAME_MAX;
10191042
char hostname[size];
1020-
double family = 0;
1043+
u32_t family = 0;
10211044

10221045
zjs_obj_get_double(argv[0], "port", &port);
10231046
zjs_obj_get_double(argv[0], "backlog", &backlog);
10241047
zjs_obj_get_string(argv[0], "host", hostname, size);
1025-
zjs_obj_get_double(argv[0], "family", &family);
1048+
zjs_obj_get_uint32(argv[0], "family", &family);
10261049

10271050
// FIXME: validate or fix input, e.g. family
10281051

@@ -1032,41 +1055,32 @@ static ZJS_DECL_FUNC(server_listen)
10321055

10331056
struct sockaddr addr;
10341057
memset(&addr, 0, sizeof(struct sockaddr));
1058+
addr.family = (family == 6) ? AF_INET6 : AF_INET; // default to IPv4
10351059

1036-
// default to IPv4
1037-
if (family == 0 || family == 4) {
1038-
family = 4;
1039-
CHECK(net_context_get(AF_INET, SOCK_STREAM, IPPROTO_TCP,
1040-
&server_h->server_ctx));
1060+
CHECK(net_context_get(addr.family, SOCK_STREAM, IPPROTO_TCP,
1061+
&server_h->server_ctx));
10411062

1063+
u32_t addrlen;
1064+
if (addr.family == AF_INET) {
10421065
struct sockaddr_in *addr4 = (struct sockaddr_in *)&addr;
1043-
1044-
addr4->sin_family = AF_INET;
10451066
addr4->sin_port = htons((int)port);
1046-
10471067
net_addr_pton(AF_INET, hostname, &addr4->sin_addr);
1068+
addrlen = sizeof(struct sockaddr_in);
10481069
} else {
1049-
CHECK(net_context_get(AF_INET6, SOCK_STREAM, IPPROTO_TCP,
1050-
&server_h->server_ctx));
1051-
10521070
struct sockaddr_in6 *addr6 = (struct sockaddr_in6 *)&addr;
1053-
1054-
addr6->sin6_family = AF_INET6;
10551071
addr6->sin6_port = htons((int)port);
1056-
10571072
net_addr_pton(AF_INET6, hostname, &addr6->sin6_addr);
1073+
addrlen = sizeof(struct sockaddr_in6);
10581074
}
10591075

1060-
CHECK(net_context_bind(server_h->server_ctx, &addr,
1061-
sizeof(struct sockaddr)));
1076+
CHECK(net_context_bind(server_h->server_ctx, &addr, addrlen));
10621077
CHECK(net_context_listen(server_h->server_ctx, (int)backlog));
10631078

10641079
server_h->listening = 1;
10651080
server_h->port = (u16_t)port;
10661081

1067-
memcpy(&server_h->local, zjs_net_config_get_ip(server_h->server_ctx),
1068-
sizeof(struct sockaddr));
1069-
server_h->local = *zjs_net_config_get_ip(server_h->server_ctx);
1082+
zjs_copy_sockaddr(&server_h->local,
1083+
zjs_net_config_get_ip(server_h->server_ctx), 0);
10701084
zjs_obj_add_boolean(this, "listening", true);
10711085

10721086
// Here we defer just to keep the call stack short; this is unless we

src/zjs_ocf_client.c

Lines changed: 15 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -56,8 +56,6 @@ struct ocf_handler {
5656

5757
static struct client_resource *resource_list = NULL;
5858

59-
#define MAX_URI_LENGTH (30)
60-
6159
static struct ocf_handler *new_ocf_handler(struct client_resource *res)
6260
{
6361
struct ocf_handler *h = zjs_malloc(sizeof(struct ocf_handler));
@@ -108,25 +106,14 @@ static jerry_value_t make_ocf_error(const char *name, const char *msg,
108106
*/
109107
static char *create_url(const char *uuid, const char *path)
110108
{
111-
// oic://<uuid>/<path>
112-
char *url = zjs_malloc(strlen(uuid) + strlen(path) + 8);
113-
int count = 0;
114-
url[0] = 'o';
115-
url[1] = 'i';
116-
url[2] = 'c';
117-
url[3] = ':';
118-
url[4] = '/';
119-
url[5] = '/';
120-
count = 6;
121-
memcpy(url + count, uuid, strlen(uuid));
122-
count += strlen(uuid);
123-
if (path[0] != '/') {
124-
url[count] = '/';
125-
count++;
109+
// create URL of form oic://<uuid>/<path>
110+
if (path[0] == '/') {
111+
path++;
126112
}
127-
memcpy(url + count, path, strlen(path));
128-
count += strlen(path);
129-
url[count] = '\0';
113+
114+
int url_len = strlen(uuid) + strlen(path) + 8;
115+
char *url = zjs_malloc(url_len);
116+
snprintf(url, url_len, "oic://%s/%s", uuid, path);
130117
return url;
131118
}
132119

@@ -388,20 +375,13 @@ static void add_resource(char *id, char *type, char *path,
388375
new->state = RES_STATE_SEARCHING;
389376

390377
if (id) {
391-
// FIXME: use strcpy instead of calling strlen 3x
392-
new->device_id = zjs_malloc(strlen(id) + 1);
393-
memcpy(new->device_id, id, strlen(id));
394-
new->device_id[strlen(id)] = '\0';
378+
new->device_id = zjs_alloc_from_string(id, NULL);
395379
}
396380
if (type) {
397-
new->resource_type = zjs_malloc(strlen(type) + 1);
398-
memcpy(new->resource_type, type, strlen(type));
399-
new->resource_type[strlen(type)] = '\0';
381+
new->resource_type = zjs_alloc_from_string(type, NULL);
400382
}
401383
if (path) {
402-
new->resource_path = zjs_malloc(strlen(path) + 1);
403-
memcpy(new->resource_path, path, strlen(path));
404-
new->resource_path[strlen(path)] = '\0';
384+
new->resource_path = zjs_alloc_from_string(path, NULL);
405385
}
406386

407387
if (jerry_value_is_function(listener)) {
@@ -446,8 +426,6 @@ static oc_discovery_flags_t discovery(const char *di,
446426
{
447427
struct ocf_handler *h = (struct ocf_handler *)user_handle;
448428
int i;
449-
int uri_len = strlen(uri);
450-
uri_len = (uri_len >= MAX_URI_LENGTH) ? MAX_URI_LENGTH - 1 : uri_len;
451429

452430
for (i = 0; i < oc_string_array_get_allocated_size(types); i++) {
453431
char *t = oc_string_array_get_item(types, i);
@@ -489,14 +467,13 @@ static oc_discovery_flags_t discovery(const char *di,
489467
cur->endpoint = endpoint;
490468

491469
if (!cur->device_id) {
492-
cur->device_id = zjs_malloc(strlen(di) + 1);
493-
memcpy(cur->device_id, di, strlen(di));
494-
cur->device_id[strlen(di)] = '\0';
470+
cur->device_id = zjs_alloc_from_string(di, NULL);
495471
}
496472
if (!cur->resource_path) {
497-
cur->resource_path = zjs_malloc(strlen(uri) + 1);
498-
memcpy(cur->resource_path, uri, uri_len);
499-
cur->resource_path[uri_len] = '\0';
473+
// TODO: before this code was pretending to limit things to a
474+
// MAX_URI_LENGTH of 30, but it wasn't working right anyway;
475+
// not sure if there was a point to that
476+
cur->resource_path = zjs_alloc_from_string(uri, NULL);
500477
}
501478
/*
502479
* Add the array of resource types to newly discovered resource

src/zjs_ocf_common.c

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -47,20 +47,17 @@ static bool ocf_foreach_prop(const jerry_value_t prop_name,
4747
{
4848
struct props_handle *handle = (struct props_handle *)data;
4949

50-
ZJS_GET_STRING(prop_name, name, OCF_MAX_PROP_NAME_LEN);
50+
jerry_size_t maxlen = OCF_MAX_PROP_NAME_LEN;
51+
char *name = zjs_alloc_from_jstring(prop_name, &maxlen);
5152

52-
/*
53-
* Use zjs_alloc_from_jstring
54-
*/
5553
// Skip id/resourcePath/resourceType because that is not a resource property
5654
// that is sent out
57-
if ((!strequal(name, "deviceId")) &&
58-
(!strequal(name, "resourcePath")) &&
59-
(!strequal(name, "resourceType"))) {
60-
handle->names_array[handle->size] =
61-
zjs_malloc(jerry_get_string_size(prop_name) + 1);
62-
memcpy(handle->names_array[handle->size], name, strlen(name));
63-
handle->names_array[handle->size][strlen(name)] = '\0';
55+
char *props[] = { "deviceId", "resourcePath", "resourceType", NULL };
56+
if (zjs_str_matches(name, props)) {
57+
zjs_free(name);
58+
}
59+
else {
60+
handle->names_array[handle->size] = name;
6461
ZVAL ret = jerry_set_property_by_index(handle->props_array,
6562
handle->size++, prop_value);
6663
if (jerry_value_has_error_flag(ret)) {

0 commit comments

Comments
 (0)