diff --git a/src/modules/Parser/html.c b/src/modules/Parser/html.c
index 9bbc8619e096b58a590b6d0865668fac9ff14d5b..eff15c29672c91bf028118dbc89a2d3982157bd3 100644
--- a/src/modules/Parser/html.c
+++ b/src/modules/Parser/html.c
@@ -2750,7 +2750,8 @@ static newstate find_end_of_container(struct parser_html_storage *this,
 	pop_stack();
       }
 
-      if (c3 < s3->s->len && index_shared_string (s3->s,c3) == this->tag_start) {
+      if (tagname->u.string->len > sp[-1].u.string->len &&
+	  c3 < s3->s->len && index_shared_string (s3->s,c3) == this->tag_start) {
 	struct pike_string *cmp =
 	  string_slice (tagname->u.string, 0, sp[-1].u.string->len);
 	if (cmp == sp[-1].u.string) {
diff --git a/src/modules/Parser/testsuite.in b/src/modules/Parser/testsuite.in
index 33fdbfdcebadcf9ae38bb7600c09c0d495e978af..d8eb5c7656c72c61fe1e0160367aa5959f34c28a 100644
--- a/src/modules/Parser/testsuite.in
+++ b/src/modules/Parser/testsuite.in
@@ -580,6 +580,12 @@ test_any([[{
 		    "12-"
 		    "13-"
 		    "14-"));
+test_any([[{
+  object p = Parser.HTML();
+  p->match_tag (0);
+  p->add_container ("foo", "");
+  return p->finish ("<foo></foobar</foo>")->read();
+}]], "");
 
 // Redefinition during parsing
 test_any([[