From: Markus Scherer Date: Tue, 16 Jun 2015 12:34:36 +0000 (+0000) Subject: ICU-11719 better memory management in genrb parseImport() using string class & auto... X-Git-Tag: milestone-59-0-1~1062 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=5b21336927ad33d3fce1f1ac5f53d03909f12452;p=icu ICU-11719 better memory management in genrb parseImport() using string class & auto pointers X-SVN-Rev: 37576 --- diff --git a/icu4c/source/tools/genrb/parse.cpp b/icu4c/source/tools/genrb/parse.cpp index c0ef6bc3bbc..f71a96bdedd 100644 --- a/icu4c/source/tools/genrb/parse.cpp +++ b/icu4c/source/tools/genrb/parse.cpp @@ -73,6 +73,7 @@ #define CLOSESQBRACKET 0x005D using icu::CharString; +using icu::LocalMemory; using icu::LocalPointer; using icu::LocalUCHARBUFPointer; using icu::StringPiece; @@ -1585,15 +1586,8 @@ parseInteger(ParseState* state, char *tag, uint32_t startline, const struct UStr static struct SResource * parseImport(ParseState* state, char *tag, uint32_t startline, const struct UString* comment, UErrorCode *status) { - struct SResource *result; - FileStream *file; - int32_t len; - uint8_t *data; - char *filename; uint32_t line; - char *fullname = NULL; - filename = getInvariantString(state, &line, NULL, status); - + LocalMemory filename(getInvariantString(state, &line, NULL, status)); if (U_FAILURE(*status)) { return NULL; @@ -1603,7 +1597,6 @@ parseImport(ParseState* state, char *tag, uint32_t startline, const struct UStri if (U_FAILURE(*status)) { - uprv_free(filename); return NULL; } @@ -1612,90 +1605,36 @@ parseImport(ParseState* state, char *tag, uint32_t startline, const struct UStri } /* Open the input file for reading */ - if (state->inputdir == NULL) - { -#if 1 - /* - * Always save file file name, even if there's - * no input directory specified. MIGHT BREAK SOMETHING - */ - int32_t filenameLength = uprv_strlen(filename); - - fullname = (char *) uprv_malloc(filenameLength + 1); - uprv_strcpy(fullname, filename); -#endif - - file = T_FileStream_open(filename, "rb"); + CharString fullname; + if (state->inputdir != NULL) { + fullname.append(state->inputdir, *status); } - else - { - - int32_t count = (int32_t)uprv_strlen(filename); - - if (state->inputdir[state->inputdirLength - 1] != U_FILE_SEP_CHAR) - { - fullname = (char *) uprv_malloc(state->inputdirLength + count + 2); - - /* test for NULL */ - if(fullname == NULL) - { - *status = U_MEMORY_ALLOCATION_ERROR; - return NULL; - } - - uprv_strcpy(fullname, state->inputdir); - - fullname[state->inputdirLength] = U_FILE_SEP_CHAR; - fullname[state->inputdirLength + 1] = '\0'; - - uprv_strcat(fullname, filename); - } - else - { - fullname = (char *) uprv_malloc(state->inputdirLength + count + 1); - - /* test for NULL */ - if(fullname == NULL) - { - *status = U_MEMORY_ALLOCATION_ERROR; - return NULL; - } - - uprv_strcpy(fullname, state->inputdir); - uprv_strcat(fullname, filename); - } - - file = T_FileStream_open(fullname, "rb"); - + fullname.appendPathPart(filename.getAlias(), *status); + if (U_FAILURE(*status)) { + return NULL; } + FileStream *file = T_FileStream_open(fullname.data(), "rb"); if (file == NULL) { - error(line, "couldn't open input file %s", filename); + error(line, "couldn't open input file %s", filename.getAlias()); *status = U_FILE_ACCESS_ERROR; return NULL; } - len = T_FileStream_size(file); - data = (uint8_t*)uprv_malloc(len * sizeof(uint8_t)); - /* test for NULL */ - if(data == NULL) + int32_t len = T_FileStream_size(file); + LocalMemory data; + if(data.allocateInsteadAndCopy(len) == NULL) { *status = U_MEMORY_ALLOCATION_ERROR; T_FileStream_close (file); return NULL; } - /* int32_t numRead = */ T_FileStream_read (file, data, len); + /* int32_t numRead = */ T_FileStream_read(file, data.getAlias(), len); T_FileStream_close (file); - result = bin_open(state->bundle, tag, len, data, fullname, comment, status); - - uprv_free(data); - uprv_free(filename); - uprv_free(fullname); - - return result; + return bin_open(state->bundle, tag, len, data.getAlias(), fullname.data(), comment, status); } static struct SResource *