Author: amassari
Date: Wed Jul 29 13:10:37 2009
New Revision: 798882

URL: http://svn.apache.org/viewvc?rev=798882&view=rev
Log:
When copying a context object, allocate new memory for fMatch and fOffsets only 
if the size of the data being copied is different, so that a user-provided 
fMatch object can be properly filled (XERCESC-1870)

Modified:
    xerces/c/trunk/src/xercesc/util/regx/RegularExpression.cpp

Modified: xerces/c/trunk/src/xercesc/util/regx/RegularExpression.cpp
URL: 
http://svn.apache.org/viewvc/xerces/c/trunk/src/xercesc/util/regx/RegularExpression.cpp?rev=798882&r1=798881&r2=798882&view=diff
==============================================================================
--- xerces/c/trunk/src/xercesc/util/regx/RegularExpression.cpp (original)
+++ xerces/c/trunk/src/xercesc/util/regx/RegularExpression.cpp Wed Jul 29 
13:10:37 2009
@@ -140,7 +140,7 @@
     }
     if(src->fMatch)
     {
-        fMatch=new Match(*src->fMatch);
+        fMatch=new (fMemoryManager) Match(*src->fMatch);
         fAdoptMatch=true;
     }
 }
@@ -152,30 +152,46 @@
         fStart=other.fStart;
         fLimit=other.fLimit;
         fLength=other.fLength;
-        fSize=other.fSize;
         fStringMaxLen=other.fStringMaxLen;
         fString=other.fString;
         fOptions=other.fOptions;
-        if (fOffsets)
-            fMemoryManager->deallocate(fOffsets);//delete [] fOffsets;
-        fOffsets=0;
-        if (fAdoptMatch)
-            delete fMatch;
-        fMatch=0;
-        fAdoptMatch=false;
 
-        fMemoryManager=other.fMemoryManager;
-        if(other.fOffsets)
+        // if offset and match are already allocated with the right size, 
reuse them 
+        // (fMatch can be provided by the user to get the data back)
+        if(fMatch && other.fMatch && 
fMatch->getNoGroups()==other.fMatch->getNoGroups())
+            *fMatch=*other.fMatch;
+        else
+        {
+            if (fAdoptMatch)
+                delete fMatch;
+            fMatch=0;
+            if(other.fMatch)
+            {
+                fMatch=new (other.fMemoryManager) Match(*other.fMatch);
+                fAdoptMatch=true;
+            }
+        }
+
+        if (fOffsets && other.fOffsets && fSize==other.fSize)
         {
-            fOffsets = (int*) fMemoryManager->allocate(fSize* sizeof(int));
             for (int i = 0; i< fSize; i++)
                 fOffsets[i] = other.fOffsets[i];
         }
-        if(other.fMatch)
+        else
         {
-            fMatch=new Match(*other.fMatch);
-            fAdoptMatch=true;
+            if(fOffsets)
+                fMemoryManager->deallocate(fOffsets);//delete [] fOffsets;
+            fOffsets=0;
+            fSize=other.fSize;
+            if(other.fOffsets)
+            {
+                fOffsets = (int*) other.fMemoryManager->allocate(fSize* 
sizeof(int));
+                for (int i = 0; i< fSize; i++)
+                    fOffsets[i] = other.fOffsets[i];
+            }
         }
+
+        fMemoryManager=other.fMemoryManager;
     }
 
     return *this;



---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to