Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Now allocates properly sized result buffer. #3657

Merged
merged 1 commit into from
Aug 1, 2016
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 12 additions & 5 deletions Cabal/Distribution/Compat/GetShortPathName.hs
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down