diff --git a/lib/http_ntlm.c b/lib/http_ntlm.c
index 3e993cbf50fa273996204c30a40df24f93b31fc7..a64f61170144c8661b6aa24de0d630a42c6ce242 100644
--- a/lib/http_ntlm.c
+++ b/lib/http_ntlm.c
@@ -713,6 +713,13 @@ CURLcode Curl_output_ntlm(struct connectdata *conn,
     size=64;
     ntlmbuf[62]=ntlmbuf[63]=0;
 
+    /* Make sure that the user and domain strings fit in the target buffer
+       before we copy them there. */
+    if(size + userlen + domlen >= sizeof(ntlmbuf)) {
+      failf(conn->data, "user + domain name too big");
+      return CURLE_OUT_OF_MEMORY;
+    }
+
     memcpy(&ntlmbuf[size], domain, domlen);
     size += domlen;