You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@xerces.apache.org by am...@apache.org on 2011/12/27 11:59:21 UTC

svn commit: r1224891 - in /xerces/c/trunk/src/xercesc/util: TransService.cpp TransService.hpp Xerces_autoconf_config.msvc.hpp

Author: amassari
Date: Tue Dec 27 10:59:21 2011
New Revision: 1224891

URL: http://svn.apache.org/viewvc?rev=1224891&view=rev
Log:
Store the transcoded buffer as a janitor object, so that memory leaks cannot occur (XERCESC-1974)

Modified:
    xerces/c/trunk/src/xercesc/util/TransService.cpp
    xerces/c/trunk/src/xercesc/util/TransService.hpp
    xerces/c/trunk/src/xercesc/util/Xerces_autoconf_config.msvc.hpp

Modified: xerces/c/trunk/src/xercesc/util/TransService.cpp
URL: http://svn.apache.org/viewvc/xerces/c/trunk/src/xercesc/util/TransService.cpp?rev=1224891&r1=1224890&r2=1224891&view=diff
==============================================================================
--- xerces/c/trunk/src/xercesc/util/TransService.cpp (original)
+++ xerces/c/trunk/src/xercesc/util/TransService.cpp Tue Dec 27 10:59:21 2011
@@ -21,7 +21,6 @@
 // ---------------------------------------------------------------------------
 //  Includes
 // ---------------------------------------------------------------------------
-#include <xercesc/util/Janitor.hpp>
 #include <xercesc/util/TransService.hpp>
 #include <xercesc/util/XML88591Transcoder.hpp>
 #include <xercesc/util/XMLASCIITranscoder.hpp>
@@ -553,7 +552,7 @@ XMLLCPTranscoder::~XMLLCPTranscoder()
 TranscodeToStr::TranscodeToStr(const XMLCh *in, const char *encoding,
                                MemoryManager *manager)
     : fString(0),
-      fBytesWritten(0),
+	  fBytesWritten(0),
       fMemoryManager(manager)
 {
     XMLTransService::Codes failReason;
@@ -568,7 +567,7 @@ TranscodeToStr::TranscodeToStr(const XML
 TranscodeToStr::TranscodeToStr(const XMLCh *in, XMLSize_t length, const char *encoding,
                                MemoryManager *manager)
     : fString(0),
-      fBytesWritten(0),
+	  fBytesWritten(0),
       fMemoryManager(manager)
 {
     XMLTransService::Codes failReason;
@@ -583,7 +582,7 @@ TranscodeToStr::TranscodeToStr(const XML
 TranscodeToStr::TranscodeToStr(const XMLCh *in, XMLTranscoder* trans,
                                MemoryManager *manager)
     : fString(0),
-      fBytesWritten(0),
+	  fBytesWritten(0),
       fMemoryManager(manager)
 {
     transcode(in, XMLString::stringLen(in), trans);
@@ -592,7 +591,7 @@ TranscodeToStr::TranscodeToStr(const XML
 TranscodeToStr::TranscodeToStr(const XMLCh *in, XMLSize_t length, XMLTranscoder* trans,
                                MemoryManager *manager)
     : fString(0),
-      fBytesWritten(0),
+	  fBytesWritten(0),
       fMemoryManager(manager)
 {
     transcode(in, length, trans);
@@ -600,8 +599,6 @@ TranscodeToStr::TranscodeToStr(const XML
 
 TranscodeToStr::~TranscodeToStr()
 {
-    if(fString)
-        fMemoryManager->deallocate(fString);
 }
 
 // ---------------------------------------------------------------------------
@@ -612,37 +609,35 @@ void TranscodeToStr::transcode(const XML
     if(!in) return;
 
     XMLSize_t allocSize = (len * sizeof(XMLCh)) + 4;
-    fString = (XMLByte*)fMemoryManager->allocate(allocSize);
+    fString.reset((XMLByte*)fMemoryManager->allocate(allocSize), fMemoryManager);
 
     XMLSize_t charsDone = 0;
 
     while(charsDone < len) {
-	    XMLSize_t charsRead = 0;
+        XMLSize_t charsRead = 0;
         fBytesWritten += trans->transcodeTo(in + charsDone, len - charsDone,
-                                            fString + fBytesWritten, allocSize - fBytesWritten,
+                                            fString.get() + fBytesWritten, allocSize - fBytesWritten,
                                             charsRead, XMLTranscoder::UnRep_Throw);
         if(charsRead == 0)
             ThrowXMLwithMemMgr(TranscodingException, XMLExcepts::Trans_BadSrcSeq, fMemoryManager);
 
         charsDone += charsRead;
 
-		if((allocSize - fBytesWritten) < (len - charsDone))
-		{
-			allocSize *= 2;
-			XMLByte *newBuf = (XMLByte*)fMemoryManager->allocate(allocSize);
-			memcpy(newBuf, fString, fBytesWritten);
-			fMemoryManager->deallocate(fString);
-			fString = newBuf;
-		}
+        if((allocSize - fBytesWritten) < (len - charsDone))
+        {
+            allocSize *= 2;
+            XMLByte *newBuf = (XMLByte*)fMemoryManager->allocate(allocSize);
+            memcpy(newBuf, fString.get(), fBytesWritten);
+            fString.reset(newBuf, fMemoryManager);
+        }
     }
 
     // null terminate
     if((fBytesWritten + 4) > allocSize) {
         allocSize = fBytesWritten + 4;
         XMLByte *newBuf = (XMLByte*)fMemoryManager->allocate(allocSize);
-        memcpy(newBuf, fString, fBytesWritten);
-        fMemoryManager->deallocate(fString);
-        fString = newBuf;
+        memcpy(newBuf, fString.get(), fBytesWritten);
+        fString.reset(newBuf, fMemoryManager);
     }
     fString[fBytesWritten + 0] = 0;
     fString[fBytesWritten + 1] = 0;
@@ -656,7 +651,7 @@ void TranscodeToStr::transcode(const XML
 TranscodeFromStr::TranscodeFromStr(const XMLByte *data, XMLSize_t length, const char *encoding,
                                    MemoryManager *manager)
     : fString(0),
-      fCharsWritten(0),
+	  fCharsWritten(0),
       fMemoryManager(manager)
 {
     XMLTransService::Codes failReason;
@@ -671,7 +666,7 @@ TranscodeFromStr::TranscodeFromStr(const
 TranscodeFromStr::TranscodeFromStr(const XMLByte *data, XMLSize_t length, XMLTranscoder *trans,
                                    MemoryManager *manager)
     : fString(0),
-      fCharsWritten(0),
+	  fCharsWritten(0),
       fMemoryManager(manager)
 {
     transcode(data, length, trans);
@@ -679,8 +674,6 @@ TranscodeFromStr::TranscodeFromStr(const
 
 TranscodeFromStr::~TranscodeFromStr()
 {
-    if(fString)
-        fMemoryManager->deallocate(fString);
 }
 
 // ---------------------------------------------------------------------------
@@ -691,7 +684,7 @@ void TranscodeFromStr::transcode(const X
     if(!in) return;
 
     XMLSize_t allocSize = length + 1;
-    fString = (XMLCh*)fMemoryManager->allocate(allocSize * sizeof(XMLCh));
+    fString.reset((XMLCh*)fMemoryManager->allocate(allocSize * sizeof(XMLCh)), fMemoryManager);
 
     XMLSize_t csSize = allocSize;
     ArrayJanitor<unsigned char> charSizes((unsigned char*)fMemoryManager->allocate(csSize * sizeof(unsigned char)),
@@ -704,32 +697,30 @@ void TranscodeFromStr::transcode(const X
             csSize = allocSize - fCharsWritten;
             charSizes.reset((unsigned char*)fMemoryManager->allocate(csSize * sizeof(unsigned char)), fMemoryManager);
         }
-	    XMLSize_t bytesRead = 0;
+        XMLSize_t bytesRead = 0;
         fCharsWritten += trans->transcodeFrom(in + bytesDone, length - bytesDone,
-                                              fString + fCharsWritten, allocSize - fCharsWritten,
+                                              fString.get() + fCharsWritten, allocSize - fCharsWritten,
                                               bytesRead, charSizes.get());
         if(bytesRead == 0)
             ThrowXMLwithMemMgr(TranscodingException, XMLExcepts::Trans_BadSrcSeq, fMemoryManager);
 
         bytesDone += bytesRead;
 
-		if(((allocSize - fCharsWritten)*sizeof(XMLCh)) < (length - bytesDone))
-		{
-			allocSize *= 2;
-			XMLCh *newBuf = (XMLCh*)fMemoryManager->allocate(allocSize * sizeof(XMLCh));
-			memcpy(newBuf, fString, fCharsWritten*sizeof(XMLCh));
-			fMemoryManager->deallocate(fString);
-			fString = newBuf;
-		}
+        if(((allocSize - fCharsWritten)*sizeof(XMLCh)) < (length - bytesDone))
+        {
+            allocSize *= 2;
+            XMLCh *newBuf = (XMLCh*)fMemoryManager->allocate(allocSize * sizeof(XMLCh));
+            memcpy(newBuf, fString.get(), fCharsWritten*sizeof(XMLCh));
+            fString.reset(newBuf, fMemoryManager);
+        }
     }
 
     // null terminate
     if((fCharsWritten + 1) > allocSize) {
         allocSize = fCharsWritten + 1;
         XMLCh *newBuf = (XMLCh*)fMemoryManager->allocate(allocSize * sizeof(XMLCh));
-        memcpy(newBuf, fString, fCharsWritten*sizeof(XMLCh));
-        fMemoryManager->deallocate(fString);
-        fString = newBuf;
+        memcpy(newBuf, fString.get(), fCharsWritten*sizeof(XMLCh));
+        fString.reset(newBuf, fMemoryManager);
     }
     fString[fCharsWritten] = 0;
 }

Modified: xerces/c/trunk/src/xercesc/util/TransService.hpp
URL: http://svn.apache.org/viewvc/xerces/c/trunk/src/xercesc/util/TransService.hpp?rev=1224891&r1=1224890&r2=1224891&view=diff
==============================================================================
--- xerces/c/trunk/src/xercesc/util/TransService.hpp (original)
+++ xerces/c/trunk/src/xercesc/util/TransService.hpp Tue Dec 27 10:59:21 2011
@@ -27,6 +27,7 @@
 #include <xercesc/framework/XMLRecognizer.hpp>
 #include <xercesc/util/RefHashTableOf.hpp>
 #include <xercesc/util/RefVectorOf.hpp>
+#include <xercesc/util/Janitor.hpp>
 
 XERCES_CPP_NAMESPACE_BEGIN
 
@@ -122,7 +123,7 @@ public :
     virtual const XMLCh* getId() const = 0;
 
     // -----------------------------------------------------------------------
-    //	Create a new transcoder for the local code page.
+    //    Create a new transcoder for the local code page.
     //
     //  @param manager The memory manager to use.
     // -----------------------------------------------------------------------
@@ -134,13 +135,13 @@ public :
     virtual void lowerCase(XMLCh* const toLowerCase) = 0;
 
     // -----------------------------------------------------------------------
-    //	Allow users to add their own encodings to the intrinsic mapping
-    //	table
-    //	Usage:
-    //		XMLTransService::addEncoding (
-    //			gMyEncodingNameString
-    //			, new ENameMapFor<MyTransClassType>(gMyEncodingNameString)
-    //		);
+    //    Allow users to add their own encodings to the intrinsic mapping
+    //    table
+    //    Usage:
+    //        XMLTransService::addEncoding (
+    //            gMyEncodingNameString
+    //            , new ENameMapFor<MyTransClassType>(gMyEncodingNameString)
+    //        );
     // -----------------------------------------------------------------------
     static void addEncoding(const XMLCh* const encoding, ENameMap* const ownMapping);
 
@@ -215,29 +216,29 @@ class XMLUTIL_EXPORT XMLTranscoder : pub
 {
 public :
 
-	/**
-	 * This enum is used by the <code>transcodeTo()</code> method
-	 * to indicate how to react to unrepresentable characters. The
-	 * <code>transcodeFrom()</code> method always works the
-	 * same. It will consider any invalid data to be an error and
-	 * throw.
-	 */
+    /**
+     * This enum is used by the <code>transcodeTo()</code> method
+     * to indicate how to react to unrepresentable characters. The
+     * <code>transcodeFrom()</code> method always works the
+     * same. It will consider any invalid data to be an error and
+     * throw.
+     */
     enum UnRepOpts
     {
-        UnRep_Throw		/**< Throw an exception */
-        , UnRep_RepChar		/**< Use the replacement char */
+        UnRep_Throw        /**< Throw an exception */
+        , UnRep_RepChar        /**< Use the replacement char */
     };
 
 
-	/** @name Destructor. */
-	//@{
+    /** @name Destructor. */
+    //@{
 
-	 /**
-	  * Destructor for XMLTranscoder
-	  *
-	  */
+     /**
+      * Destructor for XMLTranscoder
+      *
+      */
     virtual ~XMLTranscoder();
-	//@}
+    //@}
 
 
 
@@ -323,7 +324,7 @@ public :
       *    <code>XMLTranscoder</code> object is for
       */
     const XMLCh* getEncodingName() const;
-	//@}
+    //@}
 
     /** @name Getter methods*/
     //@{
@@ -337,7 +338,7 @@ public :
       */
     MemoryManager* getMemoryManager() const;
 
-	//@}
+    //@}
 
 protected :
     // -----------------------------------------------------------------------
@@ -532,7 +533,7 @@ public:
       */
     XMLSize_t length () const;
 
-	//@}
+    //@}
 
 private:
     // -----------------------------------------------------------------------
@@ -555,7 +556,7 @@ private:
     //  fBytesWritten
     //      The length of the transcoded string in bytes
     // -----------------------------------------------------------------------
-    XMLByte *fString;
+    ArrayJanitor<XMLByte> fString;
     XMLSize_t fBytesWritten;
     MemoryManager *fMemoryManager;
 };
@@ -614,7 +615,7 @@ public:
       */
     XMLSize_t length() const;
 
-	//@}
+    //@}
 
 private:
     // -----------------------------------------------------------------------
@@ -637,7 +638,7 @@ private:
     //  fCharsWritten
     //      The length of the transcoded string in characters
     // -----------------------------------------------------------------------
-    XMLCh *fString;
+    ArrayJanitor<XMLCh> fString;
     XMLSize_t fCharsWritten;
     MemoryManager *fMemoryManager;
 };
@@ -668,14 +669,13 @@ inline const XMLCh* XMLTranscoder::getEn
 // ---------------------------------------------------------------------------
 inline const XMLByte *TranscodeToStr::str() const
 {
-    return fString;
+    return fString.get();
 }
 
 inline XMLByte *TranscodeToStr::adopt()
 {
-    XMLByte *tmp = fString;
-    fString = 0;
-    return tmp;
+    fBytesWritten = 0;
+    return fString.release();
 }
 
 inline XMLSize_t TranscodeToStr::length () const
@@ -688,14 +688,13 @@ inline XMLSize_t TranscodeToStr::length 
 // ---------------------------------------------------------------------------
 inline const XMLCh *TranscodeFromStr::str() const
 {
-    return fString;
+    return fString.get();
 }
 
 inline XMLCh *TranscodeFromStr::adopt()
 {
-    XMLCh *tmp = fString;
-    fString = 0;
-    return tmp;
+    fCharsWritten = 0;
+    return fString.release();
 }
 
 inline XMLSize_t TranscodeFromStr::length() const

Modified: xerces/c/trunk/src/xercesc/util/Xerces_autoconf_config.msvc.hpp
URL: http://svn.apache.org/viewvc/xerces/c/trunk/src/xercesc/util/Xerces_autoconf_config.msvc.hpp?rev=1224891&r1=1224890&r2=1224891&view=diff
==============================================================================
--- xerces/c/trunk/src/xercesc/util/Xerces_autoconf_config.msvc.hpp (original)
+++ xerces/c/trunk/src/xercesc/util/Xerces_autoconf_config.msvc.hpp Tue Dec 27 10:59:21 2011
@@ -46,6 +46,8 @@
 // silence the warning "while compiling class-template member function xxxx : identifier was truncated to '255'
 // characters in the browser information"
 #pragma warning( disable: 4786 )
+// silence the warning "class 'XXXX' needs to have dll-interface to be used by clients of class 'YYYY'"
+#pragma warning( disable: 4251)
 
 // ---------------------------------------------------------------------------
 //  These defines have been hardcoded for the Microsoft Visual C++ compilers



---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@xerces.apache.org
For additional commands, e-mail: commits-help@xerces.apache.org