Skip to content

Commit

Permalink
Respect signedness of literals (#78)
Browse files Browse the repository at this point in the history
* Use maxBound instead of (-1) to define iNVALID_HANDLE_VALUE

The former results in an out-of-bounds literal warning (introduced in GHC 8.2).

* Menu: Use maxBound instead of (-1) to discern error

The value being compared against is unsigned so (-1) is technically out
of range.

* Window: Define cW_USE_DEFAULT as Int, not Word

Since its value is signed.

* Using NegativeLiterals

* use negative number to fit bit pattern.

* use negative number to fit bit pattern

* Weird convoluted way to get a value with the top bit set..

* Add clarifying comment.

* Mark for release.
  • Loading branch information
Mistuke authored Mar 7, 2017
1 parent 2567e43 commit 06d5849
Show file tree
Hide file tree
Showing 5 changed files with 15 additions and 10 deletions.
10 changes: 5 additions & 5 deletions Graphics/Win32/Menu.hsc
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ type MenuName = LPCTSTR

checkMenuItem :: HMENU -> MenuItem -> MenuFlag -> IO Bool
checkMenuItem menu item check = do
rv <- failIf (== -1) "CheckMenuItem" $ c_CheckMenuItem menu item check
rv <- failIf (== maxBound) "CheckMenuItem" $ c_CheckMenuItem menu item check
return (rv == mF_CHECKED)
foreign import WINDOWS_CCONV unsafe "windows.h CheckMenuItem"
c_CheckMenuItem :: HMENU -> UINT -> UINT -> IO DWORD
Expand Down Expand Up @@ -230,13 +230,13 @@ foreign import WINDOWS_CCONV unsafe "windows.h GetMenu"

getMenuDefaultItem :: HMENU -> Bool -> GMDIFlag -> IO MenuItem
getMenuDefaultItem menu bypos flags =
failIf (== -1) "GetMenuDefaultItem" $ c_GetMenuDefaultItem menu bypos flags
failIf (== maxBound) "GetMenuDefaultItem" $ c_GetMenuDefaultItem menu bypos flags
foreign import WINDOWS_CCONV unsafe "windows.h GetMenuDefaultItem"
c_GetMenuDefaultItem :: HMENU -> Bool -> UINT -> IO UINT

getMenuState :: HMENU -> MenuItem -> MenuFlag -> IO MenuState
getMenuState menu item flags =
failIf (== -1) "GetMenuState" $ c_GetMenuState menu item flags
failIf (== maxBound) "GetMenuState" $ c_GetMenuState menu item flags
foreign import WINDOWS_CCONV unsafe "windows.h GetMenuState"
c_GetMenuState :: HMENU -> UINT -> UINT -> IO MenuState

Expand All @@ -254,15 +254,15 @@ foreign import WINDOWS_CCONV unsafe "windows.h SetMenu"

getMenuItemCount :: HMENU -> IO Int
getMenuItemCount menu =
failIf (== -1) "GetMenuItemCount" $ c_GetMenuItemCount menu
failIf (== maxBound) "GetMenuItemCount" $ c_GetMenuItemCount menu
foreign import WINDOWS_CCONV unsafe "windows.h GetMenuItemCount"
c_GetMenuItemCount :: HMENU -> IO Int

type MenuID = UINT

getMenuItemID :: HMENU -> MenuItem -> IO MenuID
getMenuItemID menu item =
failIf (== -1) "GetMenuItemID" $ c_GetMenuItemID menu item
failIf (== maxBound) "GetMenuItemID" $ c_GetMenuItemID menu item
foreign import WINDOWS_CCONV unsafe "windows.h GetMenuItemID"
c_GetMenuItemID :: HMENU -> UINT -> IO MenuID

Expand Down
8 changes: 6 additions & 2 deletions Graphics/Win32/Window.hsc
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
{-# LANGUAGE CApiFFI #-}
{-# LANGUAGE NegativeLiterals #-}
#if __GLASGOW_HASKELL__ >= 701
{-# LANGUAGE Trustworthy #-}
#endif
Expand All @@ -20,7 +21,7 @@ module Graphics.Win32.Window where

import Control.Monad (liftM)
import Data.Maybe (fromMaybe)
import Data.Word (Word32)
import Data.Int (Int32)
import Foreign.ForeignPtr (withForeignPtr)
import Foreign.Marshal.Alloc (allocaBytes)
import Foreign.Ptr (FunPtr, Ptr, castFunPtrToPtr, castPtr, nullPtr)
Expand Down Expand Up @@ -185,7 +186,10 @@ type WindowStyleEx = DWORD

cW_USEDEFAULT :: Pos
-- See Note [Overflow checking and fromIntegral] in Graphics/Win32/GDI/HDC.hs
cW_USEDEFAULT = fromIntegral (#{const CW_USEDEFAULT} :: Word32)
-- Weird way to essentially get a value with the top bit set. But GHC 7.8.4 was
-- rejecting all other sane attempts.
cW_USEDEFAULT = let val = negate (#{const CW_USEDEFAULT}) :: Integer
in fromIntegral (fromIntegral val :: Int32) :: Pos

type Pos = Int

Expand Down
2 changes: 1 addition & 1 deletion System/Win32/Types.hsc
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,7 @@ nullFinalHANDLE :: ForeignPtr a
nullFinalHANDLE = unsafePerformIO (newForeignPtr_ nullPtr)

iNVALID_HANDLE_VALUE :: HANDLE
iNVALID_HANDLE_VALUE = castUINTPtrToPtr (-1)
iNVALID_HANDLE_VALUE = castUINTPtrToPtr maxBound

foreign import ccall "_open_osfhandle"
_open_osfhandle :: CIntPtr -> CInt -> IO CInt
Expand Down
2 changes: 1 addition & 1 deletion Win32.cabal
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name: Win32
version: 2.5.1.0
version: 2.5.2.0
license: BSD3
license-file: LICENSE
author: Alastair Reid, shelarcy
Expand Down
3 changes: 2 additions & 1 deletion changelog.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
# Changelog for [`Win32` package](http://hackage.haskell.org/package/Win32)

## Unreleased GIT version
## 2.5.2.0 *March 2017*

* Fix constant underflows with (-1) and unsigned numbers.
* Add `commandLineToArgv`

## 2.5.1.0 *Feb 2017*
Expand Down

2 comments on commit 06d5849

@Mistuke
Copy link
Contributor Author

@Mistuke Mistuke commented on 06d5849 Mar 7, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

TeamCity Win32 Bindings for Haskell :: Win32 Continuous builds Build 32 is now running

@Mistuke
Copy link
Contributor Author

@Mistuke Mistuke commented on 06d5849 Mar 7, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

TeamCity Win32 Bindings for Haskell :: Win32 Continuous builds Build 32 outcome was SUCCESS
Summary: Running Build time: 00:07:41

Please sign in to comment.