diff options
author | Stephan Bergmann <sbergman@redhat.com> | 2020-09-23 12:23:37 +0200 |
---|---|---|
committer | Stephan Bergmann <sbergman@redhat.com> | 2020-09-23 14:04:07 +0200 |
commit | 7346edfee5c2391f828b09baa3dc94d5834d3cd8 (patch) | |
tree | 03a3ba11281c4b6f9e61f48876369a6bf86348d1 | |
parent | 1b9fa11a0869246fe0433b79aab30dd216cf92b6 (diff) |
These PDFium-provided string sizes must always be multiples of 2
...as both FPDFTextObj_GetText (see
workdir/UnpackedTarball/pdfium/public/fpdf_edit.h) and FPDFAnnot_GetStringValue
(see workdir/UnpackedTarball/pdfium/public/fpdf_annot.h) return UTF-16LE strings
but measure their sizes in bytes. So half the returned sizes early, which
avoids over-allocating double sized sal_Unicode arrays, and is a prerequisite
for a follow-up endianness-issue fix.
Change-Id: I4565819044a44ca9435f9bffdea083d5807cfe4d
Reviewed-on: https://gerrit.libreoffice.org/c/core/+/103242
Tested-by: Jenkins
Reviewed-by: Stephan Bergmann <sbergman@redhat.com>
-rw-r--r-- | vcl/source/pdf/PDFiumLibrary.cxx | 22 |
1 files changed, 16 insertions, 6 deletions
diff --git a/vcl/source/pdf/PDFiumLibrary.cxx b/vcl/source/pdf/PDFiumLibrary.cxx index f8d5b79c8f6f..024665efc7e2 100644 --- a/vcl/source/pdf/PDFiumLibrary.cxx +++ b/vcl/source/pdf/PDFiumLibrary.cxx @@ -12,6 +12,8 @@ #if HAVE_FEATURE_PDFIUM +#include <cassert> + #include <vcl/filter/PDFiumLibrary.hxx> #include <fpdf_annot.h> #include <fpdf_edit.h> @@ -224,13 +226,17 @@ OUString PDFiumPageObject::getText(std::unique_ptr<PDFiumTextPage> const& pTextP { OUString sReturnText; - const int nBytes = FPDFTextObj_GetText(mpPageObject, pTextPage->getPointer(), nullptr, 0); + int nBytes = FPDFTextObj_GetText(mpPageObject, pTextPage->getPointer(), nullptr, 0); + assert(nBytes % 2 == 0); + nBytes /= 2; std::unique_ptr<sal_Unicode[]> pText(new sal_Unicode[nBytes]); - const int nActualBytes - = FPDFTextObj_GetText(mpPageObject, pTextPage->getPointer(), pText.get(), nBytes); - if (nActualBytes > 2) + int nActualBytes + = FPDFTextObj_GetText(mpPageObject, pTextPage->getPointer(), pText.get(), nBytes * 2); + assert(nActualBytes % 2 == 0); + nActualBytes /= 2; + if (nActualBytes > 1) sReturnText = OUString(pText.get()); return sReturnText; @@ -416,11 +422,15 @@ OUString PDFiumAnnotation::getString(OString const& rKey) { OUString rString; unsigned long nSize = FPDFAnnot_GetStringValue(mpAnnotation, rKey.getStr(), nullptr, 0); - if (nSize > 2) + assert(nSize % 2 == 0); + nSize /= 2; + if (nSize > 1) { std::unique_ptr<sal_Unicode[]> pText(new sal_Unicode[nSize]); unsigned long nStringSize = FPDFAnnot_GetStringValue( - mpAnnotation, rKey.getStr(), reinterpret_cast<FPDF_WCHAR*>(pText.get()), nSize); + mpAnnotation, rKey.getStr(), reinterpret_cast<FPDF_WCHAR*>(pText.get()), nSize * 2); + assert(nStringSize % 2 == 0); + nStringSize /= 2; if (nStringSize > 0) rString = OUString(pText.get()); } |