diff --git a/opal/include/opal_config_bottom.h b/opal/include/opal_config_bottom.h index f906278b83..2fed0820ea 100644 --- a/opal/include/opal_config_bottom.h +++ b/opal/include/opal_config_bottom.h @@ -285,6 +285,14 @@ typedef OPAL_PTRDIFF_TYPE ptrdiff_t; #define OPAL_PATH_SEP "/" #define OPAL_ENV_SEP ':' +#if defined(MAXHOSTNAMELEN) +#define OPAL_MAXHOSTNAMELEN (MAXHOSTNAMELEN + 1) +#elif defined(HOST_NAME_MAX) +#define OPAL_MAXHOSTNAMELEN (HOST_NAME_MAX + 1) +#else +/* SUSv2 guarantees that "Host names are limited to 255 bytes". */ +#define OPAL_MAXHOSTNAMELEN (255 + 1) +#endif /* * Do we want memory debugging? @@ -489,14 +497,9 @@ static inline uint16_t ntohs(uint16_t netvar) { return netvar; } #ifdef HAVE_HOSTLIB_H /* gethostname() */ #include - -#ifndef MAXHOSTNAMELEN -#define MAXHOSTNAMELEN 64 #endif #endif -#endif - /* If we're in C++, then just undefine restrict and then define it to nothing. "restrict" is not part of the C++ language, and we don't have a corresponding AC_CXX_RESTRICT to figure out what the C++ diff --git a/opal/mca/base/mca_base_component_find.c b/opal/mca/base/mca_base_component_find.c index 7739399c5b..899673dfaf 100644 --- a/opal/mca/base/mca_base_component_find.c +++ b/opal/mca/base/mca_base_component_find.c @@ -330,7 +330,7 @@ static int component_find_check (mca_base_framework_t *framework, char **request } if (!found) { - char h[MAXHOSTNAMELEN]; + char h[OPAL_MAXHOSTNAMELEN]; gethostname(h, sizeof(h)); opal_show_help("help-mca-base.txt", "find-available:not-valid", true, diff --git a/opal/mca/base/mca_base_open.c b/opal/mca/base/mca_base_open.c index aeb01e4afe..7f14ffaf02 100644 --- a/opal/mca/base/mca_base_open.c +++ b/opal/mca/base/mca_base_open.c @@ -66,7 +66,7 @@ int mca_base_open(void) { char *value; opal_output_stream_t lds; - char hostname[64]; + char hostname[OPAL_MAXHOSTNAMELEN]; int var_id; if (mca_base_opened++) { @@ -137,7 +137,7 @@ int mca_base_open(void) } else { set_defaults(&lds); } - gethostname(hostname, 64); + gethostname(hostname, sizeof(hostname)); asprintf(&lds.lds_prefix, "[%s:%05d] ", hostname, getpid()); opal_output_reopen(0, &lds); opal_output_verbose (MCA_BASE_VERBOSE_COMPONENT, 0, "mca: base: opening components"); diff --git a/opal/mca/event/libevent2022/libevent/evdns.c b/opal/mca/event/libevent2022/libevent/evdns.c index 60b1048511..f55a50a0bc 100644 --- a/opal/mca/event/libevent2022/libevent/evdns.c +++ b/opal/mca/event/libevent2022/libevent/evdns.c @@ -48,6 +48,8 @@ * Version: 0.1b */ +#include "opal_config.h" + #include #include "event2/event-config.h" @@ -121,10 +123,6 @@ #define EVDNS_LOG_WARN 1 #define EVDNS_LOG_MSG 2 -#ifndef HOST_NAME_MAX -#define HOST_NAME_MAX 255 -#endif - #include #undef MIN @@ -3108,7 +3106,7 @@ evdns_search_ndots_set(const int ndots) { static void search_set_from_hostname(struct evdns_base *base) { - char hostname[HOST_NAME_MAX + 1], *domainname; + char hostname[OPAL_MAXHOSTNAMELEN], *domainname; ASSERT_LOCKED(base); search_postfix_clear(base); diff --git a/opal/mca/hwloc/base/hwloc_base_util.c b/opal/mca/hwloc/base/hwloc_base_util.c index 50e389025f..a9c487c514 100644 --- a/opal/mca/hwloc/base/hwloc_base_util.c +++ b/opal/mca/hwloc/base/hwloc_base_util.c @@ -399,7 +399,7 @@ int opal_hwloc_base_report_bind_failure(const char *file, if (!already_reported && OPAL_HWLOC_BASE_MBFA_SILENT != opal_hwloc_base_mbfa) { - char hostname[64]; + char hostname[OPAL_MAXHOSTNAMELEN]; gethostname(hostname, sizeof(hostname)); opal_show_help("help-opal-hwloc-base.txt", "mbind failure", true, diff --git a/opal/mca/pmix/pmix114/pmix/examples/dynamic.c b/opal/mca/pmix/pmix114/pmix/examples/dynamic.c index 11f3712146..7e9fbd352e 100644 --- a/opal/mca/pmix/pmix114/pmix/examples/dynamic.c +++ b/opal/mca/pmix/pmix114/pmix/examples/dynamic.c @@ -45,12 +45,12 @@ int main(int argc, char **argv) uint32_t nprocs; char nsp2[PMIX_MAX_NSLEN+1]; pmix_app_t *app; - char hostname[1024], dir[1024]; + char hostname[PMIX_MAXHOSTNAMELEN], dir[1024]; pmix_proc_t *peers; size_t npeers, ntmp=0; char *nodelist; - gethostname(hostname, 1024); + gethostname(hostname, sizeof(hostname)); getcwd(dir, 1024); /* init us */ diff --git a/opal/mca/pmix/pmix114/pmix/include/pmix/autogen/pmix_config_bottom.h b/opal/mca/pmix/pmix114/pmix/include/pmix/autogen/pmix_config_bottom.h index fc7b6a25d5..82d62f76d6 100644 --- a/opal/mca/pmix/pmix114/pmix/include/pmix/autogen/pmix_config_bottom.h +++ b/opal/mca/pmix/pmix114/pmix/include/pmix/autogen/pmix_config_bottom.h @@ -258,6 +258,15 @@ typedef PMIX_PTRDIFF_TYPE ptrdiff_t; #define PMIX_PATH_MAX 256 #endif +#if defined(MAXHOSTNAMELEN) +#define PMIX_MAXHOSTNAMELEN (MAXHOSTNAMELEN + 1) +#elif defined(HOST_NAME_MAX) +#define PMIX_MAXHOSTNAMELEN (HOST_NAME_MAX + 1) +#else +/* SUSv2 guarantees that "Host names are limited to 255 bytes". */ +#define PMIX_MAXHOSTNAMELEN (255 + 1) +#endif + /* * Set the compile-time path-separator on this system and variable separator */ @@ -387,14 +396,9 @@ typedef PMIX_PTRDIFF_TYPE ptrdiff_t; #ifdef HAVE_HOSTLIB_H /* gethostname() */ #include - -#ifndef MAXHOSTNAMELEN -#define MAXHOSTNAMELEN 64 #endif #endif -#endif - /* If we're in C++, then just undefine restrict and then define it to nothing. "restrict" is not part of the C++ language, and we don't have a corresponding AC_CXX_RESTRICT to figure out what the C++ diff --git a/opal/mca/pmix/pmix114/pmix/src/util/output.c b/opal/mca/pmix/pmix114/pmix/src/util/output.c index 5d406e0aad..424b6814f2 100644 --- a/opal/mca/pmix/pmix114/pmix/src/util/output.c +++ b/opal/mca/pmix/pmix114/pmix/src/util/output.c @@ -125,7 +125,7 @@ PMIX_CLASS_INSTANCE(pmix_output_stream_t, pmix_object_t, construct, NULL); bool pmix_output_init(void) { int i; - char hostname[32]; + char hostname[PMIX_MAXHOSTNAMELEN]; char *str; if (initialized) { @@ -250,7 +250,7 @@ bool pmix_output_switch(int output_id, bool enable) void pmix_output_reopen_all(void) { char *str; - char hostname[32]; + char hostname[PMIX_MAXHOSTNAMELEN]; str = getenv("PMIX_OUTPUT_STDERR_FD"); if (NULL != str) { diff --git a/opal/mca/pmix/pmix114/pmix/test/simple/simpdyn.c b/opal/mca/pmix/pmix114/pmix/test/simple/simpdyn.c index f5c0b94a51..06c955486f 100644 --- a/opal/mca/pmix/pmix114/pmix/test/simple/simpdyn.c +++ b/opal/mca/pmix/pmix114/pmix/test/simple/simpdyn.c @@ -49,12 +49,12 @@ int main(int argc, char **argv) uint32_t nprocs; char nsp2[PMIX_MAX_NSLEN+1]; pmix_app_t *app; - char hostname[1024]; + char hostname[PMIX_MAXHOSTNAMELEN]; pmix_proc_t *peers; size_t npeers, ntmp=0; char *nodelist; - gethostname(hostname, 1024); + gethostname(hostname, sizeof(hostname)); /* init us */ if (PMIX_SUCCESS != (rc = PMIx_Init(&myproc))) { diff --git a/opal/mca/pmix/pmix114/pmix/test/simple/simptest.c b/opal/mca/pmix/pmix114/pmix/test/simple/simptest.c index c0bf43fcc8..87f23d6d58 100644 --- a/opal/mca/pmix/pmix114/pmix/test/simple/simptest.c +++ b/opal/mca/pmix/pmix114/pmix/test/simple/simptest.c @@ -316,9 +316,9 @@ static void set_namespace(int nprocs, char *ranks, char *nspace, pmix_op_cbfunc_t cbfunc, myxfer_t *x) { char *regex, *ppn; - char hostname[1024]; + char hostname[PMIX_MAXHOSTNAMELEN]; - gethostname(hostname, 1024); + gethostname(hostname, sizeof(hostname)); x->ninfo = 6; PMIX_INFO_CREATE(x->info, x->ninfo); diff --git a/opal/mca/pstat/test/pstat_test.c b/opal/mca/pstat/test/pstat_test.c index 22da5f058c..6d616afe2a 100644 --- a/opal/mca/pstat/test/pstat_test.c +++ b/opal/mca/pstat/test/pstat_test.c @@ -65,7 +65,7 @@ static int query(pid_t pid, opal_node_stats_t *nstats) { double dtime; - char hostname[128]; + char hostname[OPAL_MAXHOSTNAMELEN]; if (NULL != stats) { /* record the time of this sample */ @@ -83,7 +83,7 @@ static int query(pid_t pid, } if (NULL != stats) { - gethostname(hostname, 128); + gethostname(hostname, sizeof(hostname)); strncpy(stats->node, hostname, OPAL_PSTAT_MAX_STRING_LEN); stats->pid = pid; diff --git a/opal/mca/shmem/mmap/shmem_mmap_module.c b/opal/mca/shmem/mmap/shmem_mmap_module.c index 619a54adcc..d567c485b5 100644 --- a/opal/mca/shmem/mmap/shmem_mmap_module.c +++ b/opal/mca/shmem/mmap/shmem_mmap_module.c @@ -365,9 +365,8 @@ segment_create(opal_shmem_ds_t *ds_buf, * a network filesystem, the user may see a shared memory performance hit. */ if (opal_shmem_mmap_nfs_warning && opal_path_nfs(real_file_name, NULL)) { - char hn[MAXHOSTNAMELEN]; - gethostname(hn, MAXHOSTNAMELEN - 1); - hn[MAXHOSTNAMELEN - 1] = '\0'; + char hn[OPAL_MAXHOSTNAMELEN]; + gethostname(hn, sizeof(hn)); opal_show_help("help-opal-shmem-mmap.txt", "mmap on nfs", 1, hn, real_file_name); } @@ -382,9 +381,8 @@ segment_create(opal_shmem_ds_t *ds_buf, goto out; } if (!space_available) { - char hn[MAXHOSTNAMELEN]; - gethostname(hn, MAXHOSTNAMELEN - 1); - hn[MAXHOSTNAMELEN - 1] = '\0'; + char hn[OPAL_MAXHOSTNAMELEN]; + gethostname(hn, sizeof(hn)); rc = OPAL_ERR_OUT_OF_RESOURCE; opal_show_help("help-opal-shmem-mmap.txt", "target full", 1, real_file_name, hn, (unsigned long)real_size, @@ -394,9 +392,8 @@ segment_create(opal_shmem_ds_t *ds_buf, /* enough space is available, so create the segment */ if (-1 == (ds_buf->seg_id = open(real_file_name, O_CREAT | O_RDWR, 0600))) { int err = errno; - char hn[MAXHOSTNAMELEN]; - gethostname(hn, MAXHOSTNAMELEN - 1); - hn[MAXHOSTNAMELEN - 1] = '\0'; + char hn[OPAL_MAXHOSTNAMELEN]; + gethostname(hn, sizeof(hn)); opal_show_help("help-opal-shmem-mmap.txt", "sys call fail", 1, hn, "open(2)", "", strerror(err), err); rc = OPAL_ERROR; @@ -405,9 +402,8 @@ segment_create(opal_shmem_ds_t *ds_buf, /* size backing file - note the use of real_size here */ if (0 != ftruncate(ds_buf->seg_id, real_size)) { int err = errno; - char hn[MAXHOSTNAMELEN]; - gethostname(hn, MAXHOSTNAMELEN - 1); - hn[MAXHOSTNAMELEN - 1] = '\0'; + char hn[OPAL_MAXHOSTNAMELEN]; + gethostname(hn, sizeof(hn)); opal_show_help("help-opal-shmem-mmap.txt", "sys call fail", 1, hn, "ftruncate(2)", "", strerror(err), err); rc = OPAL_ERROR; @@ -418,9 +414,8 @@ segment_create(opal_shmem_ds_t *ds_buf, PROT_READ | PROT_WRITE, MAP_SHARED, ds_buf->seg_id, 0))) { int err = errno; - char hn[MAXHOSTNAMELEN]; - gethostname(hn, MAXHOSTNAMELEN - 1); - hn[MAXHOSTNAMELEN - 1] = '\0'; + char hn[OPAL_MAXHOSTNAMELEN]; + gethostname(hn, sizeof(hn)); opal_show_help("help-opal-shmem-mmap.txt", "sys call fail", 1, hn, "mmap(2)", "", strerror(err), err); rc = OPAL_ERROR; @@ -466,9 +461,8 @@ out: if (-1 != ds_buf->seg_id) { if (0 != close(ds_buf->seg_id)) { int err = errno; - char hn[MAXHOSTNAMELEN]; - gethostname(hn, MAXHOSTNAMELEN - 1); - hn[MAXHOSTNAMELEN - 1] = '\0'; + char hn[OPAL_MAXHOSTNAMELEN]; + gethostname(hn, sizeof(hn)); opal_show_help("help-opal-shmem-mmap.txt", "sys call fail", 1, hn, "close(2)", "", strerror(err), err); rc = OPAL_ERROR; @@ -500,9 +494,8 @@ segment_attach(opal_shmem_ds_t *ds_buf) if (my_pid != ds_buf->seg_cpid) { if (-1 == (ds_buf->seg_id = open(ds_buf->seg_name, O_RDWR))) { int err = errno; - char hn[MAXHOSTNAMELEN]; - gethostname(hn, MAXHOSTNAMELEN - 1); - hn[MAXHOSTNAMELEN - 1] = '\0'; + char hn[OPAL_MAXHOSTNAMELEN]; + gethostname(hn, sizeof(hn)); opal_show_help("help-opal-shmem-mmap.txt", "sys call fail", 1, hn, "open(2)", "", strerror(err), err); return NULL; @@ -512,9 +505,8 @@ segment_attach(opal_shmem_ds_t *ds_buf) PROT_READ | PROT_WRITE, MAP_SHARED, ds_buf->seg_id, 0))) { int err = errno; - char hn[MAXHOSTNAMELEN]; - gethostname(hn, MAXHOSTNAMELEN - 1); - hn[MAXHOSTNAMELEN - 1] = '\0'; + char hn[OPAL_MAXHOSTNAMELEN]; + gethostname(hn, sizeof(hn)); opal_show_help("help-opal-shmem-mmap.txt", "sys call fail", 1, hn, "mmap(2)", "", strerror(err), err); /* mmap failed, so close the file and return NULL - no error check @@ -529,9 +521,8 @@ segment_attach(opal_shmem_ds_t *ds_buf) */ if (0 != close(ds_buf->seg_id)) { int err = errno; - char hn[MAXHOSTNAMELEN]; - gethostname(hn, MAXHOSTNAMELEN - 1); - hn[MAXHOSTNAMELEN - 1] = '\0'; + char hn[OPAL_MAXHOSTNAMELEN]; + gethostname(hn, sizeof(hn)); opal_show_help("help-opal-shmem-mmap.txt", "sys call fail", 1, hn, "close(2)", "", strerror(err), err); } @@ -570,9 +561,8 @@ segment_detach(opal_shmem_ds_t *ds_buf) if (0 != munmap((void *)ds_buf->seg_base_addr, ds_buf->seg_size)) { int err = errno; - char hn[MAXHOSTNAMELEN]; - gethostname(hn, MAXHOSTNAMELEN - 1); - hn[MAXHOSTNAMELEN - 1] = '\0'; + char hn[OPAL_MAXHOSTNAMELEN]; + gethostname(hn, sizeof(hn)); opal_show_help("help-opal-shmem-mmap.txt", "sys call fail", 1, hn, "munmap(2)", "", strerror(err), err); rc = OPAL_ERROR; @@ -599,9 +589,8 @@ segment_unlink(opal_shmem_ds_t *ds_buf) if (-1 == unlink(ds_buf->seg_name)) { int err = errno; - char hn[MAXHOSTNAMELEN]; - gethostname(hn, MAXHOSTNAMELEN - 1); - hn[MAXHOSTNAMELEN - 1] = '\0'; + char hn[OPAL_MAXHOSTNAMELEN]; + gethostname(hn, sizeof(hn)); opal_show_help("help-opal-shmem-mmap.txt", "sys call fail", 1, hn, "unlink(2)", ds_buf->seg_name, strerror(err), err); return OPAL_ERROR; diff --git a/opal/mca/shmem/posix/shmem_posix_common_utils.c b/opal/mca/shmem/posix/shmem_posix_common_utils.c index 58517aff49..82c5ae65ce 100644 --- a/opal/mca/shmem/posix/shmem_posix_common_utils.c +++ b/opal/mca/shmem/posix/shmem_posix_common_utils.c @@ -88,9 +88,8 @@ shmem_posix_shm_open(char *posix_file_name_buff, size_t size) * of here. we can't be selected :-(. */ else { - char hn[MAXHOSTNAMELEN]; - gethostname(hn, MAXHOSTNAMELEN - 1); - hn[MAXHOSTNAMELEN - 1] = '\0'; + char hn[OPAL_MAXHOSTNAMELEN]; + gethostname(hn, sizeof(hn)); opal_output_verbose(10, opal_shmem_base_framework.framework_output, "shmem_posix_shm_open: disqualifying posix because " "shm_open(2) failed with error: %s (errno %d)\n", diff --git a/opal/mca/shmem/posix/shmem_posix_component.c b/opal/mca/shmem/posix/shmem_posix_component.c index 9ac4549bf1..fa8ea7d71b 100644 --- a/opal/mca/shmem/posix/shmem_posix_component.c +++ b/opal/mca/shmem/posix/shmem_posix_component.c @@ -178,9 +178,8 @@ posix_runtime_query(mca_base_module_t **module, /* free up allocated resources before we return */ if (0 != shm_unlink(tmp_buff)) { int err = errno; - char hn[MAXHOSTNAMELEN]; - gethostname(hn, MAXHOSTNAMELEN - 1); - hn[MAXHOSTNAMELEN - 1] = '\0'; + char hn[OPAL_MAXHOSTNAMELEN]; + gethostname(hn, sizeof(hn)); opal_show_help("help-opal-shmem-posix.txt", "sys call fail", 1, hn, "shm_unlink(2)", "", strerror(err), err); /* something strange happened, so consider this a run-time test diff --git a/opal/mca/shmem/posix/shmem_posix_module.c b/opal/mca/shmem/posix/shmem_posix_module.c index d441b635b9..0c8781ec6c 100644 --- a/opal/mca/shmem/posix/shmem_posix_module.c +++ b/opal/mca/shmem/posix/shmem_posix_module.c @@ -203,9 +203,8 @@ segment_create(opal_shmem_ds_t *ds_buf, /* size backing file - note the use of real_size here */ else if (0 != ftruncate(ds_buf->seg_id, real_size)) { int err = errno; - char hn[MAXHOSTNAMELEN]; - gethostname(hn, MAXHOSTNAMELEN - 1); - hn[MAXHOSTNAMELEN - 1] = '\0'; + char hn[OPAL_MAXHOSTNAMELEN]; + gethostname(hn, sizeof(hn)); opal_show_help("help-opal-shmem-posix.txt", "sys call fail", 1, hn, "ftruncate(2)", "", strerror(err), err); rc = OPAL_ERROR; @@ -215,9 +214,8 @@ segment_create(opal_shmem_ds_t *ds_buf, PROT_READ | PROT_WRITE, MAP_SHARED, ds_buf->seg_id, 0))) { int err = errno; - char hn[MAXHOSTNAMELEN]; - gethostname(hn, MAXHOSTNAMELEN - 1); - hn[MAXHOSTNAMELEN - 1] = '\0'; + char hn[OPAL_MAXHOSTNAMELEN]; + gethostname(hn, sizeof(hn)); opal_show_help("help-opal-shmem-posix.txt", "sys call fail", 1, hn, "mmap(2)", "", strerror(err), err); rc = OPAL_ERROR; @@ -267,9 +265,8 @@ out: if (-1 != ds_buf->seg_id) { if (0 != close(ds_buf->seg_id)) { int err = errno; - char hn[MAXHOSTNAMELEN]; - gethostname(hn, MAXHOSTNAMELEN - 1); - hn[MAXHOSTNAMELEN - 1] = '\0'; + char hn[OPAL_MAXHOSTNAMELEN]; + gethostname(hn, sizeof(hn)); opal_show_help("help-opal-shmem-mmap.txt", "sys call fail", 1, hn, "close(2)", "", strerror(err), err); rc = OPAL_ERROR; @@ -307,9 +304,8 @@ segment_attach(opal_shmem_ds_t *ds_buf) if (my_pid != ds_buf->seg_cpid) { if (-1 == (ds_buf->seg_id = shm_open(ds_buf->seg_name, O_RDWR, 0600))) { int err = errno; - char hn[MAXHOSTNAMELEN]; - gethostname(hn, MAXHOSTNAMELEN - 1); - hn[MAXHOSTNAMELEN - 1] = '\0'; + char hn[OPAL_MAXHOSTNAMELEN]; + gethostname(hn, sizeof(hn)); opal_show_help("help-opal-shmem-posix.txt", "sys call fail", 1, hn, "open(2)", "", strerror(err), err); return NULL; @@ -319,9 +315,8 @@ segment_attach(opal_shmem_ds_t *ds_buf) PROT_READ | PROT_WRITE, MAP_SHARED, ds_buf->seg_id, 0))) { int err = errno; - char hn[MAXHOSTNAMELEN]; - gethostname(hn, MAXHOSTNAMELEN - 1); - hn[MAXHOSTNAMELEN - 1] = '\0'; + char hn[OPAL_MAXHOSTNAMELEN]; + gethostname(hn, sizeof(hn)); opal_show_help("help-opal-shmem-posix.txt", "sys call fail", 1, hn, "mmap(2)", "", strerror(err), err); /* mmap failed, so shm_unlink and return NULL - no error check here @@ -337,9 +332,8 @@ segment_attach(opal_shmem_ds_t *ds_buf) */ if (0 != close(ds_buf->seg_id)) { int err = errno; - char hn[MAXHOSTNAMELEN]; - gethostname(hn, MAXHOSTNAMELEN - 1); - hn[MAXHOSTNAMELEN - 1] = '\0'; + char hn[OPAL_MAXHOSTNAMELEN]; + gethostname(hn, sizeof(hn)); opal_show_help("help-opal-shmem-mmap.txt", "sys call fail", 1, hn, "close(2)", "", strerror(err), err); } @@ -379,9 +373,8 @@ segment_detach(opal_shmem_ds_t *ds_buf) if (0 != munmap((void*)ds_buf->seg_base_addr, ds_buf->seg_size)) { int err = errno; - char hn[MAXHOSTNAMELEN]; - gethostname(hn, MAXHOSTNAMELEN - 1); - hn[MAXHOSTNAMELEN - 1] = '\0'; + char hn[OPAL_MAXHOSTNAMELEN]; + gethostname(hn, sizeof(hn)); opal_show_help("help-opal-shmem-posix.txt", "sys call fail", 1, hn, "munmap(2)", "", strerror(err), err); rc = OPAL_ERROR; @@ -408,9 +401,8 @@ segment_unlink(opal_shmem_ds_t *ds_buf) if (-1 == shm_unlink(ds_buf->seg_name)) { int err = errno; - char hn[MAXHOSTNAMELEN]; - gethostname(hn, MAXHOSTNAMELEN - 1); - hn[MAXHOSTNAMELEN - 1] = '\0'; + char hn[OPAL_MAXHOSTNAMELEN]; + gethostname(hn, sizeof(hn)); opal_show_help("help-opal-shmem-posix.txt", "sys call fail", 1, hn, "shm_unlink(2)", ds_buf->seg_name, strerror(err), err); return OPAL_ERROR; diff --git a/opal/mca/shmem/sysv/shmem_sysv_module.c b/opal/mca/shmem/sysv/shmem_sysv_module.c index 0ab88dc464..f6a3531aff 100644 --- a/opal/mca/shmem/sysv/shmem_sysv_module.c +++ b/opal/mca/shmem/sysv/shmem_sysv_module.c @@ -195,9 +195,8 @@ segment_create(opal_shmem_ds_t *ds_buf, if (-1 == (ds_buf->seg_id = shmget(IPC_PRIVATE, real_size, IPC_CREAT | IPC_EXCL | S_IRWXU))) { int err = errno; - char hn[MAXHOSTNAMELEN]; - gethostname(hn, MAXHOSTNAMELEN - 1); - hn[MAXHOSTNAMELEN - 1] = '\0'; + char hn[OPAL_MAXHOSTNAMELEN]; + gethostname(hn, sizeof(hn)); opal_show_help("help-opal-shmem-sysv.txt", "sys call fail", 1, hn, "shmget(2)", "", strerror(err), err); rc = OPAL_ERROR; @@ -206,9 +205,8 @@ segment_create(opal_shmem_ds_t *ds_buf, /* attach to the sement */ else if ((void *)-1 == (seg_hdrp = shmat(ds_buf->seg_id, NULL, 0))) { int err = errno; - char hn[MAXHOSTNAMELEN]; - gethostname(hn, MAXHOSTNAMELEN - 1); - hn[MAXHOSTNAMELEN - 1] = '\0'; + char hn[OPAL_MAXHOSTNAMELEN]; + gethostname(hn, sizeof(hn)); opal_show_help("help-opal-shmem-sysv.txt", "sys call fail", 1, hn, "shmat(2)", "", strerror(err), err); shmctl(ds_buf->seg_id, IPC_RMID, NULL); @@ -221,9 +219,8 @@ segment_create(opal_shmem_ds_t *ds_buf, */ else if (0 != shmctl(ds_buf->seg_id, IPC_RMID, NULL)) { int err = errno; - char hn[MAXHOSTNAMELEN]; - gethostname(hn, MAXHOSTNAMELEN - 1); - hn[MAXHOSTNAMELEN - 1] = '\0'; + char hn[OPAL_MAXHOSTNAMELEN]; + gethostname(hn, sizeof(hn)); opal_show_help("help-opal-shmem-sysv.txt", "sys call fail", 1, hn, "shmctl(2)", "", strerror(err), err); rc = OPAL_ERROR; @@ -294,9 +291,8 @@ segment_attach(opal_shmem_ds_t *ds_buf) if ((void *)-1 == (ds_buf->seg_base_addr = shmat(ds_buf->seg_id, NULL, 0))) { int err = errno; - char hn[MAXHOSTNAMELEN]; - gethostname(hn, MAXHOSTNAMELEN - 1); - hn[MAXHOSTNAMELEN - 1] = '\0'; + char hn[OPAL_MAXHOSTNAMELEN]; + gethostname(hn, sizeof(hn)); opal_show_help("help-opal-shmem-sysv.txt", "sys call fail", 1, hn, "shmat(2)", "", strerror(err), err); shmctl(ds_buf->seg_id, IPC_RMID, NULL); @@ -337,9 +333,8 @@ segment_detach(opal_shmem_ds_t *ds_buf) if (0 != shmdt((char*)ds_buf->seg_base_addr)) { int err = errno; - char hn[MAXHOSTNAMELEN]; - gethostname(hn, MAXHOSTNAMELEN - 1); - hn[MAXHOSTNAMELEN - 1] = '\0'; + char hn[OPAL_MAXHOSTNAMELEN]; + gethostname(hn, sizeof(hn)); opal_show_help("help-opal-shmem-sysv.txt", "sys call fail", 1, hn, "shmdt(2)", "", strerror(err), err); rc = OPAL_ERROR; diff --git a/opal/runtime/opal_init.c b/opal/runtime/opal_init.c index 6140b77652..6cf2f1ba04 100644 --- a/opal/runtime/opal_init.c +++ b/opal/runtime/opal_init.c @@ -269,7 +269,7 @@ opal_init_util(int* pargc, char*** pargv) { int ret; char *error = NULL; - char hostname[512]; + char hostname[OPAL_MAXHOSTNAMELEN]; if( ++opal_util_initialized != 1 ) { if( opal_util_initialized < 1 ) { @@ -294,7 +294,7 @@ opal_init_util(int* pargc, char*** pargv) * that we don't bother with fqdn and prefix issues here - we let * the RTE later replace this with a modified name if the user * requests it */ - gethostname(hostname, 512); + gethostname(hostname, sizeof(hostname)); opal_process_info.nodename = strdup(hostname); /* initialize the memory allocator */ diff --git a/opal/util/output.c b/opal/util/output.c index dd950a8c84..b17ce05779 100644 --- a/opal/util/output.c +++ b/opal/util/output.c @@ -127,7 +127,7 @@ OBJ_CLASS_INSTANCE(opal_output_stream_t, opal_object_t, construct, NULL); bool opal_output_init(void) { int i; - char hostname[32]; + char hostname[OPAL_MAXHOSTNAMELEN]; char *str; if (initialized) { @@ -177,7 +177,6 @@ bool opal_output_init(void) verbose.lds_want_stderr = true; } gethostname(hostname, sizeof(hostname)); - hostname[sizeof(hostname)-1] = '\0'; asprintf(&verbose.lds_prefix, "[%s:%05d] ", hostname, getpid()); for (i = 0; i < OPAL_OUTPUT_MAX_STREAMS; ++i) { @@ -254,7 +253,7 @@ bool opal_output_switch(int output_id, bool enable) void opal_output_reopen_all(void) { char *str; - char hostname[32]; + char hostname[OPAL_MAXHOSTNAMELEN]; str = getenv("OPAL_OUTPUT_STDERR_FD"); if (NULL != str) { diff --git a/opal/util/stacktrace.c b/opal/util/stacktrace.c index 9c631ea275..d05275032b 100644 --- a/opal/util/stacktrace.c +++ b/opal/util/stacktrace.c @@ -42,7 +42,7 @@ #define HOSTFORMAT "[%s:%05d] " -static char stacktrace_hostname[64]; +static char stacktrace_hostname[OPAL_MAXHOSTNAMELEN]; static char *unable_to_print_msg = "Unable to print stack trace!\n"; /** @@ -427,7 +427,6 @@ int opal_util_register_stackhandlers (void) bool complain, showed_help = false; gethostname(stacktrace_hostname, sizeof(stacktrace_hostname)); - stacktrace_hostname[sizeof(stacktrace_hostname) - 1] = '\0'; /* to keep these somewhat readable, only print the machine name */ for (i = 0 ; i < (int)strlen(stacktrace_hostname) ; ++i) { if (stacktrace_hostname[i] == '.') { diff --git a/opal/util/timings.c b/opal/util/timings.c index 9459ab5205..e2e0e45bba 100644 --- a/opal/util/timings.c +++ b/opal/util/timings.c @@ -75,8 +75,8 @@ int opal_timing_clocksync_read(char *fname) bool found = false; char *ptr = NULL; - char hname[1024] = "NA"; - if( gethostname(hname, 1024) ){ + char hname[OPAL_MAXHOSTNAMELEN] = "NA"; + if( gethostname(hname, sizeof(hname)) ){ opal_output(0, "opal_timing_clocksync_read(%s): Cannot gethostname", fname); } diff --git a/opal/win32/win_compat.h b/opal/win32/win_compat.h index dc7aeb45f6..c31d3bb74d 100644 --- a/opal/win32/win_compat.h +++ b/opal/win32/win_compat.h @@ -176,6 +176,7 @@ typedef unsigned int uint; #define MAXPATHLEN _MAX_PATH #define MAXHOSTNAMELEN _MAX_PATH +#define OPAL_MAXHOSTNAMELEN (MAXHOSTNAMELEN + 1) #define PATH_MAX _MAX_PATH #define WTERMSIG(EXIT_CODE) (1) #define WIFEXITED(EXIT_CODE) (1)