diff --git c/timsort.h w/timsort.h
index 5443988..b0d5892 100644
--- c/timsort.h
+++ w/timsort.h
@@ -117,7 +117,7 @@ typedef struct {
 } TIM_SORT_RUN_T;
 
 void BINARY_INSERTION_SORT(SORT_TYPE *dst, const size_t size);
-void TIM_SORT(SORT_TYPE *dst, const size_t size);
+int TIM_SORT(SORT_TYPE *dst, const size_t size);
 
 /* Function used to do a binary search for binary insertion sort */
 static int64_t BINARY_INSERTION_FIND(SORT_TYPE *dst, const SORT_TYPE x, const size_t size)
@@ -270,7 +270,9 @@ if (curr == (int64_t) size)\
   /* finish up */ \
   while (stack_curr > 1) \
   { \
-    TIM_SORT_MERGE(dst, run_stack, stack_curr, store); \
+    int res = TIM_SORT_MERGE(dst, run_stack, stack_curr, store); \
+    if (res)\
+      return res;\
     run_stack[stack_curr - 2].length += run_stack[stack_curr - 1].length; \
     stack_curr--; \
   } \
@@ -279,7 +281,7 @@ if (curr == (int64_t) size)\
     free(store->storage);\
     store->storage = NULL;\
   }\
-  return;\
+  return 0;\
 }\
 }\
 while (0)
@@ -308,22 +310,22 @@ typedef struct {
 } TEMP_STORAGE_T;
 
 
-static void TIM_SORT_RESIZE(TEMP_STORAGE_T *store, const size_t new_size)
+static int TIM_SORT_RESIZE(TEMP_STORAGE_T *store, const size_t new_size)
 {
   if (store->alloc < new_size)
   {
     SORT_TYPE *tempstore = (SORT_TYPE *)realloc(store->storage, new_size * sizeof(SORT_TYPE));
     if (tempstore == NULL)
     {
-      fprintf(stderr, "Error allocating temporary storage for tim sort: need %lu bytes", sizeof(SORT_TYPE) * new_size);
-      exit(1);
+      return -1;
     }
     store->storage = tempstore;
     store->alloc = new_size;
   }
+  return 0;
 }
 
-static void TIM_SORT_MERGE(SORT_TYPE *dst, const TIM_SORT_RUN_T *stack, const int stack_curr, TEMP_STORAGE_T *store)
+static int TIM_SORT_MERGE(SORT_TYPE *dst, const TIM_SORT_RUN_T *stack, const int stack_curr, TEMP_STORAGE_T *store)
 {
   const int64_t A = stack[stack_curr - 2].length;
   const int64_t B = stack[stack_curr - 1].length;
@@ -331,7 +333,9 @@ static void TIM_SORT_MERGE(SORT_TYPE *dst, const TIM_SORT_RUN_T *stack, const in
   SORT_TYPE *storage;
   int64_t i, j, k;
 
-  TIM_SORT_RESIZE(store, MIN(A, B));
+  int res = TIM_SORT_RESIZE(store, MIN(A, B));
+  if (res)
+    return res;
   storage = store->storage;
 
   /* left merge */
@@ -380,6 +384,7 @@ static void TIM_SORT_MERGE(SORT_TYPE *dst, const TIM_SORT_RUN_T *stack, const in
         dst[k] = dst[j--];
     }
   }
+  return 0;
 }
 
 static int TIM_SORT_COLLAPSE(SORT_TYPE *dst, TIM_SORT_RUN_T *stack, int stack_curr, TEMP_STORAGE_T *store, const size_t size)
@@ -393,7 +398,9 @@ static int TIM_SORT_COLLAPSE(SORT_TYPE *dst, TIM_SORT_RUN_T *stack, int stack_cu
     if ((stack_curr == 2) &&
         (stack[0].length + stack[1].length == (int64_t) size))
     {
-      TIM_SORT_MERGE(dst, stack, stack_curr, store);
+      int res = TIM_SORT_MERGE(dst, stack, stack_curr, store);
+      if (res)
+        return res;
       stack[0].length += stack[1].length;
       stack_curr--;
       break;
@@ -401,7 +408,9 @@ static int TIM_SORT_COLLAPSE(SORT_TYPE *dst, TIM_SORT_RUN_T *stack, int stack_cu
     /* check if the invariant is off for a stack of 2 elements */
     else if ((stack_curr == 2) && (stack[0].length <= stack[1].length))
     {
-      TIM_SORT_MERGE(dst, stack, stack_curr, store);
+      int res = TIM_SORT_MERGE(dst, stack, stack_curr, store);
+      if (res)
+        return res;
       stack[0].length += stack[1].length;
       stack_curr--;
       break;
@@ -418,14 +427,18 @@ static int TIM_SORT_COLLAPSE(SORT_TYPE *dst, TIM_SORT_RUN_T *stack, int stack_cu
     {
       if (A < C)
       {
-        TIM_SORT_MERGE(dst, stack, stack_curr - 1, store);
+        int res = TIM_SORT_MERGE(dst, stack, stack_curr - 1, store);
+        if (res)
+          return res;
         stack[stack_curr - 3].length += stack[stack_curr - 2].length;
         stack[stack_curr - 2] = stack[stack_curr - 1];
         stack_curr--;
       }
       else
       {
-        TIM_SORT_MERGE(dst, stack, stack_curr, store);
+        int res = TIM_SORT_MERGE(dst, stack, stack_curr, store);
+        if (res)
+          return res;
         stack[stack_curr - 2].length += stack[stack_curr - 1].length;
         stack_curr--;
       }
@@ -433,7 +446,9 @@ static int TIM_SORT_COLLAPSE(SORT_TYPE *dst, TIM_SORT_RUN_T *stack, int stack_cu
     /* check second invariant */
     else if (B <= C)
     {
-      TIM_SORT_MERGE(dst, stack, stack_curr, store);
+      int res = TIM_SORT_MERGE(dst, stack, stack_curr, store);
+      if (res)
+        return res;
       stack[stack_curr - 2].length += stack[stack_curr - 1].length;
       stack_curr--;
     }
@@ -443,7 +458,7 @@ static int TIM_SORT_COLLAPSE(SORT_TYPE *dst, TIM_SORT_RUN_T *stack, int stack_cu
   return stack_curr;
 }
 
-void TIM_SORT(SORT_TYPE *dst, const size_t size)
+int TIM_SORT(SORT_TYPE *dst, const size_t size)
 {
   int minrun;
   TEMP_STORAGE_T _store, *store;
@@ -455,7 +470,7 @@ void TIM_SORT(SORT_TYPE *dst, const size_t size)
   if (size < 64)
   {
     BINARY_INSERTION_SORT(dst, size);
-    return;
+    return 0;
   }
 
   /* compute the minimum run length */
@@ -475,10 +490,13 @@ void TIM_SORT(SORT_TYPE *dst, const size_t size)
     if (!CHECK_INVARIANT(run_stack, stack_curr))
     {
       stack_curr = TIM_SORT_COLLAPSE(dst, run_stack, stack_curr, store, size);
+      if (stack_curr < 0)
+        return stack_curr;
       continue;
     }
     PUSH_NEXT();
   }
+  return 0;
 }
 
 #undef SORT_CONCAT
diff --git c/xpath.c w/xpath.c
index 1f08d85..f4030dc 100644
--- c/xpath.c
+++ w/xpath.c
@@ -3426,6 +3426,8 @@ xmlXPathNodeSetSort(xmlNodeSetPtr set) {
 #ifndef WITH_TIM_SORT
     int i, j, incr, len;
     xmlNodePtr tmp;
+#else
+    int res;
 #endif
 
     if (set == NULL)
@@ -3459,7 +3461,12 @@ xmlXPathNodeSetSort(xmlNodeSetPtr set) {
 	}
     }
 #else /* WITH_TIM_SORT */
-    libxml_domnode_tim_sort(set->nodeTab, set->nodeNr);
+    res = libxml_domnode_tim_sort(set->nodeTab, set->nodeNr);
+    if (res)
+    {
+        fprintf(stderr, "Error allocating temporary storage for tim sort");
+        exit(1);
+    }
 #endif /* WITH_TIM_SORT */
 }
 
