# HG changeset patch # User Yasumasa Suenaga # Date 1525936816 -32400 # Node ID 43c4b885633577955422f94c191167be824ae28b # Parent 47afd5309aa89fd8e035ac1afdd8a26f89f6718c Bug 3586: Build warnings from GCC 8 Reviewed-by: ykubota https://github.com/HeapStats/heapstats/pull/141 diff -r 47afd5309aa8 -r 43c4b8856335 ChangeLog --- a/ChangeLog Sun Apr 08 22:02:19 2018 +0900 +++ b/ChangeLog Thu May 10 16:20:16 2018 +0900 @@ -1,3 +1,7 @@ +2018-05-10 Yasumasa Suenaga + + * Bug 3586: Build warnings from GCC 8 + 2018-04-08 Yasumasa Suenaga * Bug 3570: Build warning on Arm box diff -r 47afd5309aa8 -r 43c4b8856335 agent/src/arch/x86/heapstats_md_x86.cpp --- a/agent/src/arch/x86/heapstats_md_x86.cpp Sun Apr 08 22:02:19 2018 +0900 +++ b/agent/src/arch/x86/heapstats_md_x86.cpp Thu May 10 16:20:16 2018 +0900 @@ -2,7 +2,7 @@ * \file heapstats_md_x86.cpp * \brief Proxy library for HeapStats backend. * This file implements x86 specific code for loading backend library. - * Copyright (C) 2014 Yasumasa Suenaga + * Copyright (C) 2014-2018 Yasumasa Suenaga * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License @@ -112,10 +112,15 @@ return NULL; } - sprintf(engine_path, - "%s/heapstats-engines/libheapstats-engine-%s-" HEAPSTATS_MAJOR_VERSION - ".so", - heapstats_path, checkInstructionSet()); + int ret = snprintf(engine_path, PATH_MAX, + "%s/heapstats-engines/libheapstats-engine-%s-" + HEAPSTATS_MAJOR_VERSION ".so", + heapstats_path, checkInstructionSet()); + if (ret >= PATH_MAX) { + fprintf(stderr, + "HeapStats engine could not be loaded: engine path is too long\n"); + return NULL; + } void *hEngine = dlopen(engine_path, RTLD_NOW); if (hEngine == NULL) { diff -r 47afd5309aa8 -r 43c4b8856335 agent/src/heapstats-engines/fsUtil.cpp --- a/agent/src/heapstats-engines/fsUtil.cpp Sun Apr 08 22:02:19 2018 +0900 +++ b/agent/src/heapstats-engines/fsUtil.cpp Thu May 10 16:20:16 2018 +0900 @@ -1,7 +1,7 @@ /*! * \file fsUtil.cpp * \brief This file is utilities to access file system. - * Copyright (C) 2011-2015 Nippon Telegraph and Telephone Corporation + * Copyright (C) 2011-2018 Nippon Telegraph and Telephone Corporation * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License @@ -352,13 +352,13 @@ */ char *createUniquePath(char *path, bool isDirectory) { /* Variable for temporary path. */ - char tempPath[PATH_MAX + 1] = {0}; + char tempPath[PATH_MAX] = {0}; /* Variables for file system. */ FILE *file = NULL; DIR *dir = NULL; /* Variable for file path and extensition. */ - char ext[PATH_MAX + 1] = {0}; - char tempName[PATH_MAX + 1] = {0}; + char ext[PATH_MAX] = {0}; + char tempName[PATH_MAX] = {0}; /* Sanity check. */ if (unlikely(path == NULL || strlen(path) == 0)) { @@ -368,6 +368,9 @@ /* Search extension. */ char *extPos = strrchr(path, '.'); + +#pragma GCC diagnostic push +#pragma GCC diagnostic ignored "-Wstringop-truncation" /* If create path for file and exists extension. */ if (!isDirectory && extPos != NULL) { int pathSize = (extPos - path); @@ -383,6 +386,7 @@ /* Copy default path. */ strncpy(tempPath, path, PATH_MAX); +#pragma GCC diagnostic pop /* Try make unique path loop. */ const unsigned long int MAX_RETRY_COUNT = 1000000; @@ -419,7 +423,12 @@ } /* Make new path insert sequence number between 000000 and 999999. */ - snprintf(tempPath, PATH_MAX, "%s_%06lu%s", tempName, loopCount, ext); + int ret = snprintf(tempPath, PATH_MAX, "%s_%06lu%s", + tempName, loopCount, ext); + if (ret >= PATH_MAX) { + logger->printCritMsg("Temp path is too long: %s", tempPath); + return NULL; + } } /* If give up to try unique naming by number. */ @@ -435,7 +444,11 @@ } /* Uniquely naming by random string. */ - snprintf(tempPath, PATH_MAX, "%s_%6s%s", tempName, randStr, ext); + int ret = snprintf(tempPath, PATH_MAX, "%s_%6s%s", tempName, randStr, ext); + if (ret >= PATH_MAX) { + logger->printCritMsg("Temp path is too long: %s", tempPath); + return NULL; + } logger->printWarnMsg("Not found unique name. So used random string."); } diff -r 47afd5309aa8 -r 43c4b8856335 agent/src/heapstats-engines/jvmSockCmd.cpp --- a/agent/src/heapstats-engines/jvmSockCmd.cpp Sun Apr 08 22:02:19 2018 +0900 +++ b/agent/src/heapstats-engines/jvmSockCmd.cpp Thu May 10 16:20:16 2018 +0900 @@ -1,7 +1,7 @@ /*! * \file jvmSockCmd.cpp * \brief This file is used by thread dump. - * Copyright (C) 2011-2017 Nippon Telegraph and Telephone Corporation + * Copyright (C) 2011-2018 Nippon Telegraph and Telephone Corporation * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License @@ -216,7 +216,7 @@ */ bool TJVMSockCmd::createJvmSock(void) { /* Socket file path. */ - char sockPath[PATH_MAX + 1] = {0}; + char sockPath[PATH_MAX] = {0}; /* Search jvm socket file. */ if (!findJvmSock((char*)&sockPath, PATH_MAX)) { diff -r 47afd5309aa8 -r 43c4b8856335 agent/src/heapstats-engines/logManager.cpp --- a/agent/src/heapstats-engines/logManager.cpp Sun Apr 08 22:02:19 2018 +0900 +++ b/agent/src/heapstats-engines/logManager.cpp Thu May 10 16:20:16 2018 +0900 @@ -1,7 +1,7 @@ /*! * \file logManager.cpp * \brief This file is used collect log information. - * Copyright (C) 2011-2017 Nippon Telegraph and Telephone Corporation + * Copyright (C) 2011-2018 Nippon Telegraph and Telephone Corporation * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License @@ -1432,10 +1432,12 @@ time_t nowTimeSec = 0; struct tm time_struct = {0}; char time_str[20] = {0}; - char arcName[PATH_MAX + 1] = {0}; - char extPart[PATH_MAX + 1] = {0}; - char namePart[PATH_MAX + 1] = {0}; + char arcName[PATH_MAX] = {0}; + char extPart[PATH_MAX] = {0}; + char namePart[PATH_MAX] = {0}; +#pragma GCC diagnostic push +#pragma GCC diagnostic ignored "-Wstringop-truncation" /* Search extension. */ char *archiveFileName = conf->ArchiveFile()->get(); char *extPos = strrchr(archiveFileName, '.'); @@ -1447,6 +1449,7 @@ /* Not found extension in path. */ strncpy(namePart, archiveFileName, PATH_MAX); } +#pragma GCC diagnostic pop /* Get now datetime and convert to string. */ nowTimeSec = (time_t)(nowTime / 1000); @@ -1454,7 +1457,11 @@ strftime(time_str, 20, "%y%m%d%H%M%S", &time_struct); /* Create file name. */ - snprintf(arcName, PATH_MAX, "%s%s%s", namePart, time_str, extPart); + int ret = snprintf(arcName, PATH_MAX, "%s%s%s", namePart, time_str, extPart); + if (ret >= PATH_MAX) { + logger->printCritMsg("Archive name is too long: %s", arcName); + return NULL; + } /* Create unique file name. */ return createUniquePath(arcName, false);