Commit add87b2d authored by Ryan Harrison's avatar Ryan Harrison Committed by Commit Bot

Account for string terminator added to FPDFText_GetText strings

The documentation for this function in PDFium explictly calls out that
the caller needs to pass in a buffer that is number of characters
desired + 1, to account for the terminator.

The existing implementation does not correctly do this, grabbing count
characters instead of count - 1 characters. This causes the written
out string to be count + 1 long due to an added terminator. The
corrected implementation cannot be rolled in current, since it causes
the strings being returned to be shorter then expected.

This CL changes the call sites in Chrome to follow the documentated
behaviour, which works correctly with both the old and new
implementation of the function. For the old implementation it will
overallocate by 1, and for the for the new implementation the
allocation size will be correct.

BUG=chromium:761770,chromium:761626

Change-Id: I874a88d854c2c85c28847ade3d74bcce56d6a876
Reviewed-on: https://chromium-review.googlesource.com/650650Reviewed-by: default avatardsinclair <dsinclair@chromium.org>
Commit-Queue: Ryan Harrison <rharrison@chromium.org>
Cr-Commit-Position: refs/heads/master@{#499732}
parent cfff8af7
...@@ -2343,14 +2343,16 @@ void PDFiumEngine::SearchUsingICU(const base::string16& term, ...@@ -2343,14 +2343,16 @@ void PDFiumEngine::SearchUsingICU(const base::string16& term,
if (text_length <= 0) if (text_length <= 0)
return; return;
// Adding +1 to text_length to account for the string terminator that is
// included.
base::string16 page_text; base::string16 page_text;
PDFiumAPIStringBufferAdapter<base::string16> api_string_adapter( PDFiumAPIStringBufferAdapter<base::string16> api_string_adapter(
&page_text, text_length, false); &page_text, text_length + 1, false);
unsigned short* data = unsigned short* data =
reinterpret_cast<unsigned short*>(api_string_adapter.GetData()); reinterpret_cast<unsigned short*>(api_string_adapter.GetData());
int written = int written = FPDFText_GetText(pages_[current_page]->GetTextPage(),
FPDFText_GetText(pages_[current_page]->GetTextPage(), character_to_start_searching_from,
character_to_start_searching_from, text_length, data); text_length + 1, data);
api_string_adapter.Close(written); api_string_adapter.Close(written);
std::vector<PDFEngine::Client::SearchStringResult> results; std::vector<PDFEngine::Client::SearchStringResult> results;
......
...@@ -67,11 +67,13 @@ base::string16 PDFiumRange::GetText() const { ...@@ -67,11 +67,13 @@ base::string16 PDFiumRange::GetText() const {
} }
if (count > 0) { if (count > 0) {
PDFiumAPIStringBufferAdapter<base::string16> api_string_adapter(&rv, count, // Adding +1 to count to account for the string terminator that is included.
false); PDFiumAPIStringBufferAdapter<base::string16> api_string_adapter(
&rv, count + 1, false);
unsigned short* data = unsigned short* data =
reinterpret_cast<unsigned short*>(api_string_adapter.GetData()); reinterpret_cast<unsigned short*>(api_string_adapter.GetData());
int written = FPDFText_GetText(page_->GetTextPage(), index, count, data); int written =
FPDFText_GetText(page_->GetTextPage(), index, count + 1, data);
api_string_adapter.Close(written); api_string_adapter.Close(written);
} }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment