diff --git a/client/sql_string.cc b/client/sql_string.cc
index 4a0f2df927b9f3ee3b69571bcdf3761d71d5c583..010da7eb517236f3f5e7ca5fd57b7775af1bf988 100644
--- a/client/sql_string.cc
+++ b/client/sql_string.cc
@@ -761,7 +761,7 @@ String *copy_if_not_alloced(String *to,String *from,uint32 from_length)
 {
   if (from->Alloced_length >= from_length)
     return from;
-  if (from->alloced || !to || from == to)
+  if ((from->alloced && (from->Alloced_length != 0)) || !to || from == to)
   {
     (void) from->realloc(from_length);
     return from;
diff --git a/sql/sql_string.cc b/sql/sql_string.cc
index 4dbc2d7720679a41a00c9271636fbffef677648f..10bd3511cd2c760f3138238633beded53f48a3ae 100644
--- a/sql/sql_string.cc
+++ b/sql/sql_string.cc
@@ -796,7 +796,7 @@ String *copy_if_not_alloced(String *to,String *from,uint32 from_length)
 {
   if (from->Alloced_length >= from_length)
     return from;
-  if (from->alloced || !to || from == to)
+  if ((from->alloced && (from->Alloced_length != 0)) || !to || from == to)
   {
     (void) from->realloc(from_length);
     return from;