From 54ca3310ea35b7dc857afc59e00ffbb8f57e15ec Mon Sep 17 00:00:00 2001 From: Jeff Squyres Date: Sun, 14 Oct 2018 15:55:24 -0700 Subject: [PATCH] ompi: cleanup various string operations Several fixes to string handling: 1. strncpy() -> opal_string_copy() (because opal_string_copy() guarantees to NULL-terminate, and strncpy() does not) 2. Simplify a few places, such as: * Since opal_string_copy() guarantees to NULL terminate, eliminate some memsets(), etc. * Use opal_asprintf() to eliminate multi-step string creation There's more work that could be done; e.g., this commit doesn't attempt to clean up any strcpy() usage. Signed-off-by: Jeff Squyres --- ompi/communicator/comm.c | 7 +- ompi/communicator/comm_init.c | 16 ++--- ompi/datatype/ompi_datatype_create.c | 3 +- ompi/datatype/ompi_datatype_module.c | 9 +-- ompi/errhandler/errcode-internal.c | 72 +++++++++++++------- ompi/errhandler/errcode.c | 8 ++- ompi/errhandler/errhandler.c | 22 +++--- ompi/info/info.c | 3 +- ompi/mca/fs/base/fs_base_get_parent_dir.c | 25 +++++-- ompi/mca/pml/v/pml_v_component.c | 8 +-- ompi/mca/rte/pmix/rte_pmix_module.c | 6 +- ompi/mpi/c/comm_get_name.c | 8 ++- ompi/mpi/c/error_string.c | 5 +- ompi/mpi/c/lookup_name.c | 6 +- ompi/mpi/c/type_create_f90_complex.c | 16 ++--- ompi/mpi/c/type_create_f90_integer.c | 12 ++-- ompi/mpi/c/type_create_f90_real.c | 16 ++--- ompi/mpi/c/type_get_name.c | 6 +- ompi/mpi/c/type_set_name.c | 5 +- ompi/mpi/fortran/base/strings.c | 11 +-- ompi/mpi/tool/mpit-internal.h | 8 ++- ompi/mpiext/affinity/c/mpiext_affinity_str.c | 15 ++-- ompi/mpiext/cr/c/migrate.c | 7 +- ompi/op/op.c | 7 +- ompi/win/win.c | 9 ++- 25 files changed, 188 insertions(+), 122 deletions(-) diff --git a/ompi/communicator/comm.c b/ompi/communicator/comm.c index 228abae7ab..c5a5c4e3cf 100644 --- a/ompi/communicator/comm.c +++ b/ompi/communicator/comm.c @@ -11,7 +11,7 @@ * Copyright (c) 2004-2005 The Regents of the University of California. * All rights reserved. * Copyright (c) 2007-2011 University of Houston. All rights reserved. - * Copyright (c) 2007-2012 Cisco Systems, Inc. All rights reserved. + * Copyright (c) 2007-2018 Cisco Systems, Inc. All rights reserved * Copyright (c) 2009 Sun Microsystems, Inc. All rights reserved. * Copyright (c) 2011-2013 Inria. All rights reserved. * Copyright (c) 2011-2013 Universite Bordeaux 1 @@ -38,6 +38,7 @@ #include "opal/mca/hwloc/base/base.h" #include "opal/dss/dss.h" #include "opal/mca/pmix/pmix.h" +#include "opal/util/string_copy.h" #include "ompi/proc/proc.h" #include "opal/threads/mutex.h" @@ -1317,9 +1318,7 @@ int ompi_comm_set_name (ompi_communicator_t *comm, const char *name ) { OPAL_THREAD_LOCK(&(comm->c_lock)); - memset(comm->c_name, 0, MPI_MAX_OBJECT_NAME); - strncpy(comm->c_name, name, MPI_MAX_OBJECT_NAME); - comm->c_name[MPI_MAX_OBJECT_NAME - 1] = 0; + opal_string_copy(comm->c_name, name, MPI_MAX_OBJECT_NAME); comm->c_flags |= OMPI_COMM_NAMEISSET; OPAL_THREAD_UNLOCK(&(comm->c_lock)); diff --git a/ompi/communicator/comm_init.c b/ompi/communicator/comm_init.c index 75aac4d49e..824c068001 100644 --- a/ompi/communicator/comm_init.c +++ b/ompi/communicator/comm_init.c @@ -11,7 +11,7 @@ * Copyright (c) 2004-2005 The Regents of the University of California. * All rights reserved. * Copyright (c) 2006-2017 University of Houston. All rights reserved. - * Copyright (c) 2007-2012 Cisco Systems, Inc. All rights reserved. + * Copyright (c) 2007-2018 Cisco Systems, Inc. All rights reserved * Copyright (c) 2009 Sun Microsystems, Inc. All rights reserved. * Copyright (c) 2012-2015 Los Alamos National Security, LLC. * All rights reserved. @@ -35,6 +35,7 @@ #include "opal/util/bit_ops.h" #include "opal/util/info_subscriber.h" +#include "opal/util/string_copy.h" #include "opal/mca/pmix/pmix.h" #include "ompi/constants.h" #include "ompi/mca/pml/pml.h" @@ -139,9 +140,8 @@ int ompi_comm_init(void) OMPI_COMM_SET_PML_ADDED(&ompi_mpi_comm_world.comm); opal_pointer_array_set_item (&ompi_mpi_communicators, 0, &ompi_mpi_comm_world); - MEMCHECKER (memset (ompi_mpi_comm_world.comm.c_name, 0, MPI_MAX_OBJECT_NAME)); - strncpy (ompi_mpi_comm_world.comm.c_name, "MPI_COMM_WORLD", - strlen("MPI_COMM_WORLD")+1 ); + opal_string_copy(ompi_mpi_comm_world.comm.c_name, "MPI_COMM_WORLD", + sizeof(ompi_mpi_comm_world.comm.c_name)); ompi_mpi_comm_world.comm.c_flags |= OMPI_COMM_NAMEISSET; ompi_mpi_comm_world.comm.c_flags |= OMPI_COMM_INTRINSIC; @@ -193,8 +193,8 @@ int ompi_comm_init(void) OMPI_COMM_SET_PML_ADDED(&ompi_mpi_comm_self.comm); opal_pointer_array_set_item (&ompi_mpi_communicators, 1, &ompi_mpi_comm_self); - MEMCHECKER (memset (ompi_mpi_comm_self.comm.c_name, 0, MPI_MAX_OBJECT_NAME)); - strncpy(ompi_mpi_comm_self.comm.c_name,"MPI_COMM_SELF",strlen("MPI_COMM_SELF")+1); + opal_string_copy(ompi_mpi_comm_self.comm.c_name, "MPI_COMM_SELF", + sizeof(ompi_mpi_comm_self.comm.c_name)); ompi_mpi_comm_self.comm.c_flags |= OMPI_COMM_NAMEISSET; ompi_mpi_comm_self.comm.c_flags |= OMPI_COMM_INTRINSIC; @@ -218,8 +218,8 @@ int ompi_comm_init(void) OBJ_RETAIN( &ompi_mpi_errors_are_fatal.eh ); opal_pointer_array_set_item (&ompi_mpi_communicators, 2, &ompi_mpi_comm_null); - MEMCHECKER (memset (ompi_mpi_comm_null.comm.c_name, 0, MPI_MAX_OBJECT_NAME)); - strncpy(ompi_mpi_comm_null.comm.c_name,"MPI_COMM_NULL",strlen("MPI_COMM_NULL")+1); + opal_string_copy(ompi_mpi_comm_null.comm.c_name, "MPI_COMM_NULL", + sizeof(ompi_mpi_comm_null.comm.c_name)); ompi_mpi_comm_null.comm.c_flags |= OMPI_COMM_NAMEISSET; ompi_mpi_comm_null.comm.c_flags |= OMPI_COMM_INTRINSIC; diff --git a/ompi/datatype/ompi_datatype_create.c b/ompi/datatype/ompi_datatype_create.c index 0d59db2149..f9e39710f7 100644 --- a/ompi/datatype/ompi_datatype_create.c +++ b/ompi/datatype/ompi_datatype_create.c @@ -25,6 +25,7 @@ #include "opal/class/opal_pointer_array.h" #include "opal/util/printf.h" +#include "opal/util/string_copy.h" #include "ompi/datatype/ompi_datatype.h" #include "ompi/attribute/attribute.h" @@ -113,7 +114,7 @@ ompi_datatype_duplicate( const ompi_datatype_t* oldType, ompi_datatype_t** newTy char *new_name; opal_asprintf(&new_name, "Dup %s", oldType->name); - strncpy(new_ompi_datatype->name, new_name, MPI_MAX_OBJECT_NAME - 1); + opal_string_copy(new_ompi_datatype->name, new_name, MPI_MAX_OBJECT_NAME); new_ompi_datatype->name[MPI_MAX_OBJECT_NAME - 1] = '\0'; free(new_name); diff --git a/ompi/datatype/ompi_datatype_module.c b/ompi/datatype/ompi_datatype_module.c index ef4398f576..badfb6a04b 100644 --- a/ompi/datatype/ompi_datatype_module.c +++ b/ompi/datatype/ompi_datatype_module.c @@ -10,7 +10,7 @@ * University of Stuttgart. All rights reserved. * Copyright (c) 2004-2006 The Regents of the University of California. * All rights reserved. - * Copyright (c) 2007-2012 Cisco Systems, Inc. All rights reserved. + * Copyright (c) 2007-2018 Cisco Systems, Inc. All rights reserved * Copyright (c) 2009 Sun Microsystems, Inc. All rights reserved. * Copyright (c) 2009 Oak Ridge National Labs. All rights reserved. * Copyright (c) 2013 Los Alamos National Security, LLC. All rights @@ -32,6 +32,7 @@ #include "opal/datatype/opal_convertor_internal.h" #include "opal/util/output.h" +#include "opal/util/string_copy.h" #include "opal/class/opal_pointer_array.h" #include "ompi/datatype/ompi_datatype.h" #include "ompi/datatype/ompi_datatype_internal.h" @@ -420,7 +421,7 @@ opal_pointer_array_t ompi_datatype_f_to_c_table = {{0}}; ptype->super.desc.desc = NULL; \ ptype->super.opt_desc.desc = NULL; \ OBJ_RELEASE( ptype ); \ - strncpy( (PDATA)->name, MPIDDTNAME, MPI_MAX_OBJECT_NAME ); \ + opal_string_copy( (PDATA)->name, MPIDDTNAME, MPI_MAX_OBJECT_NAME ); \ } while(0) #define DECLARE_MPI2_COMPOSED_BLOCK_DDT( PDATA, MPIDDT, MPIDDTNAME, MPIType, FLAGS ) \ @@ -438,14 +439,14 @@ opal_pointer_array_t ompi_datatype_f_to_c_table = {{0}}; ptype->super.desc.desc = NULL; \ ptype->super.opt_desc.desc = NULL; \ OBJ_RELEASE( ptype ); \ - strncpy( (PDATA)->name, (MPIDDTNAME), MPI_MAX_OBJECT_NAME ); \ + opal_string_copy( (PDATA)->name, (MPIDDTNAME), MPI_MAX_OBJECT_NAME ); \ } while(0) #define DECLARE_MPI_SYNONYM_DDT( PDATA, MPIDDTNAME, PORIGDDT) \ do { \ /* just memcpy as it's easier this way */ \ memcpy( (PDATA), (PORIGDDT), sizeof(ompi_datatype_t) ); \ - strncpy( (PDATA)->name, MPIDDTNAME, MPI_MAX_OBJECT_NAME ); \ + opal_string_copy( (PDATA)->name, MPIDDTNAME, MPI_MAX_OBJECT_NAME ); \ /* forget the language flag */ \ (PDATA)->super.flags &= ~OMPI_DATATYPE_FLAG_DATA_LANGUAGE; \ (PDATA)->super.flags &= ~OPAL_DATATYPE_FLAG_PREDEFINED; \ diff --git a/ompi/errhandler/errcode-internal.c b/ompi/errhandler/errcode-internal.c index 37429f6ff8..8d76030a6c 100644 --- a/ompi/errhandler/errcode-internal.c +++ b/ompi/errhandler/errcode-internal.c @@ -14,6 +14,7 @@ * and Technology (RIST). All rights reserved. * Copyright (c) 2015 Los Alamos National Security, LLC. All rights * reseved. + * Copyright (c) 2018 Cisco Systems, Inc. All rights reserved * $COPYRIGHT$ * * Additional copyrights may follow @@ -27,6 +28,8 @@ #include #include "mpi.h" +#include "opal/util/string_copy.h" + #include "ompi/errhandler/errcode-internal.h" /* Table holding all error codes */ @@ -79,7 +82,8 @@ int ompi_errcode_intern_init (void) ompi_success_intern.code = OMPI_SUCCESS; ompi_success_intern.mpi_code = MPI_SUCCESS; ompi_success_intern.index = pos++; - strncpy(ompi_success_intern.errstring, "OMPI_SUCCESS", OMPI_MAX_ERROR_STRING); + opal_string_copy(ompi_success_intern.errstring, "OMPI_SUCCESS", + OMPI_MAX_ERROR_STRING); opal_pointer_array_set_item(&ompi_errcodes_intern, ompi_success_intern.index, &ompi_success_intern); @@ -87,7 +91,8 @@ int ompi_errcode_intern_init (void) ompi_error_intern.code = OMPI_ERROR; ompi_error_intern.mpi_code = MPI_ERR_OTHER; ompi_error_intern.index = pos++; - strncpy(ompi_error_intern.errstring, "OMPI_ERROR", OMPI_MAX_ERROR_STRING); + opal_string_copy(ompi_error_intern.errstring, "OMPI_ERROR", + OMPI_MAX_ERROR_STRING); opal_pointer_array_set_item(&ompi_errcodes_intern, ompi_error_intern.index, &ompi_error_intern); @@ -95,7 +100,8 @@ int ompi_errcode_intern_init (void) ompi_err_out_of_resource_intern.code = OMPI_ERR_OUT_OF_RESOURCE; ompi_err_out_of_resource_intern.mpi_code = MPI_ERR_INTERN; ompi_err_out_of_resource_intern.index = pos++; - strncpy(ompi_err_out_of_resource_intern.errstring, "OMPI_ERR_OUT_OF_RESOURCE", OMPI_MAX_ERROR_STRING); + opal_string_copy(ompi_err_out_of_resource_intern.errstring, + "OMPI_ERR_OUT_OF_RESOURCE", OMPI_MAX_ERROR_STRING); opal_pointer_array_set_item(&ompi_errcodes_intern, ompi_err_out_of_resource_intern.index, &ompi_err_out_of_resource_intern); @@ -103,7 +109,8 @@ int ompi_errcode_intern_init (void) ompi_err_temp_out_of_resource_intern.code = OMPI_ERR_TEMP_OUT_OF_RESOURCE; ompi_err_temp_out_of_resource_intern.mpi_code = MPI_ERR_INTERN; ompi_err_temp_out_of_resource_intern.index = pos++; - strncpy(ompi_err_temp_out_of_resource_intern.errstring, "OMPI_ERR_TEMP_OUT_OF_RESOURCE", OMPI_MAX_ERROR_STRING); + opal_string_copy(ompi_err_temp_out_of_resource_intern.errstring, + "OMPI_ERR_TEMP_OUT_OF_RESOURCE", OMPI_MAX_ERROR_STRING); opal_pointer_array_set_item(&ompi_errcodes_intern, ompi_err_temp_out_of_resource_intern.index, &ompi_err_temp_out_of_resource_intern); @@ -111,7 +118,8 @@ int ompi_errcode_intern_init (void) ompi_err_resource_busy_intern.code = OMPI_ERR_RESOURCE_BUSY; ompi_err_resource_busy_intern.mpi_code = MPI_ERR_INTERN; ompi_err_resource_busy_intern.index = pos++; - strncpy(ompi_err_resource_busy_intern.errstring, "OMPI_ERR_RESOURCE_BUSY", OMPI_MAX_ERROR_STRING); + opal_string_copy(ompi_err_resource_busy_intern.errstring, + "OMPI_ERR_RESOURCE_BUSY", OMPI_MAX_ERROR_STRING); opal_pointer_array_set_item(&ompi_errcodes_intern, ompi_err_resource_busy_intern.index, &ompi_err_resource_busy_intern); @@ -119,7 +127,8 @@ int ompi_errcode_intern_init (void) ompi_err_bad_param_intern.code = OMPI_ERR_BAD_PARAM; ompi_err_bad_param_intern.mpi_code = MPI_ERR_ARG; ompi_err_bad_param_intern.index = pos++; - strncpy(ompi_err_bad_param_intern.errstring, "OMPI_ERR_BAD_PARAM", OMPI_MAX_ERROR_STRING); + opal_string_copy(ompi_err_bad_param_intern.errstring, + "OMPI_ERR_BAD_PARAM", OMPI_MAX_ERROR_STRING); opal_pointer_array_set_item(&ompi_errcodes_intern, ompi_err_bad_param_intern.index, &ompi_err_bad_param_intern); @@ -127,7 +136,8 @@ int ompi_errcode_intern_init (void) ompi_err_fatal_intern.code = OMPI_ERR_FATAL; ompi_err_fatal_intern.mpi_code = MPI_ERR_INTERN; ompi_err_fatal_intern.index = pos++; - strncpy(ompi_err_fatal_intern.errstring, "OMPI_ERR_FATAL", OMPI_MAX_ERROR_STRING); + opal_string_copy(ompi_err_fatal_intern.errstring, "OMPI_ERR_FATAL", + OMPI_MAX_ERROR_STRING); opal_pointer_array_set_item(&ompi_errcodes_intern, ompi_err_fatal_intern.index, &ompi_err_fatal_intern); @@ -135,7 +145,8 @@ int ompi_errcode_intern_init (void) ompi_err_not_implemented_intern.code = OMPI_ERR_NOT_IMPLEMENTED; ompi_err_not_implemented_intern.mpi_code = MPI_ERR_INTERN; ompi_err_not_implemented_intern.index = pos++; - strncpy(ompi_err_not_implemented_intern.errstring, "OMPI_ERR_NOT_IMPLEMENTED", OMPI_MAX_ERROR_STRING); + opal_string_copy(ompi_err_not_implemented_intern.errstring, + "OMPI_ERR_NOT_IMPLEMENTED", OMPI_MAX_ERROR_STRING); opal_pointer_array_set_item(&ompi_errcodes_intern, ompi_err_not_implemented_intern.index, &ompi_err_not_implemented_intern); @@ -143,7 +154,8 @@ int ompi_errcode_intern_init (void) ompi_err_not_supported_intern.code = OMPI_ERR_NOT_SUPPORTED; ompi_err_not_supported_intern.mpi_code = MPI_ERR_INTERN; ompi_err_not_supported_intern.index = pos++; - strncpy(ompi_err_not_supported_intern.errstring, "OMPI_ERR_NOT_SUPPORTED", OMPI_MAX_ERROR_STRING); + opal_string_copy(ompi_err_not_supported_intern.errstring, + "OMPI_ERR_NOT_SUPPORTED", OMPI_MAX_ERROR_STRING); opal_pointer_array_set_item(&ompi_errcodes_intern, ompi_err_not_supported_intern.index, &ompi_err_not_supported_intern); @@ -151,7 +163,8 @@ int ompi_errcode_intern_init (void) ompi_err_interupted_intern.code = OMPI_ERR_INTERUPTED; ompi_err_interupted_intern.mpi_code = MPI_ERR_INTERN; ompi_err_interupted_intern.index = pos++; - strncpy(ompi_err_interupted_intern.errstring, "OMPI_ERR_INTERUPTED", OMPI_MAX_ERROR_STRING); + opal_string_copy(ompi_err_interupted_intern.errstring, + "OMPI_ERR_INTERUPTED", OMPI_MAX_ERROR_STRING); opal_pointer_array_set_item(&ompi_errcodes_intern, ompi_err_interupted_intern.index, &ompi_err_interupted_intern); @@ -159,7 +172,8 @@ int ompi_errcode_intern_init (void) ompi_err_would_block_intern.code = OMPI_ERR_WOULD_BLOCK; ompi_err_would_block_intern.mpi_code = MPI_ERR_INTERN; ompi_err_would_block_intern.index = pos++; - strncpy(ompi_err_would_block_intern.errstring, "OMPI_ERR_WOULD_BLOCK", OMPI_MAX_ERROR_STRING); + opal_string_copy(ompi_err_would_block_intern.errstring, + "OMPI_ERR_WOULD_BLOCK", OMPI_MAX_ERROR_STRING); opal_pointer_array_set_item(&ompi_errcodes_intern, ompi_err_would_block_intern.index, &ompi_err_would_block_intern); @@ -167,7 +181,8 @@ int ompi_errcode_intern_init (void) ompi_err_in_errno_intern.code = OMPI_ERR_IN_ERRNO; ompi_err_in_errno_intern.mpi_code = MPI_ERR_INTERN; ompi_err_in_errno_intern.index = pos++; - strncpy(ompi_err_in_errno_intern.errstring, "OMPI_ERR_IN_ERRNO", OMPI_MAX_ERROR_STRING); + opal_string_copy(ompi_err_in_errno_intern.errstring, + "OMPI_ERR_IN_ERRNO", OMPI_MAX_ERROR_STRING); opal_pointer_array_set_item(&ompi_errcodes_intern, ompi_err_in_errno_intern.index, &ompi_err_in_errno_intern); @@ -175,7 +190,8 @@ int ompi_errcode_intern_init (void) ompi_err_unreach_intern.code = OMPI_ERR_UNREACH; ompi_err_unreach_intern.mpi_code = MPI_ERR_INTERN; ompi_err_unreach_intern.index = pos++; - strncpy(ompi_err_unreach_intern.errstring, "OMPI_ERR_UNREACH", OMPI_MAX_ERROR_STRING); + opal_string_copy(ompi_err_unreach_intern.errstring, + "OMPI_ERR_UNREACH", OMPI_MAX_ERROR_STRING); opal_pointer_array_set_item(&ompi_errcodes_intern, ompi_err_unreach_intern.index, &ompi_err_unreach_intern); @@ -183,7 +199,8 @@ int ompi_errcode_intern_init (void) ompi_err_not_found_intern.code = OMPI_ERR_NOT_FOUND; ompi_err_not_found_intern.mpi_code = MPI_ERR_INTERN; ompi_err_not_found_intern.index = pos++; - strncpy(ompi_err_not_found_intern.errstring, "OMPI_ERR_NOT_FOUND", OMPI_MAX_ERROR_STRING); + opal_string_copy(ompi_err_not_found_intern.errstring, + "OMPI_ERR_NOT_FOUND", OMPI_MAX_ERROR_STRING); opal_pointer_array_set_item(&ompi_errcodes_intern, ompi_err_not_found_intern.index, &ompi_err_not_found_intern); @@ -191,7 +208,8 @@ int ompi_errcode_intern_init (void) ompi_err_buffer_intern.code = OMPI_ERR_BUFFER; ompi_err_buffer_intern.mpi_code = MPI_ERR_BUFFER; ompi_err_buffer_intern.index = pos++; - strncpy(ompi_err_buffer_intern.errstring, "OMPI_ERR_BUFFER", OMPI_MAX_ERROR_STRING); + opal_string_copy(ompi_err_buffer_intern.errstring, + "OMPI_ERR_BUFFER", OMPI_MAX_ERROR_STRING); opal_pointer_array_set_item(&ompi_errcodes_intern, ompi_err_buffer_intern.index, &ompi_err_buffer_intern); @@ -199,7 +217,8 @@ int ompi_errcode_intern_init (void) ompi_err_request_intern.code = OMPI_ERR_REQUEST; ompi_err_request_intern.mpi_code = MPI_ERR_REQUEST; ompi_err_request_intern.index = pos++; - strncpy(ompi_err_request_intern.errstring, "OMPI_ERR_REQUEST", OMPI_MAX_ERROR_STRING); + opal_string_copy(ompi_err_request_intern.errstring, + "OMPI_ERR_REQUEST", OMPI_MAX_ERROR_STRING); opal_pointer_array_set_item(&ompi_errcodes_intern, ompi_err_request_intern.index, &ompi_err_request_intern); @@ -207,7 +226,8 @@ int ompi_errcode_intern_init (void) ompi_err_rma_sync_intern.code = OMPI_ERR_RMA_SYNC; ompi_err_rma_sync_intern.mpi_code = MPI_ERR_RMA_SYNC; ompi_err_rma_sync_intern.index = pos++; - strncpy(ompi_err_rma_sync_intern.errstring, "OMPI_ERR_RMA_SYNC", OMPI_MAX_ERROR_STRING); + opal_string_copy(ompi_err_rma_sync_intern.errstring, + "OMPI_ERR_RMA_SYNC", OMPI_MAX_ERROR_STRING); opal_pointer_array_set_item(&ompi_errcodes_intern, ompi_err_rma_sync_intern.index, &ompi_err_rma_sync_intern); @@ -215,7 +235,8 @@ int ompi_errcode_intern_init (void) ompi_err_rma_shared_intern.code = OMPI_ERR_RMA_SHARED; ompi_err_rma_shared_intern.mpi_code = MPI_ERR_RMA_SHARED; ompi_err_rma_shared_intern.index = pos++; - strncpy(ompi_err_rma_shared_intern.errstring, "OMPI_ERR_RMA_SHARED", OMPI_MAX_ERROR_STRING); + opal_string_copy(ompi_err_rma_shared_intern.errstring, + "OMPI_ERR_RMA_SHARED", OMPI_MAX_ERROR_STRING); opal_pointer_array_set_item(&ompi_errcodes_intern, ompi_err_rma_shared_intern.index, &ompi_err_rma_shared_intern); @@ -223,7 +244,8 @@ int ompi_errcode_intern_init (void) ompi_err_rma_attach_intern.code = OMPI_ERR_RMA_ATTACH; ompi_err_rma_attach_intern.mpi_code = MPI_ERR_RMA_ATTACH; ompi_err_rma_attach_intern.index = pos++; - strncpy(ompi_err_rma_attach_intern.errstring, "OMPI_ERR_RMA_ATTACH", OMPI_MAX_ERROR_STRING); + opal_string_copy(ompi_err_rma_attach_intern.errstring, + "OMPI_ERR_RMA_ATTACH", OMPI_MAX_ERROR_STRING); opal_pointer_array_set_item(&ompi_errcodes_intern, ompi_err_rma_attach_intern.index, &ompi_err_rma_attach_intern); @@ -231,7 +253,8 @@ int ompi_errcode_intern_init (void) ompi_err_rma_range_intern.code = OMPI_ERR_RMA_RANGE; ompi_err_rma_range_intern.mpi_code = MPI_ERR_RMA_RANGE; ompi_err_rma_range_intern.index = pos++; - strncpy(ompi_err_rma_range_intern.errstring, "OMPI_ERR_RMA_RANGE", OMPI_MAX_ERROR_STRING); + opal_string_copy(ompi_err_rma_range_intern.errstring, + "OMPI_ERR_RMA_RANGE", OMPI_MAX_ERROR_STRING); opal_pointer_array_set_item(&ompi_errcodes_intern, ompi_err_rma_range_intern.index, &ompi_err_rma_range_intern); @@ -239,7 +262,8 @@ int ompi_errcode_intern_init (void) ompi_err_rma_conflict_intern.code = OMPI_ERR_RMA_CONFLICT; ompi_err_rma_conflict_intern.mpi_code = MPI_ERR_RMA_CONFLICT; ompi_err_rma_conflict_intern.index = pos++; - strncpy(ompi_err_rma_conflict_intern.errstring, "OMPI_ERR_RMA_CONFLICT", OMPI_MAX_ERROR_STRING); + opal_string_copy(ompi_err_rma_conflict_intern.errstring, + "OMPI_ERR_RMA_CONFLICT", OMPI_MAX_ERROR_STRING); opal_pointer_array_set_item(&ompi_errcodes_intern, ompi_err_rma_conflict_intern.index, &ompi_err_rma_conflict_intern); @@ -247,7 +271,8 @@ int ompi_errcode_intern_init (void) ompi_err_win_intern.code = OMPI_ERR_WIN; ompi_err_win_intern.mpi_code = MPI_ERR_WIN; ompi_err_win_intern.index = pos++; - strncpy(ompi_err_win_intern.errstring, "OMPI_ERR_WIN", OMPI_MAX_ERROR_STRING); + opal_string_copy(ompi_err_win_intern.errstring, + "OMPI_ERR_WIN", OMPI_MAX_ERROR_STRING); opal_pointer_array_set_item(&ompi_errcodes_intern, ompi_err_win_intern.index, &ompi_err_win_intern); @@ -255,7 +280,8 @@ int ompi_errcode_intern_init (void) ompi_err_rma_flavor_intern.code = OMPI_ERR_RMA_FLAVOR; ompi_err_rma_flavor_intern.mpi_code = MPI_ERR_RMA_FLAVOR; ompi_err_rma_flavor_intern.index = pos++; - strncpy(ompi_err_rma_flavor_intern.errstring, "OMPI_ERR_RMA_FLAVOR", OMPI_MAX_ERROR_STRING); + opal_string_copy(ompi_err_rma_flavor_intern.errstring, + "OMPI_ERR_RMA_FLAVOR", OMPI_MAX_ERROR_STRING); opal_pointer_array_set_item(&ompi_errcodes_intern, ompi_err_rma_flavor_intern.index, &ompi_err_rma_flavor_intern); diff --git a/ompi/errhandler/errcode.c b/ompi/errhandler/errcode.c index d807a1ae81..03225085b3 100644 --- a/ompi/errhandler/errcode.c +++ b/ompi/errhandler/errcode.c @@ -11,7 +11,7 @@ * Copyright (c) 2004-2005 The Regents of the University of California. * All rights reserved. * Copyright (c) 2006 University of Houston. All rights reserved. - * Copyright (c) 2013 Cisco Systems, Inc. All rights reserved. + * Copyright (c) 2013-2018 Cisco Systems, Inc. All rights reserved * Copyright (c) 2013 Los Alamos National Security, LLC. All rights * reserved. * Copyright (c) 2015 Research Organization for Information Science @@ -30,6 +30,8 @@ #include "mpi.h" +#include "opal/util/string_copy.h" + #include "ompi/errhandler/errcode.h" #include "ompi/constants.h" @@ -124,7 +126,7 @@ do { \ OBJ_CONSTRUCT(&(VAR), ompi_mpi_errcode_t); \ (VAR).code = (ERRCODE); \ (VAR).cls = (ERRCODE); \ - strncpy((VAR).errstring, (TXT), MPI_MAX_ERROR_STRING); \ + opal_string_copy((VAR).errstring, (TXT), MPI_MAX_ERROR_STRING); \ opal_pointer_array_set_item(&ompi_mpi_errcodes, (ERRCODE), &(VAR)); \ } while (0) @@ -353,7 +355,7 @@ int ompi_mpi_errnum_add_string(int errnum, const char *errstring, int len) len = MPI_MAX_ERROR_STRING; } - strncpy ( errcodep->errstring, errstring, len ); + opal_string_copy( errcodep->errstring, errstring, len ); return OMPI_SUCCESS; } diff --git a/ompi/errhandler/errhandler.c b/ompi/errhandler/errhandler.c index fac3e65e91..1a12cc54a3 100644 --- a/ompi/errhandler/errhandler.c +++ b/ompi/errhandler/errhandler.c @@ -10,7 +10,7 @@ * University of Stuttgart. All rights reserved. * Copyright (c) 2004-2005 The Regents of the University of California. * All rights reserved. - * Copyright (c) 2008-2012 Cisco Systems, Inc. All rights reserved. + * Copyright (c) 2008-2018 Cisco Systems, Inc. All rights reserved * Copyright (c) 2009 Sun Microsystems, Inc. All rights reserved. * Copyright (c) 2015 Research Organization for Information Science * and Technology (RIST). All rights reserved. @@ -32,6 +32,7 @@ #include "ompi/errhandler/errhandler_predefined.h" #include "opal/class/opal_pointer_array.h" #include "opal/mca/pmix/pmix.h" +#include "opal/util/string_copy.h" /* @@ -98,9 +99,8 @@ int ompi_errhandler_init(void) ompi_mpi_errhandler_null.eh.eh_file_fn = NULL; ompi_mpi_errhandler_null.eh.eh_win_fn = NULL ; ompi_mpi_errhandler_null.eh.eh_fort_fn = NULL; - strncpy (ompi_mpi_errhandler_null.eh.eh_name, "MPI_ERRHANDLER_NULL", - strlen("MPI_ERRHANDLER_NULL")+1 ); - + opal_string_copy(ompi_mpi_errhandler_null.eh.eh_name, "MPI_ERRHANDLER_NULL", + sizeof(ompi_mpi_errhandler_null.eh.eh_name)); OBJ_CONSTRUCT( &ompi_mpi_errors_are_fatal.eh, ompi_errhandler_t ); if( ompi_mpi_errors_are_fatal.eh.eh_f_to_c_index != OMPI_ERRORS_ARE_FATAL_FORTRAN ) @@ -111,8 +111,9 @@ int ompi_errhandler_init(void) ompi_mpi_errors_are_fatal.eh.eh_file_fn = ompi_mpi_errors_are_fatal_file_handler; ompi_mpi_errors_are_fatal.eh.eh_win_fn = ompi_mpi_errors_are_fatal_win_handler ; ompi_mpi_errors_are_fatal.eh.eh_fort_fn = NULL; - strncpy (ompi_mpi_errors_are_fatal.eh.eh_name, "MPI_ERRORS_ARE_FATAL", - strlen("MPI_ERRORS_ARE_FATAL")+1 ); + opal_string_copy(ompi_mpi_errors_are_fatal.eh.eh_name, + "MPI_ERRORS_ARE_FATAL", + sizeof(ompi_mpi_errors_are_fatal.eh.eh_name)); OBJ_CONSTRUCT( &ompi_mpi_errors_return.eh, ompi_errhandler_t ); if( ompi_mpi_errors_return.eh.eh_f_to_c_index != OMPI_ERRORS_RETURN_FORTRAN ) @@ -123,8 +124,8 @@ int ompi_errhandler_init(void) ompi_mpi_errors_return.eh.eh_file_fn = ompi_mpi_errors_return_file_handler; ompi_mpi_errors_return.eh.eh_win_fn = ompi_mpi_errors_return_win_handler; ompi_mpi_errors_return.eh.eh_fort_fn = NULL; - strncpy (ompi_mpi_errors_return.eh.eh_name, "MPI_ERRORS_RETURN", - strlen("MPI_ERRORS_RETURN")+1 ); + opal_string_copy(ompi_mpi_errors_return.eh.eh_name, "MPI_ERRORS_RETURN", + sizeof(ompi_mpi_errors_return.eh.eh_name)); /* If we're going to use C++, functions will be fixed up during MPI::Init. Note that it is proper to use ERRHANDLER_LANG_C here; @@ -142,8 +143,9 @@ int ompi_errhandler_init(void) ompi_mpi_errors_throw_exceptions.eh.eh_win_fn = ompi_mpi_errors_are_fatal_win_handler ; ompi_mpi_errors_throw_exceptions.eh.eh_fort_fn = NULL; - strncpy (ompi_mpi_errors_throw_exceptions.eh.eh_name, "MPI_ERRORS_THROW_EXCEPTIONS", - strlen("MPI_ERRORS_THROW_EXCEPTIONS")+1 ); + opal_string_copy(ompi_mpi_errors_throw_exceptions.eh.eh_name, + "MPI_ERRORS_THROW_EXCEPTIONS", + sizeof(ompi_mpi_errors_throw_exceptions.eh.eh_name)); /* All done */ diff --git a/ompi/info/info.c b/ompi/info/info.c index 03904b07b0..812279db74 100644 --- a/ompi/info/info.c +++ b/ompi/info/info.c @@ -10,7 +10,7 @@ * University of Stuttgart. All rights reserved. * Copyright (c) 2004-2005 The Regents of the University of California. * All rights reserved. - * Copyright (c) 2007-2015 Cisco Systems, Inc. All rights reserved. + * Copyright (c) 2007-2018 Cisco Systems, Inc. All rights reserved * Copyright (c) 2009 Sun Microsystems, Inc. All rights reserved. * Copyright (c) 2012-2015 Los Alamos National Security, LLC. All rights * reserved. @@ -43,7 +43,6 @@ #include "opal/util/argv.h" #include "opal/util/opal_getcwd.h" #include "opal/util/output.h" -#include "opal/util/strncpy.h" #include "opal/util/info.h" #include "ompi/info/info.h" diff --git a/ompi/mca/fs/base/fs_base_get_parent_dir.c b/ompi/mca/fs/base/fs_base_get_parent_dir.c index 07155efa69..9acaeb7579 100644 --- a/ompi/mca/fs/base/fs_base_get_parent_dir.c +++ b/ompi/mca/fs/base/fs_base_get_parent_dir.c @@ -12,6 +12,7 @@ * Copyright (c) 2008-2016 University of Houston. All rights reserved. * Copyright (c) 2015-2016 Research Organization for Information Science * and Technology (RIST). All rights reserved. + * Copyright (c) 2018 Cisco Systems, Inc. All rights reserved * $COPYRIGHT$ * * Additional copyrights may follow @@ -21,12 +22,15 @@ #include "ompi_config.h" + #include -#include "ompi/mca/mca.h" #include "opal/mca/base/base.h" #include "opal/util/path.h" +#include "opal/util/printf.h" +#include "opal/util/string_copy.h" +#include "ompi/mca/mca.h" #include "ompi/mca/fs/fs.h" #include "ompi/mca/fs/base/base.h" #include "ompi/mca/common/ompio/common_ompio.h" @@ -53,6 +57,11 @@ void mca_fs_base_get_parent_dir ( char *filename, char **dirnamep) char *dir = NULL, *slash; struct stat statbuf; + if (strlen(filename) < 1) { + opal_asprintf(dirnamep, ".%s", OPAL_PATH_SEP); + return; + } + err = lstat(filename, &statbuf); if (err || (!S_ISLNK(statbuf.st_mode))) { @@ -86,10 +95,16 @@ void mca_fs_base_get_parent_dir ( char *filename, char **dirnamep) } slash = strrchr(dir, '/'); - if (!slash) strncpy(dir, ".", 2); - else { - if (slash == dir) *(dir + 1) = '\0'; - else *slash = '\0'; + if (!slash) { + // It is guaranteed in this case that "dir" will be at least 2 + // characters long. + opal_string_copy(dir, ".", 2); + } else { + if (slash == dir) { + *(dir + 1) = '\0'; + } else { + *slash = '\0'; + } } *dirnamep = dir; diff --git a/ompi/mca/pml/v/pml_v_component.c b/ompi/mca/pml/v/pml_v_component.c index 0b5048d733..1095e4e7f1 100644 --- a/ompi/mca/pml/v/pml_v_component.c +++ b/ompi/mca/pml/v/pml_v_component.c @@ -21,6 +21,7 @@ #include "opal/mca/base/base.h" #include "opal/mca/base/mca_base_component_repository.h" #include "opal/util/printf.h" +#include "opal/util/string_copy.h" #include "ompi/constants.h" #include "ompi/mca/pml/base/base.h" #include "ompi/mca/vprotocol/vprotocol.h" @@ -146,10 +147,9 @@ static int mca_pml_v_component_close(void) opal_asprintf(&new_name, "%s]v%s", mca_pml_v.host_pml_component.pmlm_version.mca_component_name, mca_vprotocol_component.pmlm_version.mca_component_name); - size_t len = sizeof(mca_pml_base_selected_component.pmlm_version.mca_component_name); - strncpy(mca_pml_base_selected_component.pmlm_version.mca_component_name, - new_name, len - 1); - mca_pml_base_selected_component.pmlm_version.mca_component_name[len - 1] = '\0'; + opal_string_copy(mca_pml_base_selected_component.pmlm_version.mca_component_name, + new_name, + sizeof(mca_pml_base_selected_component.pmlm_version.mca_component_name)); free(new_name); /* Replace finalize */ diff --git a/ompi/mca/rte/pmix/rte_pmix_module.c b/ompi/mca/rte/pmix/rte_pmix_module.c index 9defda5da0..43a1074c9d 100644 --- a/ompi/mca/rte/pmix/rte_pmix_module.c +++ b/ompi/mca/rte/pmix/rte_pmix_module.c @@ -5,7 +5,7 @@ * Copyright (c) 2012-2014 The University of Tennessee and The University * of Tennessee Research Foundation. All rights * reserved. - * Copyright (c) 2014 Cisco Systems, Inc. All rights reserved. + * Copyright (c) 2014-2018 Cisco Systems, Inc. All rights reserved * Copyright (c) 2018 Amazon.com, Inc. or its affiliates. All Rights reserved. * $COPYRIGHT$ */ @@ -38,6 +38,7 @@ #include "opal/util/printf.h" #include "opal/util/proc.h" #include "opal/util/show_help.h" +#include "opal/util/string_copy.h" #include "opal/mca/hwloc/base/base.h" #include "opal/mca/pmix/base/base.h" #include "opal/threads/threads.h" @@ -430,7 +431,8 @@ static int ompi_pmix_snprintf_jobid(char *jobid_string, size_t size, const opal_ /* check for wildcard value - handle appropriately */ if (OPAL_JOBID_WILDCARD == jobid) { - (void)strncpy(jobid_string, OPAL_SCHEMA_WILDCARD_STRING, size); + (void)opal_string_copy(jobid_string, + OPAL_SCHEMA_WILDCARD_STRING, size); } else { rc = snprintf(jobid_string, size, "%ld", (long) jobid); if (0 > rc) { diff --git a/ompi/mpi/c/comm_get_name.c b/ompi/mpi/c/comm_get_name.c index 9225456445..c4904f13bf 100644 --- a/ompi/mpi/c/comm_get_name.c +++ b/ompi/mpi/c/comm_get_name.c @@ -9,7 +9,7 @@ * University of Stuttgart. All rights reserved. * Copyright (c) 2004-2005 The Regents of the University of California. * All rights reserved. - * Copyright (c) 2006-2008 Cisco Systems, Inc. All rights reserved. + * Copyright (c) 2006-2018 Cisco Systems, Inc. All rights reserved * Copyright (c) 2015-2017 Research Organization for Information Science * and Technology (RIST). All rights reserved. * $COPYRIGHT$ @@ -23,12 +23,14 @@ #include +#include "opal/threads/mutex.h" +#include "opal/util/string_copy.h" + #include "ompi/mpi/c/bindings.h" #include "ompi/runtime/params.h" #include "ompi/communicator/communicator.h" #include "ompi/errhandler/errhandler.h" #include "ompi/totalview.h" -#include "opal/threads/mutex.h" #include "ompi/memchecker.h" #if OMPI_BUILD_MPI_PROFILING @@ -72,7 +74,7 @@ int MPI_Comm_get_name(MPI_Comm comm, char *name, int *length) able to completely fit into MPI_MAX_OBJECT_NAME bytes (i.e., name+\0). */ if ( comm->c_flags & OMPI_COMM_NAMEISSET ) { - strncpy(name, comm->c_name, MPI_MAX_OBJECT_NAME); + opal_string_copy(name, comm->c_name, MPI_MAX_OBJECT_NAME); *length = (int) strlen(comm->c_name); } else { name[0] = '\0'; diff --git a/ompi/mpi/c/error_string.c b/ompi/mpi/c/error_string.c index 4a8b056e22..e2589e4adb 100644 --- a/ompi/mpi/c/error_string.c +++ b/ompi/mpi/c/error_string.c @@ -12,6 +12,7 @@ * Copyright (c) 2006 University of Houston. All rights reserved. * Copyright (c) 2015 Research Organization for Information Science * and Technology (RIST). All rights reserved. + * Copyright (c) 2018 Cisco Systems, Inc. All rights reserved * $COPYRIGHT$ * * Additional copyrights may follow @@ -21,6 +22,8 @@ #include "ompi_config.h" #include +#include "opal/util/string_copy.h" + #include "ompi/mpi/c/bindings.h" #include "ompi/runtime/params.h" #include "ompi/communicator/communicator.h" @@ -53,7 +56,7 @@ int MPI_Error_string(int errorcode, char *string, int *resultlen) } tmpstring = ompi_mpi_errnum_get_string (errorcode); - strncpy(string, tmpstring, MPI_MAX_ERROR_STRING); + opal_string_copy(string, tmpstring, MPI_MAX_ERROR_STRING); *resultlen = (int)strlen(string); return MPI_SUCCESS; diff --git a/ompi/mpi/c/lookup_name.c b/ompi/mpi/c/lookup_name.c index 42a71c367c..8f6cdf9351 100644 --- a/ompi/mpi/c/lookup_name.c +++ b/ompi/mpi/c/lookup_name.c @@ -15,7 +15,7 @@ * Copyright (c) 2015-2016 Intel, Inc. All rights reserved. * Copyright (c) 2015 Research Organization for Information Science * and Technology (RIST). All rights reserved. - * Copyright (c) 2015 Cisco Systems, Inc. All rights reserved. + * Copyright (c) 2015-2018 Cisco Systems, Inc. All rights reserved * $COPYRIGHT$ * * Additional copyrights may follow @@ -28,6 +28,7 @@ #include "opal/class/opal_list.h" #include "opal/mca/pmix/pmix.h" #include "opal/util/show_help.h" +#include "opal/util/string_copy.h" #include "ompi/mpi/c/bindings.h" #include "ompi/runtime/params.h" @@ -138,7 +139,8 @@ int MPI_Lookup_name(const char *service_name, MPI_Info info, char *port_name) return OMPI_ERRHANDLER_INVOKE(MPI_COMM_WORLD, ret, FUNC_NAME); } - strncpy ( port_name, pdat->value.data.string, MPI_MAX_PORT_NAME ); + opal_string_copy( port_name, pdat->value.data.string, + MPI_MAX_PORT_NAME ); OPAL_LIST_DESTRUCT(&results); OPAL_CR_EXIT_LIBRARY(); diff --git a/ompi/mpi/c/type_create_f90_complex.c b/ompi/mpi/c/type_create_f90_complex.c index 687b954515..3016b3509c 100644 --- a/ompi/mpi/c/type_create_f90_complex.c +++ b/ompi/mpi/c/type_create_f90_complex.c @@ -27,6 +27,8 @@ #include "ompi_config.h" +#include + #include "opal/util/printf.h" #include "ompi/mpi/c/bindings.h" @@ -34,8 +36,6 @@ #include "ompi/communicator/communicator.h" #include "ompi/errhandler/errhandler.h" -#include - #if OMPI_BUILD_MPI_PROFILING #if OPAL_HAVE_WEAK_SYMBOLS #pragma weak MPI_Type_create_f90_complex = PMPI_Type_create_f90_complex @@ -110,12 +110,12 @@ int MPI_Type_create_f90_complex(int p, int r, MPI_Datatype *newtype) */ datatype->super.flags |= OMPI_DATATYPE_FLAG_PREDEFINED; /* Mark the datatype as a special F90 convenience type */ - char *new_name; - opal_asprintf(&new_name, "COMBINER %s", (*newtype)->name); - size_t max_len = MPI_MAX_OBJECT_NAME; - strncpy(datatype->name, new_name, max_len - 1); - datatype->name[max_len - 1] = '\0'; - free(new_name); + // Specifically using opal_snprintf() here (instead of + // snprintf()) so that over-eager compilers do not warn us + // that we may be truncating the output. We *know* that the + // output may be truncated, and that's ok. + opal_snprintf(datatype->name, sizeof(datatype->name), + "COMBINER %s", (*newtype)->name); a_i[0] = &p; a_i[1] = &r; diff --git a/ompi/mpi/c/type_create_f90_integer.c b/ompi/mpi/c/type_create_f90_integer.c index 79fff68cd6..11998eb7e8 100644 --- a/ompi/mpi/c/type_create_f90_integer.c +++ b/ompi/mpi/c/type_create_f90_integer.c @@ -106,12 +106,12 @@ int MPI_Type_create_f90_integer(int r, MPI_Datatype *newtype) */ datatype->super.flags |= OMPI_DATATYPE_FLAG_PREDEFINED; /* Mark the datatype as a special F90 convenience type */ - char *new_name; - opal_asprintf(&new_name, "COMBINER %s", (*newtype)->name); - size_t max_len = MPI_MAX_OBJECT_NAME; - strncpy(datatype->name, new_name, max_len - 1); - datatype->name[max_len - 1] = '\0'; - free(new_name); + // Specifically using opal_snprintf() here (instead of + // snprintf()) so that over-eager compilers do not warn us + // that we may be truncating the output. We *know* that the + // output may be truncated, and that's ok. + opal_snprintf(datatype->name, sizeof(datatype->name), + "COMBINER %s", (*newtype)->name); a_i[0] = &r; ompi_datatype_set_args( datatype, 1, a_i, 0, NULL, 0, NULL, MPI_COMBINER_F90_INTEGER ); diff --git a/ompi/mpi/c/type_create_f90_real.c b/ompi/mpi/c/type_create_f90_real.c index 43b279a356..0f2b8ace10 100644 --- a/ompi/mpi/c/type_create_f90_real.c +++ b/ompi/mpi/c/type_create_f90_real.c @@ -27,6 +27,8 @@ #include "ompi_config.h" +#include + #include "opal/util/printf.h" #include "ompi/mpi/c/bindings.h" @@ -34,8 +36,6 @@ #include "ompi/communicator/communicator.h" #include "ompi/errhandler/errhandler.h" -#include - #if OMPI_BUILD_MPI_PROFILING #if OPAL_HAVE_WEAK_SYMBOLS #pragma weak MPI_Type_create_f90_real = PMPI_Type_create_f90_real @@ -110,12 +110,12 @@ int MPI_Type_create_f90_real(int p, int r, MPI_Datatype *newtype) */ datatype->super.flags |= OMPI_DATATYPE_FLAG_PREDEFINED; /* Mark the datatype as a special F90 convenience type */ - char *new_name; - opal_asprintf(&new_name, "COMBINER %s", (*newtype)->name); - size_t max_len = MPI_MAX_OBJECT_NAME; - strncpy(datatype->name, new_name, max_len - 1); - datatype->name[max_len - 1] = '\0'; - free(new_name); + // Specifically using opal_snprintf() here (instead of + // snprintf()) so that over-eager compilers do not warn us + // that we may be truncating the output. We *know* that the + // output may be truncated, and it's ok. + opal_snprintf(datatype->name, sizeof(datatype->name), + "COMBINER %s", (*newtype)->name); ompi_datatype_set_args( datatype, 2, a_i, 0, NULL, 0, NULL, MPI_COMBINER_F90_REAL ); diff --git a/ompi/mpi/c/type_get_name.c b/ompi/mpi/c/type_get_name.c index b7c9d4a8b0..387a21e1b2 100644 --- a/ompi/mpi/c/type_get_name.c +++ b/ompi/mpi/c/type_get_name.c @@ -9,7 +9,7 @@ * University of Stuttgart. All rights reserved. * Copyright (c) 2004-2005 The Regents of the University of California. * All rights reserved. - * Copyright (c) 2008 Cisco Systems, Inc. All rights reserved. + * Copyright (c) 2008-2018 Cisco Systems, Inc. All rights reserved * Copyright (c) 2015 Research Organization for Information Science * and Technology (RIST). All rights reserved. * $COPYRIGHT$ @@ -23,6 +23,8 @@ #include +#include "opal/util/string_copy.h" + #include "ompi/mpi/c/bindings.h" #include "ompi/runtime/params.h" #include "ompi/communicator/communicator.h" @@ -71,6 +73,6 @@ int MPI_Type_get_name(MPI_Datatype type, char *type_name, int *resultlen) able to completely fit into MPI_MAX_OBJECT_NAME bytes (i.e., name+\0). */ *resultlen = (int)strlen(type->name); - strncpy(type_name, type->name, MPI_MAX_OBJECT_NAME); + opal_string_copy(type_name, type->name, MPI_MAX_OBJECT_NAME); return MPI_SUCCESS; } diff --git a/ompi/mpi/c/type_set_name.c b/ompi/mpi/c/type_set_name.c index f043082663..ed5f06d403 100644 --- a/ompi/mpi/c/type_set_name.c +++ b/ompi/mpi/c/type_set_name.c @@ -14,6 +14,7 @@ * reserved. * Copyright (c) 2015 Research Organization for Information Science * and Technology (RIST). All rights reserved. + * Copyright (c) 2018 Cisco Systems, Inc. All rights reserved * $COPYRIGHT$ * * Additional copyrights may follow @@ -25,6 +26,8 @@ #include +#include "opal/util/string_copy.h" + #include "ompi/mpi/c/bindings.h" #include "ompi/runtime/params.h" #include "ompi/communicator/communicator.h" @@ -66,6 +69,6 @@ int MPI_Type_set_name (MPI_Datatype type, const char *type_name) if( length >= MPI_MAX_OBJECT_NAME ) { length = MPI_MAX_OBJECT_NAME - 1; } - strncpy( type->name, type_name, length ); + opal_string_copy( type->name, type_name, length ); return MPI_SUCCESS; } diff --git a/ompi/mpi/fortran/base/strings.c b/ompi/mpi/fortran/base/strings.c index c8996afba6..95645a1d90 100644 --- a/ompi/mpi/fortran/base/strings.c +++ b/ompi/mpi/fortran/base/strings.c @@ -25,8 +25,10 @@ #include #include -#include "ompi/constants.h" #include "opal/util/argv.h" +#include "opal/util/string_copy.h" + +#include "ompi/constants.h" #include "ompi/mpi/fortran/base/fortran_base_strings.h" @@ -65,9 +67,10 @@ int ompi_fortran_string_f2c(char *fstr, int len, char **cstr) /* Copy F77 string into C string and NULL terminate it. */ if (len > 0) { - strncpy(*cstr, fstr, len); + opal_string_copy(*cstr, fstr, len + 1); + } else { + (*cstr)[0] = '\0'; } - (*cstr)[len] = '\0'; return OMPI_SUCCESS; } @@ -91,7 +94,7 @@ int ompi_fortran_string_c2f(char *cstr, char *fstr, int len) { int i; - strncpy(fstr, cstr, len); + opal_string_copy(fstr, cstr, len); for (i = strlen(cstr); i < len; ++i) { fstr[i] = ' '; } diff --git a/ompi/mpi/tool/mpit-internal.h b/ompi/mpi/tool/mpit-internal.h index fb6c6b6868..9dd5d260ea 100644 --- a/ompi/mpi/tool/mpit-internal.h +++ b/ompi/mpi/tool/mpit-internal.h @@ -4,6 +4,7 @@ * reserved. * Copyright (c) 2011 UT-Battelle, LLC. All rights reserved. * Copyright (c) 2017 IBM Corporation. All rights reserved. + * Copyright (c) 2018 Cisco Systems, Inc. All rights reserved * $COPYRIGHT$ * * Additional copyrights may follow @@ -14,9 +15,11 @@ #if !defined(MPIT_INTERNAL_H) #define MPIT_INTERNAL_H -#include "ompi/include/ompi_config.h" +#include "opal/util/string_copy.h" #include "opal/mca/base/mca_base_var.h" #include "opal/mca/base/mca_base_pvar.h" + +#include "ompi/include/ompi_config.h" #include "ompi/runtime/params.h" #include "ompi/communicator/communicator.h" #include "ompi/constants.h" @@ -64,8 +67,7 @@ static inline void mpit_copy_string (char *dest, int *len, const char *source) *len = strlen (source) + 1; } - strncpy (dest, source, *len); - dest[*len - 1] = '\0'; + opal_string_copy (dest, source, *len); } else { *len = strlen (source) + 1; } diff --git a/ompi/mpiext/affinity/c/mpiext_affinity_str.c b/ompi/mpiext/affinity/c/mpiext_affinity_str.c index 6ccfa551a4..e6e20419b4 100644 --- a/ompi/mpiext/affinity/c/mpiext_affinity_str.c +++ b/ompi/mpiext/affinity/c/mpiext_affinity_str.c @@ -2,7 +2,7 @@ * Copyright (c) 2004-2009 The Trustees of Indiana University and Indiana * University Research and Technology * Corporation. All rights reserved. - * Copyright (c) 2010-2015 Cisco Systems, Inc. All rights reserved. + * Copyright (c) 2010-2018 Cisco Systems, Inc. All rights reserved * Copyright (c) 2010 Oracle and/or its affiliates. All rights reserved. * Copyright (c) 2012 Los Alamos National Security, LLC. All rights * reserved. @@ -31,6 +31,7 @@ #include "opal/mca/hwloc/base/base.h" #include "opal/runtime/opal.h" +#include "opal/util/string_copy.h" #include "ompi/communicator/communicator.h" #include "ompi/errhandler/errhandler.h" @@ -101,7 +102,7 @@ static int get_rsrc_ompi_bound(char str[OMPI_AFFINITY_STRING_MAX]) /* If OMPI did not bind, indicate that */ if (!ompi_rte_proc_is_bound) { - strncpy(str, ompi_nobind_str, OMPI_AFFINITY_STRING_MAX - 1); + opal_string_copy(str, ompi_nobind_str, OMPI_AFFINITY_STRING_MAX); return OMPI_SUCCESS; } @@ -113,7 +114,7 @@ static int get_rsrc_ompi_bound(char str[OMPI_AFFINITY_STRING_MAX]) ompi_proc_applied_binding); } if (OPAL_ERR_NOT_BOUND == ret) { - strncpy(str, not_bound_str, OMPI_AFFINITY_STRING_MAX - 1); + opal_string_copy(str, not_bound_str, OMPI_AFFINITY_STRING_MAX); ret = OMPI_SUCCESS; } return ret; @@ -163,7 +164,7 @@ static int get_rsrc_current_binding(char str[OMPI_AFFINITY_STRING_MAX]) opal_hwloc_topology, boundset); if (OPAL_ERR_NOT_BOUND == ret) { - strncpy(str, not_bound_str, OMPI_AFFINITY_STRING_MAX - 1); + opal_string_copy(str, not_bound_str, OMPI_AFFINITY_STRING_MAX); ret = OMPI_SUCCESS; } } @@ -293,7 +294,7 @@ static int get_layout_ompi_bound(char str[OMPI_AFFINITY_STRING_MAX]) /* If OMPI did not bind, indicate that */ if (!ompi_rte_proc_is_bound) { - strncpy(str, ompi_nobind_str, OMPI_AFFINITY_STRING_MAX - 1); + opal_string_copy(str, ompi_nobind_str, OMPI_AFFINITY_STRING_MAX); return OMPI_SUCCESS; } @@ -306,7 +307,7 @@ static int get_layout_ompi_bound(char str[OMPI_AFFINITY_STRING_MAX]) ompi_proc_applied_binding); } if (OPAL_ERR_NOT_BOUND == ret) { - strncpy(str, not_bound_str, OMPI_AFFINITY_STRING_MAX - 1); + opal_string_copy(str, not_bound_str, OMPI_AFFINITY_STRING_MAX); ret = OMPI_SUCCESS; } @@ -356,7 +357,7 @@ static int get_layout_current_binding(char str[OMPI_AFFINITY_STRING_MAX]) opal_hwloc_topology, boundset); if (OPAL_ERR_NOT_BOUND == ret) { - strncpy(str, not_bound_str, OMPI_AFFINITY_STRING_MAX - 1); + opal_string_copy(str, not_bound_str, OMPI_AFFINITY_STRING_MAX); ret = OMPI_SUCCESS; } } diff --git a/ompi/mpiext/cr/c/migrate.c b/ompi/mpiext/cr/c/migrate.c index 00c014e142..2ac1c28cf6 100644 --- a/ompi/mpiext/cr/c/migrate.c +++ b/ompi/mpiext/cr/c/migrate.c @@ -2,7 +2,7 @@ * Copyright (c) 2004-2010 The Trustees of Indiana University and Indiana * University Research and Technology * Corporation. All rights reserved. - * Copyright (c) 2012 Cisco Systems, Inc. All rights reserved. + * Copyright (c) 2012-2018 Cisco Systems, Inc. All rights reserved * $COPYRIGHT$ * * Additional copyrights may follow @@ -12,6 +12,8 @@ #include "ompi_config.h" #include +#include "opal/util/string_copy.h" + #include "ompi/mpi/c/bindings.h" #include "ompi/info/info.h" #include "ompi/runtime/params.h" @@ -59,8 +61,7 @@ int OMPI_CR_Migrate(MPI_Comm comm, char *hostname, int rank, MPI_Info *info) if( NULL == hostname ) { loc_hostname[0] = '\0'; } else { - strncpy(loc_hostname, hostname, strlen(hostname)); - loc_hostname[strlen(hostname)] = '\0'; + opal_string_copy(loc_hostname, hostname, sizeof(loc_hostname)); } my_vpid = (int) OMPI_PROC_MY_NAME->vpid; diff --git a/ompi/op/op.c b/ompi/op/op.c index f1f4bf6f26..b9559ceb55 100644 --- a/ompi/op/op.c +++ b/ompi/op/op.c @@ -10,7 +10,7 @@ * University of Stuttgart. All rights reserved. * Copyright (c) 2004-2005 The Regents of the University of California. * All rights reserved. - * Copyright (c) 2007-2012 Cisco Systems, Inc. All rights reserved. + * Copyright (c) 2007-2018 Cisco Systems, Inc. All rights reserved * Copyright (c) 2009 Sun Microsystems, Inc. All rights reserved. * Copyright (c) 2013 Los Alamos National Security, LLC. All rights * reserved. @@ -26,6 +26,7 @@ #include "ompi_config.h" #include "opal/class/opal_pointer_array.h" +#include "opal/util/string_copy.h" #include "ompi/constants.h" #include "ompi/op/op.h" @@ -356,7 +357,7 @@ ompi_op_t *ompi_op_create_user(bool commute, new_op->o_flags |= OMPI_OP_FLAGS_COMMUTE; } - strncpy(new_op->o_name, "USER OP", sizeof(new_op->o_name) - 1); + opal_string_copy(new_op->o_name, "USER OP", sizeof(new_op->o_name)); new_op->o_name[sizeof(new_op->o_name) - 1] = '\0'; /* Set the user-defined callback function. The "fort_fn" member @@ -423,7 +424,7 @@ static int add_intrinsic(ompi_op_t *op, int fort_handle, int flags, /* Set the members */ op->o_flags = flags; - strncpy(op->o_name, name, sizeof(op->o_name) - 1); + opal_string_copy(op->o_name, name, sizeof(op->o_name)); op->o_name[sizeof(op->o_name) - 1] = '\0'; /* Perform the selection on this op to fill in the function diff --git a/ompi/win/win.c b/ompi/win/win.c index bd388f967e..b4bc150d89 100644 --- a/ompi/win/win.c +++ b/ompi/win/win.c @@ -11,7 +11,7 @@ * Copyright (c) 2004-2005 The Regents of the University of California. * All rights reserved. * Copyright (c) 2009 Sun Microsystems, Inc. All rights reserved. - * Copyright (c) 2009-2012 Cisco Systems, Inc. All rights reserved. + * Copyright (c) 2009-2018 Cisco Systems, Inc. All rights reserved * Copyright (c) 2013-2015 Los Alamos National Security, LLC. All rights * reserved. * Copyright (c) 2015-2017 Research Organization for Information Science @@ -27,6 +27,7 @@ #include "ompi_config.h" #include "opal/util/info_subscriber.h" +#include "opal/util/string_copy.h" #include "mpi.h" #include "ompi/win/win.h" @@ -383,9 +384,7 @@ int ompi_win_set_name(ompi_win_t *win, const char *win_name) { OPAL_THREAD_LOCK(&(win->w_lock)); - memset(win->w_name, 0, MPI_MAX_OBJECT_NAME); - strncpy(win->w_name, win_name, MPI_MAX_OBJECT_NAME); - win->w_name[MPI_MAX_OBJECT_NAME - 1] = 0; + opal_string_copy(win->w_name, win_name, MPI_MAX_OBJECT_NAME); OPAL_THREAD_UNLOCK(&(win->w_lock)); return OMPI_SUCCESS; @@ -396,7 +395,7 @@ int ompi_win_get_name(ompi_win_t *win, char *win_name, int *length) { OPAL_THREAD_LOCK(&(win->w_lock)); - strncpy(win_name, win->w_name, MPI_MAX_OBJECT_NAME); + opal_string_copy(win_name, win->w_name, MPI_MAX_OBJECT_NAME); *length = (int)strlen(win->w_name); OPAL_THREAD_UNLOCK(&(win->w_lock));