From a03714e25d9205790bad4ef32f546c626f3a5c18 Mon Sep 17 00:00:00 2001 From: simonmar Date: Tue, 12 Mar 2002 15:28:25 +0000 Subject: [PATCH] [project @ 2002-03-12 15:28:25 by simonmar] Add a magic number to the beginning of interface files (0x1face :-) to avoid trying to read old text-style interfaces as binary (the usual result is an attempt to allocate a ByteArray larger than the available memory size and an obscure crash). --- ghc/compiler/utils/Binary.hs | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/ghc/compiler/utils/Binary.hs b/ghc/compiler/utils/Binary.hs index cea5c0f..bf0cf2c 100644 --- a/ghc/compiler/utils/Binary.hs +++ b/ghc/compiler/utils/Binary.hs @@ -53,6 +53,7 @@ module Binary import {-# SOURCE #-} Module import FastString import Unique +import Panic import UniqFM #if __GLASGOW_HASKELL__ < 503 @@ -620,6 +621,10 @@ undef = error "Binary.BinHandleState" getBinFileWithDict :: Binary a => FilePath -> IO a getBinFileWithDict file_path = do bh <- Binary.readBinMem file_path + magic <- get bh + when (magic /= binaryInterfaceMagic) $ + throwDyn (ProgramError ( + "magic number mismatch: old/corrupt interface file?")) dict_p <- Binary.get bh -- get the dictionary ptr data_p <- tellBin bh seekBin bh dict_p @@ -630,9 +635,12 @@ getBinFileWithDict file_path = do initBinMemSize = (1024*1024) :: Int +binaryInterfaceMagic = 0x1face :: Word32 + putBinFileWithDict :: Binary a => FilePath -> Module -> a -> IO () putBinFileWithDict file_path mod a = do bh <- openBinMem initBinMemSize mod + put_ bh binaryInterfaceMagic p <- tellBin bh put_ bh p -- placeholder for ptr to dictionary put_ bh a -- 1.7.10.4