From a3c0b0537132b961292813ae9c91e1b069f003c1 Mon Sep 17 00:00:00 2001 From: UnknownShadow200 Date: Sat, 5 Aug 2017 23:56:42 +1000 Subject: [PATCH 1/2] Allocate 2 instead of 1 byte per char for buffer used in WinGLNative.HandleDropFiles DragQueryFile returns number of characters. Previously, a buffer of 'number of characters' bytes was being allocated. This change fixes crashing when the platform uses a character set with more than one byte per character. (unicode charset has 2 bytes) Fixes #626 --- src/OpenTK/Platform/Windows/WinGLNative.cs | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/src/OpenTK/Platform/Windows/WinGLNative.cs b/src/OpenTK/Platform/Windows/WinGLNative.cs index 460b24fe..5b31bac0 100644 --- a/src/OpenTK/Platform/Windows/WinGLNative.cs +++ b/src/OpenTK/Platform/Windows/WinGLNative.cs @@ -692,10 +692,11 @@ namespace OpenTK.Platform.Windows for (uint i = 0; i < filesCounter; ++i) { // Don't forget about \0 at the end - uint fileNameSize = Functions.DragQueryFile(hDrop, i, IntPtr.Zero, 0) + 1; - IntPtr str = Marshal.AllocHGlobal((int)fileNameSize); + uint filenameChars = Functions.DragQueryFile(hDrop, i, IntPtr.Zero, 0) + 1; + int filenameSize = (int)(filenameChars * 2); // for unicode char set, 2 bytes per character + IntPtr str = Marshal.AllocHGlobal(filenameSize); - Functions.DragQueryFile(hDrop, i, str, fileNameSize); + Functions.DragQueryFile(hDrop, i, str, filenameChars); string dropString = Marshal.PtrToStringAuto(str); OnFileDrop(dropString); From 5f158997b803cd94410f4b1f291735f6b0f34f16 Mon Sep 17 00:00:00 2001 From: UnknownShadow200 Date: Sun, 6 Aug 2017 08:36:51 +1000 Subject: [PATCH 2/2] Use Marshal.SystemDefaultCharSize instead of hardcoding 2 bytes per char Also move Marshal.FreeHGlobal up because buffer doesn't need to persist after PtrToStringAuto was called. --- src/OpenTK/Platform/Windows/WinGLNative.cs | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/src/OpenTK/Platform/Windows/WinGLNative.cs b/src/OpenTK/Platform/Windows/WinGLNative.cs index 5b31bac0..c1cd5fc3 100644 --- a/src/OpenTK/Platform/Windows/WinGLNative.cs +++ b/src/OpenTK/Platform/Windows/WinGLNative.cs @@ -693,15 +693,14 @@ namespace OpenTK.Platform.Windows { // Don't forget about \0 at the end uint filenameChars = Functions.DragQueryFile(hDrop, i, IntPtr.Zero, 0) + 1; - int filenameSize = (int)(filenameChars * 2); // for unicode char set, 2 bytes per character + int filenameSize = (int)(filenameChars * Marshal.SystemDefaultCharSize); IntPtr str = Marshal.AllocHGlobal(filenameSize); Functions.DragQueryFile(hDrop, i, str, filenameChars); string dropString = Marshal.PtrToStringAuto(str); - OnFileDrop(dropString); - Marshal.FreeHGlobal(str); + OnFileDrop(dropString); } Functions.DragFinish(hDrop);