From 555e68a19de851edcf9247531744f4045bbedaf7 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Wed, 4 Oct 2017 13:38:24 +0300 Subject: [PATCH] [debug] fix off by one error in DebugObjectToUtf8Array We've copied exactly `toCopy` bytes, so the byte after the last copied has index `toCopy`. Note that if `toCopy` is exactly (bufferSize - 1), the previous code would write to memory outside of the buffer. --- runtime/src/main/cpp/KDebug.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/runtime/src/main/cpp/KDebug.cpp b/runtime/src/main/cpp/KDebug.cpp index 6415abd902f..8418b1c9f50 100644 --- a/runtime/src/main/cpp/KDebug.cpp +++ b/runtime/src/main/cpp/KDebug.cpp @@ -52,7 +52,7 @@ RUNTIME_USED KInt Konan_DebugObjectToUtf8Array(KRef obj, char* buffer, KInt buff if (data == nullptr) return 0; KInt toCopy = data->count_ > bufferSize - 1 ? bufferSize - 1 : data->count_; ::memcpy(buffer, ByteArrayAddressOfElementAt(data, 0), toCopy); - buffer[toCopy + 1] = '\0'; + buffer[toCopy] = '\0'; return toCopy + 1; }