summaryrefslogtreecommitdiff
path: root/sal
diff options
context:
space:
mode:
authorStephan Bergmann <sbergman@redhat.com>2020-09-04 20:26:58 +0200
committerStephan Bergmann <sbergman@redhat.com>2020-09-05 12:29:10 +0200
commit9abaa6492899b3a8e467b08ec8a084ed3db7f518 (patch)
treecf223bf313df7f1eb3f55f17b24542df0b526482 /sal
parent7003fe557bd2223e9af1ed0eb6c97b209686a8ab (diff)
Make OUString(char16_t const[N]) ctor check for embedded NULs
...and fix the detected fallout. That ctor only started to get used recently with a1570b6052ae9c9349282027c9007b071589bce6 "Make the OUString ConstCharArrayDetector::TypeUtf16 overloads are actually used", but it turns out that that also gave rise to that ctor being picked in error. To better guard against such erroneous uses, make that ctor assert that the given array does not contain embedded NUL characters, see the new sal/qa/rtl/strings/nonconstarray.cxx tests. The one place where that assert would fire during `make check` is fixed now in SwWW8ImplReader::ImportDopTypography. That assert would also fire for tow OUStringLiteral-related tests in the recently added test::oustring::StringLiterals::checkEmbeddedNul, so drop those for how. They cna presumably be added back (with reversed logic values) when OUStringLiteral is changed similarly to how OStringLiteral was changed in 4b9e440c51be3e40326bc90c33ae69885bfb51e4 "Turn OStringLiteral into a consteval'ed, static-refcound rtl_String". Change-Id: I6056244003a20f77ba0d953538d25edcbd562211 Reviewed-on: https://gerrit.libreoffice.org/c/core/+/102063 Tested-by: Jenkins Reviewed-by: Stephan Bergmann <sbergman@redhat.com>
Diffstat (limited to 'sal')
-rw-r--r--sal/CppunitTest_sal_rtl.mk1
-rw-r--r--sal/qa/rtl/strings/nonconstarray.cxx94
-rw-r--r--sal/qa/rtl/strings/test_oustring_stringliterals.cxx2
3 files changed, 95 insertions, 2 deletions
diff --git a/sal/CppunitTest_sal_rtl.mk b/sal/CppunitTest_sal_rtl.mk
index c2eaa72daa26..f559202c54d5 100644
--- a/sal/CppunitTest_sal_rtl.mk
+++ b/sal/CppunitTest_sal_rtl.mk
@@ -29,6 +29,7 @@ $(eval $(call gb_CppunitTest_add_exception_objects,sal_rtl,\
sal/qa/rtl/process/rtl_Process \
sal/qa/rtl/random/rtl_random \
sal/qa/rtl/ref/rtl_ref \
+ sal/qa/rtl/strings/nonconstarray \
sal/qa/rtl/strings/test_strings_replace \
sal/qa/rtl/strings/test_ostring \
sal/qa/rtl/strings/test_ostring_concat \
diff --git a/sal/qa/rtl/strings/nonconstarray.cxx b/sal/qa/rtl/strings/nonconstarray.cxx
new file mode 100644
index 000000000000..4b66e4e311c3
--- /dev/null
+++ b/sal/qa/rtl/strings/nonconstarray.cxx
@@ -0,0 +1,94 @@
+/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4; fill-column: 100 -*- */
+/*
+ * This file is part of the LibreOffice project.
+ *
+ * This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/.
+ */
+
+#include <sal/config.h>
+
+#include <cppunit/TestAssert.h>
+#include <cppunit/TestFixture.h>
+#include <cppunit/extensions/HelperMacros.h>
+#include <rtl/string.hxx>
+#include <rtl/ustring.hxx>
+
+namespace
+{
+class Test : public CppUnit::TestFixture
+{
+private:
+ void testOString()
+ {
+ struct S
+ {
+ char a[4];
+ };
+ S s{ "x\0y" };
+ CPPUNIT_ASSERT_EQUAL(sal_Int32(1), OString(s.a).getLength());
+ // Ideally, the below would work the same as the above. However, the const reference makes
+ // the ConstCharArrayDetector instead of the NonConstCharArrayDetector kick in, so that the
+ // call to OString(r.a) would fire the ConstCharArrayDetector<T>::isValid assert (and in
+ // NDEBUG builds the CPPUNIT_ASSERT_EQUAL would fail with 3 != 1):
+ if ((false))
+ {
+ S const& r = s;
+ CPPUNIT_ASSERT_EQUAL(sal_Int32(1), OString(r.a).getLength());
+ }
+ }
+
+ void testOUStringChar()
+ {
+ struct S
+ {
+ char a[4];
+ };
+ S s{ "x\0y" };
+ // This would fail to compile, as there is no OUString ctor taking a
+ // NonConstCharArrayDetector char array:
+#if 0
+ CPPUNIT_ASSERT_EQUAL(sal_Int32(1), OUString(s.a).getLength());
+#endif
+ // Ideally, the below would fail to compile the same as the above. However, the const
+ // reference makes the ConstCharArrayDetector instead of the NonConstCharArrayDetector kick
+ // in, so that the call to OUString(r.a) would fire the ConstCharArrayDetector<T>::isValid
+ // assert (and in NDEBUG builds the CPPUNIT_ASSERT_EQUAL would fail with 3 != 1):
+ if ((false))
+ {
+ S const& r = s;
+ CPPUNIT_ASSERT_EQUAL(sal_Int32(1), OUString(r.a).getLength());
+ }
+ }
+
+ void testOUStringChar16t()
+ {
+ struct S
+ {
+ char16_t a[4];
+ };
+ S s{ u"x\0y" };
+ CPPUNIT_ASSERT_EQUAL(sal_Int32(1), OUString(s.a).getLength());
+ // Ideally, the below would work the same as the above. However, the const reference makes
+ // the ConstCharArrayDetector instead of the NonConstCharArrayDetector kick in, so that the
+ // call to OUString(r.a) would fire the ConstCharArrayDetector<T>::isValid assert (and in
+ // NDEBUG builds the CPPUNIT_ASSERT_EQUAL would fail with 3 != 1)::
+ if ((false))
+ {
+ S const& r = s;
+ CPPUNIT_ASSERT_EQUAL(sal_Int32(1), OUString(r.a).getLength());
+ }
+ }
+
+ CPPUNIT_TEST_SUITE(Test);
+ CPPUNIT_TEST(testOString);
+ CPPUNIT_TEST(testOUStringChar);
+ CPPUNIT_TEST(testOUStringChar16t);
+ CPPUNIT_TEST_SUITE_END();
+};
+
+CPPUNIT_TEST_SUITE_REGISTRATION(Test);
+}
+
+/* vim:set shiftwidth=4 softtabstop=4 expandtab cinoptions=b1,g0,N-s cinkeys+=0=break: */
diff --git a/sal/qa/rtl/strings/test_oustring_stringliterals.cxx b/sal/qa/rtl/strings/test_oustring_stringliterals.cxx
index a1c79f8cc9e5..26fe12b5cf96 100644
--- a/sal/qa/rtl/strings/test_oustring_stringliterals.cxx
+++ b/sal/qa/rtl/strings/test_oustring_stringliterals.cxx
@@ -400,8 +400,6 @@ void test::oustring::StringLiterals::checkEmbeddedNul() {
CPPUNIT_ASSERT(s.startsWith(u"foo\0hidden"));
CPPUNIT_ASSERT(!s.startsWith(u"foo\0hidden"s));
CPPUNIT_ASSERT(!s.startsWith(u"foo\0hidden"sv));
- CPPUNIT_ASSERT(!s.startsWith(rtlunittest::OUStringLiteral(a)));
- CPPUNIT_ASSERT(!s.startsWith(rtlunittest::OUStringLiteral(u"foo\0hidden")));
}
} // namespace