Add missing checks. Fix crash on failed allocation.
[openjpeg.git] / src / lib / openjp2 / opj_malloc.c
index a170d8527c475244a044a5b706b83ee677aec947..66ce0316b7a75706b0276ba51cd60a5d1de5827d 100644 (file)
@@ -5,6 +5,7 @@
  * are granted under this license.
  *
  * Copyright (c) 2015, Mathieu Malaterre <mathieu.malaterre@gmail.com>
+ * Copyright (c) 2015, Matthieu Darbois
  * All rights reserved.
  *
  * Redistribution and use in source and binary forms, with or without
  */
 #define OPJ_SKIP_POISON
 #include "opj_malloc.h"
+#include "opj_config_private.h"
 #include <stdlib.h>
+#include <string.h>
+#include <inttypes.h>
+#include <assert.h>
 
-static inline void *opj_aligned_alloc(size_t alignment, size_t size)
+static inline void *opj_aligned_alloc_n(size_t alignment, size_t size)
 {
-#ifndef HAVE_ALIGNED_ALLOC
-  /* older linux */
-#ifdef HAVE_MEMALIGN
-  assert( size % alignment == 0 );
-  return memalign( alignment, size );
-#endif /* HAVE_MEMALIGN */
+  void* ptr;
 
-/* _MSC_VER */
-#ifdef HAVE__ALIGNED_MALLOC
-  return _aligned_malloc( alignment, size );
-#endif /* HAVE__ALIGNED_MALLOC */
+  /* alignment shall be power of 2 */
+  assert( (alignment != 0U) && ((alignment & (alignment - 1U)) == 0U));
 
-/* MacOSX / clang */
-#if defined(HAVE_POSIX_MEMALIGN) && !defined(HAVE_MEMALIGN)
-  void* ptr;
+#if defined(HAVE_POSIX_MEMALIGN)
+  /* aligned_alloc requires c11, restrict to posix_memalign for now. Quote:
+   * This function was introduced in POSIX 1003.1d. Although this function is
+   * superseded by aligned_alloc, it is more portable to older POSIX systems
+   * that do not support ISO C11.  */
   if (posix_memalign (&ptr, alignment, size))
   {
     ptr = NULL;
   }
+  /* older linux */
+#elif defined(HAVE_MEMALIGN)
+  ptr = memalign( alignment, size );
+/* _MSC_VER */
+#elif defined(HAVE__ALIGNED_MALLOC)
+  ptr = _aligned_malloc( alignment, size );
+#else
+/* TODO: _mm_malloc(x,y) */
+#error missing aligned alloc function
+#endif
   return ptr;
-#endif /* HAVE_POSIX_MEMALIGN */
+}
+static inline void *opj_aligned_realloc_n(void *ptr, size_t alignment, size_t size)
+{
+  void *r_ptr;
+
+  /* alignment shall be power of 2 */
+  assert( (alignment != 0U) && ((alignment & (alignment - 1U)) == 0U));
 
-#else /* HAVE_ALIGNED_ALLOC */
-  return aligned_alloc( alignment, size );
-#endif /* HAVE_ALIGNED_ALLOC */
+/* no portable aligned realloc */
+#if defined(HAVE_POSIX_MEMALIGN) || defined(HAVE_MEMALIGN)
+  /* glibc doc states one can mixed aligned malloc with realloc */
+  r_ptr = realloc( ptr, size ); /* fast path */
+  /* we simply use `size_t` to cast, since we are only interest in binary AND
+   * operator */
+  if( ((size_t)r_ptr & (alignment - 1U)) != 0U ) {
+    /* this is non-trivial to implement a portable aligned realloc, so use a
+     * simple approach where we do not need a function that return the size of an
+     * allocated array (eg. _msize on Windows, malloc_size on MacOS,
+     * malloc_usable_size on systems with glibc) */
+    void *a_ptr = opj_aligned_alloc_n(alignment, size);
+    if (a_ptr != NULL) {
+      memcpy(a_ptr, r_ptr, size);
+    }
+    free( r_ptr );
+    r_ptr = a_ptr;
+  }
+/* _MSC_VER */
+#elif defined(HAVE__ALIGNED_MALLOC)
+  r_ptr = _aligned_realloc( ptr, size, alignment );
+#else
 /* TODO: _mm_malloc(x,y) */
+#error missing aligned realloc function
+#endif
+       return r_ptr;
 }
-
 void * opj_malloc(size_t size)
 {
   return malloc(size);
 }
-
 void * opj_calloc(size_t numOfElements, size_t sizeOfElements)
 {
   return calloc(numOfElements, sizeOfElements);
@@ -74,7 +110,11 @@ void * opj_calloc(size_t numOfElements, size_t sizeOfElements)
 
 void *opj_aligned_malloc(size_t size)
 {
-  return opj_aligned_alloc(size,16);
+  return opj_aligned_alloc_n(16u,size);
+}
+void * opj_aligned_realloc(void *ptr, size_t size)
+{
+  return opj_aligned_realloc_n(ptr,16u,size);
 }
 
 void opj_aligned_free(void* ptr)