diff --git a/MagickCore/string.c b/MagickCore/string.c
index 1c9eb85..c5479f1 100644
--- a/MagickCore/string.c
+++ b/MagickCore/string.c
@@ -213,13 +213,15 @@
     *string_info;
 
   string_info=AcquireStringInfo(0);
-  if (string_info->datum != (unsigned char *) NULL)
-    string_info->datum=(unsigned char *) RelinquishMagickMemory(
-      string_info->datum);
+  if (~length < MaxTextExtent)
+    ThrowFatalException(ResourceLimitFatalError,"MemoryAllocationFailed");
   string_info->length=length;
-  if (~string_info->length >= (MaxTextExtent-1))
-    string_info->datum=(unsigned char *) AcquireQuantumMemory(
-      string_info->length+MaxTextExtent,sizeof(*string_info->datum));
+  if (string_info->datum == (unsigned char *) NULL)
+    string_info->datum=(unsigned char *) AcquireQuantumMemory(length+
+      MaxTextExtent,sizeof(*string_info->datum));
+  else
+    string_info->datum=(unsigned char *) ResizeQuantumMemory(string_info->datum,
+      length+MaxTextExtent,sizeof(*string_info->datum));
   if (string_info->datum == (unsigned char *) NULL)
     {
       string_info=DestroyStringInfo(string_info);
@@ -650,11 +652,11 @@
   string[length]='\0';
   file=close(file)-1;
   string_info=AcquireStringInfo(0);
+  (void) CopyMagickString(string_info->path,filename,MaxTextExtent);
+  string_info->length=length;
   if (string_info->datum != (unsigned char *) NULL)
     string_info->datum=(unsigned char *) RelinquishMagickMemory(
       string_info->datum);
-  (void) CopyMagickString(string_info->path,filename,MaxTextExtent);
-  string_info->length=length;
   string_info->datum=(unsigned char *) string;
   return(string_info);
 }
@@ -1032,10 +1034,10 @@
   (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",filename);
   assert(exception != (ExceptionInfo *) NULL);
   string_info=AcquireStringInfo(0);
+  (void) CopyMagickString(string_info->path,filename,MaxTextExtent);
   if (string_info->datum != (unsigned char *) NULL)
     string_info->datum=(unsigned char *) RelinquishMagickMemory(
       string_info->datum);
-  (void) CopyMagickString(string_info->path,filename,MaxTextExtent);
   string_info->datum=FileToBlob(filename,extent,&string_info->length,exception);
   if (string_info->datum == (unsigned char *) NULL)
     {