[Haskell-cafe] ANNOUNCE: Important bug fix for regex-pcre ByteStrings.

ChrisK haskell at list.mightyreason.com
Tue Nov 20 11:15:00 EST 2007


Greetings,

There are new version 0.82 and 0.93 of regex-posix.  If you use regex-posix with
Data.ByteString then you should upgrade to obtain a fix for a crash error.

There are new version of regex-pcre available on hackage and the two darcs
repositories:
http://darcs.haskell.org/packages/regex-pcre/ for "stable" version 0.82
and
http://darcs.haskell.org/packages/regex-unstable/regex-pcre/ for "unstable"
version 0.93

The new version regex-pcre-0.82 replaces the broken version 0.81
The new version regex-pcre-0.93 replaces the broken version 0.92

I apologize for the error.  I took the address of the ByteString to give to the
pcre library by using unsafeUseAsCStringLen.  I had not appreciated that this
can return a null pointer for an empty ByteString.  My code caught this null
pointer and reported it, however, and so a good error message sent in by a user
allowed me to quickly find and hopefully fix it.

For version 0.92 the error also applied to Lazy ByteStrings.

In case anyone is curious, the fixed code checks the pointer returned by
unsafeUseAsCStringLen and replaces a null pointer to a non-null pointer to a
private private bytestring "myEmpty" with the length set to 0 by "trim":

> {-# INLINE asCStringLen #-}
> asCStringLen :: ByteString -> (CStringLen -> IO a) -> IO a
> asCStringLen s op = B.unsafeUseAsCStringLen s checked
>   where checked cs@(ptr,_) | ptr == nullPtr = B.unsafeUseAsCStringLen myEmpty (op . trim)
>                            | otherwise = op cs
>         myEmpty = B.pack [0]
>         trim (ptr,_) = (ptr,0)

This fix is motivated by desiring to ensure asCStringLen never passes a null
pointer to "op" and that no memory copy operations are used.

The safe "useAsCString(Len)" do not appear capable of returning a null pointer.
Only "unsafeUseAsCString(Len)" on an empty bytestring might return a null pointer.

-- 
Chris



More information about the Haskell-Cafe mailing list