diff options
author | Stephan Bergmann <sbergman@redhat.com> | 2022-03-31 10:03:39 +0200 |
---|---|---|
committer | Stephan Bergmann <sbergman@redhat.com> | 2022-03-31 16:58:39 +0200 |
commit | b66387574ef9c83cbfff622468496b6f0ac4d571 (patch) | |
tree | 6052d794f4d6400cdf3595e78e7a60c8d1dca3a0 /sal | |
parent | d940013e1726eba4d043110d5a01367c8d3f0a05 (diff) |
Fix -Werror=array-bounds
> In file included from svtools/source/svrtf/parrtf.cxx:28:
> In member function ‘typename rtl::libreoffice_internal::ConstCharArrayDetector<T, rtl::OUStringBuffer&>::TypeUtf16 rtl::OUStringBuffer::operator=(T&) [with T = const rtl::OUStringChar_]’,
> inlined from ‘virtual int SvRTFParser::GetNextToken_()’ at svtools/source/svrtf/parrtf.cxx:183:94:
> include/rtl/ustrbuf.hxx:352:20: error: array subscript ‘unsigned int[0]’ is partly outside array bounds of ‘rtl::OUStringChar [1]’ {aka ‘const rtl::OUStringChar_ [1]’} [-Werror=array-bounds]
> 352 | std::memcpy(
> | ~~~~~~~~~~~^
> 353 | pData->buffer,
> | ~~~~~~~~~~~~~~
> 354 | libreoffice_internal::ConstCharArrayDetector<T>::toPointer(literal),
> | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
> 355 | (n + 1) * sizeof (sal_Unicode)); //TODO: check for overflow
> | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
> svtools/source/svrtf/parrtf.cxx: In member function ‘virtual int SvRTFParser::GetNextToken_()’:
> svtools/source/svrtf/parrtf.cxx:183:94: note: object ‘<anonymous>’ of size 2
> 183 | aToken = OUStringChar( static_cast<sal_Unicode>(nTokenValue) );
> | ^
as seen with recent GCC 12 trunk in an --enable-optimized build.
And add a test, even though it is relatively likely that the OUStringChar
temporary is followed by null bytes, which would make the test happen to
erroneously succeed. But at least tools like ASan or Valgrind could catch that.
(For the corresponding OStringChar and OStringBuffer scenario, this issue does
not arise, as OStringChar is not covered by ConstCharArrayDetector, so the
correpsonding OStringBuffer assignment operator is OK memcpy'ing n+1 elements.
There /are/ similar issues with string_view assignment operators for both
O[U]StringBuffer, which will be addressed in a later commit.)
Change-Id: Ia131d763aa5f8df45b9625f296408cc935df96ac
Reviewed-on: https://gerrit.libreoffice.org/c/core/+/132354
Tested-by: Jenkins
Reviewed-by: Stephan Bergmann <sbergman@redhat.com>
Diffstat (limited to 'sal')
-rw-r--r-- | sal/qa/rtl/oustringbuffer/test_oustringbuffer_assign.cxx | 5 |
1 files changed, 5 insertions, 0 deletions
diff --git a/sal/qa/rtl/oustringbuffer/test_oustringbuffer_assign.cxx b/sal/qa/rtl/oustringbuffer/test_oustringbuffer_assign.cxx index 10f378c1acf9..da5a4634e94b 100644 --- a/sal/qa/rtl/oustringbuffer/test_oustringbuffer_assign.cxx +++ b/sal/qa/rtl/oustringbuffer/test_oustringbuffer_assign.cxx @@ -13,6 +13,7 @@ #include <cppunit/TestAssert.h> #include <cppunit/extensions/HelperMacros.h> +#include <o3tl/cppunittraitshelper.hxx> #include <rtl/ustrbuf.hxx> #include <rtl/ustring.hxx> @@ -66,6 +67,10 @@ private: b4 = OUStringLiteral(u"1") + "234567890123456"; CPPUNIT_ASSERT_EQUAL(s3, b4.toString()); CPPUNIT_ASSERT_EQUAL(sal_Int32(32), b4.getCapacity()); + b4 = OUStringChar('a'); + CPPUNIT_ASSERT_EQUAL(sal_Int32(1), b4.getLength()); + CPPUNIT_ASSERT_EQUAL(u'a', b4.getStr()[0]); + CPPUNIT_ASSERT_EQUAL(u'\0', b4.getStr()[1]); } CPPUNIT_TEST_SUITE(Test); |