From 7423edce9bae1e53d0cce1d6715de183d1ef8e39 Mon Sep 17 00:00:00 2001 From: Dongqi Chen Date: Thu, 2 Apr 2020 17:25:45 +0800 Subject: [PATCH] Fix mtce-common build error with gcc-8.2.1 gcc-8.2.1 reports "Werror=format-truncation" error due to there is possibility the string be truncated, add return value check could avoid the error. Signed-off-by: Shuicheng Lin Signed-off-by: Dongqi Chen Change-Id: I8fa08077e47ee3777a50f018af77b3e8fc6191f9 Story: 2007506 Task: 39278 --- mtce-common/src/common/msgClass.cpp | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/mtce-common/src/common/msgClass.cpp b/mtce-common/src/common/msgClass.cpp index 2caf7185..b11201f8 100644 --- a/mtce-common/src/common/msgClass.cpp +++ b/mtce-common/src/common/msgClass.cpp @@ -330,12 +330,17 @@ int msgClassAddr::getAddressFromInterface(const char* interface, char* address, // is the intf hostname char iface_hostname[MAX_CHARS_HOSTNAME]; memset(iface_hostname, 0, sizeof(iface_hostname)); - snprintf(iface_hostname, sizeof(iface_hostname), - "%s%s", hostname, - (((interface_type == CLSTR_IFACE)) ? CLUSTER_HOST_SUFFIX : "")); + int ret = snprintf(iface_hostname, sizeof(iface_hostname), + "%s%s", hostname, + (((interface_type == CLSTR_IFACE)) ? CLUSTER_HOST_SUFFIX : "")); + if (ret >= (int)sizeof(iface_hostname)) + { + elog("%s hostname is too long ; truncated. interface_type: %d", + hostname, interface_type); + } struct addrinfo *res = NULL; - int ret = getaddrinfo(iface_hostname, NULL, NULL, &res); + ret = getaddrinfo(iface_hostname, NULL, NULL, &res); if(ret) { elog("%s ip address resolution failed (err: %s)",