Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(502)

Side by Side Diff: pdf/pdfium/pdfium_page.cc

Issue 848073003: PDF: Yet another stab at getting WriteInto() buffer sizes correct. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Fix accessibility code that can no longer occur Created 5 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « pdf/pdfium/pdfium_engine.cc ('k') | pdf/pdfium/pdfium_range.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2010 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "pdf/pdfium/pdfium_page.h" 5 #include "pdf/pdfium/pdfium_page.h"
6 6
7 #include <math.h> 7 #include <math.h>
8 8
9 #include "base/logging.h" 9 #include "base/logging.h"
10 #include "base/strings/string_number_conversions.h" 10 #include "base/strings/string_number_conversions.h"
(...skipping 179 matching lines...) Expand 10 before | Expand all | Expand 10 after
190 base::ListValue* text_nodes = new base::ListValue(); 190 base::ListValue* text_nodes = new base::ListValue();
191 191
192 if (area == DOCLINK_AREA) { 192 if (area == DOCLINK_AREA) {
193 std::string url = kDocLinkURLPrefix + base::IntToString(targets[0].page); 193 std::string url = kDocLinkURLPrefix + base::IntToString(targets[0].page);
194 text_nodes->Append(CreateURLNode(text_utf8, url)); 194 text_nodes->Append(CreateURLNode(text_utf8, url));
195 } else if (area == WEBLINK_AREA && link) { 195 } else if (area == WEBLINK_AREA && link) {
196 text_nodes->Append(CreateURLNode(text_utf8, targets[0].url)); 196 text_nodes->Append(CreateURLNode(text_utf8, targets[0].url));
197 } else if (area == WEBLINK_AREA && !link) { 197 } else if (area == WEBLINK_AREA && !link) {
198 size_t start = 0; 198 size_t start = 0;
199 for (size_t i = 0; i < targets.size(); ++i) { 199 for (size_t i = 0; i < targets.size(); ++i) {
200 // Remove the extra NULL character at end. 200 // If there is an extra NULL character at end, find() will not return any
201 // Otherwise, find() will not return any matches. 201 // matches. There should not be any though.
202 if (targets[i].url.size() > 0 && 202 if (!targets[i].url.empty())
203 targets[i].url[targets[i].url.size() - 1] == '\0') { 203 DCHECK(targets[i].url[targets[i].url.size() - 1] != '\0');
raymes 2015/01/14 05:05:52 just double checking: does DCHECK ever evaluate to
Lei Zhang 2015/01/15 00:42:08 Sorry, having a bit of trouble parsing the questio
raymes 2015/01/15 01:22:57 I was just checking that it's ok to omit the {} fr
204 targets[i].url.resize(targets[i].url.size() - 1);
205 }
206 // There should only ever be one NULL character
207 DCHECK(targets[i].url[targets[i].url.size() - 1] != '\0');
208 204
209 // PDFium may change the case of generated links. 205 // PDFium may change the case of generated links.
210 std::string lowerCaseURL = base::StringToLowerASCII(targets[i].url); 206 std::string lowerCaseURL = base::StringToLowerASCII(targets[i].url);
211 std::string lowerCaseText = base::StringToLowerASCII(text_utf8); 207 std::string lowerCaseText = base::StringToLowerASCII(text_utf8);
212 size_t pos = lowerCaseText.find(lowerCaseURL, start); 208 size_t pos = lowerCaseText.find(lowerCaseURL, start);
213 size_t length = targets[i].url.size(); 209 size_t length = targets[i].url.size();
214 if (pos == std::string::npos) { 210 if (pos == std::string::npos) {
215 // Check if the link is a "mailto:" URL 211 // Check if the link is a "mailto:" URL
216 if (lowerCaseURL.compare(0, 7, "mailto:") == 0) { 212 if (lowerCaseURL.compare(0, 7, "mailto:") == 0) {
217 pos = lowerCaseText.find(lowerCaseURL.substr(7), start); 213 pos = lowerCaseText.find(lowerCaseURL.substr(7), start);
(...skipping 98 matching lines...) Expand 10 before | Expand all | Expand 10 after
316 if (dest) 312 if (dest)
317 return GetDestinationTarget(dest, target); 313 return GetDestinationTarget(dest, target);
318 // TODO(gene): We don't fully support all types of the in-document 314 // TODO(gene): We don't fully support all types of the in-document
319 // links. Need to implement that. There is a bug to track that: 315 // links. Need to implement that. There is a bug to track that:
320 // http://code.google.com/p/chromium/issues/detail?id=55776 316 // http://code.google.com/p/chromium/issues/detail?id=55776
321 } break; 317 } break;
322 case PDFACTION_URI: { 318 case PDFACTION_URI: {
323 if (target) { 319 if (target) {
324 size_t buffer_size = 320 size_t buffer_size =
325 FPDFAction_GetURIPath(engine_->doc(), action, NULL, 0); 321 FPDFAction_GetURIPath(engine_->doc(), action, NULL, 0);
326 if (buffer_size > 1) { 322 if (buffer_size > 0) {
327 void* data = WriteInto(&target->url, buffer_size); 323 // FPDFAction_GetURIPath() will write out a null-terminated byte
328 FPDFAction_GetURIPath(engine_->doc(), action, data, buffer_size); 324 // stream into the std::string holding |data|. Although most
325 // std::string implementations are null-terminated, the code here
326 // should not depend on this implementation detail. Thus
327 // WriteInto() allocates an extra byte to take the null-terminator
328 // and then resizes the string to get rid of it.
329 void* data = WriteInto(&target->url, buffer_size + 1);
raymes 2015/01/14 05:05:52 :( you're right we need to be careful that the API
Lei Zhang 2015/01/15 00:42:08 I'm not sure where to put it. It could also just b
raymes 2015/01/15 01:22:57 I guess I was thinking something like what's below
330 size_t bytes_written = FPDFAction_GetURIPath(
331 engine_->doc(), action, data, buffer_size);
raymes 2015/01/14 05:05:52 In this case can't we just DCHECK that buffer_size
Lei Zhang 2015/01/15 00:42:08 Sure. I suppose the if condition should always be
332 if (bytes_written > 0) {
333 DCHECK_EQ('\0', target->url[bytes_written - 1]);
334 target->url.resize(bytes_written - 1);
335 } else {
336 target->url.clear();
337 }
329 } 338 }
330 } 339 }
331 return WEBLINK_AREA; 340 return WEBLINK_AREA;
332 } break; 341 } break;
333 // TODO(gene): We don't support PDFACTION_REMOTEGOTO and PDFACTION_LAUNCH 342 // TODO(gene): We don't support PDFACTION_REMOTEGOTO and PDFACTION_LAUNCH
334 // at the moment. 343 // at the moment.
335 } 344 }
336 } 345 }
337 346
338 return NONSELECTABLE_AREA; 347 return NONSELECTABLE_AREA;
(...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after
400 if (calculated_links_) 409 if (calculated_links_)
401 return; 410 return;
402 411
403 calculated_links_ = true; 412 calculated_links_ = true;
404 FPDF_PAGELINK links = FPDFLink_LoadWebLinks(GetTextPage()); 413 FPDF_PAGELINK links = FPDFLink_LoadWebLinks(GetTextPage());
405 int count = FPDFLink_CountWebLinks(links); 414 int count = FPDFLink_CountWebLinks(links);
406 for (int i = 0; i < count; ++i) { 415 for (int i = 0; i < count; ++i) {
407 base::string16 url; 416 base::string16 url;
408 int url_length = FPDFLink_GetURL(links, i, NULL, 0); 417 int url_length = FPDFLink_GetURL(links, i, NULL, 0);
409 if (url_length > 0) { 418 if (url_length > 0) {
419 // FPDFLink_GetURL() will write out a null-terminated 16-bit char
420 // stream into the base::string16 holding |data|. Although string
421 // implementations may be null-terminated, the code here should not
422 // depend on this implementation detail. Thus WriteInto() allocates an
423 // extra byte to take the null-terminator and then resizes the string to
424 // get rid of it.
410 unsigned short* data = 425 unsigned short* data =
411 reinterpret_cast<unsigned short*>(WriteInto(&url, url_length + 1)); 426 reinterpret_cast<unsigned short*>(WriteInto(&url, url_length + 1));
412 FPDFLink_GetURL(links, i, data, url_length); 427 int actual_length = FPDFLink_GetURL(links, i, data, url_length);
raymes 2015/01/14 05:05:52 in this case again I think it may be sufficient to
428 if (actual_length > 0) {
429 DCHECK_EQ(L'\0', url[actual_length - 1]);
430 url.resize(actual_length - 1);
431 } else {
432 url.clear();
433 }
413 } 434 }
414 Link link; 435 Link link;
415 link.url = base::UTF16ToUTF8(url); 436 link.url = base::UTF16ToUTF8(url);
416 437
417 // If the link cannot be converted to a pp::Var, then it is not possible to 438 // If the link cannot be converted to a pp::Var, then it is not possible to
418 // pass it to JS. In this case, ignore the link like other PDF viewers. 439 // pass it to JS. In this case, ignore the link like other PDF viewers.
419 // See http://crbug.com/312882 for an example. 440 // See http://crbug.com/312882 for an example.
420 pp::Var link_var(link.url); 441 pp::Var link_var(link.url);
421 if (!link_var.is_string()) 442 if (!link_var.is_string())
422 continue; 443 continue;
(...skipping 73 matching lines...) Expand 10 before | Expand all | Expand 10 after
496 page_->loading_count_--; 517 page_->loading_count_--;
497 } 518 }
498 519
499 PDFiumPage::Link::Link() { 520 PDFiumPage::Link::Link() {
500 } 521 }
501 522
502 PDFiumPage::Link::~Link() { 523 PDFiumPage::Link::~Link() {
503 } 524 }
504 525
505 } // namespace chrome_pdf 526 } // namespace chrome_pdf
OLDNEW
« no previous file with comments | « pdf/pdfium/pdfium_engine.cc ('k') | pdf/pdfium/pdfium_range.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698