Fix memory leaks and alignment issues (thanks rixed at happyleptic.org)
[ocaml-bitstring.git] / bitstring_c.c
index 5570f05..b9ad109 100644 (file)
@@ -29,6 +29,7 @@
 #include <stdlib.h>
 #include <stdint.h>
 #include <byteswap.h>
+#include <string.h>
 
 #include <caml/mlvalues.h>
 #include <caml/fail.h>
@@ -74,7 +75,7 @@
   {                                                                    \
     type *ptr = (type *) ((void *) String_val (strv) + Int_val (offv));        \
     type r;                                                            \
-    r = *ptr;                                                          \
+    memcpy(&r, ptr, sizeof(r));                                        \
     swap_##endian(size,r);                                             \
     return Val_int (r);                                                        \
   }
@@ -86,8 +87,6 @@ fastpath1(16,be,signed,int16_t)
 fastpath1(16,le,signed,int16_t)
 fastpath1(16,ne,signed,int16_t)
 
-/* XXX This probably doesn't work on ARCH_ALIGN_INT64 platforms. */
-
 #define fastpath2(size,endian,signed,type,rval)                                \
   CAMLprim value                                                       \
   ocaml_bitstring_extract_fastpath_int##size##_##endian##_##signed     \
@@ -95,7 +94,7 @@ fastpath1(16,ne,signed,int16_t)
   {                                                                    \
     type *ptr = (type *) ((void *) String_val (strv) + Int_val (offv));        \
     type r;                                                            \
-    r = *ptr;                                                          \
+    memcpy(&r, ptr, sizeof(r));                                        \
     swap_##endian(size,r);                                             \
     rval(rv) = r;                                                      \
     return rv;                                                         \
@@ -108,9 +107,32 @@ fastpath2(32,be,signed,int32_t,Int32_val)
 fastpath2(32,le,signed,int32_t,Int32_val)
 fastpath2(32,ne,signed,int32_t,Int32_val)
 
-fastpath2(64,be,unsigned,uint64_t,Int64_val)
-fastpath2(64,le,unsigned,uint64_t,Int64_val)
-fastpath2(64,ne,unsigned,uint64_t,Int64_val)
-fastpath2(64,be,signed,int64_t,Int64_val)
-fastpath2(64,le,signed,int64_t,Int64_val)
-fastpath2(64,ne,signed,int64_t,Int64_val)
+/* Special care needs to be taken on ARCH_ALIGN_INT64 platforms
+   (hppa and sparc in Debian). */
+
+#ifdef ARCH_ALIGN_INT64
+#include <caml/memory.h>
+#include <caml/alloc.h>
+#define fastpath3(size,endian,signed,type,rval)                                \
+  CAMLprim value                                                       \
+  ocaml_bitstring_extract_fastpath_int##size##_##endian##_##signed     \
+  (value strv, value offv, value rv)                                   \
+  {                                                                    \
+    type *ptr = (type *) ((void *) String_val (strv) + Int_val (offv));        \
+    type r;                                                            \
+    memcpy(&r, ptr, sizeof(r));                                        \
+    swap_##endian(size,r);                                             \
+    memcpy(Data_custom_val(rv), &r, sizeof(r));                        \
+    return rv;                                                         \
+  }
+
+#else
+#define fastpath3 fastpath2
+#endif
+
+fastpath3(64,be,unsigned,uint64_t,Int64_val)
+fastpath3(64,le,unsigned,uint64_t,Int64_val)
+fastpath3(64,ne,unsigned,uint64_t,Int64_val)
+fastpath3(64,be,signed,int64_t,Int64_val)
+fastpath3(64,le,signed,int64_t,Int64_val)
+fastpath3(64,ne,signed,int64_t,Int64_val)