From b88a0f7fc616b3b777abb72d2165d234376cd211 Mon Sep 17 00:00:00 2001 From: Andrea Bocci Date: Sat, 2 May 2020 00:31:47 +0200 Subject: [PATCH] [SYCL] Fix GCC 8 compilation warnings GCC 8 introduces a warning when strncpy may truncate a string, leaving it without the terminating NULL character [-Wstringop-truncation]. To guarantee that the terminator is present: - increase the size of the target buffer by 1; - set the last character of the buffer to NULL. Signed-off-by: Andrea Bocci --- sycl/source/detail/config.cpp | 3 ++- sycl/source/detail/config.hpp | 2 +- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/sycl/source/detail/config.cpp b/sycl/source/detail/config.cpp index 8258bae4ed412..544dce4b2cb76 100644 --- a/sycl/source/detail/config.cpp +++ b/sycl/source/detail/config.cpp @@ -27,7 +27,7 @@ namespace detail { #define CONFIG(Name, MaxSize, CompileTimeDef) \ const char *SYCLConfigBase::MValueFromFile = nullptr; \ - char SYCLConfigBase::MStorage[MaxSize]; \ + char SYCLConfigBase::MStorage[MaxSize + 1]; \ const char *const SYCLConfigBase::MCompileTimeDef = \ getStrOrNullptr(STRINGIFY_LINE(CompileTimeDef)); \ const char *const SYCLConfigBase::MConfigName = STRINGIFY_LINE(Name); @@ -40,6 +40,7 @@ static void initValue(const char *Key, const char *Value) { #define CONFIG(Name, MaxSize, CompileTimeDef) \ if (0 == strncmp(Key, SYCLConfigBase::MConfigName, MAX_CONFIG_NAME)) { \ strncpy(SYCLConfigBase::MStorage, Value, MaxSize); \ + SYCLConfigBase::MStorage[MaxSize] = '\0'; \ SYCLConfigBase::MValueFromFile = SYCLConfigBase::MStorage; \ return; \ } diff --git a/sycl/source/detail/config.hpp b/sycl/source/detail/config.hpp index e1d571d8dfb3d..b1510004be08f 100644 --- a/sycl/source/detail/config.hpp +++ b/sycl/source/detail/config.hpp @@ -63,7 +63,7 @@ template class SYCLConfigBase; public: \ /*Preallocated storage for config value which is extracted from a config \ * file*/ \ - static char MStorage[MaxSize]; \ + static char MStorage[MaxSize + 1]; \ /*Points to the storage if config is set in the file, nullptr otherwise*/ \ static const char *MValueFromFile; \ /*The name of the config*/ \