diff --git i/HTMLparser.c w/HTMLparser.c
index 1d6f321..bf9426e 100644
--- i/HTMLparser.c
+++ w/HTMLparser.c
@@ -4,6 +4,8 @@
  * See Copyright for the status of this software.
  *
  * daniel@veillard.com
+ *
+ * Portions Copyright (c) 2013 Apple Inc.  All rights reserved.
  */
 
 #define IN_LIBXML
@@ -29,6 +31,12 @@
 #ifdef HAVE_ZLIB_H
 #include <zlib.h>
 #endif
+#ifdef HAVE_STDINT_H
+#include <stdint.h>
+#endif
+#ifndef SIZE_MAX
+#define SIZE_MAX (~(size_t) 0)
+#endif
 
 #include <libxml/xmlmemory.h>
 #include <libxml/tree.h>
@@ -6288,7 +6296,15 @@ htmlCreateFileParserCtxt(const char *filename, const char *encoding)
 
     /* set encoding */
     if (encoding) {
-        content = xmlMallocAtomic (xmlStrlen(content_line) + strlen(encoding) + 1);
+        size_t content_line_len = xmlStrlen(content_line);
+        size_t encoding_len = strlen(encoding);
+        /* check for possible overflow */
+        if (content_line_len > (SIZE_MAX - encoding_len - 1)) {
+            xmlFreeParserCtxt(ctxt);
+            return(NULL);
+        }
+
+        content = xmlMallocAtomic(content_line_len + encoding_len + 1);
         if (content) {
             strcpy ((char *)content, (char *)content_line);
             strcat ((char *)content, (char *)encoding);
