Skip to content

Commit 5d6d197

Browse files
committed
ICU-22466 Fix incorrect memory read while the locale is bogus
ICU-22466 Fix illegal read ICU-22466 Fix memory issue
1 parent 667ee72 commit 5d6d197

3 files changed

Lines changed: 18 additions & 1 deletion

File tree

icu4c/source/common/loclikely.cpp

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -483,7 +483,16 @@ _uloc_addLikelySubtags(const char* localeID,
483483
if(U_FAILURE(*err)) {
484484
goto error;
485485
}
486-
icu::LSR lsr = likelySubtags->makeMaximizedLsrFrom(icu::Locale::createFromName(localeID), true, *err);
486+
// We need to keep l on the stack because lsr may point into internal
487+
// memory of l.
488+
icu::Locale l = icu::Locale::createFromName(localeID);
489+
if (l.isBogus()) {
490+
goto error;
491+
}
492+
icu::LSR lsr = likelySubtags->makeMaximizedLsrFrom(l, true, *err);
493+
if(U_FAILURE(*err)) {
494+
goto error;
495+
}
487496
const char* language = lsr.language;
488497
if (uprv_strcmp(language, "und") == 0) {
489498
language = "";

icu4c/source/common/loclikelysubtags.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -456,6 +456,10 @@ XLikelySubtags::~XLikelySubtags() {
456456
LSR XLikelySubtags::makeMaximizedLsrFrom(const Locale &locale,
457457
bool returnInputIfUnmatch,
458458
UErrorCode &errorCode) const {
459+
if (locale.isBogus()) {
460+
errorCode = U_ILLEGAL_ARGUMENT_ERROR;
461+
return LSR("", "", "", LSR::EXPLICIT_LSR);
462+
}
459463
const char *name = locale.getName();
460464
if (uprv_isAtSign(name[0]) && name[1] == 'x' && name[2] == '=') { // name.startsWith("@x=")
461465
// Private use language tag x-subtag-subtag... which CLDR changes to

icu4c/source/test/cintltst/cloctst.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6886,6 +6886,10 @@ static void TestIsRightToLeft() {
68866886
if(uloc_isRightToLeft("root") || !uloc_isRightToLeft("EN-HEBR")) {
68876887
log_err("uloc_isRightToLeft() failed");
68886888
}
6889+
// ICU-22466 Make sure no crash when locale is bogus
6890+
uloc_isRightToLeft(
6891+
"uF-Vd_u-VaapoPos-u1-Pos-u1-Pos-u1-Pos-u1-oPos-u1-Pufu1-PuosPos-u1-Pos-u1-Pos-u1-Pzghu1-Pos-u1-PoP-u1@osus-u1");
6892+
uloc_isRightToLeft("-Xa");
68896893
}
68906894

68916895
typedef struct {

0 commit comments

Comments
 (0)