From 6624fe46280e4f01b7e091b073652a0355904517 Mon Sep 17 00:00:00 2001 From: Laurent Morichetti Date: Thu, 22 Apr 2021 11:12:53 -0700 Subject: [PATCH] Fix a compilation error with gcc-9.3.0 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit On Ubuntu 20.04, in Release mode, gcc fails with this error: In file included from /usr/include/string.h:495, from /opt/rocm/include/hsa/hsa_api_trace.h:57, from ../rocprofiler/src/util/hsa_rsrc_factory.h:29, from ../rocprofiler/src/util/hsa_rsrc_factory.cpp:25: In function ‘char* strncpy(char*, const char*, size_t)’, inlined from ‘const util::AgentInfo* util::HsaRsrcFactory::AddAgentInfo(hsa_agent_t)’ at ../rocprofiler/src/util/hsa_rsrc_factory.cpp:323:12: /usr/include/x86_64-linux-gnu/bits/string_fortified.h:106:34: error: ‘char* __builtin___strncpy_chk(char*, const char*, long unsigned int, long unsigned int)’ specified bound depends on the length of the source argument [-Werror=stringop-overflow=] 106 | return __builtin___strncpy_chk (__dest, __src, __len, __bos (__dest)); | ~~~~~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ../rocprofiler/src/util/hsa_rsrc_factory.cpp: In member function ‘const util::AgentInfo* util::HsaRsrcFactory::AddAgentInfo(hsa_agent_t)’: ../rocprofiler/src/util/hsa_rsrc_factory.cpp:322:39: note: length computed here 322 | const int gfxip_label_len = strlen(agent_info->name) - 2; | ~~~~~~^~~~~~~~~~~~~~~~~~ The error is caused by the following 2 lines: const int gfxip_label_len = strlen(agent_info->name) - 2; strncpy(agent_info->gfxip, agent_info->name, gfxip_label_len); The size argument to strncpy should not depend on the input string. Since the terminating character is not considered (the copy is at most len - 2 bytes), using memcpy is preferable. Also, make sure the destination does not overflow by clamping the size. Change-Id: I0c5cf7e0daf4cd6fcf7092efb1d9fd4c02a6c639 [ROCm/rocprofiler commit: 304d3366affb94f2f8e3a62915ff68b8901cd822] --- projects/rocprofiler/src/util/hsa_rsrc_factory.cpp | 4 ++-- projects/rocprofiler/test/util/hsa_rsrc_factory.cpp | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/projects/rocprofiler/src/util/hsa_rsrc_factory.cpp b/projects/rocprofiler/src/util/hsa_rsrc_factory.cpp index 737a6d1624..9d980312a1 100644 --- a/projects/rocprofiler/src/util/hsa_rsrc_factory.cpp +++ b/projects/rocprofiler/src/util/hsa_rsrc_factory.cpp @@ -336,8 +336,8 @@ const AgentInfo* HsaRsrcFactory::AddAgentInfo(const hsa_agent_t agent) { agent_info->dev_id = agent; agent_info->dev_type = HSA_DEVICE_TYPE_GPU; hsa_api_.hsa_agent_get_info(agent, HSA_AGENT_INFO_NAME, agent_info->name); - const int gfxip_label_len = strlen(agent_info->name) - 2; - strncpy(agent_info->gfxip, agent_info->name, gfxip_label_len); + const int gfxip_label_len = std::min (strlen(agent_info->name) - 2, sizeof (agent_info->gfxip) - 1); + memcpy(agent_info->gfxip, agent_info->name, gfxip_label_len); agent_info->gfxip[gfxip_label_len] = '\0'; hsa_api_.hsa_agent_get_info(agent, HSA_AGENT_INFO_WAVEFRONT_SIZE, &agent_info->max_wave_size); hsa_api_.hsa_agent_get_info(agent, HSA_AGENT_INFO_QUEUE_MAX_SIZE, &agent_info->max_queue_size); diff --git a/projects/rocprofiler/test/util/hsa_rsrc_factory.cpp b/projects/rocprofiler/test/util/hsa_rsrc_factory.cpp index 3cbb4b33e4..ccc46c0b0f 100644 --- a/projects/rocprofiler/test/util/hsa_rsrc_factory.cpp +++ b/projects/rocprofiler/test/util/hsa_rsrc_factory.cpp @@ -330,8 +330,8 @@ const AgentInfo* HsaRsrcFactory::AddAgentInfo(const hsa_agent_t agent) { agent_info->dev_id = agent; agent_info->dev_type = HSA_DEVICE_TYPE_GPU; hsa_api_.hsa_agent_get_info(agent, HSA_AGENT_INFO_NAME, agent_info->name); - const int gfxip_label_len = strlen(agent_info->name) - 2; - strncpy(agent_info->gfxip, agent_info->name, gfxip_label_len); + const int gfxip_label_len = std::min (strlen(agent_info->name) - 2, sizeof (agent_info->gfxip) - 1); + memcpy(agent_info->gfxip, agent_info->name, gfxip_label_len); agent_info->gfxip[gfxip_label_len] = '\0'; hsa_api_.hsa_agent_get_info(agent, HSA_AGENT_INFO_WAVEFRONT_SIZE, &agent_info->max_wave_size); hsa_api_.hsa_agent_get_info(agent, HSA_AGENT_INFO_QUEUE_MAX_SIZE, &agent_info->max_queue_size);