Opened 20 months ago

Last modified 18 months ago

#7161 new bug

hSetNewlineMode and hSetEncoding can be performed on closed and semi-closed handles

Reported by: duncan Owned by:
Priority: normal Milestone: 7.8.3
Component: libraries/base Version: 7.6.1-rc1
Keywords: Cc:
Operating System: Unknown/Multiple Architecture: Unknown/Multiple
Type of failure: None/Unknown Difficulty: Unknown
Test Case: Blocked By:
Blocking: Related Tickets:


The hSetNewlineMode and hSetEncoding functions from GHC/IO/Handle.hs do not check that the Handle is in an open mode. It is possible to use them on closed handles. hSetEncoding on a closed Handle triggers a segfault. Similarly, the operations are also both possible on semi-closed handles, and given the way hGetContents is implemented, this will affect the result of hGetContents which is clearly against the intention of the hGetContents/semi-closed stuff.

Both functions use the withAllHandles__ helper. Unlike similar helpers like wantReadableHandle_ this one doesn't do any handle mode checking.

Additionally, hSetBuffering and hSetBinary mode also use the withAllHandles__ pattern and don't obviously check for an open handle but I've not verified this.

Change History (1)

comment:1 Changed 18 months ago by igloo

  • Difficulty set to Unknown
  • Milestone set to 7.8.1

Can you give us a way to reproduce the segfault please? I tried:

module Main (main) where

import System.IO

main :: IO ()
main = do h <- openFile "tmp" WriteMode
          hPutStrLn h "Test"
          hClose h
          hSetEncoding h utf8
          hSetEncoding h utf32
          hSetEncoding h utf8
          print h
$ ghc --make q
$ ./q
{handle: tmp}

but got no segfault.

Note: See TracTickets for help on using tickets.