Code breaks with '-O', any ideas.
Donald Bruce Stewart
dons at cse.unsw.edu.au
Mon Mar 5 19:29:08 EST 2007
haskell2:
> 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
Ah, this is my fault! The old api used 'useAsCSTringLen' unsafely. Its a
zero-copying version. The default is now copyCStringLen. See this thread
for the issues involved:
http://www.haskell.org/pipermail/libraries/2007-January/006783.html
and for how it has been resolved:
http://www.haskell.org/pipermail/libraries/2007-January/006801.html
So yes, using 'copyCStringlen' is the appropriate thing to do, if you
don't want to guarantee the C side of things.
-- Don
More information about the Glasgow-haskell-users
mailing list