leave chain in consistent state on errors
diff --git a/src/core/ngx_output_chain.c b/src/core/ngx_output_chain.c
index ee23e8f..6d6d241 100644
--- a/src/core/ngx_output_chain.c
+++ b/src/core/ngx_output_chain.c
@@ -314,12 +314,11 @@
 
 #endif
 
+        cl->next = NULL;
         *ll = cl;
         ll = &cl->next;
     }
 
-    *ll = NULL;
-
     return NGX_OK;
 }