Skip to content

Commit e9e4d2a

Browse files
committed
Handle asprintf errors with opal_asprintf wrapper
The Open MPI code base assumed that asprintf always behaved like the FreeBSD variant, where ptr is set to NULL on error. However, the C standard (and Linux) only guarantee that the return code will be -1 on error and leave ptr undefined. Rather than fix all the usage in the code, we use opal_asprintf() wrapper instead, which guarantees the BSD-like behavior of ptr always being set to NULL. In addition to being correct, this will fix many, many warnings in the Open MPI code base. Signed-off-by: Brian Barrett <bbarrett@amazon.com>
1 parent c087365 commit e9e4d2a

File tree

108 files changed

+697
-482
lines changed

Some content is hidden

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

108 files changed

+697
-482
lines changed

contrib/build-mca-comps-outside-of-tree/btl_tcp2_component.c

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
* Copyright (c) 2007-2010 Cisco Systems, Inc. All rights reserved.
1313
* Copyright (c) 2008 Sun Microsystems, Inc. All rights reserved.
1414
* Copyright (c) 2009 Oak Ridge National Laboratory
15+
* Copyright (c) 2018 Amazon.com, Inc. or its affiliates. All Rights reserved.
1516
* $COPYRIGHT$
1617
*
1718
* Additional copyrights may follow
@@ -55,6 +56,7 @@
5556
#include "opal/util/argv.h"
5657
#include "opal/util/net.h"
5758
#include "opal/util/opal_sos.h"
59+
#include "opal/util/printf.h"
5860

5961
#include "orte/types.h"
6062
#include "orte/util/show_help.h"
@@ -225,7 +227,7 @@ int mca_btl_tcp2_component_open(void)
225227
mca_btl_tcp2_component.tcp_port_min );
226228
mca_btl_tcp2_component.tcp_port_min = 1024;
227229
}
228-
asprintf( &message,
230+
opal_asprintf( &message,
229231
"The number of ports where the TCP BTL will try to bind (default %d)."
230232
" This parameter together with the port min, define a range of ports"
231233
" where Open MPI will open sockets.",
@@ -244,7 +246,7 @@ int mca_btl_tcp2_component_open(void)
244246
mca_btl_tcp2_component.tcp6_port_min );
245247
mca_btl_tcp2_component.tcp6_port_min = 1024;
246248
}
247-
asprintf( &message,
249+
opal_asprintf( &message,
248250
"The number of ports where the TCP BTL will try to bind (default %d)."
249251
" This parameter together with the port min, define a range of ports"
250252
" where Open MPI will open sockets.",

contrib/check_unnecessary_headers.sh

100755100644
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ SEARCH_HEADER[37]="opal/util/os_dirpath.h opal_os_dirpath_create opal_os_dirpath
122122
SEARCH_HEADER[38]="opal/util/os_path.h opal_os_path opal_make_filename_os_friendly"
123123
SEARCH_HEADER[39]="opal/util/output.h opal_output_stream_t opal_output_init opal_output_finalize opal_output_open opal_output_reopen opal_output_switch opal_output_reopen_all opal_output_close opal_output opal_output_verbose opal_output_vverbose opal_output_string opal_output_vstring opal_output_set_verbosity opal_output_get_verbosity opal_output_set_output_file_info OPAL_OUTPUT OPAL_OUTPUT_VERBOSE"
124124
SEARCH_HEADER[40]="opal/util/path.h opal_path_find opal_path_findv opal_path_is_absolute opal_find_absolute_path opal_path_access"
125-
SEARCH_HEADER[41]="opal/util/printf.h snprintf vsnprintf asprintf vasprintf"
125+
SEARCH_HEADER[41]="opal/util/printf.h snprintf vsnprintf"
126126
SEARCH_HEADER[42]="opal/util/show_help.h opal_show_help_init opal_show_help_finalize opal_show_help opal_show_vhelp opal_show_help_string opal_show_help_finish_parsing"
127127
SEARCH_HEADER[43]="opal/util/strncpy.h opal_strncpy"
128128
SEARCH_HEADER[44]="opal/util/sys_limits.h opal_sys_limits opal_util_init_sys_limits"

contrib/scaling/mpi_memprobe.c

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
#include "mpi.h"
1212
#include "opal/mca/pmix/pmix.h"
1313
#include "opal/util/argv.h"
14+
#include "opal/util/printf.h"
1415
#include "orte/runtime/runtime.h"
1516
#include "orte/util/proc_info.h"
1617
#include "orte/util/name_fns.h"
@@ -113,19 +114,19 @@ static void sample(void)
113114
wait_for_release = true;
114115
/* log my own results as a single string so the output
115116
* doesn't get garbled on the other end */
116-
asprintf(&tmp, "Data for node %s", orte_process_info.nodename);
117+
opal_asprintf(&tmp, "Data for node %s", orte_process_info.nodename);
117118
opal_argv_append_nosize(&answer, tmp);
118119
free(tmp);
119120
OPAL_LIST_FOREACH(kv, &response, opal_value_t) {
120121
lt = (opal_list_t*)kv->data.ptr;
121122
if (NULL != lt) {
122123
OPAL_LIST_FOREACH(ival, lt, opal_value_t) {
123124
if (0 == strcmp(ival->key, OPAL_PMIX_DAEMON_MEMORY)) {
124-
asprintf(&tmp, "\tDaemon: %f", ival->data.fval);
125+
opal_asprintf(&tmp, "\tDaemon: %f", ival->data.fval);
125126
opal_argv_append_nosize(&answer, tmp);
126127
free(tmp);
127128
} else if (0 == strcmp(ival->key, OPAL_PMIX_CLIENT_AVG_MEMORY)) {
128-
asprintf(&tmp, "\tClient: %f", ival->data.fval);
129+
opal_asprintf(&tmp, "\tClient: %f", ival->data.fval);
129130
opal_argv_append_nosize(&answer, tmp);
130131
free(tmp);
131132
} else {

ompi/communicator/comm_cid.c

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
* and Technology (RIST). All rights reserved.
2323
* Copyright (c) 2016 IBM Corporation. All rights reserved.
2424
* Copyright (c) 2017 Mellanox Technologies. All rights reserved.
25+
* Copyright (c) 2018 Amazon.com, Inc. or its affiliates. All Rights reserved.
2526
* $COPYRIGHT$
2627
*
2728
* Additional copyrights may follow
@@ -33,6 +34,7 @@
3334

3435
#include "opal/dss/dss.h"
3536
#include "opal/mca/pmix/pmix.h"
37+
#include "opal/util/printf.h"
3638

3739
#include "ompi/proc/proc.h"
3840
#include "ompi/communicator/communicator.h"
@@ -900,7 +902,7 @@ static int ompi_comm_allreduce_pmix_reduce_complete (ompi_comm_request_t *reques
900902
opal_dss.unload(&sbuf, (void**)&info.data.bo.bytes, &info.data.bo.size);
901903
OBJ_DESTRUCT(&sbuf);
902904

903-
bytes_written = asprintf(&info.key,
905+
bytes_written = opal_asprintf(&info.key,
904906
cid_context->send_first ? "%s:%s:send:%d"
905907
: "%s:%s:recv:%d",
906908
cid_context->port_string,
@@ -910,7 +912,7 @@ static int ompi_comm_allreduce_pmix_reduce_complete (ompi_comm_request_t *reques
910912
if (bytes_written == -1) {
911913
opal_output_verbose (verbosity_level, output_id, "writing info.key failed\n");
912914
} else {
913-
bytes_written = asprintf(&pdat.value.key,
915+
bytes_written = opal_asprintf(&pdat.value.key,
914916
cid_context->send_first ? "%s:%s:recv:%d"
915917
: "%s:%s:send:%d",
916918
cid_context->port_string,

ompi/datatype/ompi_datatype_create.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
* University of Stuttgart. All rights reserved.
1111
* Copyright (c) 2009 Oak Ridge National Labs. All rights reserved.
1212
* Copyright (c) 2010-2018 Cisco Systems, Inc. All rights reserved
13+
* Copyright (c) 2018 Amazon.com, Inc. or its affiliates. All Rights reserved.
1314
* $COPYRIGHT$
1415
*
1516
* Additional copyrights may follow
@@ -23,6 +24,7 @@
2324
#include <string.h>
2425

2526
#include "opal/class/opal_pointer_array.h"
27+
#include "opal/util/printf.h"
2628
#include "ompi/datatype/ompi_datatype.h"
2729
#include "ompi/attribute/attribute.h"
2830

@@ -110,7 +112,7 @@ ompi_datatype_duplicate( const ompi_datatype_t* oldType, ompi_datatype_t** newTy
110112
new_ompi_datatype->args = NULL;
111113

112114
char *new_name;
113-
asprintf(&new_name, "Dup %s", oldType->name);
115+
opal_asprintf(&new_name, "Dup %s", oldType->name);
114116
strncpy(new_ompi_datatype->name, new_name, MPI_MAX_OBJECT_NAME - 1);
115117
new_ompi_datatype->name[MPI_MAX_OBJECT_NAME - 1] = '\0';
116118
free(new_name);

ompi/debuggers/dlopen_test.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
/*
22
* Copyright (c) 2009-2018 Cisco Systems, Inc. All rights reserved.
3+
* Copyright (c) 2018 Amazon.com, Inc. or its affiliates. All Rights reserved.
34
* $COPYRIGHT$
45
*
56
* Additional copyrights may follow
@@ -15,6 +16,7 @@
1516
#include <unistd.h>
1617

1718
#include "opal/runtime/opal.h"
19+
#include "opal/util/printf.h"
1820
#include "opal/mca/dl/base/base.h"
1921

2022
#if !OPAL_HAVE_DL_SUPPORT
@@ -119,7 +121,7 @@ static int do_test(void)
119121
get here, manually prefix the filename with .libs/ and try
120122
again. */
121123
char *rel_filename;
122-
asprintf(&rel_filename, ".libs/%s", filename);
124+
opal_asprintf(&rel_filename, ".libs/%s", filename);
123125
if (NULL == rel_filename) {
124126
return 1;
125127
}

ompi/debuggers/ompi_debuggers.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
* Copyright (c) 2007-2016 Cisco Systems, Inc. All rights reserved.
1414
* Copyright (c) 2012-2015 Los Alamos National Security, LLC. All rights
1515
* reserved.
16+
* Copyright (c) 2018 Amazon.com, Inc. or its affiliates. All Rights reserved.
1617
* $COPYRIGHT$
1718
*
1819
* Additional copyrights may follow
@@ -48,6 +49,7 @@
4849

4950
#include "opal/mca/base/base.h"
5051
#include "opal/util/argv.h"
52+
#include "opal/util/printf.h"
5153
#include "opal/mca/installdirs/installdirs.h"
5254
#include "debuggers.h"
5355
#include "ompi/mca/rte/rte.h"
@@ -132,7 +134,7 @@ static void check(char *dir, char *file, char **locations)
132134
{
133135
char *str;
134136

135-
asprintf(&str, "%s/%s.so", dir, file);
137+
opal_asprintf(&str, "%s/%s.so", dir, file);
136138

137139
#if defined(HAVE_SYS_STAT_H)
138140
{

ompi/dpm/dpm.c

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
* Copyright (c) 2013-2017 Intel, Inc. All rights reserved.
1919
* Copyright (c) 2014-2017 Research Organization for Information Science
2020
* and Technology (RIST). All rights reserved.
21+
* Copyright (c) 2018 Amazon.com, Inc. or its affiliates. All Rights reserved.
2122
* $COPYRIGHT$
2223
*
2324
* Additional copyrights may follow
@@ -41,6 +42,7 @@
4142
#include "opal/util/opal_getcwd.h"
4243
#include "opal/util/proc.h"
4344
#include "opal/util/show_help.h"
45+
#include "opal/util/printf.h"
4446
#include "opal/dss/dss.h"
4547
#include "opal/mca/hwloc/base/base.h"
4648
#include "opal/mca/pmix/pmix.h"
@@ -153,7 +155,7 @@ int ompi_dpm_connect_accept(ompi_communicator_t *comm, int root,
153155
return OMPI_ERR_NOT_SUPPORTED;
154156
}
155157
opal_argv_append_nosize(&members, nstring);
156-
(void)asprintf(&nstring, "%d", size);
158+
(void)opal_asprintf(&nstring, "%d", size);
157159
opal_argv_append_nosize(&members, nstring);
158160
free(nstring);
159161
} else {
@@ -208,11 +210,11 @@ int ompi_dpm_connect_accept(ompi_communicator_t *comm, int root,
208210
OBJ_CONSTRUCT(&info, opal_value_t);
209211
OBJ_CONSTRUCT(&pdat, opal_pmix_pdata_t);
210212
if (send_first) {
211-
(void)asprintf(&info.key, "%s:connect", port_string);
212-
(void)asprintf(&pdat.value.key, "%s:accept", port_string);
213+
(void)opal_asprintf(&info.key, "%s:connect", port_string);
214+
(void)opal_asprintf(&pdat.value.key, "%s:accept", port_string);
213215
} else {
214-
(void)asprintf(&info.key, "%s:accept", port_string);
215-
(void)asprintf(&pdat.value.key, "%s:connect", port_string);
216+
(void)opal_asprintf(&info.key, "%s:accept", port_string);
217+
(void)opal_asprintf(&pdat.value.key, "%s:connect", port_string);
216218
}
217219
info.type = OPAL_STRING;
218220
info.data.string = opal_argv_join(members, ':');
@@ -812,7 +814,7 @@ int ompi_dpm_spawn(int count, const char *array_of_commands[],
812814
info = OBJ_NEW(opal_value_t);
813815
info->key = strdup(OPAL_PMIX_PPR);
814816
info->type = OPAL_STRING;
815-
(void)asprintf(&(info->data.string), "%s:n", slot_list);
817+
(void)opal_asprintf(&(info->data.string), "%s:n", slot_list);
816818
opal_list_append(&job_info, &info->super);
817819
}
818820
ompi_info_get (array_of_info[i], "pernode", sizeof(slot_list) - 1, slot_list, &flag);

ompi/errhandler/errhandler_predefined.c

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
* Copyright (c) 2012 Los Alamos National Security, LLC.
1717
* All rights reserved.
1818
* Copyright (c) 2016 Intel, Inc. All rights reserved.
19+
* Copyright (c) 2018 Amazon.com, Inc. or its affiliates. All Rights reserved.
1920
* $COPYRIGHT$
2021
*
2122
* Additional copyrights may follow
@@ -194,7 +195,7 @@ static void backend_fatal_aggregate(char *type,
194195
arg = va_arg(arglist, char*);
195196
va_end(arglist);
196197

197-
if (asprintf(&prefix, "[%s:%05d]",
198+
if (opal_asprintf(&prefix, "[%s:%05d]",
198199
ompi_process_info.nodename,
199200
(int) ompi_process_info.pid) == -1) {
200201
prefix = NULL;
@@ -207,7 +208,7 @@ static void backend_fatal_aggregate(char *type,
207208
if (NULL != error_code) {
208209
err_msg = ompi_mpi_errnum_get_string(*error_code);
209210
if (NULL == err_msg) {
210-
if (asprintf(&err_msg, unknown_error_code,
211+
if (opal_asprintf(&err_msg, unknown_error_code,
211212
*error_code) == -1) {
212213
err_msg = NULL;
213214
opal_output(0, "%s", "Could not write to err_msg");
@@ -333,7 +334,7 @@ static void backend_fatal_no_aggregate(char *type,
333334
}
334335

335336
if (NULL != name) {
336-
/* Don't use asprintf() here because there may be stack /
337+
/* Don't use opal_asprintf() here because there may be stack /
337338
heap corruption by the time we're invoked, so just do
338339
it on the stack */
339340
str[0] = '\0';

ompi/mca/coll/fca/coll_fca_component.c

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
* and Technology (RIST). All rights reserved.
66
* Copyright (c) 2015 Los Alamos National Security, LLC. All rights
77
* reserved.
8+
* Copyright (c) 2018 Amazon.com, Inc. or its affiliates. All Rights reserved.
89
* $COPYRIGHT$
910
*
1011
* Additional copyrights may follow
@@ -27,9 +28,9 @@
2728
#ifdef HAVE_UNISTD_H
2829
#include <unistd.h>
2930
#endif
30-
#include "opal/mca/installdirs/installdirs.h"
31-
3231

32+
#include "opal/mca/installdirs/installdirs.h"
33+
#include "opal/util/printf.h"
3334

3435
#include "coll_fca.h"
3536

@@ -1212,10 +1213,10 @@ static char *fca_check_file(char *file)
12121213
static char *fca_get_spec_file(void)
12131214
{
12141215
char *file;
1215-
asprintf(&file, "%s/etc/fca_mpi_spec.ini", COLL_FCA_HOME);
1216+
opal_asprintf(&file, "%s/etc/fca_mpi_spec.ini", COLL_FCA_HOME);
12161217
if (NULL == fca_check_file(file)) {
12171218
free(file);
1218-
asprintf(&file, "%s/../fca/etc/fca_mpi_spec.ini", opal_install_dirs.prefix);
1219+
opal_asprintf(&file, "%s/../fca/etc/fca_mpi_spec.ini", opal_install_dirs.prefix);
12191220
if (NULL == fca_check_file(file)) {
12201221
free(file);
12211222
return NULL;

0 commit comments

Comments
 (0)