--- camel/camel-folder-thread.c.orig	2016-10-22 16:10:28.000000000 -0400
+++ camel/camel-folder-thread.c	2016-10-22 16:34:17.000000000 -0400
@@ -425,12 +425,15 @@
 		return 1;
 }
 
+#define MAX_ALLOCA_CHILDREN 1024
+
 static void
 sort_thread (CamelFolderThreadNode **cp)
 {
 	CamelFolderThreadNode *c, *head, **carray;
 	gint size = 0;
-
+	gboolean need_to_free_carray = FALSE;
+	
 	c = *cp;
 	while (c) {
 		/* sort the children while we're at it */
@@ -441,7 +444,12 @@
 	}
 	if (size < 2)
 		return;
-	carray = alloca (size * sizeof (CamelFolderThreadNode *));
+	if (size > MAX_ALLOCA_CHILDREN) {
+		carray = g_new (CamelFolderThreadNode *, size);
+		need_to_free_carray = TRUE;
+	} else {
+		carray = alloca (size * sizeof (CamelFolderThreadNode *));
+	}
 	c = *cp;
 	size = 0;
 	while (c) {
@@ -461,6 +469,9 @@
 		size--;
 	} while (size >= 0);
 	*cp = head;
+	if (need_to_free_carray) {
+		g_free (carray);
+	}
 }
 
 static guint
