Przeglądaj źródła

Merge pull request #4393 from AttilaVamos/HPCC-9324-improvement

HPCC-9324 Error handler missing after malloc() call several places in  eclrtl.cpp

Reviewed-By: Gavin Halliday <gavin.halliday@lexisnexis.com>Reviewed-By: Richard Chapman <rchapman@hpccsystems.com>
Richard Chapman 12 lat temu
rodzic
commit
c2794f27c4
2 zmienionych plików z 97 dodań i 79 usunięć
  1. 96 78
      rtl/eclrtl/eclrtl.cpp
  2. 1 1
      rtl/eclrtl/eclrtl.hpp

+ 96 - 78
rtl/eclrtl/eclrtl.cpp

@@ -64,10 +64,16 @@ MODULE_EXIT()
 
 //=============================================================================
 // Miscellaneous string functions...
-
 ECLRTL_API void * rtlMalloc(size32_t size)
 {
-    return malloc(size);
+    void * retVal = malloc(size);
+
+    if( size && !retVal)
+    {
+        PrintStackReport();
+        rtlThrowOutOfMemory(0, "Memory allocation error!");
+    }
+    return retVal;
 }
 
 void rtlFree(void *ptr)
@@ -77,7 +83,14 @@ void rtlFree(void *ptr)
 
 ECLRTL_API void * rtlRealloc(void * _ptr, size32_t size)
 {
-    return realloc(_ptr, size);
+    void * retVal = realloc(_ptr, size);
+
+    if( (0 < size) && (NULL == retVal))
+    {
+        PrintStackReport();
+        rtlThrowOutOfMemory(0, "Memory reallocation error!");
+    }
+    return retVal;
 }
 
 //=============================================================================
@@ -291,7 +304,7 @@ bool vunicodeNeedsNormalize(UChar * in, UErrorCode * err)
 
 void unicodeReplaceNormalized(unsigned inlen, UChar * in, UErrorCode * err)
 {
-    UChar * buff = (UChar *)malloc(inlen*2);
+    UChar * buff = (UChar *)rtlMalloc(inlen*2);
     unsigned len = unorm_normalize(in, inlen, UNORM_NFC, 0, buff, inlen, err);
     while(len<inlen) buff[len++] = 0x0020;
     memcpy(in, buff, inlen);
@@ -300,7 +313,7 @@ void unicodeReplaceNormalized(unsigned inlen, UChar * in, UErrorCode * err)
 
 void vunicodeReplaceNormalized(unsigned inlen, UChar * in, UErrorCode * err)
 {
-    UChar * buff = (UChar *)malloc(inlen*2);
+    UChar * buff = (UChar *)rtlMalloc(inlen*2);
     unsigned len = unorm_normalize(in, -1, UNORM_NFC, 0, buff, inlen-1, err);
     buff[len] = 0x0000;
     memcpy(in, buff, inlen);
@@ -310,14 +323,14 @@ void vunicodeReplaceNormalized(unsigned inlen, UChar * in, UErrorCode * err)
 void unicodeGetNormalized(unsigned & outlen, UChar * & out, unsigned inlen, UChar * in, UErrorCode * err)
 {
     outlen = unorm_normalize(in, inlen, UNORM_NFC, 0, 0, 0, err);
-    out = (UChar *)malloc(outlen*2);
+    out = (UChar *)rtlMalloc(outlen*2);
     unorm_normalize(in, inlen, UNORM_NFC, 0, out, outlen, err);
 }
 
 void vunicodeGetNormalized(UChar * & out, unsigned inlen, UChar * in, UErrorCode * err)
 {
     unsigned outlen = unorm_normalize(in, inlen, UNORM_NFC, 0, 0, 0, err);
-    out = (UChar *)malloc((outlen+1)*2);
+    out = (UChar *)rtlMalloc((outlen+1)*2);
     unorm_normalize(in, inlen, UNORM_NFC, 0, out, outlen, err);
     out[outlen] = 0x0000;
 }
@@ -546,7 +559,7 @@ void rtlInt8ToStr(size32_t l, char * t, __int64 val)
 
 #define intToUnknownStringBody() \
     unsigned len = numtostr(temp, val); \
-    char * result = (char *)malloc(len); \
+    char * result = (char *)rtlMalloc(len); \
     memcpy(result, temp, len); \
     l = len; \
     t = result;
@@ -626,7 +639,7 @@ void rtl_ls82en(size32_t l, char * t, __int64 val)
 #define intToUnknownEbcdicStringBody() \
     unsigned alen = numtostr(astr, val); \
     rtlStrToEStrX(elen,estr,alen,astr); \
-    char * result = (char *)malloc(elen); \
+    char * result = (char *)rtlMalloc(elen); \
     memcpy(result, estr, elen); \
     l = elen; \
     t = result;
@@ -819,7 +832,7 @@ void rtlRealToStrX(size32_t & l, char * & t, double val)
     StringBuffer temp;
     temp.append(val);
     unsigned len = temp.length();
-    char * result = (char *)malloc(len);
+    char * result = (char *)rtlMalloc(len);
     memcpy(result,temp.str(),len);
     l = len;
     t = result;
@@ -830,7 +843,7 @@ void rtlRealToStrX(size32_t & l, char * & t, float val)
     StringBuffer temp;
     temp.append(val);
     unsigned len = temp.length();
-    char * result = (char *)malloc(len);
+    char * result = (char *)rtlMalloc(len);
     memcpy(result,temp.str(),len);
     l = len;
     t = result;
@@ -1148,7 +1161,7 @@ void rtlIntFormat(unsigned & len, char * & target, __int64 value, unsigned width
         return;
     }
     len = width;
-    target = (char *)malloc(width);
+    target = (char *)rtlMalloc(width);
     holeIntFormat(width, target, value, width, flags);
 }
 
@@ -1161,7 +1174,7 @@ void rtlRealFormat(unsigned & len, char * & target, double value, unsigned width
         return;
     }
     len = width;
-    target = (char *)malloc(width);
+    target = (char *)rtlMalloc(width);
     holeRealFormat(width, target, value, width, places);
 }
 
@@ -1202,7 +1215,7 @@ void rtlBoolToStrX(unsigned & tlen, char * & tgt, bool src)
 {
     if (src)
     {
-        char * ret = (char *)malloc(1);
+        char * ret = (char *)rtlMalloc(1);
         ret[0] = '1';
         tlen = 1;
         tgt = ret;
@@ -1319,7 +1332,7 @@ void rtlVStrToVStr(unsigned tlen, void * tgt, const char * src)
 char *rtlCreateQuotedString(unsigned _len_tgt,char * tgt)
 {
     // Add ' at start and end. MORE! also needs to handle embedded quotes
-    char * result = (char *)malloc(_len_tgt + 3);
+    char * result = (char *)rtlMalloc(_len_tgt + 3);
     result[0] = '\'';
     memcpy(result+1, tgt, _len_tgt);
     result[_len_tgt+1] = '\'';
@@ -1346,7 +1359,7 @@ void rtlConcat(unsigned & tlen, char * * tgt, ...)
     }
     va_end(args);
 
-    char * buffer = (char *)malloc(totalLength);
+    char * buffer = (char *)rtlMalloc(totalLength);
     char * cur = buffer;
     va_start(args, tgt);
     for (;;)
@@ -1380,7 +1393,7 @@ void rtlConcatVStr(char * * tgt, ...)
     }
     va_end(args);
 
-    char * buffer = (char *)malloc(totalLength+1);
+    char * buffer = (char *)rtlMalloc(totalLength+1);
     char * cur = buffer;
     va_start(args, tgt);
     for (;;)
@@ -1414,7 +1427,7 @@ void rtlConcatUnicode(unsigned & tlen, UChar * * tgt, ...)
     }
     va_end(args);
 
-    UChar * buffer = (UChar *)malloc(totalLength*2); //I *believe* this is a valid upper limit, as an NFC concatenation can only be shorter than the sum of its parts
+    UChar * buffer = (UChar *)rtlMalloc(totalLength*2); //I *believe* this is a valid upper limit, as an NFC concatenation can only be shorter than the sum of its parts
     unsigned idx = 0;
     UErrorCode err = U_ZERO_ERROR;
     va_start(args, tgt);
@@ -1449,7 +1462,7 @@ void rtlConcatVUnicode(UChar * * tgt, ...)
     }
     va_end(args);
 
-    UChar * buffer = (UChar *)malloc((totalLength+1)*2); //I *believe* this is a valid upper limit, as an NFC concatenation can only be shorter than the sum of its parts
+    UChar * buffer = (UChar *)rtlMalloc((totalLength+1)*2); //I *believe* this is a valid upper limit, as an NFC concatenation can only be shorter than the sum of its parts
     unsigned idx = 0;
     UErrorCode err = U_ZERO_ERROR;
     va_start(args, tgt);
@@ -1656,7 +1669,7 @@ void rtlUnicodeStrcpy(UChar * tgt, UChar const * src)
 void rtlConcatExtend(unsigned & tlen, char * & tgt, unsigned slen, const char * src)
 {
     unsigned len = tlen + slen;
-    tgt = (char *)realloc(tgt, len);
+    tgt = (char *)rtlRealloc(tgt, len);
     memcpy(tgt+tlen, src, slen);
     tlen = len;
 }
@@ -1664,7 +1677,7 @@ void rtlConcatExtend(unsigned & tlen, char * & tgt, unsigned slen, const char *
 void rtlConcatUnicodeExtend(size32_t & tlen, UChar * & tgt, size32_t slen, const UChar * src)
 {
     unsigned len = tlen + slen;
-    tgt = (UChar *)realloc(tgt, len * sizeof(UChar));
+    tgt = (UChar *)rtlRealloc(tgt, len * sizeof(UChar));
     memcpy(tgt+tlen, src, slen * sizeof(UChar));
     tlen = len;
 }
@@ -1705,7 +1718,7 @@ void * doSubStrFT(unsigned & tlen, unsigned slen, const void * src, unsigned fro
     clipFromTo(from, to, slen);
 
     unsigned copylen = to - from;
-    char * buffer = (char *)malloc(len);
+    char * buffer = (char *)rtlMalloc(len);
     memcpy(buffer, (byte *)src+from, copylen);
     if (copylen < len)
         memset(buffer+copylen, fillChar, len-copylen);
@@ -1718,7 +1731,7 @@ void rtlSubStrFX(unsigned & tlen, char * & tgt, unsigned slen, const char * src,
     normalizeFrom(from, slen);
 
     tlen = slen-from;
-    tgt = (char *) malloc(tlen);
+    tgt = (char *) rtlMalloc(tlen);
     memcpy(tgt, src+from, tlen);
 }
 
@@ -1765,7 +1778,7 @@ void rtlSubDataFX(unsigned & tlen, void * & tgt, unsigned slen, const void * src
     normalizeFrom(from, slen);
 
     tlen = slen-from;
-    tgt = (char *) malloc(tlen);
+    tgt = (char *) rtlMalloc(tlen);
     memcpy(tgt, (const byte *)src+from, tlen);
 }
 
@@ -1775,7 +1788,7 @@ void rtlUnicodeSubStrFTX(unsigned & tlen, UChar * & tgt, unsigned slen, UChar co
     tlen = to - from;
     clipFromTo(from, to, slen);
 
-    tgt = (UChar *)malloc(tlen*2);
+    tgt = (UChar *)rtlMalloc(tlen*2);
     unsigned copylen = to - from;
     memcpy(tgt, src+from, copylen*2);
     while(copylen<tlen)
@@ -1787,7 +1800,7 @@ void rtlUnicodeSubStrFX(unsigned & tlen, UChar * & tgt, unsigned slen, UChar con
     normalizeFrom(from, slen);
 
     tlen = slen - from;
-    tgt = (UChar *)malloc(tlen*2);
+    tgt = (UChar *)rtlMalloc(tlen*2);
     memcpy(tgt, src+from, tlen*2);
 }
 
@@ -1798,7 +1811,7 @@ void rtlSubQStrFTX(unsigned & tlen, char * & tgt, unsigned slen, char const * sr
     tlen = to - from;
     clipFromTo(from, to, slen);
 
-    tgt = (char *)malloc(rtlQStrSize(tlen));
+    tgt = (char *)rtlMalloc(rtlQStrSize(tlen));
     copyQStrRange(tlen, tgt, src, from, to);
 }
 
@@ -1807,7 +1820,7 @@ void rtlSubQStrFX(unsigned & tlen, char * & tgt, unsigned slen, char const * src
     normalizeFrom(from, slen);
 
     tlen = slen - from;
-    tgt = (char *)malloc(rtlQStrSize(tlen));
+    tgt = (char *)rtlMalloc(rtlQStrSize(tlen));
 
     copyQStrRange(tlen, tgt, src, from, slen);
 }
@@ -1947,7 +1960,7 @@ inline void rtlTrimUtf8Start(unsigned & trimLen, size32_t & trimSize, size32_t l
 
 inline char * rtlDupSubString(const char * src, unsigned len)
 {
-    char * buffer = (char *)malloc(len + 1);
+    char * buffer = (char *)rtlMalloc(len + 1);
     memcpy(buffer, src, len);
     buffer[len] = 0;
     return buffer;
@@ -1955,7 +1968,7 @@ inline char * rtlDupSubString(const char * src, unsigned len)
 
 inline UChar * rtlDupSubUnicode(UChar const * src, unsigned len)
 {
-    UChar * buffer = (UChar *)malloc((len + 1) * 2);
+    UChar * buffer = (UChar *)rtlMalloc((len + 1) * 2);
     memcpy(buffer, src, len*2);
     buffer[len] = 0x00;
     return buffer;
@@ -2196,7 +2209,7 @@ unsigned rtlTrimVStrLenNonBlank(const char * t)
 void rtlTrimAll(unsigned & tlen, char * & tgt, unsigned slen, const char * src)
 {
     tlen = rtlTrimStrLenNonBlank(slen, src);
-    char * buffer = (char *)malloc(tlen + 1);
+    char * buffer = (char *)rtlMalloc(tlen + 1);
     int ind = 0;
     for(unsigned i = 0; i < slen; i++) {
         if(src[i] != ' ') {
@@ -2218,7 +2231,7 @@ void rtlTrimUnicodeAll(unsigned & tlen, UChar * & tgt, unsigned slen, const UCha
     UnicodeString tgtStr;
     normalizeUnicodeString(rawStr, tgtStr); // normalized in case crazy string like [combining accent] [space] [vowel]
     tlen = tgtStr.length();
-    tgt = (UChar *)malloc((tlen+1)*2);
+    tgt = (UChar *)rtlMalloc((tlen+1)*2);
     tgtStr.extract(0, tlen, tgt);
     tgt[tlen] = 0x0000;
 }
@@ -2226,7 +2239,7 @@ void rtlTrimUnicodeAll(unsigned & tlen, UChar * & tgt, unsigned slen, const UCha
 void rtlTrimVAll(unsigned & tlen, char * & tgt, const char * src)
 {
     tlen = rtlTrimVStrLenNonBlank(src);
-    char * buffer = (char *)malloc(tlen + 1);
+    char * buffer = (char *)rtlMalloc(tlen + 1);
     int ind = 0;
     int i = 0;
     while(src[i] != 0) {
@@ -2513,7 +2526,7 @@ void rtlKeyUnicodeX(unsigned & tlen, void * & tgt, unsigned slen, const UChar *
     while(slen && u_isUWhiteSpace(src[slen-1])) slen--;
     UCollator * coll = queryRTLLocale(locale)->queryCollator();
     tlen = ucol_getSortKey(coll, src, slen, 0, 0);
-    tgt = malloc(tlen);
+    tgt = rtlMalloc(tlen);
     ucol_getSortKey(coll, src, slen, (unsigned char *)tgt, tlen);
 }
 
@@ -2522,7 +2535,7 @@ void rtlKeyUnicodeStrengthX(unsigned & tlen, void * & tgt, unsigned slen, const
     while(slen && u_isUWhiteSpace(src[slen-1])) slen--;
     UCollator * coll = queryRTLLocale(locale)->queryCollator(strength);
     tlen = ucol_getSortKey(coll, src, slen, 0, 0);
-    tgt = malloc(tlen);
+    tgt = rtlMalloc(tlen);
     ucol_getSortKey(coll, src, slen, (unsigned char *)tgt, tlen);
 }
 
@@ -2589,7 +2602,7 @@ void rtlStringToUpper(size32_t l, char * t)
 
 void rtlUnicodeToLower(size32_t l, UChar * t, char const * locale)
 {
-    UChar * buff = (UChar *)malloc(l*2);
+    UChar * buff = (UChar *)rtlMalloc(l*2);
     UErrorCode err = U_ZERO_ERROR;
     u_strToLower(buff, l, t, l, locale, &err);
     unicodeNormalizedCopy(buff, t, l);
@@ -2597,7 +2610,7 @@ void rtlUnicodeToLower(size32_t l, UChar * t, char const * locale)
 
 void rtlUnicodeToLowerX(size32_t & lenout, UChar * & out, size32_t l, const UChar * t, char const * locale)
 {
-    out = (UChar *)malloc(l*2);
+    out = (UChar *)rtlMalloc(l*2);
     lenout = l;
     UErrorCode err = U_ZERO_ERROR;
     u_strToLower(out, l, t, l, locale, &err);
@@ -2605,7 +2618,7 @@ void rtlUnicodeToLowerX(size32_t & lenout, UChar * & out, size32_t l, const UCha
 
 void rtlUnicodeToUpper(size32_t l, UChar * t, char const * locale)
 {
-    UChar * buff = (UChar *)malloc(l*2);
+    UChar * buff = (UChar *)rtlMalloc(l*2);
     UErrorCode err = U_ZERO_ERROR;
     u_strToUpper(buff, l, t, l, locale, &err);
     unicodeNormalizedCopy(buff, t, l);
@@ -3057,7 +3070,7 @@ void rtlCodepageToUnicodeX(unsigned & outlen, UChar * & out, unsigned inlen, cha
     UErrorCode err = U_ZERO_ERROR;
     outlen = ucnv_toUChars(conv, 0, 0, in, inlen, &err);
     if(err==U_BUFFER_OVERFLOW_ERROR) err = U_ZERO_ERROR;
-    out = (UChar *)malloc(outlen*2);
+    out = (UChar *)rtlMalloc(outlen*2);
     ucnv_toUChars(conv, out, outlen, in, inlen, &err);
 }
 
@@ -3069,7 +3082,7 @@ UChar * rtlCodepageToVUnicodeX(unsigned inlen, char const * in, char const * cod
     UErrorCode err = U_ZERO_ERROR;
     unsigned outlen = ucnv_toUChars(conv, 0, 0, in, inlen, &err);
     if(err == U_BUFFER_OVERFLOW_ERROR) err = U_ZERO_ERROR;
-    UChar * out = (UChar *)malloc((outlen+1)*2);
+    UChar * out = (UChar *)rtlMalloc((outlen+1)*2);
     ucnv_toUChars(conv,  out, outlen, in, inlen, &err);
     out[outlen] = 0x0000;
     vunicodeEnsureIsNormalizedX(outlen, out);
@@ -3095,7 +3108,7 @@ void rtlCodepageToUnicodeXUnescape(unsigned & outlen, UChar * & out, unsigned in
     UnicodeString normalized;
     normalizeUnicodeString(unescaped, normalized);
     outlen = normalized.length();
-    out = (UChar *)malloc(outlen*2);
+    out = (UChar *)rtlMalloc(outlen*2);
     normalized.extract(0, outlen, out);
 }
 
@@ -3109,7 +3122,7 @@ void rtlUnicodeToCodepageX(unsigned & outlen, char * & out, unsigned inlen, UCha
     UErrorCode err = U_ZERO_ERROR;
     outlen = ucnv_fromUChars(conv, 0, 0, in, inlen, &err);
     if(err == U_BUFFER_OVERFLOW_ERROR) err = U_ZERO_ERROR;
-    out = (char *)malloc(outlen);
+    out = (char *)rtlMalloc(outlen);
     ucnv_fromUChars(conv, out, outlen, in, inlen, &err);
 }
 
@@ -3128,7 +3141,7 @@ char * rtlUnicodeToVCodepageX(unsigned inlen, UChar const * in, char const * cod
     UErrorCode err = U_ZERO_ERROR;
     unsigned outlen = ucnv_fromUChars(conv, 0, 0, in, inlen, &err);
     if(err == U_BUFFER_OVERFLOW_ERROR) err = U_ZERO_ERROR;
-    char * out = (char *)malloc(outlen+1);
+    char * out = (char *)rtlMalloc(outlen+1);
     ucnv_fromUChars(conv, out, outlen, in, inlen, &err);
     out[outlen] = 0x00;
     return out;
@@ -3169,7 +3182,7 @@ void rtlUnicodeToEscapedStrX(unsigned & outlen, char * & out, unsigned inlen, UC
     StringBuffer outbuff;
     escapeUnicode(inlen, in, outbuff);
     outlen = outbuff.length();
-    out = (char *)malloc(outlen);
+    out = (char *)rtlMalloc(outlen);
     memcpy(out, outbuff.str(), outlen);
 }
 
@@ -3179,7 +3192,7 @@ void rtlUnicodeToQuotedUTF8X(unsigned & outlen, char * & out, unsigned inlen, UC
     unicode.findAndReplace("'", "\\'");
     //pre-flight length - may be more efficient to guess length and only re-extract if guess no good, but what to guess?
     outlen = unicode.extract(0, unicode.length(), 0, 0, UTF8_CODEPAGE);
-    out = (char *)malloc(outlen);
+    out = (char *)rtlMalloc(outlen);
     unicode.extract(0, unicode.length(), out, outlen, UTF8_CODEPAGE);
 }
 
@@ -3203,7 +3216,7 @@ bool rtlCodepageToCodepageX(unsigned & outlen, char * & out, unsigned maxoutlen,
     UErrorCode err = U_ZERO_ERROR;
 
     //GH->PG is there a better way of coding this with out temporary buffer?
-    char * tempBuffer = (char *)malloc(maxoutlen);
+    char * tempBuffer = (char *)rtlMalloc(maxoutlen);
     char * target = tempBuffer;
     ucnv_convertEx(outconv, inconv, &target, tempBuffer+maxoutlen, &in, in+inlen, NULL, NULL, NULL, NULL, TRUE, TRUE, &err);
     unsigned len = target - tempBuffer;
@@ -3212,7 +3225,7 @@ bool rtlCodepageToCodepageX(unsigned & outlen, char * & out, unsigned maxoutlen,
         out = tempBuffer;
     else
     {
-        out = (char *)realloc(tempBuffer, len);
+        out = (char *)rtlRealloc(tempBuffer, len);
         if (!out)
             out = tempBuffer;
     }
@@ -3235,7 +3248,7 @@ int rtlSingleUtf8ToCodepage(char * out, unsigned inlen, char const * in, char co
 
 void rtlStrToDataX(unsigned & tlen, void * & tgt, unsigned slen, const void * src)
 {
-    void * data  = malloc(slen);
+    void * data  = rtlMalloc(slen);
     memcpy(data, src, slen);
 
     tgt = data;
@@ -3244,7 +3257,7 @@ void rtlStrToDataX(unsigned & tlen, void * & tgt, unsigned slen, const void * sr
 
 void rtlStrToStrX(unsigned & tlen, char * & tgt, unsigned slen, const void * src)
 {
-    char * data  = (char *)malloc(slen);
+    char * data  = (char *)rtlMalloc(slen);
     memcpy(data, src, slen);
 
     tgt = data;
@@ -3253,7 +3266,7 @@ void rtlStrToStrX(unsigned & tlen, char * & tgt, unsigned slen, const void * src
 
 char * rtlStrToVStrX(unsigned slen, const void * src)
 {
-    char * data  = (char *)malloc(slen+1);
+    char * data  = (char *)rtlMalloc(slen+1);
     memcpy(data, src, slen);
     data[slen] = 0;
     return data;
@@ -3268,7 +3281,7 @@ char * rtlEStrToVStrX(unsigned slen, const char * src)
 
 void rtlEStrToStrX(unsigned & tlen, char * & tgt, unsigned slen, const char * src)
 {
-    char * data  = (char *)malloc(slen);
+    char * data  = (char *)rtlMalloc(slen);
     rtlEStrToStr(slen, data, slen, src);
 
     tgt = data;
@@ -3277,7 +3290,7 @@ void rtlEStrToStrX(unsigned & tlen, char * & tgt, unsigned slen, const char * sr
 
 void rtlStrToEStrX(unsigned & tlen, char * & tgt, unsigned slen, const char * src)
 {
-    char * data  = (char *)malloc(slen);
+    char * data  = (char *)rtlMalloc(slen);
     rtlStrToEStr(slen, data, slen, src);
 
     tgt = data;
@@ -3633,7 +3646,7 @@ ECLRTL_API void rtlTestGetPrimes(unsigned & num, void * & data)
     unsigned numPrimes = 6;
     unsigned size = sizeof(unsigned) * numPrimes;
 
-    unsigned * primes = (unsigned *)malloc(size);
+    unsigned * primes = (unsigned *)rtlMalloc(size);
     primes[0] = 1;
     primes[1] = 2;
     primes[2] = 3;
@@ -3648,7 +3661,7 @@ ECLRTL_API void rtlTestGetPrimes(unsigned & num, void * & data)
 ECLRTL_API void rtlTestFibList(bool & outAll, size32_t & outSize, void * & outData, bool inAll, size32_t inSize, const void * inData)
 {
     const unsigned * inList = (const unsigned *)inData;
-    unsigned * outList = (unsigned *)malloc(inSize);
+    unsigned * outList = (unsigned *)rtlMalloc(inSize);
     unsigned * curOut = outList;
     unsigned count = inSize / sizeof(*inList);
     unsigned prev = 0;
@@ -3697,6 +3710,11 @@ void rtlSysFail(int code, const char *msg)
     throw MakeStringException(MSGAUD_user, code, "%s", msg);
 }
 
+void rtlThrowOutOfMemory(int code, const char *msg)
+{
+    throw static_cast<IUserException *>(new CRtlFailException(code, msg));
+}
+
 void rtlReportRowOverflow(unsigned size, unsigned max)
 {
     throw MakeStringException(MSGAUD_user, 1000, "Row size %u exceeds the maximum size specified(%u)", size, max);
@@ -3748,7 +3766,7 @@ void deserializeDataX(size32_t & len, void * & data, MemoryBuffer &in)
 {
     free(data);
     in.read(sizeof(len), &len);
-    data = malloc(len);
+    data = rtlMalloc(len);
     in.read(len, data);
 }
 
@@ -3756,7 +3774,7 @@ void deserializeStringX(size32_t & len, char * & data, MemoryBuffer &in)
 {
     free(data);
     in.read(sizeof(len), &len);
-    data = (char *)malloc(len);
+    data = (char *)rtlMalloc(len);
     in.read(len, data);
 }
 
@@ -3764,7 +3782,7 @@ char * deserializeCStringX(MemoryBuffer &in)
 {
     unsigned len;
     in.read(sizeof(len), &len);
-    char * data = (char *)malloc(len+1);
+    char * data = (char *)rtlMalloc(len+1);
     in.read(len, data);
     data[len] = 0;
     return data;
@@ -3774,7 +3792,7 @@ void deserializeUnicodeX(size32_t & len, UChar * & data, MemoryBuffer &in)
 {
     free(data);
     in.read(sizeof(len), &len);
-    data = (UChar *)malloc(len*sizeof(UChar));
+    data = (UChar *)rtlMalloc(len*sizeof(UChar));
     in.read(len*sizeof(UChar), data);
 }
 
@@ -3783,7 +3801,7 @@ void deserializeUtf8X(size32_t & len, char * & data, MemoryBuffer &in)
     free(data);
     in.read(sizeof(len), &len);
     unsigned size = rtlUtf8Size(len, in.readDirect(0));
-    data = (char *)malloc(size);
+    data = (char *)rtlMalloc(size);
     in.read(size, data);
 }
 
@@ -3791,7 +3809,7 @@ UChar * deserializeVUnicodeX(MemoryBuffer &in)
 {
     unsigned len;
     in.read(sizeof(len), &len);
-    UChar * data = (UChar *)malloc((len+1)*sizeof(UChar));
+    UChar * data = (UChar *)rtlMalloc((len+1)*sizeof(UChar));
     in.read(len*sizeof(UChar), data);
     data[len] = 0;
     return data;
@@ -3802,7 +3820,7 @@ void deserializeSet(bool & isAll, size32_t & len, void * & data, MemoryBuffer &i
     free(data);
     in.read(isAll);
     in.read(sizeof(len), &len);
-    data = malloc(len);
+    data = rtlMalloc(len);
     in.read(len, data);
 }
 
@@ -4114,14 +4132,14 @@ void rtlVUnicodeToVUnicode(size32_t outlen, UChar * out, UChar const *in)
 
 void rtlUnicodeToUnicodeX(unsigned & tlen, UChar * & tgt, unsigned slen, UChar const * src)
 {
-    tgt  = (UChar *)malloc(slen*2);
+    tgt  = (UChar *)rtlMalloc(slen*2);
     memcpy(tgt, src, slen*2);
     tlen = slen;
 }
 
 UChar * rtlUnicodeToVUnicodeX(unsigned slen, UChar const * src)
 {
-    UChar * data  = (UChar *)malloc((slen+1)*2);
+    UChar * data  = (UChar *)rtlMalloc((slen+1)*2);
     memcpy(data, src, slen*2);
     data[slen] = 0x0000;
     return data;
@@ -4260,7 +4278,7 @@ void rtlUtf8ToUtf8(size32_t outlen, char * out, size32_t inlen, const char *in)
 void rtlUtf8ToUtf8X(size32_t & outlen, char * & out, size32_t inlen, const char *in)
 {
     unsigned insize = rtlUtf8Size(inlen, in);
-    char * buffer = (char *)malloc(insize);
+    char * buffer = (char *)rtlMalloc(insize);
     memcpy(buffer, in, insize);
     outlen = inlen;
     out = buffer;
@@ -4404,7 +4422,7 @@ ECLRTL_API void rtlUtf8SubStrFTX(unsigned & tlen, char * & tgt, unsigned slen, c
     unsigned startOffset = rtlUtf8Size(from, src);
     unsigned copySize = rtlUtf8Size(copylen, src+startOffset);
 
-    char * buffer = (char *)malloc(copySize + fillSize);
+    char * buffer = (char *)rtlMalloc(copySize + fillSize);
     memcpy(buffer, (byte *)src+startOffset, copySize);
     if (fillSize)
         memset(buffer+copySize, ' ', fillSize);
@@ -4419,7 +4437,7 @@ ECLRTL_API void rtlUtf8SubStrFX(unsigned & tlen, char * & tgt, unsigned slen, ch
     unsigned startOffset = rtlUtf8Size(from, src);
     unsigned copySize = rtlUtf8Size(len, src+startOffset);
 
-    char * buffer = (char *)malloc(copySize);
+    char * buffer = (char *)rtlMalloc(copySize);
     memcpy(buffer, (byte *)src+startOffset, copySize);
     tlen = len;
     tgt = buffer;
@@ -4583,7 +4601,7 @@ public:
         {
             if (_keep)
             {
-                sample = (char *)malloc(_len + 1);  //required for findstr
+                sample = (char *)rtlMalloc(_len + 1);  //required for findstr
                 memcpy(sample, _str + _from, _len);
                 sample[_len] = (char)NULL;
                 matched = boost::regex_search(sample, subs, *regEx);
@@ -4614,7 +4632,7 @@ public:
         if (matched && (n < subs.size()))
         {
             outlen = subs[n].second - subs[n].first;
-            out = (char *)malloc(outlen);
+            out = (char *)rtlMalloc(outlen);
             memcpy(out, subs[n].first, outlen);
         }
         else
@@ -4684,7 +4702,7 @@ public:
             throw MakeStringException(0, "Error in regex replace: %s (regex: %s)", e.what(), regEx.str().c_str());
         }
         outlen = tgt.length();
-        out = (char *)malloc(outlen);
+        out = (char *)rtlMalloc(outlen);
         memcpy(out, tgt.data(), outlen);
     }
 
@@ -4753,7 +4771,7 @@ public:
             int32_t start = n ? matcher->start(n, uerr) : matcher->start(uerr);
             int32_t end = n ? matcher->end(n, uerr) : matcher->end(uerr);
             outlen = end - start;
-            out = (UChar *)malloc(outlen*2);
+            out = (UChar *)rtlMalloc(outlen*2);
             sample.extract(start, outlen, out);
         }
         else
@@ -4837,7 +4855,7 @@ public:
         UnicodeString const fmt(replace, rlen);
         UnicodeString const tgt = replacer->replaceAll(fmt, err);
         outlen = tgt.length();
-        out = (UChar *)malloc(outlen*2);
+        out = (UChar *)rtlMalloc(outlen*2);
         tgt.extract(0, outlen, out);
         delete replacer;
     }
@@ -4945,7 +4963,7 @@ ECLRTL_API void rtlCreateRange(size32_t & outlen, char * & out, unsigned fieldLe
     }
 
     outlen = fieldLen;
-    out = (char *)malloc(fieldLen);
+    out = (char *)rtlMalloc(fieldLen);
     if (len >= compareLen)
         memcpy(out, str, compareLen);
     else
@@ -5018,7 +5036,7 @@ ECLRTL_API void rtlCreateUnicodeRange(size32_t & outlen, UChar * & out, unsigned
     }
 
     outlen = fieldLen;
-    out = (UChar *)malloc(fieldLen*sizeof(UChar));
+    out = (UChar *)rtlMalloc(fieldLen*sizeof(UChar));
     if (len >= compareLen)
         memcpy(out, str, compareLen*sizeof(UChar));
     else
@@ -5097,13 +5115,13 @@ public:
     {
         //convert from source to utf-16: try to avoid preflighting by guessing upper bound
         //unicode length in UChars equal source length in chars if single byte encoding, and be less for multibyte
-        UChar * ubuff = (UChar *)malloc(sourceLength*2);
+        UChar * ubuff = (UChar *)rtlMalloc(sourceLength*2);
         int32_t ulen = ucnv_toUChars(srccnv, ubuff, sourceLength, source, sourceLength, &uerr);
         if(ulen > (int32_t)sourceLength)
         {
             //okay, so our guess was wrong, and we have to reallocate
             free(ubuff);
-            ubuff = (UChar *)malloc(ulen*2);
+            ubuff = (UChar *)rtlMalloc(ulen*2);
             ucnv_toUChars(srccnv, ubuff, ulen, source, sourceLength, &uerr);
         }
         if(preflight)
@@ -5111,13 +5129,13 @@ public:
             //convert from utf-16 to target: preflight to get buffer of exactly the right size
             UErrorCode uerr2 = uerr; //preflight has to use copy of error code, as it is considered an 'error'
             int32_t tlen = ucnv_fromUChars(tgtcnv, 0, 0, ubuff, ulen, &uerr2);
-            target = (char *)malloc(tlen);
+            target = (char *)rtlMalloc(tlen);
             targetLength = ucnv_fromUChars(tgtcnv, target, tlen, ubuff, ulen, &uerr);
         }
         else
         {
             //convert from utf-16 to target: avoid preflighting by allocating buffer of maximum size
-            target = (char *)malloc(ulen*tgtMaxRatio);
+            target = (char *)rtlMalloc(ulen*tgtMaxRatio);
             targetLength = ucnv_fromUChars(tgtcnv, target, ulen*tgtMaxRatio, ubuff, ulen, &uerr);
         }
         free(ubuff);
@@ -5447,7 +5465,7 @@ void rtlRowBuilder::forceAvailable(size32_t size)
 {
     const size32_t chunkSize = 64;
     maxsize = (size + chunkSize-1) & ~(chunkSize-1);
-    ptr = realloc(ptr, maxsize);
+    ptr = rtlRealloc(ptr, maxsize);
 }
 
 //---------------------------------------------------------------------------

+ 1 - 1
rtl/eclrtl/eclrtl.hpp

@@ -91,7 +91,6 @@ interface ICompiledUStrRegExpr
 };
 
 //-----------------------------------------------------------------------------
-
 ECLRTL_API void * rtlMalloc(size32_t size);
 ECLRTL_API void rtlFree(void * x);
 ECLRTL_API void * rtlRealloc(void * _ptr, size32_t size);
@@ -384,6 +383,7 @@ ECLRTL_API void rtlSysFail(int code, const char *msg);
 ECLRTL_API void rtlFailUnexpected();
 ECLRTL_API void rtlFailOnAssert();
 ECLRTL_API void rtlFailDivideByZero();
+ECLRTL_API void rtlThrowOutOfMemory(int code, const char *msg);
 
 ECLRTL_API void rtlReportFieldOverflow(unsigned size, unsigned max, const char * name);
 ECLRTL_API void rtlReportRowOverflow(unsigned size, unsigned max);