diff --git a/shared/offline_compiler/source/default_cache_config.cpp b/shared/offline_compiler/source/default_cache_config.cpp index 5fa6078a30..c715d8169e 100644 --- a/shared/offline_compiler/source/default_cache_config.cpp +++ b/shared/offline_compiler/source/default_cache_config.cpp @@ -1,5 +1,5 @@ /* - * Copyright (C) 2022-2024 Intel Corporation + * Copyright (C) 2022-2025 Intel Corporation * * SPDX-License-Identifier: MIT * @@ -32,14 +32,12 @@ int64_t getSetting(const char *settingName, int64_t defaultValue) { } std::string getSetting(const char *settingName, const std::string &value) { - char *envValue; - std::string keyValue; - keyValue.assign(value); - - envValue = IoFunctions::getenvPtr(settingName); + std::string keyValue = value; + char *envValue = IoFunctions::getEnvironmentVariable(settingName); if (envValue) { keyValue.assign(envValue); } + return keyValue; } diff --git a/shared/source/helpers/constants.h b/shared/source/helpers/constants.h index 2e8084e000..3ba5312b5d 100644 --- a/shared/source/helpers/constants.h +++ b/shared/source/helpers/constants.h @@ -97,4 +97,5 @@ inline constexpr uint32_t minimalSyncBufferSize = 12; inline constexpr uint32_t gpuHangCheckTimeInUS = 500'000; inline constexpr double defaultProfilingTimerResolution = 83.333; inline constexpr uint64_t nsecPerSec = 1000000000ull; -} // namespace CommonConstants \ No newline at end of file +inline constexpr uint32_t maxAllowedEnvVariableSize = 4096u; +} // namespace CommonConstants diff --git a/shared/source/os_interface/debug_env_reader.cpp b/shared/source/os_interface/debug_env_reader.cpp index eaa8af62d9..46e7f7f283 100644 --- a/shared/source/os_interface/debug_env_reader.cpp +++ b/shared/source/os_interface/debug_env_reader.cpp @@ -1,5 +1,5 @@ /* - * Copyright (C) 2020-2023 Intel Corporation + * Copyright (C) 2020-2025 Intel Corporation * * SPDX-License-Identifier: MIT * @@ -69,9 +69,7 @@ int64_t EnvironmentVariableReader::getSetting(const char *settingName, int64_t d } std::string EnvironmentVariableReader::getSetting(const char *settingName, const std::string &value, DebugVarPrefix &type) { - char *envValue; - std::string keyValue; - keyValue.assign(value); + std::string keyValue = value; auto prefixString = ApiSpecificConfig::getPrefixStrings(); auto prefixType = ApiSpecificConfig::getPrefixTypes(); @@ -80,7 +78,8 @@ std::string EnvironmentVariableReader::getSetting(const char *settingName, const for (const auto &prefix : prefixString) { std::string neoKey = prefix; neoKey += settingName; - envValue = IoFunctions::getenvPtr(neoKey.c_str()); + auto envValue = IoFunctions::getEnvironmentVariable(neoKey.c_str()); + if (envValue) { keyValue.assign(envValue); type = prefixType[i]; @@ -93,14 +92,14 @@ std::string EnvironmentVariableReader::getSetting(const char *settingName, const } std::string EnvironmentVariableReader::getSetting(const char *settingName, const std::string &value) { - char *envValue; - std::string keyValue; - keyValue.assign(value); + std::string keyValue = value; + char *envValue = IoFunctions::getEnvironmentVariable(settingName); - envValue = IoFunctions::getenvPtr(settingName); if (envValue) { keyValue.assign(envValue); } + return keyValue; } + } // namespace NEO diff --git a/shared/source/os_interface/windows/debug_registry_reader.cpp b/shared/source/os_interface/windows/debug_registry_reader.cpp index 66601f9a5a..d3a2eefed8 100644 --- a/shared/source/os_interface/windows/debug_registry_reader.cpp +++ b/shared/source/os_interface/windows/debug_registry_reader.cpp @@ -1,5 +1,5 @@ /* - * Copyright (C) 2018-2024 Intel Corporation + * Copyright (C) 2018-2025 Intel Corporation * * SPDX-License-Identifier: MIT * @@ -188,7 +188,6 @@ std::string RegistryReader::getSetting(const char *settingName, const std::strin std::string keyValue = value; if (!(getSettingStringCommon(settingName, keyValue))) { - char *envValue; auto prefixString = ApiSpecificConfig::getPrefixStrings(); auto prefixType = ApiSpecificConfig::getPrefixTypes(); @@ -197,7 +196,8 @@ std::string RegistryReader::getSetting(const char *settingName, const std::strin for (const auto &prefix : prefixString) { std::string neoKey = prefix; neoKey += settingName; - envValue = IoFunctions::getenvPtr(neoKey.c_str()); + auto envValue = IoFunctions::getEnvironmentVariable(neoKey.c_str()); + if (envValue) { keyValue.assign(envValue); type = prefixType[i]; @@ -214,7 +214,8 @@ std::string RegistryReader::getSetting(const char *settingName, const std::strin std::string keyValue = value; if (!(getSettingStringCommon(settingName, keyValue))) { - const char *envValue = IoFunctions::getenvPtr(settingName); + const char *envValue = IoFunctions::getEnvironmentVariable(settingName); + if (envValue) { keyValue.assign(envValue); } diff --git a/shared/source/utilities/io_functions.h b/shared/source/utilities/io_functions.h index 89b05c4736..ed62cdad5c 100644 --- a/shared/source/utilities/io_functions.h +++ b/shared/source/utilities/io_functions.h @@ -1,11 +1,14 @@ /* - * Copyright (C) 2020-2024 Intel Corporation + * Copyright (C) 2020-2025 Intel Corporation * * SPDX-License-Identifier: MIT * */ #pragma once +#include "shared/source/helpers/constants.h" +#include "shared/source/helpers/string.h" + #include #include #include @@ -49,6 +52,17 @@ inline bool getEnvToBool(const char *name) { return false; return (0 == strcmp("1", env)); } -} // namespace IoFunctions +inline char *getEnvironmentVariable(const char *name) { + + char *environmentVariable = getenvPtr(name); + + if (strnlen_s(environmentVariable, CommonConstants::maxAllowedEnvVariableSize) < CommonConstants::maxAllowedEnvVariableSize) { + return environmentVariable; + } + + return nullptr; +} + +} // namespace IoFunctions } // namespace NEO diff --git a/shared/test/unit_test/os_interface/debug_env_reader_tests.cpp b/shared/test/unit_test/os_interface/debug_env_reader_tests.cpp index e75df0c6d2..4298a3f0cb 100644 --- a/shared/test/unit_test/os_interface/debug_env_reader_tests.cpp +++ b/shared/test/unit_test/os_interface/debug_env_reader_tests.cpp @@ -1,5 +1,5 @@ /* - * Copyright (C) 2018-2024 Intel Corporation + * Copyright (C) 2018-2025 Intel Corporation * * SPDX-License-Identifier: MIT * @@ -487,4 +487,35 @@ TEST_F(DebugEnvReaderTests, givenEnvironmentVariableReaderWhenCreateOsReaderWith std::unique_ptr settingsReader(SettingsReader::createOsReader(false, "")); EXPECT_NE(nullptr, settingsReader); } + +TEST_F(DebugEnvReaderTests, givenTooLongEnvValueWhenGetEnvironmentVariableIsCalledThenNullptrIsReturned) { + { + auto maxAllowedEnvVariableSize = CommonConstants::maxAllowedEnvVariableSize; + const char *testingVariableName = "test"; + VariableBackup mockGetenvCalledBackup(&IoFunctions::mockGetenvCalled, 0); + { + + std::string veryLongPath(maxAllowedEnvVariableSize + 1u, 'a'); + veryLongPath.back() = '\0'; + std::unordered_map mockableEnvs = {{testingVariableName, veryLongPath.c_str()}}; + VariableBackup *> mockableEnvValuesBackup(&IoFunctions::mockableEnvValues, &mockableEnvs); + auto envValue = IoFunctions::getEnvironmentVariable(testingVariableName); + + ASSERT_NE(nullptr, veryLongPath.c_str()); + EXPECT_EQ(nullptr, envValue); + } + + { + std::string goodPath(maxAllowedEnvVariableSize, 'a'); + goodPath.back() = '\0'; + std::unordered_map mockableEnvs = {{testingVariableName, goodPath.c_str()}}; + VariableBackup *> mockableEnvValuesBackup(&IoFunctions::mockableEnvValues, &mockableEnvs); + auto envValue = IoFunctions::getEnvironmentVariable(testingVariableName); + + ASSERT_NE(nullptr, goodPath.c_str()); + EXPECT_EQ(mockableEnvs.at(testingVariableName).c_str(), envValue); + } + } +} + } // namespace NEO