Skip to content
This repository was archived by the owner on Jan 23, 2023. It is now read-only.

Commit 1133fbc

Browse files
committed
Use std::vector instead of calloc
This matches what we do in other places in calendarData.cpp, the RAII pattern will make it easier to not leak memory.
1 parent ab9afce commit 1133fbc

File tree

1 file changed

+6
-18
lines changed

1 file changed

+6
-18
lines changed

src/corefx/System.Globalization.Native/calendarData.cpp

Lines changed: 6 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -329,20 +329,15 @@ bool InvokeCallbackForDatePattern(const char* locale,
329329
UErrorCode ignore = U_ZERO_ERROR;
330330
int32_t patternLen = udat_toPattern(pFormat, false, nullptr, 0, &ignore);
331331

332-
UChar* pattern = (UChar*)calloc(patternLen + 1, sizeof(UChar));
332+
std::vector<UChar> pattern(patternLen + 1, '\0');
333333

334-
if (pattern == nullptr)
335-
return false;
336-
337-
udat_toPattern(pFormat, false, pattern, patternLen + 1, &err);
334+
udat_toPattern(pFormat, false, pattern.data(), patternLen + 1, &err);
338335

339336
if (U_SUCCESS(err))
340337
{
341-
callback(pattern, context);
338+
callback(pattern.data(), context);
342339
}
343340

344-
free(pattern);
345-
346341
return U_SUCCESS(err);
347342
}
348343

@@ -368,22 +363,15 @@ bool InvokeCallbackForDateTimePattern(const char* locale,
368363
UErrorCode ignore = U_ZERO_ERROR;
369364
int32_t patternLen = udatpg_getBestPattern(pGenerator, patternSkeleton, -1, nullptr, 0, &ignore);
370365

371-
UChar* bestPattern = (UChar*)calloc(patternLen + 1, sizeof(UChar));
372-
373-
if (bestPattern == nullptr)
374-
{
375-
return false;
376-
}
366+
std::vector<UChar> bestPattern(patternLen + 1, '\0');
377367

378-
udatpg_getBestPattern(pGenerator, patternSkeleton, -1, bestPattern, patternLen + 1, &err);
368+
udatpg_getBestPattern(pGenerator, patternSkeleton, -1, bestPattern.data(), patternLen + 1, &err);
379369

380370
if (U_SUCCESS(err))
381371
{
382-
callback(bestPattern, context);
372+
callback(bestPattern.data(), context);
383373
}
384374

385-
free(bestPattern);
386-
387375
return U_SUCCESS(err);
388376
}
389377

0 commit comments

Comments
 (0)