Skip to content

Commit cef6cf0

Browse files
committed
opal: update some string handling
Make various string handling locations a little more robust. Signed-off-by: Jeff Squyres <jsquyres@cisco.com>
1 parent e6de42c commit cef6cf0

File tree

5 files changed

+24
-25
lines changed

5 files changed

+24
-25
lines changed

opal/mca/btl/tcp/btl_tcp_endpoint.c

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
* Copyright (c) 2004-2005 The Regents of the University of California.
1111
* All rights reserved.
1212
* Copyright (c) 2007-2008 Sun Microsystems, Inc. All rights reserved.
13-
* Copyright (c) 2013 Cisco Systems, Inc. All rights reserved.
13+
* Copyright (c) 2013-2018 Cisco Systems, Inc. All rights reserved
1414
* Copyright (c) 2014 Intel, Inc. All rights reserved.
1515
* Copyright (c) 2015 Research Organization for Information Science
1616
* and Technology (RIST). All rights reserved.
@@ -56,6 +56,7 @@
5656
#include "opal/util/show_help.h"
5757
#include "opal/util/proc.h"
5858
#include "opal/util/printf.h"
59+
#include "opal/util/string_copy.h"
5960
#include "opal/mca/btl/base/btl_base_error.h"
6061

6162
#include "btl_tcp.h"
@@ -404,7 +405,8 @@ mca_btl_tcp_endpoint_send_connect_ack(mca_btl_base_endpoint_t* btl_endpoint)
404405
OPAL_PROCESS_NAME_HTON(guid);
405406

406407
mca_btl_tcp_endpoint_hs_msg_t hs_msg;
407-
strcpy(hs_msg.magic_id, mca_btl_tcp_magic_id_string);
408+
opal_string_copy(hs_msg.magic_id, mca_btl_tcp_magic_id_string,
409+
sizeof(hs_msg.magic_id));
408410
hs_msg.guid = guid;
409411

410412
if(sizeof(hs_msg) !=

opal/test/reachable/reachable_netlink.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
/*
22
* Copyright (c) 2017 Amazon.com, Inc. or its affiliates. All Rights
33
* reserved.
4+
* Copyright (c) 2018 Cisco Systems, Inc. All rights reserved
45
* $COPYRIGHT$
56
*
67
* Additional copyrights may follow
@@ -137,7 +138,7 @@ int main(int argc, char **argv)
137138
break;
138139
default:
139140
family = "Unknown";
140-
strcpy(addr, "Unknown");
141+
opal_string_copy(addr, "Unknown", sizeof(addr));
141142
break;
142143
}
143144

@@ -165,7 +166,7 @@ int main(int argc, char **argv)
165166
break;
166167
default:
167168
family = "Unknown";
168-
strcpy(addr, "Unknown");
169+
opal_string_copy(addr, "Unknown", sizeof(addr));
169170
break;
170171
}
171172

opal/util/opal_pty.c

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
* University of Stuttgart. All rights reserved.
1010
* Copyright (c) 2004-2005 The Regents of the University of California.
1111
* All rights reserved.
12+
* Copyright (c) 2018 Cisco Systems, Inc. All rights reserved
1213
* $COPYRIGHT$
1314
*
1415
* Additional copyrights may follow
@@ -135,7 +136,9 @@ int opal_openpty(int *amaster, int *aslave, char *name,
135136
return -1;
136137
}
137138
if (name) {
138-
strcpy(name, line);
139+
// We don't know the max length of name, but we do know the
140+
// max length of the source, so at least use that.
141+
opal_string_copy(name, line, sizeof(line));
139142
}
140143
#ifndef TCSAFLUSH
141144
#define TCSAFLUSH TCSETAF

opal/util/os_path.c

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -54,14 +54,12 @@ char *opal_os_path(int relative, ...)
5454
va_end(ap);
5555

5656
if (0 == num_elements) { /* must be looking for a simple answer */
57-
path = (char *)malloc(3);
58-
path[0] = '\0';
57+
size_t len = 3;
58+
path = (char *)calloc(len, sizeof(char));
5959
if (relative) {
60-
strcpy(path, ".");
61-
strcat(path, path_sep);
62-
} else {
63-
strcpy(path, path_sep);
64-
}
60+
path[0] = '.';
61+
}
62+
strncat(path, path_sep, len);
6563
return(path);
6664
}
6765

@@ -76,28 +74,27 @@ char *opal_os_path(int relative, ...)
7674
return(NULL);
7775
}
7876

79-
path = (char *)malloc(total_length);
77+
path = (char *)calloc(total_length, sizeof(char));
8078
if (NULL == path) {
8179
return(NULL);
8280
}
83-
path[0] = 0;
8481

8582
if (relative) {
86-
strcpy(path, ".");
83+
path[0] = '.';
8784
}
8885

8986
va_start(ap, relative);
9087
if( NULL != (element = va_arg(ap, char*)) ) {
9188
if (path_sep[0] != element[0]) {
92-
strcat(path, path_sep);
89+
strncat(path, path_sep, total_length);
9390
}
9491
strcat(path, element);
9592
}
9693
while (NULL != (element=va_arg(ap, char*))) {
9794
if (path_sep[0] != element[0]) {
98-
strcat(path, path_sep);
95+
strncat(path, path_sep, total_length);
9996
}
100-
strcat(path, element);
97+
strncat(path, element, total_length);
10198
}
10299

103100
va_end(ap);

opal/util/path.c

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
* University of Stuttgart. All rights reserved.
1010
* Copyright (c) 2004-2005 The Regents of the University of California.
1111
* All rights reserved.
12-
* Copyright (c) 2009-2014 Cisco Systems, Inc. All rights reserved.
12+
* Copyright (c) 2009-2018 Cisco Systems, Inc. All rights reserved
1313
* Copyright (c) 2010 IBM Corporation. All rights reserved.
1414
* Copyright (c) 2012-2013 Los Alamos National Security, LLC.
1515
* All rights reserved.
@@ -77,6 +77,7 @@
7777
#include "opal/util/path.h"
7878
#include "opal/util/os_path.h"
7979
#include "opal/util/argv.h"
80+
#include "opal/util/printf.h"
8081

8182
/*
8283
* Sanity check to ensure we have either statfs or statvfs
@@ -146,12 +147,7 @@ char *opal_path_find(char *fname, char **pathv, int mode, char **envv)
146147
if (!delimit) {
147148
fullpath = opal_path_access(fname, env, mode);
148149
} else {
149-
pfix = (char*) malloc(strlen(env) + strlen(delimit) + 1);
150-
if (NULL == pfix) {
151-
return NULL;
152-
}
153-
strcpy(pfix, env);
154-
strcat(pfix, delimit);
150+
opal_asprintf(&pfix, "%s%s", env, delimit);
155151
fullpath = opal_path_access(fname, pfix, mode);
156152
free(pfix);
157153
}

0 commit comments

Comments
 (0)