check malloc return value
diff --git a/cs.c b/cs.c
index 04bc615..1d1634d 100644
--- a/cs.c
+++ b/cs.c
@@ -447,6 +447,12 @@
 
 	total_size = sizeof(cs_insn) * cache_size;
 	total = cs_mem_malloc(total_size);
+	if (total == NULL)
+	{
+		//malloc failed
+		handle->errnum = CS_ERR_MEM;
+		return 0;
+	}
 	insn_cache = total;
 
 	while (size > 0) {