diff --git a/mysql-test/r/xml.result b/mysql-test/r/xml.result
index d213ef98885..52f80000015 100644
--- a/mysql-test/r/xml.result
+++ b/mysql-test/r/xml.result
@@ -546,7 +546,7 @@ select extractvalue('aB','a|/b');
extractvalue('aB','a|/b')
a
select extractvalue('A','/');
-ERROR HY000: XPATH syntax error: '>'
+ERROR HY000: XPATH syntax error: ''
select extractvalue('bb!','//b!');
ERROR HY000: XPATH syntax error: '!'
select extractvalue('ABC','/a/descendant::*');
@@ -613,3 +613,5 @@ select extractValue('1','position()');
ERROR HY000: XPATH syntax error: ''
select extractValue('1','last()');
ERROR HY000: XPATH syntax error: ''
+select extractValue('1','/e/');
+ERROR HY000: XPATH syntax error: ''
diff --git a/mysql-test/t/xml.test b/mysql-test/t/xml.test
index ae1c9cf3b6b..af3ec2d827e 100644
--- a/mysql-test/t/xml.test
+++ b/mysql-test/t/xml.test
@@ -286,3 +286,12 @@ select extractvalue('Jack' collate latin1_bin,'/a[contains(../a,"j")]');
select extractValue('1','position()');
--error 1105
select extractValue('1','last()');
+
+
+#
+# Bug #18172 XML: Extractvalue() accepts mallformed
+# XPath without a XPath syntax error
+#
+--error 1105
+select extractValue('1','/e/');
+
diff --git a/sql/item_xmlfunc.cc b/sql/item_xmlfunc.cc
index da39c1e4409..8e5efa9f0f5 100644
--- a/sql/item_xmlfunc.cc
+++ b/sql/item_xmlfunc.cc
@@ -1561,10 +1561,13 @@ static int my_xpath_parse_AbsoluteLocationPath(MY_XPATH *xpath)
return my_xpath_parse_RelativeLocationPath(xpath);
}
+ if (my_xpath_parse_term(xpath, MY_XPATH_LEX_EOF))
+ return 1;
+
if (my_xpath_parse_RelativeLocationPath(xpath))
return 1;
- return 1;
+ return 0;
}