Skip to content
This repository has been archived by the owner on Feb 2, 2023. It is now read-only.

Commit

Permalink
Browse files Browse the repository at this point in the history
8262829: Native crash in Win32PrintServiceLookup.getAllPrinterNames()
Backport-of: a6e34b3d1c6e2e278fe2d26d7e9028898a1c01b6
  • Loading branch information
Yuri Nesterenko committed May 19, 2021
1 parent 14df120 commit 31a8f04
Showing 1 changed file with 20 additions and 7 deletions.
27 changes: 20 additions & 7 deletions src/java.desktop/windows/native/libawt/windows/WPrinterJob.cpp
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2000, 2020, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2000, 2021, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
Expand Down Expand Up @@ -133,12 +133,25 @@ static jobjectArray getPrinterNames(JNIEnv *env, DWORD flags) {
jobjectArray nameArray;

try {
::EnumPrinters(flags,
NULL, 4, NULL, 0, &cbNeeded, &cReturned);
pPrinterEnum = new BYTE[cbNeeded];
::EnumPrinters(flags,
NULL, 4, pPrinterEnum, cbNeeded, &cbNeeded,
&cReturned);
::EnumPrinters(flags, NULL, 4, NULL,
0, &cbNeeded, &cReturned);

BOOL bResult;
int nCount = 0;
do {
if (pPrinterEnum != NULL) {
delete [] pPrinterEnum;
}
pPrinterEnum = new BYTE[cbNeeded];

bResult = ::EnumPrinters(flags, NULL, 4, pPrinterEnum,
cbNeeded, &cbNeeded, &cReturned);
} while (!bResult && ++nCount < 5);

if (!bResult) {
// No printers in case of error
cReturned = 0;
}

if (cReturned > 0) {
nameArray = env->NewObjectArray(cReturned, clazz, NULL);
Expand Down

1 comment on commit 31a8f04

@openjdk-notifier
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.