diff --git a/newlib/doc/makedoc.c b/newlib/doc/makedoc.c
index 966349c51..81aa6f941 100644
--- a/newlib/doc/makedoc.c
+++ b/newlib/doc/makedoc.c
@@ -135,6 +135,11 @@ catchar (string_type *buffer, char ch)
   {
     buffer->size *=2;
     buffer->ptr = realloc(buffer->ptr, buffer->size);
+    if (!buffer->ptr)
+    {
+      fprintf(stderr,"Can't allocate memory\n");
+      exit(1);
+    }
   }
 
   buffer->ptr[buffer->write_idx ++ ] = ch;