diff --git a/mysql-test/r/xml.result b/mysql-test/r/xml.result
index a1f2c80e7667ad722bd83e2881139673caaaea9b..552f4896698d556ac45c5242ab9bcc7ba8562cd2 100644
--- a/mysql-test/r/xml.result
+++ b/mysql-test/r/xml.result
@@ -1012,3 +1012,14 @@ select ExtractValue('<a>a</a>', '/a[@x=@y0123456789_0123456789_0123456789_012345
 ERROR HY000: XPATH error: comparison of two nodesets is not supported: '=@y0123456789_0123456789_0123456'
 select ExtractValue('<a>a</a>', '/a[@x=$y0123456789_0123456789_0123456789_0123456789]');
 ERROR HY000: Unknown XPATH variable at: '$y0123456789_0123456789_01234567'
+select updatexml(NULL, 1, 1), updatexml(1, NULL, 1), updatexml(1, 1, NULL);
+updatexml(NULL, 1, 1)	updatexml(1, NULL, 1)	updatexml(1, 1, NULL)
+NULL	NULL	NULL
+select updatexml(NULL, NULL, 1), updatexml(1, NULL, NULL), 
+updatexml(NULL, 1, NULL);
+updatexml(NULL, NULL, 1)	updatexml(1, NULL, NULL)	updatexml(NULL, 1, NULL)
+NULL	NULL	NULL
+select updatexml(NULL, NULL, NULL);
+updatexml(NULL, NULL, NULL)
+NULL
+End of 5.1 tests
diff --git a/mysql-test/t/xml.test b/mysql-test/t/xml.test
index 1d16652ab1edf1b633e1b3561d96a07e3d8bea92..6c7d9af1b63019a66aa71ee7814143fe5fefb378 100644
--- a/mysql-test/t/xml.test
+++ b/mysql-test/t/xml.test
@@ -533,3 +533,14 @@ select UpdateXML('<a>a</a>',repeat('a b ',1000),'');
 select ExtractValue('<a>a</a>', '/a[@x=@y0123456789_0123456789_0123456789_0123456789]');
 --error 1105
 select ExtractValue('<a>a</a>', '/a[@x=$y0123456789_0123456789_0123456789_0123456789]');
+
+#
+# Bug #31438: updatexml still crashes
+#
+
+select updatexml(NULL, 1, 1), updatexml(1, NULL, 1), updatexml(1, 1, NULL);
+select updatexml(NULL, NULL, 1), updatexml(1, NULL, NULL), 
+       updatexml(NULL, 1, NULL);
+select updatexml(NULL, NULL, NULL);
+
+--echo End of 5.1 tests
diff --git a/sql/item_xmlfunc.cc b/sql/item_xmlfunc.cc
index 15be9c97b6e333de31bd9e13a83c310431dcf305..1a6c15a4d2ec7b965be35eac88cc965a574cbce7 100644
--- a/sql/item_xmlfunc.cc
+++ b/sql/item_xmlfunc.cc
@@ -2580,7 +2580,8 @@ void Item_xml_str_func::fix_length_and_dec()
     return;
   }
 
-  xp= args[1]->val_str(&tmp);
+  if (!(xp= args[1]->val_str(&tmp)))
+    return;
   my_xpath_init(&xpath);
   xpath.cs= collation.collation;
   xpath.debug= 0;