Skip to content

Commit

Permalink
PDF: Fix extra NUL characters from incorrect WriteInto() calls.
Browse files Browse the repository at this point in the history
BUG=445307

Review URL: https://codereview.chromium.org/833263003

Cr-Commit-Position: refs/heads/master@{#310185}
  • Loading branch information
leizleiz authored and Commit bot committed Jan 7, 2015
1 parent 243f4a1 commit 9ea0450
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 13 deletions.
16 changes: 12 additions & 4 deletions pdf/pdfium/pdfium_engine.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2050,12 +2050,20 @@ void PDFiumEngine::SearchUsingICU(const base::string16& term,
}
if (text_length <= 0)
return;

unsigned short* data =
reinterpret_cast<unsigned short*>(WriteInto(&page_text, text_length + 1));
FPDFText_GetText(pages_[current_page]->GetTextPage(),
character_to_start_searching_from,
text_length,
data);
// |written| includes the trailing terminator, so get rid of the trailing
// NUL character by calling resize().
int written = FPDFText_GetText(pages_[current_page]->GetTextPage(),
character_to_start_searching_from,
text_length,
data);
if (written < 1)
page_text.resize(0);
else
page_text.resize(written - 1);

std::vector<PDFEngine::Client::SearchStringResult> results;
client_->SearchString(
page_text.c_str(), term.c_str(), case_sensitive, &results);
Expand Down
4 changes: 2 additions & 2 deletions pdf/pdfium/pdfium_page.cc
Original file line number Diff line number Diff line change
Expand Up @@ -324,7 +324,7 @@ PDFiumPage::Area PDFiumPage::GetLinkTarget(
size_t buffer_size =
FPDFAction_GetURIPath(engine_->doc(), action, NULL, 0);
if (buffer_size > 1) {
void* data = WriteInto(&target->url, buffer_size + 1);
void* data = WriteInto(&target->url, buffer_size);
FPDFAction_GetURIPath(engine_->doc(), action, data, buffer_size);
}
}
Expand Down Expand Up @@ -406,7 +406,7 @@ void PDFiumPage::CalculateLinks() {
for (int i = 0; i < count; ++i) {
base::string16 url;
int url_length = FPDFLink_GetURL(links, i, NULL, 0);
if (url_length > 1) { // WriteInto needs at least 2 characters.
if (url_length > 0) {
unsigned short* data =
reinterpret_cast<unsigned short*>(WriteInto(&url, url_length + 1));
FPDFLink_GetURL(links, i, data, url_length);
Expand Down
18 changes: 11 additions & 7 deletions pdf/pdfium/pdfium_range.cc
Original file line number Diff line number Diff line change
Expand Up @@ -59,20 +59,24 @@ std::vector<pp::Rect> PDFiumRange::GetScreenRects(const pp::Point& offset,
base::string16 PDFiumRange::GetText() {
int index = char_index_;
int count = char_count_;
if (!count)
return base::string16();
base::string16 rv;
if (count < 0) {
count *= -1;
index -= count - 1;
}

base::string16 rv;
unsigned short* data =
reinterpret_cast<unsigned short*>(WriteInto(&rv, count + 1));
if (data) {
if (count > 0) {
unsigned short* data =
reinterpret_cast<unsigned short*>(WriteInto(&rv, count + 1));
// |written| includes the trailing terminator, so get rid of the trailing
// NUL character by calling resize().
int written = FPDFText_GetText(page_->GetTextPage(), index, count, data);
rv.reserve(written);
if (written < 1)
rv.resize(0);
else
rv.resize(written - 1);
}

return rv;
}

Expand Down

0 comments on commit 9ea0450

Please sign in to comment.