diff --git a/Text/Regex/Posix.hs b/Text/Regex/Posix.hs
--- a/Text/Regex/Posix.hs
+++ b/Text/Regex/Posix.hs
@@ -76,6 +76,6 @@
 
 getVersion_Text_Regex_Posix :: Version
 getVersion_Text_Regex_Posix =
-  Version { versionBranch = [0,91]
+  Version { versionBranch = [0,92]
           , versionTags = ["unstable"]
           }
diff --git a/Text/Regex/Posix/ByteString.hs b/Text/Regex/Posix/ByteString.hs
--- a/Text/Regex/Posix/ByteString.hs
+++ b/Text/Regex/Posix/ByteString.hs
@@ -53,12 +53,13 @@
 
 import Data.Array(Array,listArray)
 import Data.ByteString(ByteString)
-import qualified Data.ByteString as B(empty,useAsCString,last,take,drop)
+import qualified Data.ByteString as B(empty,useAsCString,last,take,drop,null)
 import qualified Data.ByteString.Base as B(unsafeUseAsCString)
 import System.IO.Unsafe(unsafePerformIO)
 import Text.Regex.Base.RegexLike(RegexMaker(..),RegexContext(..),RegexLike(..),MatchOffset,MatchLength)
 import Text.Regex.Posix.Wrap -- all
 import Text.Regex.Base.Impl(polymatch,polymatchM)
+import Foreign.C.String(CString)
 
 instance RegexContext Regex ByteString ByteString where
   match = polymatch
@@ -76,22 +77,13 @@
 
 instance RegexLike Regex ByteString where
   matchTest regex bs = unsafePerformIO $
-    let asCString = if (0==B.last bs)
-                      then B.unsafeUseAsCString
-                      else B.useAsCString
-    in asCString bs (wrapTest regex) >>=  unwrap
+    asCString bs (wrapTest regex) >>=  unwrap
   matchOnce regex bs = unsafePerformIO $ 
     execute regex bs >>= unwrap
   matchAll regex bs = unsafePerformIO $
-    let asCString = if (0==B.last bs)
-                      then B.unsafeUseAsCString
-                      else B.useAsCString
-    in asCString bs (wrapMatchAll regex) >>= unwrap
+    asCString bs (wrapMatchAll regex) >>= unwrap
   matchCount regex bs = unsafePerformIO $
-    let asCString = if (0==B.last bs)
-                      then B.unsafeUseAsCString
-                      else B.useAsCString
-    in asCString bs (wrapCount regex) >>= unwrap
+    asCString bs (wrapCount regex) >>= unwrap
 
 -- ---------------------------------------------------------------------
 -- | Compiles a regular expression
@@ -101,10 +93,7 @@
         -> ByteString  -- ^ The regular expression to compile
         -> IO (Either WrapError Regex)      -- ^ Returns: the compiled regular expression
 compile c e pattern =
-  let asCString = if (0==B.last pattern)
-                    then B.unsafeUseAsCString
-                    else B.useAsCString
-  in asCString pattern (wrapCompile c e)
+  asCString pattern (wrapCompile c e)
 
 
 -- ---------------------------------------------------------------------
@@ -119,9 +108,6 @@
                 -- string, or:
                 --   'Just' an array of (offset,length) pairs where index 0 is whole match, and the rest are the captured subexpressions.
 execute regex bs = do
-  let asCString = if (0==B.last bs)
-                     then B.unsafeUseAsCString
-                     else B.useAsCString
   maybeStartEnd <- asCString bs (wrapMatch regex)
   case maybeStartEnd of
     Right Nothing -> return (Right Nothing)
@@ -135,9 +121,6 @@
         -> ByteString -- ^ String to match against
         -> IO (Either WrapError (Maybe (ByteString, ByteString, ByteString, [ByteString])))
 regexec regex bs = do
-  let asCString = if (0==B.last bs)
-                    then B.unsafeUseAsCString
-                    else B.useAsCString
   let getSub (start,stop) | start == unusedRegOffset = B.empty
                           | otherwise = B.take (fi (stop-start)) . B.drop (fi start) $ bs
       matchedParts [] = (B.empty,B.empty,bs,[]) -- no information
@@ -158,3 +141,8 @@
 
 fi :: (Integral i,Num n) => i->n
 fi = fromIntegral
+
+asCString :: ByteString -> (CString -> IO a) -> IO a
+asCString bs = if (not (B.null bs)) && (0==B.last bs)
+                  then B.unsafeUseAsCString bs
+                  else B.useAsCString bs
diff --git a/Text/Regex/Posix/ByteString/Lazy.hs b/Text/Regex/Posix/ByteString/Lazy.hs
--- a/Text/Regex/Posix/ByteString/Lazy.hs
+++ b/Text/Regex/Posix/ByteString/Lazy.hs
@@ -53,7 +53,7 @@
   ) where
 
 import Data.Array(Array)
-import qualified Data.ByteString.Lazy as L (ByteString,toChunks,fromChunks,last,snoc)
+import qualified Data.ByteString.Lazy as L (ByteString,null,toChunks,fromChunks,last,snoc)
 import qualified Data.ByteString as B(ByteString,concat)
 import qualified Data.ByteString.Base as B(unsafeUseAsCString)
 import System.IO.Unsafe(unsafePerformIO)
@@ -79,7 +79,7 @@
 
 {-# INLINE asCString #-}
 asCString :: L.ByteString -> (CString -> IO a) -> IO a
-asCString s = if (0==L.last s)
+asCString s = if (not (L.null s)) && (0==L.last s)
                 then B.unsafeUseAsCString (fromLazy s)
                 else B.unsafeUseAsCString (fromLazy (L.snoc s 0))
 
@@ -117,7 +117,7 @@
                 -- ^ Returns: 'Nothing' if the regex did not match the
                 -- string, or:
                 --   'Just' an array of (offset,length) pairs where index 0 is whole match, and the rest are the captured subexpressions.
-execute regex bs = if (0==L.last bs)
+execute regex bs = if (not (L.null bs)) && (0==L.last bs)
                      then BS.execute regex (fromLazy bs)
                      else BS.execute regex (fromLazy (L.snoc bs 0))
 
@@ -125,7 +125,7 @@
         -> L.ByteString -- ^ String to match against
         -> IO (Either WrapError (Maybe (L.ByteString, L.ByteString, L.ByteString, [L.ByteString])))
 regexec regex bs = do
-  x <- if (0==L.last bs)
+  x <- if (not (L.null bs)) && (0==L.last bs)
          then BS.regexec regex (fromLazy bs)
          else BS.regexec regex (fromLazy (L.snoc bs 0))
   return $ case x of
diff --git a/regex-posix.cabal b/regex-posix.cabal
--- a/regex-posix.cabal
+++ b/regex-posix.cabal
@@ -1,5 +1,5 @@
 Name:                   regex-posix
-Version:                0.91
+Version:                0.92
 -- Cabal-Version:       >=1.1.4
 License:                BSD3
 License-File:           LICENSE
