Skip to content

Commit f6e8d52

Browse files
committed
add callback function to properly report errors
1 parent d721e8b commit f6e8d52

File tree

2 files changed

+27
-0
lines changed

2 files changed

+27
-0
lines changed

llvm/lib/Support/CharSet.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,12 @@ CharSetConverterICU::convertString(StringRef Source,
148148
Result.resize_for_overwrite(Capacity);
149149
char *Output = static_cast<char *>(Result.data());
150150
UErrorCode EC = U_ZERO_ERROR;
151+
152+
ucnv_setToUCallBack(&*FromConvDesc, UCNV_TO_U_CALLBACK_STOP, NULL, NULL, NULL,
153+
&EC);
154+
ucnv_setToUCallBack(&*ToConvDesc, UCNV_TO_U_CALLBACK_STOP, NULL, NULL, NULL,
155+
&EC);
156+
151157
do {
152158
EC = U_ZERO_ERROR;
153159
const char *Input = In;

llvm/unittests/Support/CharSetTest.cpp

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,8 @@ static const char EarthISO2022[] =
5050
"\x45\x61\x72\x74\x68\x1B\x24\x42\x43\x4F\x35\x65\x1B\x28\x42";
5151
static const char EarthIBM939[] =
5252
"\xc5\x81\x99\xa3\x88\x0e\x45\xc2\x48\xdb\x0f";
53+
static const char EarthUTFExtraPartial[] =
54+
"\x45\x61\x72\x74\x68\xe5\x9c\xb0\xe7\x90\x83\xe5";
5355

5456
TEST(CharSet, FromUTF8) {
5557
// Hello string.
@@ -171,6 +173,25 @@ TEST(CharSet, ShiftState2022) {
171173
EXPECT_STREQ(EarthISO2022, static_cast<std::string>(Dst).c_str());
172174
}
173175

176+
TEST(CharSet, ShiftState2022Partial) {
177+
// Earth string.
178+
StringRef Src(EarthUTFExtraPartial);
179+
SmallString<8> Dst;
180+
181+
ErrorOr<CharSetConverter> ConvTo2022 =
182+
CharSetConverter::create("UTF-8", "ISO-2022-JP");
183+
// Stop test if conversion is not supported (no underlying iconv support).
184+
if (!ConvTo2022) {
185+
ASSERT_EQ(ConvTo2022.getError(),
186+
std::make_error_code(std::errc::invalid_argument));
187+
return;
188+
}
189+
190+
// Check that the string is properly converted.
191+
std::error_code EC = ConvTo2022->convert(Src, Dst);
192+
EXPECT_TRUE(EC);
193+
}
194+
174195
TEST(CharSet, ShiftStateIBM939) {
175196
// Earth string.
176197
StringRef Src(EarthUTF);

0 commit comments

Comments
 (0)