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

Side by Side Diff: printing/backend/win_helper.cc

Issue 2919153002: Avoid crash in DocumentProperties (Closed)
Patch Set: Created 3 years, 6 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 | « no previous file | no next file » | 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) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 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 "printing/backend/win_helper.h" 5 #include "printing/backend/win_helper.h"
6 6
7 #include <stddef.h> 7 #include <stddef.h>
8 8
9 #include <algorithm> 9 #include <algorithm>
10 #include <memory> 10 #include <memory>
(...skipping 453 matching lines...) Expand 10 before | Expand all | Expand 10 after
464 const char* xps_color = color ? kXpsTicketColor : kXpsTicketMonochrome; 464 const char* xps_color = color ? kXpsTicketColor : kXpsTicketMonochrome;
465 std::string xps_ticket = base::StringPrintf(kXpsTicketTemplate, xps_color); 465 std::string xps_ticket = base::StringPrintf(kXpsTicketTemplate, xps_color);
466 std::unique_ptr<DEVMODE, base::FreeDeleter> ticket = 466 std::unique_ptr<DEVMODE, base::FreeDeleter> ticket =
467 printing::XpsTicketToDevMode(printer_name, xps_ticket); 467 printing::XpsTicketToDevMode(printer_name, xps_ticket);
468 if (!ticket) 468 if (!ticket)
469 return default_ticket; 469 return default_ticket;
470 470
471 return ticket; 471 return ticket;
472 } 472 }
473 473
474 bool IsPrinterRPCSOnly(HANDLE printer) { 474 bool IsPrinterRPCSOnly(const wchar_t* name, const wchar_t* port) {
475 PrinterInfo5 info_5;
476 if (!info_5.Init(printer))
477 return false;
478 const wchar_t* name = info_5.get()->pPrinterName;
479 const wchar_t* port = info_5.get()->pPortName;
480 int num_languages = 475 int num_languages =
481 DeviceCapabilities(name, port, DC_PERSONALITY, NULL, NULL); 476 DeviceCapabilities(name, port, DC_PERSONALITY, NULL, NULL);
482 if (num_languages != 1) 477 if (num_languages != 1)
483 return false; 478 return false;
484 std::vector<wchar_t> buf(33, 0); 479 std::vector<wchar_t> buf(33, 0);
485 DeviceCapabilities(name, port, DC_PERSONALITY, buf.data(), NULL); 480 DeviceCapabilities(name, port, DC_PERSONALITY, buf.data(), NULL);
486 static constexpr wchar_t kRPCSLanguage[] = L"RPCS"; 481 static constexpr wchar_t kRPCSLanguage[] = L"RPCS";
487 return wcscmp(buf.data(), kRPCSLanguage) == 0; 482 return wcscmp(buf.data(), kRPCSLanguage) == 0;
488 } 483 }
489 484
485 bool PrinterHasValidPaperSize(const wchar_t* name, const wchar_t* port) {
486 return DeviceCapabilities(name, port, DC_PAPERSIZE, NULL, NULL) > 0;
Lei Zhang 2017/06/02 21:29:54 nit: Can we write nullptr in new code?
rbpotter 2017/06/03 01:23:24 Done.
487 }
488
490 std::unique_ptr<DEVMODE, base::FreeDeleter> CreateDevMode(HANDLE printer, 489 std::unique_ptr<DEVMODE, base::FreeDeleter> CreateDevMode(HANDLE printer,
491 DEVMODE* in) { 490 DEVMODE* in) {
492 LONG buffer_size = DocumentProperties( 491 LONG buffer_size = DocumentProperties(
493 NULL, printer, const_cast<wchar_t*>(L""), NULL, NULL, 0); 492 NULL, printer, const_cast<wchar_t*>(L""), NULL, NULL, 0);
494 if (buffer_size < static_cast<int>(sizeof(DEVMODE))) 493 if (buffer_size < static_cast<int>(sizeof(DEVMODE)))
495 return std::unique_ptr<DEVMODE, base::FreeDeleter>(); 494 return std::unique_ptr<DEVMODE, base::FreeDeleter>();
496 495
497 // Some drivers request buffers with size smaller than dmSize + dmDriverExtra. 496 // Some drivers request buffers with size smaller than dmSize + dmDriverExtra.
498 // crbug.com/421402 497 // crbug.com/421402
499 buffer_size *= 2; 498 buffer_size *= 2;
500 499
501 std::unique_ptr<DEVMODE, base::FreeDeleter> out( 500 std::unique_ptr<DEVMODE, base::FreeDeleter> out(
502 reinterpret_cast<DEVMODE*>(calloc(buffer_size, 1))); 501 reinterpret_cast<DEVMODE*>(calloc(buffer_size, 1)));
503 DWORD flags = (in ? (DM_IN_BUFFER) : 0) | DM_OUT_BUFFER; 502 DWORD flags = (in ? (DM_IN_BUFFER) : 0) | DM_OUT_BUFFER;
504 503
504 PrinterInfo5 info_5;
505 if (!info_5.Init(printer))
506 return std::unique_ptr<DEVMODE, base::FreeDeleter>();
Lei Zhang 2017/06/02 21:29:54 I think you can just return nullptr. If so, maybe
rbpotter 2017/06/03 01:23:24 Done.
507 const wchar_t* name = info_5.get()->pPrinterName;
508 const wchar_t* port = info_5.get()->pPortName;
509
505 // Check for RPCS drivers on Windows 8+ as DocumentProperties will crash if 510 // Check for RPCS drivers on Windows 8+ as DocumentProperties will crash if
506 // called on one of these printers. See crbug.com/679160 511 // called on one of these printers. Also check that valid paper sizes exist;
507 if (base::win::GetVersion() >= base::win::VERSION_WIN8 && 512 // some old drivers return no paper sizes and crash in DocumentProperties if
508 IsPrinterRPCSOnly(printer)) { 513 // used with Win10. See crbug.com/679160
Lei Zhang 2017/06/02 21:29:54 Append "Also ..." to the existing comment and refe
rbpotter 2017/06/03 01:23:24 Done.
514 if ((base::win::GetVersion() >= base::win::VERSION_WIN8 &&
515 IsPrinterRPCSOnly(name, port)) ||
516 !PrinterHasValidPaperSize(name, port)) {
509 return std::unique_ptr<DEVMODE, base::FreeDeleter>(); 517 return std::unique_ptr<DEVMODE, base::FreeDeleter>();
510 } 518 }
511 519
512 if (DocumentProperties( 520 if (DocumentProperties(
513 NULL, printer, const_cast<wchar_t*>(L""), out.get(), in, flags) != 521 NULL, printer, const_cast<wchar_t*>(L""), out.get(), in, flags) !=
514 IDOK) { 522 IDOK) {
515 return std::unique_ptr<DEVMODE, base::FreeDeleter>(); 523 return std::unique_ptr<DEVMODE, base::FreeDeleter>();
516 } 524 }
517 int size = out->dmSize; 525 int size = out->dmSize;
518 int extra_size = out->dmDriverExtra; 526 int extra_size = out->dmDriverExtra;
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after
553 *canceled = (result == IDCANCEL); 561 *canceled = (result == IDCANCEL);
554 if (result != IDOK) 562 if (result != IDOK)
555 return std::unique_ptr<DEVMODE, base::FreeDeleter>(); 563 return std::unique_ptr<DEVMODE, base::FreeDeleter>();
556 int size = out->dmSize; 564 int size = out->dmSize;
557 int extra_size = out->dmDriverExtra; 565 int extra_size = out->dmDriverExtra;
558 CHECK_GE(buffer_size, size + extra_size); 566 CHECK_GE(buffer_size, size + extra_size);
559 return out; 567 return out;
560 } 568 }
561 569
562 } // namespace printing 570 } // namespace printing
OLDNEW
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698