Skip to content

Commit 68a61da

Browse files
committed
ICU-22497 Fix buffer-overflow READ for toLanguateTag
1 parent 35645ab commit 68a61da

File tree

2 files changed

+11
-2
lines changed

2 files changed

+11
-2
lines changed

icu4c/source/common/uloc_tag.cpp

+3-2
Original file line numberDiff line numberDiff line change
@@ -1952,8 +1952,8 @@ _appendPrivateuseToLanguageTag(const char* localeID, icu::ByteSink& sink, UBool
19521952
len = (int32_t)uprv_strlen(PRIVUSE_VARIANT_PREFIX);
19531953
if (reslen < capacity) {
19541954
uprv_memcpy(tmpAppend + reslen, PRIVUSE_VARIANT_PREFIX, uprv_min(len, capacity - reslen));
1955+
reslen += uprv_min(len, capacity - reslen);
19551956
}
1956-
reslen += len;
19571957

19581958
if (reslen < capacity) {
19591959
tmpAppend[reslen++] = SEP;
@@ -1965,8 +1965,8 @@ _appendPrivateuseToLanguageTag(const char* localeID, icu::ByteSink& sink, UBool
19651965
len = (int32_t)uprv_strlen(pPriv);
19661966
if (reslen < capacity) {
19671967
uprv_memcpy(tmpAppend + reslen, pPriv, uprv_min(len, capacity - reslen));
1968+
reslen += uprv_min(len, capacity - reslen);
19681969
}
1969-
reslen += len;
19701970
}
19711971
}
19721972
/* reset private use starting position */
@@ -1984,6 +1984,7 @@ _appendPrivateuseToLanguageTag(const char* localeID, icu::ByteSink& sink, UBool
19841984

19851985
if (U_SUCCESS(*status)) {
19861986
len = reslen;
1987+
U_ASSERT(reslen <= capacity);
19871988
sink.Append(tmpAppend, len);
19881989
}
19891990
}

icu4c/source/test/intltest/loctest.cpp

+8
Original file line numberDiff line numberDiff line change
@@ -5980,6 +5980,8 @@ void LocaleTest::TestToLanguageTag() {
59805980
{"und-Latn-x-private", "und-Latn-x-private"},
59815981
{"und-1994-biske-rozaj", "und-1994-biske-rozaj"},
59825982
{"und-1994-biske-rozaj-x-private", "und-1994-biske-rozaj-x-private"},
5983+
// ICU-22497
5984+
{"-ins0-ins17Rz-yqyq-UWLF-uRyq-UWLF-uRRyq-UWLF-uR-UWLF-uRns0-ins17Rz-yq-UWLF-uRyq-UWLF-uRRyq-LF-uRyq-UWLF-uRRyq-UWLF-uRq-UWLF-uRyq-UWLF-uRRyq-UWLF-uR", ""},
59835985
};
59845986
int32_t i;
59855987
for (i=0; i < UPRV_LENGTHOF(testCases); i++) {
@@ -5993,6 +5995,12 @@ void LocaleTest::TestToLanguageTag() {
59935995
tag.c_str(),
59945996
u_errorName(status));
59955997
}
5998+
// Test ICU-22497
5999+
status = U_ZERO_ERROR;
6000+
icu::Locale locale(otag.c_str());
6001+
char buf[245];
6002+
icu::CheckedArrayByteSink sink(buf, sizeof(buf));
6003+
locale.toLanguageTag(sink, status);
59966004
}
59976005
}
59986006

0 commit comments

Comments
 (0)