Code breaks with '-O', any ideas.

David Brown haskell2 at davidb.org
Mon Mar 5 16:21:42 EST 2007


David Brown wrote:
> Ian Lynagh wrote:
>> On Mon, Mar 05, 2007 at 10:51:46AM -0800, David Brown wrote:
>>> My program 'harchive' <http://www.davidb.org/darcs/harchive/> breaks
>>> if compiled with '-O'.  Just wondering if anyone has suggestions on
>>> how to figure out what might be causing this.
>> What do you mean by "breaks"?
>
> I'm now rebuilding individual parts with -O to see if I can isolate
> the problem(s).

Well, that helped.  I found the problem.  I had something like:

   import qualified Data.ByteString as B
 
   let hash = B.pack $ replicate (#const SHA_DIGEST_LENGTH) 0
   ...
   B.useAsCStringLen hash $ \ (hdata, _) -> do
     c_sha1Final hash ctx
   return hash

But this seems to be causing the hash to be garbage, and possibly have
c_sha1Final write a 20-byte hash into some garbled piece of memory.

Ireplaced this with

   let hashLen = (#const SHA_DIGEST_LENGTH)
   hashData <- mallocForeignPtrBytes hashLen
   withForeignPtr hashData $ \hashP -> do
      c_sha1Final hashP ctx
      B.copyCStringLen (hashP, hashLen)

and the code works both with and without optimization.

Dave



More information about the Glasgow-haskell-users mailing list