From 3c1b6190cee8c10e3e56dccbc69f52b8065f1180 Mon Sep 17 00:00:00 2001 From: Ken Bateman Date: Mon, 1 Aug 2016 17:04:46 -0400 Subject: [PATCH] Now allocates properly sized result buffer. --- Cabal/Distribution/Compat/GetShortPathName.hs | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) diff --git a/Cabal/Distribution/Compat/GetShortPathName.hs b/Cabal/Distribution/Compat/GetShortPathName.hs index 795d90d6060..9f676aaeaea 100644 --- a/Cabal/Distribution/Compat/GetShortPathName.hs +++ b/Cabal/Distribution/Compat/GetShortPathName.hs @@ -32,16 +32,23 @@ foreign import WINAPI unsafe "windows.h GetShortPathNameW" -- | On Windows, retrieves the short path form of the specified path. On -- non-Windows, does nothing. See https://github.com/haskell/cabal/issues/3185. +-- +-- From MS's GetShortPathName docs: +-- +-- Passing NULL for [the second] parameter and zero for cchBuffer +-- will always return the required buffer size for a +-- specified lpszLongPath. +-- getShortPathName :: FilePath -> IO FilePath getShortPathName path = - Win32.withTString path $ \c_path -> + Win32.withTString path $ \c_path -> do + c_len <- Win32.failIfZero "GetShortPathName #1 failed!" $ + c_GetShortPathName c_path Win32.nullPtr 0 + let arr_len = fromIntegral c_len allocaArray arr_len $ \c_out -> do - void $ Win32.failIfZero "GetShortPathName failed!" $ + void $ Win32.failIfZero "GetShortPathName #2 failed!" $ c_GetShortPathName c_path c_out c_len Win32.peekTString c_out - where - arr_len = length path + 1 - c_len = fromIntegral arr_len #else