From e9a45f101f09bbdd13853ae26b35626881a4df2c Mon Sep 17 00:00:00 2001 From: Brian Barrett Date: Thu, 21 Sep 2017 11:41:39 -0700 Subject: [PATCH] reachable: Fix string length Coverity warning Make sure hostnames are null terminated, even when they were too long to fit in the hostname buffer. Fixes: CID 1418232 Signed-off-by: Brian Barrett --- opal/mca/reachable/netlink/reachable_netlink_module.c | 2 ++ opal/mca/reachable/weighted/reachable_weighted.c | 2 ++ 2 files changed, 4 insertions(+) diff --git a/opal/mca/reachable/netlink/reachable_netlink_module.c b/opal/mca/reachable/netlink/reachable_netlink_module.c index 3bb82049a8c..d7550e0d6d6 100644 --- a/opal/mca/reachable/netlink/reachable_netlink_module.c +++ b/opal/mca/reachable/netlink/reachable_netlink_module.c @@ -95,9 +95,11 @@ static int get_weights(opal_if_t *local_if, opal_if_t *remote_if) strncpy(str_local, opal_net_get_hostname((struct sockaddr *)&local_if->if_addr), sizeof(str_local)); + str_local[sizeof(str_local) - 1] = '\0'; strncpy(str_remote, opal_net_get_hostname((struct sockaddr *)&remote_if->if_addr), sizeof(str_remote)); + str_remote[sizeof(str_remote) - 1] = '\0'; /* initially, assume no connection is possible */ weight = calculate_weight(0, 0, CQ_NO_CONNECTION); diff --git a/opal/mca/reachable/weighted/reachable_weighted.c b/opal/mca/reachable/weighted/reachable_weighted.c index ecd68ac03ec..5157c9469f5 100644 --- a/opal/mca/reachable/weighted/reachable_weighted.c +++ b/opal/mca/reachable/weighted/reachable_weighted.c @@ -121,7 +121,9 @@ static int get_weights(opal_if_t *local_if, opal_if_t *remote_if) /* opal_net_get_hostname returns a static buffer. Great for single address printfs, need to copy in this case */ strncpy(str_local, opal_net_get_hostname(local_sockaddr), sizeof(str_local)); + str_local[sizeof(str_local) - 1] = '\0'; strncpy(str_remote, opal_net_get_hostname(remote_sockaddr), sizeof(str_remote)); + str_remote[sizeof(str_remote) - 1] = '\0'; /* initially, assume no connection is possible */ weight = calculate_weight(0, 0, CQ_NO_CONNECTION);