summaryrefslogtreecommitdiff
path: root/sal
diff options
context:
space:
mode:
authorStephan Bergmann <sbergman@redhat.com>2019-09-04 14:31:30 +0200
committerStephan Bergmann <sbergman@redhat.com>2019-09-04 19:56:33 +0200
commitcd563e7b807fe038ebefb228e70bc587c040d17d (patch)
tree9f0ed635bb96ea419d26b84e262067e85bd986e3 /sal
parent1496a1831d1be0a2d24be9fe3ecf627b2664e938 (diff)
Do not exclude Unicode noncharacters from rtl_convertUnicodeToText
For one, that broke round-tripping with e.g. UTF-8 (see the test case added to Test::testComplex in sal/qa/rtl/textenc/rtl_textcvt.cxx) which did not treat noncharacters as invalid. For another, <https://unicode.org/faq/private_use.html#nonchar7> is meanwhile quite clear on the matter: "Q: Are noncharacters prohibited in interchange? "A: This question has led to some controversy, because the Unicode Standard has been somewhat ambiguous about the status of noncharacters. The formal wording of the definition of 'noncharacter' in the standard has always indicated that noncharacters 'should never be interchanged.' That led some people to assume that the definition actually meant 'shall not be interchanged' and that therefore the presence of a noncharacter in any Unicode string immediately rendered that string malformed according to the standard. But the intended use of noncharacters requires the ability to exchange them in a limited context, at least across APIs and even through data files and other means of 'interchange', so that they can be processed as intended. The choice of the word 'should' in the original definition was deliberate, and indicated that one should not try to interchange noncharacters precisely because their interpretation is strictly internal to whatever implementation uses them, so they have no publicly interchangeable semantics. But other informative wording in the text of the core specification and in the character names list was differently and more strongly worded, leading to contradictory interpretations. "Given this ambiguity of intent, in 2013 the UTC issued Corrigendum #9, which deleted the phrase 'and that should never be interchanged' from the definition of noncharacters, to make it clear that prohibition from interchange is not part of the formal definition of noncharacters. Corrigendum #9 has been incorporated into the core specification for Unicode 7.0. "Q: Are noncharacters invalid in Unicode strings and UTFs? "A: Absolutely not. Noncharacters do not cause a Unicode string to be ill-formed in any UTF. This can be seen explicitly in the table above, where every noncharacter code point has a well-formed representation in UTF-32, in UTF-16, and in UTF-8. An implementation which converts noncharacter code points between one UTF representation and another must preserve these values correctly. The fact that they are called 'noncharacters' and are not intended for open interchange does not mean that they are somehow illegal or invalid code points which make strings containing them invalid." Change-Id: I4fcc0156e3d2fd305a7c7bb0c7b3dbef846c9e64 Reviewed-on: https://gerrit.libreoffice.org/78598 Tested-by: Jenkins Reviewed-by: Stephan Bergmann <sbergman@redhat.com>
Diffstat (limited to 'sal')
-rw-r--r--sal/qa/rtl/textenc/rtl_textcvt.cxx30
-rw-r--r--sal/textenc/convertbig5hkscs.cxx12
-rw-r--r--sal/textenc/converteuctw.cxx14
-rw-r--r--sal/textenc/convertgb18030.cxx14
-rw-r--r--sal/textenc/convertisciidevangari.cxx17
-rw-r--r--sal/textenc/convertiso2022cn.cxx14
-rw-r--r--sal/textenc/convertiso2022jp.cxx14
-rw-r--r--sal/textenc/convertiso2022kr.cxx14
-rw-r--r--sal/textenc/convertsinglebytetobmpunicode.cxx12
-rw-r--r--sal/textenc/tcvtutf8.cxx11
-rw-r--r--sal/textenc/unichars.hxx8
11 files changed, 110 insertions, 50 deletions
diff --git a/sal/qa/rtl/textenc/rtl_textcvt.cxx b/sal/qa/rtl/textenc/rtl_textcvt.cxx
index 2f5359b32c77..af9ccca345e7 100644
--- a/sal/qa/rtl/textenc/rtl_textcvt.cxx
+++ b/sal/qa/rtl/textenc/rtl_textcvt.cxx
@@ -457,6 +457,8 @@ public:
void testInvalidUtf8();
+ void testInvalidUnicode();
+
void testSRCBUFFERTOSMALL();
void testMime();
@@ -471,6 +473,7 @@ public:
CPPUNIT_TEST(testComplexCut);
CPPUNIT_TEST(testInvalidUtf7);
CPPUNIT_TEST(testInvalidUtf8);
+ CPPUNIT_TEST(testInvalidUnicode);
CPPUNIT_TEST(testSRCBUFFERTOSMALL);
CPPUNIT_TEST(testMime);
CPPUNIT_TEST(testWindows);
@@ -2336,6 +2339,15 @@ void Test::testComplex() {
true,
false,
RTL_UNICODETOTEXT_FLAGS_UNDEFINED_ERROR },
+ { RTL_TEXTENCODING_UTF8,
+ RTL_CONSTASCII_STRINGPARAM("\xEF\xBF\xBF"),
+ {0xFFFF},
+ 1,
+ false,
+ true,
+ true,
+ false,
+ RTL_UNICODETOTEXT_FLAGS_UNDEFINED_ERROR },
// Test Java UTF-8:
@@ -2960,6 +2972,24 @@ void Test::testInvalidUtf8() {
}
}
+void Test::testInvalidUnicode() {
+ auto const converter = rtl_createUnicodeToTextConverter(RTL_TEXTENCODING_UTF8);
+ CPPUNIT_ASSERT(converter != nullptr);
+ sal_Unicode const input[] = {0xDC00}; // lone low surrogate
+ char buf[TEST_STRING_SIZE];
+ sal_uInt32 info;
+ sal_Size converted;
+ auto const size = rtl_convertUnicodeToText(
+ converter, nullptr, input, SAL_N_ELEMENTS(input), buf, TEST_STRING_SIZE,
+ (RTL_UNICODETOTEXT_FLAGS_UNDEFINED_ERROR | RTL_UNICODETOTEXT_FLAGS_INVALID_ERROR
+ | RTL_UNICODETOTEXT_FLAGS_FLUSH),
+ &info, &converted);
+ CPPUNIT_ASSERT_EQUAL(sal_Size(0), size);
+ CPPUNIT_ASSERT_EQUAL(RTL_UNICODETOTEXT_INFO_ERROR | RTL_UNICODETOTEXT_INFO_INVALID, info);
+ CPPUNIT_ASSERT_EQUAL(sal_Size(1), converted);
+ rtl_destroyTextToUnicodeConverter(converter);
+}
+
void Test::testSRCBUFFERTOSMALL() {
rtl_TextToUnicodeConverter cv = rtl_createTextToUnicodeConverter(
RTL_TEXTENCODING_EUC_JP);
diff --git a/sal/textenc/convertbig5hkscs.cxx b/sal/textenc/convertbig5hkscs.cxx
index eaed0c7a36d2..77484666982e 100644
--- a/sal/textenc/convertbig5hkscs.cxx
+++ b/sal/textenc/convertbig5hkscs.cxx
@@ -21,6 +21,7 @@
#include <cassert>
+#include <rtl/character.hxx>
#include <rtl/textcvt.h>
#include <sal/types.h>
@@ -333,6 +334,11 @@ sal_Size ImplConvertUnicodeToBig5Hkscs(void const * pData,
nHighSurrogate = static_cast<sal_Unicode>(nChar);
continue;
}
+ else if (ImplIsLowSurrogate(nChar))
+ {
+ bUndefined = false;
+ goto bad_input;
+ }
}
else if (ImplIsLowSurrogate(nChar))
nChar = ImplCombineSurrogates(nHighSurrogate, nChar);
@@ -342,11 +348,7 @@ sal_Size ImplConvertUnicodeToBig5Hkscs(void const * pData,
goto bad_input;
}
- if (ImplIsLowSurrogate(nChar) || ImplIsNoncharacter(nChar))
- {
- bUndefined = false;
- goto bad_input;
- }
+ assert(rtl::isUnicodeScalarValue(nChar));
if (nChar < 0x80)
if (pDestBufPtr != pDestBufEnd)
diff --git a/sal/textenc/converteuctw.cxx b/sal/textenc/converteuctw.cxx
index abc214402636..edb3c07fa934 100644
--- a/sal/textenc/converteuctw.cxx
+++ b/sal/textenc/converteuctw.cxx
@@ -19,6 +19,9 @@
#include <sal/config.h>
+#include <cassert>
+
+#include <rtl/character.hxx>
#include <rtl/textcvt.h>
#include <sal/types.h>
@@ -342,6 +345,11 @@ sal_Size ImplConvertUnicodeToEucTw(void const * pData,
nHighSurrogate = static_cast<sal_Unicode>(nChar);
continue;
}
+ else if (ImplIsLowSurrogate(nChar))
+ {
+ bUndefined = false;
+ goto bad_input;
+ }
}
else if (ImplIsLowSurrogate(nChar))
nChar = ImplCombineSurrogates(nHighSurrogate, nChar);
@@ -351,11 +359,7 @@ sal_Size ImplConvertUnicodeToEucTw(void const * pData,
goto bad_input;
}
- if (ImplIsLowSurrogate(nChar) || ImplIsNoncharacter(nChar))
- {
- bUndefined = false;
- goto bad_input;
- }
+ assert(rtl::isUnicodeScalarValue(nChar));
if (nChar < 0x80)
if (pDestBufPtr != pDestBufEnd)
diff --git a/sal/textenc/convertgb18030.cxx b/sal/textenc/convertgb18030.cxx
index 9aa64970421b..88f5a999d87d 100644
--- a/sal/textenc/convertgb18030.cxx
+++ b/sal/textenc/convertgb18030.cxx
@@ -19,6 +19,9 @@
#include <sal/config.h>
+#include <cassert>
+
+#include <rtl/character.hxx>
#include <rtl/textcvt.h>
#include <sal/types.h>
@@ -332,6 +335,11 @@ sal_Size ImplConvertUnicodeToGb18030(void const * pData,
nHighSurrogate = static_cast<sal_Unicode>(nChar);
continue;
}
+ else if (ImplIsLowSurrogate(nChar))
+ {
+ bUndefined = false;
+ goto bad_input;
+ }
}
else if (ImplIsLowSurrogate(nChar))
nChar = ImplCombineSurrogates(nHighSurrogate, nChar);
@@ -341,11 +349,7 @@ sal_Size ImplConvertUnicodeToGb18030(void const * pData,
goto bad_input;
}
- if (ImplIsLowSurrogate(nChar) || ImplIsNoncharacter(nChar))
- {
- bUndefined = false;
- goto bad_input;
- }
+ assert(rtl::isUnicodeScalarValue(nChar));
if (nChar < 0x80)
if (pDestBufPtr != pDestBufEnd)
diff --git a/sal/textenc/convertisciidevangari.cxx b/sal/textenc/convertisciidevangari.cxx
index 9e6583119252..b8566ed6a51e 100644
--- a/sal/textenc/convertisciidevangari.cxx
+++ b/sal/textenc/convertisciidevangari.cxx
@@ -7,10 +7,16 @@
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/
+#include <sal/config.h>
+
+#include <cassert>
+
#include "converter.hxx"
#include "unichars.hxx"
#include "convertisciidevangari.hxx"
#include "convertsinglebytetobmpunicode.hxx"
+
+#include <rtl/character.hxx>
#include <rtl/textcvt.h>
using namespace sal::detail::textenc;
@@ -261,6 +267,11 @@ sal_Size UnicodeToIsciiDevanagari::convert(sal_Unicode const* pSrcBuf, sal_Size
cHighSurrogate = static_cast< sal_Unicode >(c);
continue;
}
+ else if (ImplIsLowSurrogate(c))
+ {
+ bUndefined = false;
+ goto bad_input;
+ }
}
else if (ImplIsLowSurrogate(c))
{
@@ -271,11 +282,7 @@ sal_Size UnicodeToIsciiDevanagari::convert(sal_Unicode const* pSrcBuf, sal_Size
bUndefined = false;
goto bad_input;
}
- if (ImplIsLowSurrogate(c) || ImplIsNoncharacter(c))
- {
- bUndefined = false;
- goto bad_input;
- }
+ assert(rtl::isUnicodeScalarValue(c));
//halant + halant E8 E8 -> halant + ZWNJ 094D 200C
//halant + nukta E8 E9 halant + ZWJ 094D 200D
diff --git a/sal/textenc/convertiso2022cn.cxx b/sal/textenc/convertiso2022cn.cxx
index 76aee21f04b4..9e89c27486db 100644
--- a/sal/textenc/convertiso2022cn.cxx
+++ b/sal/textenc/convertiso2022cn.cxx
@@ -19,6 +19,9 @@
#include <sal/config.h>
+#include <cassert>
+
+#include <rtl/character.hxx>
#include <rtl/textcvt.h>
#include <sal/types.h>
@@ -558,6 +561,11 @@ sal_Size ImplConvertUnicodeToIso2022Cn(void const * pData,
nHighSurrogate = static_cast<sal_Unicode>(nChar);
continue;
}
+ else if (ImplIsLowSurrogate(nChar))
+ {
+ bUndefined = false;
+ goto bad_input;
+ }
}
else if (ImplIsLowSurrogate(nChar))
nChar = ImplCombineSurrogates(nHighSurrogate, nChar);
@@ -567,11 +575,7 @@ sal_Size ImplConvertUnicodeToIso2022Cn(void const * pData,
goto bad_input;
}
- if (ImplIsLowSurrogate(nChar) || ImplIsNoncharacter(nChar))
- {
- bUndefined = false;
- goto bad_input;
- }
+ assert(rtl::isUnicodeScalarValue(nChar));
if (nChar == 0x0A || nChar == 0x0D) // LF, CR
{
diff --git a/sal/textenc/convertiso2022jp.cxx b/sal/textenc/convertiso2022jp.cxx
index 565c09ab36f5..ef2abeefcdb4 100644
--- a/sal/textenc/convertiso2022jp.cxx
+++ b/sal/textenc/convertiso2022jp.cxx
@@ -19,6 +19,9 @@
#include <sal/config.h>
+#include <cassert>
+
+#include <rtl/character.hxx>
#include <rtl/textcvt.h>
#include <sal/types.h>
@@ -377,6 +380,11 @@ sal_Size ImplConvertUnicodeToIso2022Jp(void const * pData,
nHighSurrogate = static_cast<sal_Unicode>(nChar);
continue;
}
+ else if (ImplIsLowSurrogate(nChar))
+ {
+ bUndefined = false;
+ goto bad_input;
+ }
}
else if (ImplIsLowSurrogate(nChar))
nChar = ImplCombineSurrogates(nHighSurrogate, nChar);
@@ -386,11 +394,7 @@ sal_Size ImplConvertUnicodeToIso2022Jp(void const * pData,
goto bad_input;
}
- if (ImplIsLowSurrogate(nChar) || ImplIsNoncharacter(nChar))
- {
- bUndefined = false;
- goto bad_input;
- }
+ assert(rtl::isUnicodeScalarValue(nChar));
if (nChar == 0x0A || nChar == 0x0D) // LF, CR
{
diff --git a/sal/textenc/convertiso2022kr.cxx b/sal/textenc/convertiso2022kr.cxx
index 6169969812d2..5c7971ba5af6 100644
--- a/sal/textenc/convertiso2022kr.cxx
+++ b/sal/textenc/convertiso2022kr.cxx
@@ -19,6 +19,9 @@
#include <sal/config.h>
+#include <cassert>
+
+#include <rtl/character.hxx>
#include <rtl/textcvt.h>
#include <sal/types.h>
@@ -355,6 +358,11 @@ sal_Size ImplConvertUnicodeToIso2022Kr(void const * pData,
nHighSurrogate = static_cast<sal_Unicode>(nChar);
continue;
}
+ else if (ImplIsLowSurrogate(nChar))
+ {
+ bUndefined = false;
+ goto bad_input;
+ }
}
else if (ImplIsLowSurrogate(nChar))
nChar = ImplCombineSurrogates(nHighSurrogate, nChar);
@@ -364,11 +372,7 @@ sal_Size ImplConvertUnicodeToIso2022Kr(void const * pData,
goto bad_input;
}
- if (ImplIsLowSurrogate(nChar) || ImplIsNoncharacter(nChar))
- {
- bUndefined = false;
- goto bad_input;
- }
+ assert(rtl::isUnicodeScalarValue(nChar));
if (nChar == 0x0A || nChar == 0x0D) // LF, CR
{
diff --git a/sal/textenc/convertsinglebytetobmpunicode.cxx b/sal/textenc/convertsinglebytetobmpunicode.cxx
index b883411cbffa..6dc7891a3001 100644
--- a/sal/textenc/convertsinglebytetobmpunicode.cxx
+++ b/sal/textenc/convertsinglebytetobmpunicode.cxx
@@ -19,8 +19,10 @@
#include <sal/config.h>
+#include <cassert>
#include <cstddef>
+#include <rtl/character.hxx>
#include <rtl/textcvt.h>
#include <sal/types.h>
@@ -113,16 +115,18 @@ sal_Size rtl_textenc_convertBmpUnicodeToSingleByte(
highSurrogate = static_cast< sal_Unicode >(c);
continue;
}
+ else if (ImplIsLowSurrogate(c))
+ {
+ undefined = false;
+ goto bad_input;
+ }
} else if (ImplIsLowSurrogate(c)) {
c = ImplCombineSurrogates(highSurrogate, c);
} else {
undefined = false;
goto bad_input;
}
- if (ImplIsLowSurrogate(c) || ImplIsNoncharacter(c)) {
- undefined = false;
- goto bad_input;
- }
+ assert(rtl::isUnicodeScalarValue(c));
// Linearly searching through the ranges if probably fastest, assuming
// that most converted characters belong to the ASCII subset:
for (std::size_t i = 0; i < entries; ++i) {
diff --git a/sal/textenc/tcvtutf8.cxx b/sal/textenc/tcvtutf8.cxx
index a2430ff7c075..950d810e8b85 100644
--- a/sal/textenc/tcvtutf8.cxx
+++ b/sal/textenc/tcvtutf8.cxx
@@ -19,7 +19,10 @@
#include <sal/config.h>
+#include <cassert>
+
#include <sal/types.h>
+#include <rtl/character.hxx>
#include <rtl/textcvt.h>
#include "converter.hxx"
@@ -347,15 +350,17 @@ sal_Size ImplConvertUnicodeToUtf8(
nHighSurrogate = static_cast<sal_Unicode>(nChar);
continue;
}
+ else if (ImplIsLowSurrogate(nChar) && !bJavaUtf8)
+ {
+ goto bad_input;
+ }
}
else if (ImplIsLowSurrogate(nChar) && !bJavaUtf8)
nChar = ImplCombineSurrogates(nHighSurrogate, nChar);
else
goto bad_input;
- if ((ImplIsLowSurrogate(nChar) && !bJavaUtf8)
- || ImplIsNoncharacter(nChar))
- goto bad_input;
+ assert(bJavaUtf8 ? nChar <= 0xFFFF : rtl::isUnicodeScalarValue(nChar));
if (nChar <= 0x7F && (!bJavaUtf8 || nChar != 0))
if (pDestBufPtr != pDestBufEnd)
diff --git a/sal/textenc/unichars.hxx b/sal/textenc/unichars.hxx
index 0bcd6f710518..e11e47493183 100644
--- a/sal/textenc/unichars.hxx
+++ b/sal/textenc/unichars.hxx
@@ -29,14 +29,6 @@
#define RTL_TEXTENC_UNICODE_REPLACEMENT_CHARACTER 0xFFFD
-inline bool ImplIsNoncharacter(sal_uInt32 nUtf32)
-{
- return (nUtf32 >= 0xFDD0 && nUtf32 <= 0xFDEF)
- || (nUtf32 & 0xFFFF) >= 0xFFFE
- || !rtl::isUnicodeCodePoint(nUtf32);
-}
- // All code points that are noncharacters, as of Unicode 3.1.1.
-
bool ImplIsControlOrFormat(sal_uInt32 nUtf32);
inline bool ImplIsHighSurrogate(sal_uInt32 nUtf32)