From 24aba046bf287388622e2fdc34bb66a6cca40876 Mon Sep 17 00:00:00 2001
From: unknown <Sinisa@sinisa.nasamreza.org>
Date: Mon, 14 Apr 2003 21:46:24 +0300
Subject: [PATCH] Fix for a NAN problem in FORMAT(...) string function ..

---
 mysql-test/r/func_misc.result | 3 +++
 mysql-test/t/func_misc.test   | 1 +
 sql/item_strfunc.cc           | 2 +-
 3 files changed, 5 insertions(+), 1 deletion(-)

diff --git a/mysql-test/r/func_misc.result b/mysql-test/r/func_misc.result
index 4eed80c4cc..67f43ca5ba 100644
--- a/mysql-test/r/func_misc.result
+++ b/mysql-test/r/func_misc.result
@@ -10,3 +10,6 @@ inet_aton("255.255.255.255.255")	inet_aton("255.255.1.255")	inet_aton("0.1.255")
 select inet_ntoa(1099511627775),inet_ntoa(4294902271),inet_ntoa(511);
 inet_ntoa(1099511627775)	inet_ntoa(4294902271)	inet_ntoa(511)
 NULL	255.255.1.255	0.0.1.255
+select format("nan",2);
+format("nan",2)
+nan
diff --git a/mysql-test/t/func_misc.test b/mysql-test/t/func_misc.test
index d48b17e87a..d9ae288cc5 100644
--- a/mysql-test/t/func_misc.test
+++ b/mysql-test/t/func_misc.test
@@ -7,3 +7,4 @@ select format(1.5555,0),format(123.5555,1),format(1234.5555,2),format(12345.5555
 select inet_ntoa(inet_aton("255.255.255.255.255.255.255.255"));
 select inet_aton("255.255.255.255.255"),inet_aton("255.255.1.255"),inet_aton("0.1.255");
 select inet_ntoa(1099511627775),inet_ntoa(4294902271),inet_ntoa(511);
+select format("nan",2);
diff --git a/sql/item_strfunc.cc b/sql/item_strfunc.cc
index 0f1ce8adba..2496e1acab 100644
--- a/sql/item_strfunc.cc
+++ b/sql/item_strfunc.cc
@@ -1477,7 +1477,7 @@ String *Item_func_format::val_str(String *str)
   if (nr < 0)
     str_length--;				// Don't count sign
   length=str->length()+(diff=(str_length- dec-1)/3);
-  if (diff)
+  if (diff && diff < 330) // size of buff ...
   {
     char *tmp,*pos;
     str=copy_if_not_alloced(&tmp_str,str,length);
-- 
2.30.9