Skip to content

Commit 28e51ad

Browse files
committed
opal: convert from strncpy() -> opal_string_copy()
In many cases, this was a simple string replace. In a few places, it entailed: 1. Updating some comments and removing now-redundant foo[size-1]='\0' statements. 2. Updating passing (size-1) to (size) (because opal_string_copy() wants the entire destination buffer length). This commit actually fixes a bunch of potential (yet quite unlikely) bugs where we could have ended up with non-null-terminated strings. Signed-off-by: Jeff Squyres <jsquyres@cisco.com>
1 parent e1cf875 commit 28e51ad

Some content is hidden

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

42 files changed

+250
-240
lines changed

opal/mca/base/mca_base_component_repository.c

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@
4343
#include "opal/constants.h"
4444
#include "opal/class/opal_hash_table.h"
4545
#include "opal/util/basename.h"
46+
#include "opal/util/string_copy.h"
4647

4748
#if OPAL_HAVE_DL_SUPPORT
4849

@@ -167,12 +168,8 @@ static int process_repository_item (const char *filename, void *data)
167168
return OPAL_ERR_OUT_OF_RESOURCE;
168169
}
169170

170-
/* strncpy does not guarantee a \0 */
171-
ri->ri_type[MCA_BASE_MAX_TYPE_NAME_LEN] = '\0';
172-
strncpy (ri->ri_type, type, MCA_BASE_MAX_TYPE_NAME_LEN);
173-
174-
ri->ri_name[MCA_BASE_MAX_TYPE_NAME_LEN] = '\0';
175-
strncpy (ri->ri_name, name, MCA_BASE_MAX_COMPONENT_NAME_LEN);
171+
opal_string_copy (ri->ri_type, type, MCA_BASE_MAX_TYPE_NAME_LEN);
172+
opal_string_copy (ri->ri_name, name, MCA_BASE_MAX_COMPONENT_NAME_LEN);
176173

177174
opal_list_append (component_list, &ri->super);
178175

opal/mca/btl/openib/btl_openib_ini.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
#endif
3636

3737
#include "opal/util/show_help.h"
38+
#include "opal/util/string_copy.h"
3839

3940
#include "btl_openib.h"
4041
#include "btl_openib_lex.h"
@@ -324,7 +325,7 @@ static int parse_line(parsed_section_values_t *sv)
324325
}
325326
key_buffer = tmp;
326327
}
327-
strncpy(key_buffer, btl_openib_ini_yytext, key_buffer_len);
328+
opal_string_copy(key_buffer, btl_openib_ini_yytext, key_buffer_len);
328329

329330
/* The first thing we have to see is an "=" */
330331
val = btl_openib_ini_yylex();

opal/mca/btl/usnic/btl_usnic_cagent.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
#include "opal/types.h"
2828
#include "opal/util/output.h"
2929
#include "opal/util/fd.h"
30+
#include "opal/util/string_copy.h"
3031

3132
#include "btl_usnic.h"
3233
#include "btl_usnic_connectivity.h"
@@ -1189,7 +1190,7 @@ int opal_btl_usnic_connectivity_agent_init(void)
11891190

11901191
memset(&address, 0, sizeof(struct sockaddr_un));
11911192
address.sun_family = AF_UNIX;
1192-
strncpy(address.sun_path, ipc_filename, sizeof(address.sun_path) - 1);
1193+
opal_string_copy(address.sun_path, ipc_filename, sizeof(address.sun_path));
11931194

11941195
if (bind(ipc_accept_fd, (struct sockaddr *) &address,
11951196
sizeof(struct sockaddr_un)) != 0) {

opal/mca/btl/usnic/btl_usnic_cclient.c

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
#include "opal/mca/event/event.h"
2929
#include "opal/util/output.h"
3030
#include "opal/util/fd.h"
31+
#include "opal/util/string_copy.h"
3132

3233
#include "btl_usnic.h"
3334
#include "btl_usnic_module.h"
@@ -103,7 +104,7 @@ int opal_btl_usnic_connectivity_client_init(void)
103104
struct sockaddr_un address;
104105
memset(&address, 0, sizeof(struct sockaddr_un));
105106
address.sun_family = AF_UNIX;
106-
strncpy(address.sun_path, ipc_filename, sizeof(address.sun_path) - 1);
107+
opal_string_copy(address.sun_path, ipc_filename, sizeof(address.sun_path));
107108

108109
int count = 0;
109110
while (1) {
@@ -195,10 +196,10 @@ int opal_btl_usnic_connectivity_listen(opal_btl_usnic_module_t *module)
195196
}
196197

197198
/* Ensure to NULL-terminate the passed strings */
198-
strncpy(cmd.nodename, opal_process_info.nodename,
199-
CONNECTIVITY_NODENAME_LEN - 1);
200-
strncpy(cmd.usnic_name, module->linux_device_name,
201-
CONNECTIVITY_IFNAME_LEN - 1);
199+
opal_string_copy(cmd.nodename, opal_process_info.nodename,
200+
CONNECTIVITY_NODENAME_LEN);
201+
opal_string_copy(cmd.usnic_name, module->linux_device_name,
202+
CONNECTIVITY_IFNAME_LEN);
202203

203204
if (OPAL_SUCCESS != opal_fd_write(agent_fd, sizeof(cmd), &cmd)) {
204205
OPAL_ERROR_LOG(OPAL_ERR_IN_ERRNO);
@@ -255,7 +256,7 @@ int opal_btl_usnic_connectivity_ping(uint32_t src_ipv4_addr, int src_port,
255256
.max_msg_size = max_msg_size
256257
};
257258
/* Ensure to NULL-terminate the passed string */
258-
strncpy(cmd.dest_nodename, dest_nodename, CONNECTIVITY_NODENAME_LEN - 1);
259+
opal_string_copy(cmd.dest_nodename, dest_nodename, CONNECTIVITY_NODENAME_LEN);
259260

260261
if (OPAL_SUCCESS != opal_fd_write(agent_fd, sizeof(cmd), &cmd)) {
261262
OPAL_ERROR_LOG(OPAL_ERR_IN_ERRNO);

opal/mca/btl/usnic/btl_usnic_proc.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
#include "opal/util/show_help.h"
2929
#include "opal/constants.h"
3030
#include "opal/util/bipartite_graph.h"
31+
#include "opal/util/string_copy.h"
3132

3233
#include "btl_usnic_compat.h"
3334
#include "btl_usnic.h"
@@ -239,8 +240,8 @@ static int create_proc(opal_proc_t *opal_proc,
239240
char protostr[32];
240241
proto = mca_btl_usnic_component.transport_protocol;
241242
memset(protostr, 0, sizeof(protostr));
242-
strncpy(protostr, fi_tostr(&proto, FI_TYPE_PROTOCOL),
243-
sizeof(protostr) - 1);
243+
opal_string_copy(protostr, fi_tostr(&proto, FI_TYPE_PROTOCOL),
244+
sizeof(protostr));
244245
proto = proc->proc_modex->protocol;
245246
opal_show_help("help-mpi-btl-usnic.txt",
246247
"transport mismatch",

opal/mca/if/bsdx_ipv4/if_bsdx.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515

1616
#include "opal/constants.h"
1717
#include "opal/util/output.h"
18+
#include "opal/util/string_copy.h"
1819
#include "opal/mca/if/if.h"
1920

2021
static int if_bsdx_open(void);
@@ -125,7 +126,7 @@ static int if_bsdx_open(void)
125126
/* fill values into the opal_if_t */
126127
memcpy(&a4, &(sin_addr->sin_addr), sizeof(struct in_addr));
127128

128-
strncpy(intf->if_name, cur_ifaddrs->ifa_name, IF_NAMESIZE);
129+
opal_string_copy(intf->if_name, cur_ifaddrs->ifa_name, IF_NAMESIZE);
129130
intf->if_index = opal_list_get_size(&opal_if_list) + 1;
130131
((struct sockaddr_in*) &intf->if_addr)->sin_addr = a4;
131132
((struct sockaddr_in*) &intf->if_addr)->sin_family = AF_INET;

opal/mca/if/bsdx_ipv6/if_bsdx_ipv6.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
#include "opal_config.h"
1212
#include "opal/constants.h"
1313
#include "opal/util/output.h"
14+
#include "opal/util/string_copy.h"
1415

1516
#include <string.h>
1617
#ifdef HAVE_UNISTD_H
@@ -198,7 +199,7 @@ static int if_bsdx_ipv6_open(void)
198199
return OPAL_ERR_OUT_OF_RESOURCE;
199200
}
200201
intf->af_family = AF_INET6;
201-
strncpy(intf->if_name, cur_ifaddrs->ifa_name, IF_NAMESIZE);
202+
opal_string_copy(intf->if_name, cur_ifaddrs->ifa_name, IF_NAMESIZE);
202203
intf->if_index = opal_list_get_size(&opal_if_list) + 1;
203204
((struct sockaddr_in6*) &intf->if_addr)->sin6_addr = a6;
204205
((struct sockaddr_in6*) &intf->if_addr)->sin6_family = AF_INET6;

opal/mca/if/linux_ipv6/if_linux_ipv6.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@
6262
#include "opal/constants.h"
6363
#include "opal/util/if.h"
6464
#include "opal/util/output.h"
65+
#include "opal/util/string_copy.h"
6566
#include "opal/mca/if/if.h"
6667
#include "opal/mca/if/base/base.h"
6768

@@ -143,7 +144,7 @@ static int if_linux_ipv6_open(void)
143144
}
144145

145146
/* now construct the opal_if_t */
146-
strncpy(intf->if_name, ifname, IF_NAMESIZE);
147+
opal_string_copy(intf->if_name, ifname, IF_NAMESIZE);
147148
intf->if_index = opal_list_get_size(&opal_if_list)+1;
148149
intf->if_kernel_index = (uint16_t) idx;
149150
((struct sockaddr_in6*) &intf->if_addr)->sin6_addr = a6;

opal/mca/if/posix_ipv4/if_posix.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919

2020
#include "opal/constants.h"
2121
#include "opal/util/output.h"
22+
#include "opal/util/string_copy.h"
2223
#include "opal/mca/if/if.h"
2324
#include "opal/mca/if/base/base.h"
2425

@@ -218,7 +219,7 @@ static int if_posix_open(void)
218219

219220
/* copy entry over into our data structure */
220221
memset(intf->if_name, 0, sizeof(intf->if_name));
221-
strncpy(intf->if_name, ifr->ifr_name, sizeof(intf->if_name) - 1);
222+
opal_string_copy(intf->if_name, ifr->ifr_name, sizeof(intf->if_name));
222223
intf->if_flags = ifr->ifr_flags;
223224

224225
/* every new address gets its own internal if_index */

opal/mca/if/solaris_ipv6/if_solaris_ipv6.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515

1616
#include "opal/constants.h"
1717
#include "opal/util/output.h"
18+
#include "opal/util/string_copy.h"
1819
#include "opal/mca/if/if.h"
1920

2021
static int if_solaris_ipv6_open(void);
@@ -96,7 +97,7 @@ static int if_solaris_ipv6_open(void)
9697
i += sizeof (*lifreq)) {
9798

9899
lifreq = (struct lifreq *)((caddr_t)lifconf.lifc_buf + i);
99-
strncpy (lifquery.lifr_name, lifreq->lifr_name,
100+
opal_string_copy (lifquery.lifr_name, lifreq->lifr_name,
100101
sizeof (lifquery.lifr_name));
101102

102103
/* lookup kernel index */
@@ -140,7 +141,7 @@ static int if_solaris_ipv6_open(void)
140141
}
141142
intf->af_family = AF_INET6;
142143

143-
strncpy (intf->if_name, lifreq->lifr_name, IF_NAMESIZE);
144+
opal_string_copy (intf->if_name, lifreq->lifr_name, IF_NAMESIZE);
144145
intf->if_index = opal_list_get_size(&opal_if_list)+1;
145146
memcpy(&intf->if_addr, my_addr, sizeof (*my_addr));
146147
intf->if_mask = 64;

0 commit comments

Comments
 (0)