You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@xerces.apache.org by pe...@apache.org on 2004/08/23 18:06:49 UTC

cvs commit: xml-xerces/c/src/xercesc/util XMLBigInteger.cpp XMLAbstractDoubleFloat.cpp

peiyongz    2004/08/23 09:06:49

  Modified:    c/src/xercesc/util XMLBigInteger.cpp
                        XMLAbstractDoubleFloat.cpp
  Log:
  Fix to memory leakage in getCanRep
  
  Revision  Changes    Path
  1.13      +5 -1      xml-xerces/c/src/xercesc/util/XMLBigInteger.cpp
  
  Index: XMLBigInteger.cpp
  ===================================================================
  RCS file: /home/cvs/xml-xerces/c/src/xercesc/util/XMLBigInteger.cpp,v
  retrieving revision 1.12
  retrieving revision 1.13
  diff -u -r1.12 -r1.13
  --- XMLBigInteger.cpp	17 Aug 2004 21:09:04 -0000	1.12
  +++ XMLBigInteger.cpp	23 Aug 2004 16:06:49 -0000	1.13
  @@ -56,6 +56,9 @@
   
   /*
    * $Log$
  + * Revision 1.13  2004/08/23 16:06:49  peiyongz
  + * Fix to memory leakage in getCanRep
  + *
    * Revision 1.12  2004/08/17 21:09:04  peiyongz
    * canRep for nonPositivieInteger
    *
  @@ -135,6 +138,7 @@
       try 
       {
           XMLCh* retBuf = (XMLCh*) memMgr->allocate( (XMLString::stringLen(rawData) + 2) * sizeof(XMLCh));
  +        ArrayJanitor<XMLCh> jan(retBuf, memMgr);
           int    sign = 0;
   
           XMLBigInteger::parseBigInteger(rawData, retBuf, sign);
  @@ -158,10 +162,10 @@
               XMLCh* retBuffer = (XMLCh*) memMgr->allocate( (XMLString::stringLen(retBuf) + 2) * sizeof(XMLCh));
               retBuffer[0] = chDash;
               XMLString::copyString(&(retBuffer[1]), retBuf);
  -            memMgr->deallocate(retBuf);
               return retBuffer;
           }
   
  +        jan.release();
           return retBuf;
   
       }//
  
  
  
  1.27      +98 -99    xml-xerces/c/src/xercesc/util/XMLAbstractDoubleFloat.cpp
  
  Index: XMLAbstractDoubleFloat.cpp
  ===================================================================
  RCS file: /home/cvs/xml-xerces/c/src/xercesc/util/XMLAbstractDoubleFloat.cpp,v
  retrieving revision 1.26
  retrieving revision 1.27
  diff -u -r1.26 -r1.27
  --- XMLAbstractDoubleFloat.cpp	29 Jan 2004 11:48:46 -0000	1.26
  +++ XMLAbstractDoubleFloat.cpp	23 Aug 2004 16:06:49 -0000	1.27
  @@ -57,6 +57,9 @@
   /*
    * $Id$
    * $Log$
  + * Revision 1.27  2004/08/23 16:06:49  peiyongz
  + * Fix to memory leakage in getCanRep
  + *
    * Revision 1.26  2004/01/29 11:48:46  cargilld
    * Code cleanup changes to get rid of various compiler diagnostic messages.
    *
  @@ -517,126 +520,122 @@
   {
       // before anything, let's look for special tokens since that
       // breaks the calls to parse below.
  -    if(XMLString::equals(rawData, XMLUni::fgNegINFString)
  -            || XMLString::equals(rawData, XMLUni::fgPosINFString)
  -            || XMLString::equals(rawData, XMLUni::fgNaNString))
  +    if(XMLString::equals(rawData, XMLUni::fgNegINFString) || 
  +       XMLString::equals(rawData, XMLUni::fgPosINFString) || 
  +       XMLString::equals(rawData, XMLUni::fgNaNString)     )
       {
           return XMLString::replicate(rawData, memMgr);
       }
   
       try 
       {
  +        int    strLen = XMLString::stringLen(rawData);
  +        XMLCh* manStr = (XMLCh*) memMgr->allocate((strLen + 1) * sizeof(XMLCh));
  +        ArrayJanitor<XMLCh> janManStr(manStr, memMgr);
  +        XMLCh* manBuf = (XMLCh*) memMgr->allocate((strLen + 1) * sizeof(XMLCh));
  +        ArrayJanitor<XMLCh> janManBuf(manBuf, memMgr);
  +        XMLCh* expStr = (XMLCh*) memMgr->allocate((strLen + 1) * sizeof(XMLCh));
  +        ArrayJanitor<XMLCh> janExpStr(expStr, memMgr);
  +        XMLCh* retBuffer = (XMLCh*) memMgr->allocate((strLen + 8) * sizeof(XMLCh));
  +        ArrayJanitor<XMLCh> janRetBuffer(retBuffer, memMgr);
  +        retBuffer[0] = 0;
   
  -    int    strLen = XMLString::stringLen(rawData);
  -    XMLCh* manStr = (XMLCh*) memMgr->allocate((strLen + 1) * sizeof(XMLCh));
  -    ArrayJanitor<XMLCh> janManStr(manStr, memMgr);
  -    XMLCh* manBuf = (XMLCh*) memMgr->allocate((strLen + 1) * sizeof(XMLCh));
  -    ArrayJanitor<XMLCh> janManBuf(manBuf, memMgr);
  -
  -    XMLCh* expStr = (XMLCh*) memMgr->allocate((strLen + 1) * sizeof(XMLCh));
  -    ArrayJanitor<XMLCh> janExp(expStr, memMgr);
  -
  -    XMLCh* retBuffer = (XMLCh*) memMgr->allocate((strLen + 8) * sizeof(XMLCh));
  -    retBuffer[0] = 0;
  -
  -    int sign, totalDigits, fractDigits;
  -    int expValue = 0;
  -
  -    const XMLCh* ePosition = XMLString::findAny(rawData, expSign);
  -
  -    /***
  -     *  parse mantissa and exp separately
  -     ***/
  -    if (!ePosition)
  -    {
  -        XMLBigDecimal::parseDecimal(rawData, manBuf, sign, totalDigits, fractDigits, memMgr);
  -        expValue = 0;
  -    }
  -    else
  -    {
  -        int    manLen = ePosition - rawData;
  -        XMLString::copyNString(manStr, rawData, manLen);
  -        *(manStr + manLen) = chNull;
  -        XMLBigDecimal::parseDecimal(manStr, manBuf, sign, totalDigits, fractDigits, memMgr);
  -
  -        int    expLen = strLen - manLen - 1;
  -        ePosition++;
  -        XMLString::copyNString(expStr, ePosition, expLen);
  -        *(expStr + expLen) = chNull;
  -        expValue = XMLString::parseInt(expStr); 
  -    }
  -
  -    if ( (sign == 0) || (totalDigits == 0) )
  -    {
  -        retBuffer[0] = chDigit_0;
  -        retBuffer[1] = chPeriod;
  -        retBuffer[2] = chDigit_0;
  -        retBuffer[3] = chLatin_E;
  -        retBuffer[4] = chDigit_0;
  -        retBuffer[5] = chNull;
  -    }
  -    else
  -    {
  -        XMLCh* retPtr = retBuffer;
  +        int sign, totalDigits, fractDigits, expValue = 0;
  +
  +        const XMLCh* ePosition = XMLString::findAny(rawData, expSign);
   
  -        if (sign == -1)
  +        /***
  +         *  parse mantissa and exp separately
  +        ***/
  +        if (!ePosition)
           {
  -            *retPtr++ = chDash;
  +            XMLBigDecimal::parseDecimal(rawData, manBuf, sign, totalDigits, fractDigits, memMgr);
  +            expValue = 0;
           }
  -
  -        *retPtr++ = manBuf[0];
  -        *retPtr++ = chPeriod;
  -
  -        //XMLBigDecimal::parseDecimal() will eliminate trailing zeros
  -        // iff there is a decimal points
  -        // eg. 56.7800e0  -> manBuf = 5678, totalDigits = 4, fractDigits = 2
  -        // we print it as 5.678e1
  -        //
  -        // but it wont remove trailing zeros if there is no decimal point.
  -        // eg.  567800e0 -> manBuf = 567800, totalDigits = 6, fractDigits = 0
  -        // we print it 5.67800e5
  -        //
  -        // for the latter, we need to print it as 5.678e5 instead
  -        //
  -        XMLCh* endPtr = manBuf + totalDigits;
  -
  -        if (fractDigits == 0)
  +        else
           {
  -            while(*(endPtr - 1) == chDigit_0)
  -                endPtr--;
  +            int    manLen = ePosition - rawData;
  +            XMLString::copyNString(manStr, rawData, manLen);
  +            *(manStr + manLen) = chNull;
  +            XMLBigDecimal::parseDecimal(manStr, manBuf, sign, totalDigits, fractDigits, memMgr);
  +
  +            int    expLen = strLen - manLen - 1;
  +            ePosition++;
  +            XMLString::copyNString(expStr, ePosition, expLen);
  +            *(expStr + expLen) = chNull;
  +            expValue = XMLString::parseInt(expStr); 
           }
   
  -        int remainLen = endPtr - &(manBuf[1]);
  -
  -        if (remainLen)
  +        if ( (sign == 0) || (totalDigits == 0) )
           {
  -            XMLString::copyNString(retPtr, &(manBuf[1]), remainLen);
  -            retPtr += remainLen;
  +            retBuffer[0] = chDigit_0;
  +            retBuffer[1] = chPeriod;
  +            retBuffer[2] = chDigit_0;
  +            retBuffer[3] = chLatin_E;
  +            retBuffer[4] = chDigit_0;
  +            retBuffer[5] = chNull;
           }
           else
           {
  -            *retPtr++ = chDigit_0;
  -        }
  +            XMLCh* retPtr = retBuffer;
   
  -        /***
  -         * 
  -         *  . adjust expValue
  -         *   
  -         *  new_fractDigits = totalDigits - 1  
  -         *  new_expValue = old_expValue + (new_fractDigits - fractDigits)
  -         *
  -         ***/
  -        expValue += (totalDigits - 1) - fractDigits ;
  -        XMLString::binToText(expValue, expStr, strLen, 10, memMgr);
  -        *retPtr++  = chLatin_E;
  -        *retPtr = chNull;
  +            if (sign == -1)
  +            {
  +                *retPtr++ = chDash;
  +            }
   
  +            *retPtr++ = manBuf[0];
  +            *retPtr++ = chPeriod;
   
  -        XMLString::catString(&(retBuffer[0]), expStr);
  +            //XMLBigDecimal::parseDecimal() will eliminate trailing zeros
  +            // iff there is a decimal points
  +            // eg. 56.7800e0  -> manBuf = 5678, totalDigits = 4, fractDigits = 2
  +            // we print it as 5.678e1
  +            //
  +            // but it wont remove trailing zeros if there is no decimal point.
  +            // eg.  567800e0 -> manBuf = 567800, totalDigits = 6, fractDigits = 0
  +            // we print it 5.67800e5
  +            //
  +            // for the latter, we need to print it as 5.678e5 instead
  +            //
  +            XMLCh* endPtr = manBuf + totalDigits;
   
  -    }
  +            if (fractDigits == 0)
  +            {
  +                while(*(endPtr - 1) == chDigit_0)
  +                    endPtr--;
  +            }
  +
  +            int remainLen = endPtr - &(manBuf[1]);
  +
  +            if (remainLen)
  +            {
  +                XMLString::copyNString(retPtr, &(manBuf[1]), remainLen);
  +                retPtr += remainLen;
  +            }
  +            else
  +            {
  +                *retPtr++ = chDigit_0;
  +            }
  +
  +            /***
  +             * 
  +             *  . adjust expValue
  +             *   
  +             *  new_fractDigits = totalDigits - 1  
  +             *  new_expValue = old_expValue + (new_fractDigits - fractDigits)
  +             *
  +             ***/
  +            expValue += (totalDigits - 1) - fractDigits ;
  +            XMLString::binToText(expValue, expStr, strLen, 10, memMgr);
  +            *retPtr++  = chLatin_E;
  +            *retPtr = chNull;
  +
  +            XMLString::catString(&(retBuffer[0]), expStr);
  +        }
   
  -    return retBuffer;
  +        janRetBuffer.release();
  +        return retBuffer;
   
       } //try
   
  
  
  

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