diff --git a/src/api.c b/src/api.c
index 2e633180df14a1245628a9fc9b2bb7711b59cfd0_c3JjL2FwaS5j..633b17bd65f41398b755204c7b1b51e3deaf86e2_c3JjL2FwaS5j 100644
--- a/src/api.c
+++ b/src/api.c
@@ -117,7 +117,12 @@
 YAML_DECLARE(int)
 yaml_stack_extend(void **start, void **top, void **end)
 {
-    void *new_start = yaml_realloc(*start, ((char *)*end - (char *)*start)*2);
+    void *new_start;
+
+    if ((char *)*end - (char *)*start >= INT_MAX / 2)
+	return 0;
+
+    new_start = yaml_realloc(*start, ((char *)*end - (char *)*start)*2);
 
     if (!new_start) return 0;