diff --git a/opencl/source/context/context.h b/opencl/source/context/context.h index 88b4f5ceab..a23d2c049b 100644 --- a/opencl/source/context/context.h +++ b/opencl/source/context/context.h @@ -8,6 +8,7 @@ #pragma once #include "shared/source/debug_settings/debug_settings_manager.h" #include "shared/source/helpers/common_types.h" +#include "shared/source/helpers/string.h" #include "shared/source/helpers/vec.h" #include "shared/source/unified_memory/unified_memory.h" @@ -135,7 +136,7 @@ class Context : public BaseObject<_cl_context> { DEBUG_BREAK_IF(contextCallback == nullptr); DEBUG_BREAK_IF(driverDiagnostics == nullptr); char hint[DriverDiagnostics::maxHintStringSize]; - snprintf(hint, DriverDiagnostics::maxHintStringSize, DriverDiagnostics::hintFormat[performanceHint], std::forward(args)..., 0); + snprintf_s(hint, DriverDiagnostics::maxHintStringSize, DriverDiagnostics::maxHintStringSize, DriverDiagnostics::hintFormat[performanceHint], std::forward(args)..., 0); if (driverDiagnostics->validFlags(flags)) { if (contextCallback) { contextCallback(hint, &flags, sizeof(flags), userData); diff --git a/shared/source/helpers/string.h b/shared/source/helpers/string.h index 668c1c0e8b..d4c0dbf8bf 100644 --- a/shared/source/helpers/string.h +++ b/shared/source/helpers/string.h @@ -92,6 +92,28 @@ inline int memmove_s(void *dst, size_t numberOfElements, const void *src, size_t return 0; } +template +inline int snprintf_s(char *buffer, size_t sizeOfBuffer, size_t count, const char *format, Args &&...args) { + if ((buffer == nullptr) || (format == nullptr)) { + return -EINVAL; + } + + return snprintf(buffer, sizeOfBuffer, format, std::forward(args)...); +} + +#endif + +#if defined(_WIN32) + +template +inline int snprintf_s(char *buffer, size_t sizeOfBuffer, size_t count, const char *format, Args &&...args) { + if ((buffer == nullptr) || (format == nullptr)) { + return -EINVAL; + } + + return _snprintf_s(buffer, sizeOfBuffer, count, format, std::forward(args)...); +} + #endif template diff --git a/shared/test/common/helpers/string_tests.cpp b/shared/test/common/helpers/string_tests.cpp index ac31e552f3..3bbc2cd8d0 100644 --- a/shared/test/common/helpers/string_tests.cpp +++ b/shared/test/common/helpers/string_tests.cpp @@ -148,3 +148,21 @@ TEST(StringHelpers, GivenParamsWhenUsingMemcpyThenReturnIsCorrect) { } #endif + +TEST(StringHelpers, GivenParamsWhenUsingSnprintfsThenReturnIsCorrect) { + char buffer[15] = ""; + const char *fmtStr = "World!"; + + int retVal1 = snprintf_s(buffer, sizeof(buffer), sizeof(buffer), "Hello %s", fmtStr); + ASSERT_EQ(12, retVal1); + ASSERT_EQ(0, std::strcmp("Hello World!", buffer)); + + int retVal2 = snprintf_s(nullptr, sizeof(buffer), sizeof(buffer), "Hello %s", fmtStr); + ASSERT_EQ(-EINVAL, retVal2); + + int retVal3 = snprintf_s(buffer, sizeof(buffer), sizeof(buffer), nullptr, fmtStr); + ASSERT_EQ(-EINVAL, retVal3); + + int retVal4 = snprintf_s(nullptr, sizeof(buffer), sizeof(buffer), nullptr, fmtStr); + ASSERT_EQ(-EINVAL, retVal4); +}