mirror of
https://github.com/unicode-org/icu.git
synced 2025-04-07 06:25:30 +00:00
ICU-21768 Fixed (u_snprintf improperly counts the required buffer size). Modified TestSnprintf to test the null buffer case.
This commit is contained in:
parent
80ee559205
commit
1393face12
2 changed files with 16 additions and 3 deletions
|
@ -41,6 +41,12 @@ u_sprintf_write(void *context,
|
|||
int32_t count)
|
||||
{
|
||||
u_localized_print_string *output = (u_localized_print_string *)context;
|
||||
|
||||
/* just calculating buffer size */
|
||||
if (output->str == 0) {
|
||||
return count;
|
||||
}
|
||||
|
||||
int32_t size = ufmt_min(count, output->available);
|
||||
|
||||
u_strncpy(output->str + (output->len - output->available), str, size);
|
||||
|
@ -58,6 +64,12 @@ u_sprintf_pad_and_justify(void *context,
|
|||
int32_t written = 0;
|
||||
int32_t lengthOfResult = resultLen;
|
||||
|
||||
/* just calculating buffer size */
|
||||
if (output->str == 0 &&
|
||||
info->fWidth != -1 && resultLen < info->fWidth) {
|
||||
return info->fWidth;
|
||||
}
|
||||
|
||||
resultLen = ufmt_min(resultLen, output->available);
|
||||
|
||||
/* pad and justify, if needed */
|
||||
|
|
|
@ -315,9 +315,10 @@ static void TestLocalizedString(void) {
|
|||
#if !UCONFIG_NO_FORMATTING
|
||||
#define Test_u_snprintf(limit, format, value, expectedSize, expectedStr) UPRV_BLOCK_MACRO_BEGIN { \
|
||||
u_uastrncpy(testStr, "xxxxxxxxxxxxxx", UPRV_LENGTHOF(testStr));\
|
||||
size = u_snprintf(testStr, limit, format, value);\
|
||||
size = u_snprintf(0, 0, format, value);\
|
||||
written = u_snprintf(testStr, limit, format, value);\
|
||||
u_austrncpy(cTestResult, testStr, UPRV_LENGTHOF(cTestResult));\
|
||||
if (size != expectedSize || strcmp(cTestResult, expectedStr) != 0) {\
|
||||
if (size != written || size != expectedSize || strcmp(cTestResult, expectedStr) != 0) {\
|
||||
log_err("Unexpected formatting. size=%d expectedSize=%d cTestResult=%s expectedStr=%s\n",\
|
||||
size, expectedSize, cTestResult, expectedStr);\
|
||||
}\
|
||||
|
@ -332,7 +333,7 @@ static void TestSnprintf(void) {
|
|||
#if !UCONFIG_NO_FORMATTING
|
||||
UChar testStr[256];
|
||||
char cTestResult[256];
|
||||
int32_t size;
|
||||
int32_t size, written;
|
||||
|
||||
Test_u_snprintf(0, "%d", 123, 3, "xxxxxxxxxxxxxx");
|
||||
Test_u_snprintf(2, "%d", 123, 3, "12xxxxxxxxxxxx");
|
||||
|
|
Loading…
Add table
Reference in a new issue