Opened 3 years ago

Last modified 3 months ago

#7161 infoneeded bug

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

Reported by: duncan Owned by: ekmett
Priority: normal Milestone: 7.12.1
Component: Core Libraries Version: 7.6.1-rc1
Keywords: Cc: core-libraries-committee@…
Operating System: Unknown/Multiple Architecture: Unknown/Multiple
Type of failure: None/Unknown Test Case:
Blocked By: Blocking:
Related Tickets: Differential Revisions:


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 (5)

comment:1 Changed 2 years 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.

comment:2 Changed 11 months ago by thoughtpolice

  • Milestone changed from 7.8.3 to 7.10.1

Moving to 7.10.1

comment:3 Changed 6 months ago by thoughtpolice

  • Component changed from libraries/base to Core Libraries
  • Owner set to ekmett

Moving over to new owning component 'Core Libraries'.

comment:4 Changed 4 months ago by thomie

  • Cc core-libraries-committee@… added
  • Status changed from new to infoneeded

comment:5 Changed 3 months ago by thoughtpolice

  • Milestone changed from 7.10.1 to 7.12.1

Moving to 7.12.1 milestone; if you feel this is an error and should be addressed sooner, please move it back to the 7.10.1 milestone.

Note: See TracTickets for help on using tickets.