From 6b12c9cdef1394fd3d31d78b46484df8997f5f66 Mon Sep 17 00:00:00 2001 From: Ram Viswanadha Date: Tue, 22 May 2001 18:00:55 +0000 Subject: [PATCH] ICU-954 Fix memory leak and converter flush X-SVN-Rev: 4753 --- icu4c/source/tools/toolutil/ucbuf.c | 44 ++++++++++++++--------------- 1 file changed, 22 insertions(+), 22 deletions(-) diff --git a/icu4c/source/tools/toolutil/ucbuf.c b/icu4c/source/tools/toolutil/ucbuf.c index 8c088ea044..253528b69d 100644 --- a/icu4c/source/tools/toolutil/ucbuf.c +++ b/icu4c/source/tools/toolutil/ucbuf.c @@ -22,7 +22,8 @@ #include "unicode/ustring.h" #include "ucbuf.h" -#define MAX_BUF 1000 +#define MAX_IN_BUF 1000 +#define MAX_U_BUF 1500 /* Autodetects UTF8, UTF-16-BigEndian and UTF-16-LittleEndian BOMs*/ U_CAPI UBool U_EXPORT2 @@ -59,25 +60,20 @@ ucbuf_fillucbuf( UCHARBUF* buf,UErrorCode* err){ int numRead=0; int offset=0; - cbuf =(char*)uprv_malloc(sizeof(char) * MAX_BUF); - if(buf->buffer==NULL){ - /* allocate buffers */ - pTarget = (UChar*) uprv_malloc(sizeof(UChar)* MAX_BUF); - }else{ - pTarget = buf->buffer; - /* check if we arrived here without exhausting the buffer*/ - if(buf->currentPosbufLimit){ - offset= buf->bufLimit-buf->currentPos; - memmove(buf->buffer,buf->currentPos,offset* sizeof(UChar)); - memset(pTarget+offset,0xff,sizeof(UChar)*(MAX_BUF-offset)); - }else{ - memset(pTarget,0xff,sizeof(UChar)*MAX_BUF); - } - + cbuf =(char*)uprv_malloc(sizeof(char) * MAX_IN_BUF); + pTarget = buf->buffer; + /* check if we arrived here without exhausting the buffer*/ + if(buf->currentPosbufLimit){ + offset= buf->bufLimit-buf->currentPos; + memmove(buf->buffer,buf->currentPos,offset* sizeof(UChar)); } + +#if DEBUG + memset(pTarget+offset,0xff,sizeof(UChar)*(MAX_IN_BUF-offset)); +#endif /* read the file */ - numRead=T_FileStream_read(buf->in,cbuf,MAX_BUF-offset); + numRead=T_FileStream_read(buf->in,cbuf,MAX_IN_BUF-offset); buf->remaining-=numRead; target=pTarget; @@ -93,9 +89,8 @@ ucbuf_fillucbuf( UCHARBUF* buf,UErrorCode* err){ } }else{ u_charsToUChars(cbuf,target+offset,numRead); - numRead=((buf->remaining>MAX_BUF)? MAX_BUF:numRead+offset); + numRead=((buf->remaining>MAX_IN_BUF)? MAX_IN_BUF:numRead+offset); } - buf->buffer= pTarget; buf->currentPos = pTarget; buf->bufLimit=pTarget+numRead; uprv_free(cbuf); @@ -183,11 +178,16 @@ ucbuf_open(FileStream* in, const char* cp,UErrorCode* err){ if(buf){ buf->in=in; buf->remaining=T_FileStream_size(in); - buf->buffer=NULL; - buf->currentPos=NULL; - buf->bufLimit=NULL; + buf->buffer=(UChar*) uprv_malloc(sizeof(UChar)* MAX_U_BUF); + if (buf->buffer == NULL) { + *err = U_MEMORY_ALLOCATION_ERROR; + return NULL; + } + buf->currentPos=buf->buffer; + buf->bufLimit=buf->buffer; if(*cp!='\0'){ buf->conv=ucnv_open(cp,err); + buf->remaining-=3; }else{ buf->conv=NULL; }