diff --git a/opencl/test/unit_test/program/printf_helper_tests.cpp b/opencl/test/unit_test/program/printf_helper_tests.cpp index fc763ed372..c407ab5116 100644 --- a/opencl/test/unit_test/program/printf_helper_tests.cpp +++ b/opencl/test/unit_test/program/printf_helper_tests.cpp @@ -269,6 +269,21 @@ TEST_P(PrintfUint32Test, GivenPrintfFormatWhenConatinsUintThenInstertValueIntoSt EXPECT_STREQ(referenceOutput, actualOutput); } +TEST_P(PrintfUint32Test, GivenPrintfFormatWhenBufferSizeExceedsPrintfBufferCapacityThenUseSmallerValue) { + auto input = GetParam(); + printFormatter = std::unique_ptr(new PrintFormatter(static_cast(data->getUnderlyingBuffer()), 0, is32bit, kernelInfo->patchInfo.stringDataMap)); + + auto stringIndex = injectFormatString(input.format); + storeData(stringIndex); + injectValue(input.value); + + char referenceOutput[PrintFormatter::maxPrintfOutputLength] = ""; + char actualOutput[PrintFormatter::maxPrintfOutputLength] = ""; + printFormatter->printKernelOutput([&actualOutput](char *str) { strncpy_s(actualOutput, PrintFormatter::maxPrintfOutputLength, str, PrintFormatter::maxPrintfOutputLength); }); + + EXPECT_STREQ(referenceOutput, actualOutput); +} + INSTANTIATE_TEST_CASE_P(PrintfUint32Test, PrintfUint32Test, ::testing::ValuesIn(uintValues)); diff --git a/shared/source/program/print_formatter.cpp b/shared/source/program/print_formatter.cpp index 195cd54077..68ad50ce12 100644 --- a/shared/source/program/print_formatter.cpp +++ b/shared/source/program/print_formatter.cpp @@ -25,7 +25,9 @@ void PrintFormatter::printKernelOutput(const std::function &print) currentOffset = 0; // first 4 bytes of the buffer store the actual size of data that was written by printf from within EUs - read(&printfOutputBufferSize); + uint32_t printfOutputBufferSizeRead = 0; + read(&printfOutputBufferSizeRead); + printfOutputBufferSize = std::min(printfOutputBufferSizeRead, printfOutputBufferSize); uint32_t stringIndex = 0; while (currentOffset + 4 <= printfOutputBufferSize) {