ICU-4384 Handle malloc error issue in new parsing code.

X-SVN-Rev: 23185
This commit is contained in:
Michael Ow 2008-01-08 23:42:53 +00:00
parent b0900e7924
commit c80e40dcb9
2 changed files with 34 additions and 6 deletions

View File

@ -1,7 +1,7 @@
/*
******************************************************************************
*
* Copyright (C) 2001-2007, International Business Machines
* Copyright (C) 2001-2008, International Business Machines
* Corporation and others. All Rights Reserved.
*
******************************************************************************
@ -221,6 +221,7 @@ u_vsnprintf_u(UChar *buffer,
va_list ap)
{
int32_t written = 0; /* haven't written anything yet */
int32_t result = 0; /* test the return value of u_printf_parse */
u_localized_print_string outStr;
@ -237,8 +238,8 @@ u_vsnprintf_u(UChar *buffer,
}
/* parse and print the whole format string */
u_printf_parse(&g_sprintf_stream_handler, patternSpecification, &outStr, &outStr, &outStr.fBundle, &written, ap);
result = u_printf_parse(&g_sprintf_stream_handler, patternSpecification, &outStr, &outStr, &outStr.fBundle, &written, ap);
/* Terminate the buffer, if there's room. */
if (outStr.available > 0) {
buffer[outStr.len - outStr.available] = 0x0000;
@ -247,6 +248,10 @@ u_vsnprintf_u(UChar *buffer,
/* Release the cloned bundle, if we cloned it. */
u_locbund_close(&outStr.fBundle);
// parsing error
if (result < 0) {
return result;
}
/* return # of UChars written */
return written;
}

View File

@ -1,7 +1,7 @@
/*
******************************************************************************
*
* Copyright (C) 1998-2007, International Business Machines
* Copyright (C) 1998-2008, International Business Machines
* Corporation and others. All Rights Reserved.
*
******************************************************************************
@ -1091,6 +1091,24 @@ ufmt_args* parseArguments(const UChar *alias, va_list ap) {
islonglong = (UBool*)uprv_malloc(sizeof(UBool) * size);
arglist = (ufmt_args*)uprv_malloc(sizeof(ufmt_args) * size);
// If malloc failed, return NULL
if (!typelist || !islonglong || !arglist) {
if (typelist) {
uprv_free(typelist);
}
if (islonglong) {
uprv_free(islonglong);
}
if (arglist) {
uprv_free(arglist);
}
arglist = -1;
goto endParse;
}
/* reset alias back to the beginning */
alias = aliasStart;
@ -1174,7 +1192,7 @@ ufmt_args* parseArguments(const UChar *alias, va_list ap) {
uprv_free(typelist);
uprv_free(islonglong);
endParse:
return arglist;
}
@ -1205,8 +1223,13 @@ u_printf_parse(const u_printf_stream_handler *streamHandler,
if (!locStringContext || locStringContext->available >= 0) {
/* get the parsed list of argument types */
arglist = parseArguments(orgAlias, ap);
// Return error if parsing failed.
if (arglist == -1) {
return arglist;
}
}
/* iterate through the pattern */
while(!locStringContext || locStringContext->available >= 0) {