diff --git a/ChangeLog.md b/ChangeLog.md
--- a/ChangeLog.md
+++ b/ChangeLog.md
@@ -1,3 +1,9 @@
+# ChangeLog for streaming-commons
+
+## 0.2.1.1
+
+* Fix a failing test case (invalid `ByteString` copying), does not affect library itself
+
 ## 0.2.1.0
 
 * Change `bindRandomPortGen` to use binding to port 0
diff --git a/Data/Streaming/Network.hs b/Data/Streaming/Network.hs
--- a/Data/Streaming/Network.hs
+++ b/Data/Streaming/Network.hs
@@ -200,9 +200,6 @@
 
 -- | Bind to a random port number. Especially useful for writing network tests.
 --
--- This will attempt 30 different port numbers before giving up and throwing an
--- exception.
---
 -- Since 0.1.1
 bindRandomPortGen :: SocketType -> HostPreference -> IO (Int, Socket)
 bindRandomPortGen sockettype s = do
diff --git a/cbits/text-helper.c b/cbits/text-helper.c
--- a/cbits/text-helper.c
+++ b/cbits/text-helper.c
@@ -14,13 +14,17 @@
 void _hs_streaming_commons_memcpy(void *dest, size_t doff, const void *src, size_t soff,
 		     size_t n)
 {
-  memcpy(dest + (doff<<1), src + (soff<<1), n<<1);
+  char *cdest = dest;
+  const char *csrc = src;
+  memcpy(cdest + (doff<<1), csrc + (soff<<1), n<<1);
 }
 
 int _hs_streaming_commons_memcmp(const void *a, size_t aoff, const void *b, size_t boff,
 		    size_t n)
 {
-  return memcmp(a + (aoff<<1), b + (boff<<1), n<<1);
+  const char *ca = a;
+  const char *cb = b;
+  return memcmp(ca + (aoff<<1), cb + (boff<<1), n<<1);
 }
 
 #define UTF8_ACCEPT 0
@@ -68,8 +72,8 @@
  * an UTF16 array
  */
 void
-_hs_streaming_commons_decode_latin1(uint16_t *dest, const uint8_t const *src,
-                       const uint8_t const *srcend)
+_hs_streaming_commons_decode_latin1(uint16_t *dest, const uint8_t *src,
+                       const uint8_t *srcend)
 {
   const uint8_t *p = src;
 
@@ -130,14 +134,14 @@
 #if defined(__GNUC__) || defined(__clang__)
 static inline uint8_t const *
 _hs_streaming_commons_decode_utf8_int(uint16_t *const dest, size_t *destoff,
-			 const uint8_t const **src, const uint8_t const *srcend,
+			 const uint8_t **src, const uint8_t *srcend,
 			 uint32_t *codepoint0, uint32_t *state0)
   __attribute((always_inline));
 #endif
 
 static inline uint8_t const *
 _hs_streaming_commons_decode_utf8_int(uint16_t *const dest, size_t *destoff,
-			 const uint8_t const **src, const uint8_t const *srcend,
+			 const uint8_t **src, const uint8_t *srcend,
 			 uint32_t *codepoint0, uint32_t *state0)
 {
   uint16_t *d = dest + *destoff;
@@ -158,7 +162,7 @@
 
     if (state == UTF8_ACCEPT) {
       while (s < srcend - 4) {
-	codepoint = *((uint32_t *) s);
+	codepoint = *((const uint32_t *) s);
 	if ((codepoint & 0x80808080) != 0)
 	  break;
 	s += 4;
@@ -202,8 +206,8 @@
 
 uint8_t const *
 _hs_streaming_commons_decode_utf8_state(uint16_t *const dest, size_t *destoff,
-                           const uint8_t const **src,
-			   const uint8_t const *srcend,
+                           const uint8_t **src,
+			   const uint8_t *srcend,
                            uint32_t *codepoint0, uint32_t *state0)
 {
   uint8_t const *ret = _hs_streaming_commons_decode_utf8_int(dest, destoff, src, srcend,
@@ -243,7 +247,7 @@
  ascii:
 #if defined(__x86_64__)
   while (srcend - src >= 4) {
-    uint64_t w = *((uint64_t *) src);
+    uint64_t w = *((const uint64_t *) src);
 
     if (w & 0xFF80FF80FF80FF80ULL) {
       if (!(w & 0x000000000000FF80ULL)) {
diff --git a/cbits/zlib-helper.c b/cbits/zlib-helper.c
--- a/cbits/zlib-helper.c
+++ b/cbits/zlib-helper.c
@@ -27,14 +27,14 @@
 	return deflateInit2(stream, level, Z_DEFLATED, methodBits, memlevel, strategy);
 }
 
-int streaming_commons_inflate_set_dictionary(z_stream *stream, const char* dictionary, 
+int streaming_commons_inflate_set_dictionary(z_stream *stream, const char* dictionary,
                             unsigned int dictLength) {
-        return inflateSetDictionary(stream, dictionary, dictLength);
+        return inflateSetDictionary(stream, (const Bytef *)dictionary, dictLength);
 }
 
-int streaming_commons_deflate_set_dictionary(z_stream *stream, const char* dictionary, 
+int streaming_commons_deflate_set_dictionary(z_stream *stream, const char* dictionary,
                             unsigned int dictLength) {
-        return deflateSetDictionary(stream, dictionary, dictLength);
+        return deflateSetDictionary(stream, (const Bytef *)dictionary, dictLength);
 }
 
 void streaming_commons_free_z_stream_inflate (z_stream *stream)
@@ -45,13 +45,13 @@
 
 void streaming_commons_set_avail_in (z_stream *stream, char *buff, unsigned int avail)
 {
-	stream->next_in = buff;
+	stream->next_in = (Bytef *)buff;
 	stream->avail_in = avail;
 }
 
 void streaming_commons_set_avail_out (z_stream *stream, char *buff, unsigned int avail)
 {
-	stream->next_out = buff;
+	stream->next_out = (Bytef *)buff;
 	stream->avail_out = avail;
 }
 
@@ -72,7 +72,7 @@
 
 char* streaming_commons_get_next_in (z_stream *stream)
 {
-	return stream->next_in;
+	return (char *)stream->next_in;
 }
 
 void streaming_commons_free_z_stream_deflate (z_stream *stream)
diff --git a/streaming-commons.cabal b/streaming-commons.cabal
--- a/streaming-commons.cabal
+++ b/streaming-commons.cabal
@@ -1,5 +1,5 @@
 name:                streaming-commons
-version:             0.2.1.0
+version:             0.2.1.1
 synopsis:            Common lower-level functions needed by various streaming data libraries
 description:         Provides low-dependency functionality commonly needed by various streaming data libraries, such as conduit and pipes.
 homepage:            https://github.com/fpco/streaming-commons
diff --git a/test/Data/Streaming/ByteString/BuilderSpec.hs b/test/Data/Streaming/ByteString/BuilderSpec.hs
--- a/test/Data/Streaming/ByteString/BuilderSpec.hs
+++ b/test/Data/Streaming/ByteString/BuilderSpec.hs
@@ -7,7 +7,6 @@
 
 import qualified Data.ByteString as S
 import Data.ByteString.Char8 ()
-import qualified Data.ByteString.Unsafe as S
 import qualified Data.ByteString.Builder as B
 import Data.ByteString.Builder (Builder)
 import qualified Data.ByteString.Builder.Internal as B
@@ -107,12 +106,20 @@
 
         builderSpec
 
-        prop "toByteStringIO idempotent to toLazyByteString" $ \bss' -> do
+        let prop_idempotent i bss' = do
             let bss = mconcat (map (B.byteString . S.pack) bss')
             ior <- newIORef []
             toByteStringIOWith 16
-                               (\s -> do s' <- S.useAsCStringLen s S.unsafePackCStringLen
-                                         modifyIORef ior (s' :))
+                               (\s -> do let s' = S.copy s
+                                         s' `seq` modifyIORef ior (s' :))
                                bss
             chunks <- readIORef ior
-            L.fromChunks (reverse chunks) `shouldBe` B.toLazyByteString bss
+            let have = L.unpack (L.fromChunks (reverse chunks))
+                want = L.unpack (B.toLazyByteString bss)
+            (i, have) `shouldBe` (i, want)
+
+        prop "toByteStringIO idempotent to toLazyByteString" (prop_idempotent (0::Int))
+
+        it "toByteStringIO idempotent to toLazyBytestring, specific case" $ do
+            let bss' = replicate 10 [0..255]
+            mapM_ (\i -> prop_idempotent i bss') [(1::Int)..100]
