8292279: (fs) Use try-with-resources to release NativeBuffer

Reviewed-by: alanb
This commit is contained in:
Andrey Turbanov 2022-08-17 07:18:29 +00:00
parent a25e1dc53c
commit 1d9c2f7a6e
13 changed files with 61 additions and 168 deletions

View file

@ -52,18 +52,12 @@ public class RegistryFileTypeDetector
// query HKEY_CLASSES_ROOT\<ext>
String key = filename.substring(dot);
NativeBuffer keyBuffer = null;
NativeBuffer nameBuffer = null;
try {
keyBuffer = WindowsNativeDispatcher.asNativeBuffer(key);
nameBuffer = WindowsNativeDispatcher.asNativeBuffer("Content Type");
try (NativeBuffer keyBuffer = WindowsNativeDispatcher.asNativeBuffer(key);
NativeBuffer nameBuffer = WindowsNativeDispatcher.asNativeBuffer("Content Type")) {
return queryStringValue(keyBuffer.address(), nameBuffer.address());
} catch (WindowsException we) {
we.rethrowAsIOException(file.toString());
return null; // keep compiler happy
} finally {
nameBuffer.release();
keyBuffer.release();
}
}

View file

@ -1,5 +1,5 @@
/*
* Copyright (c) 2008, 2021, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2008, 2022, 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
@ -119,8 +119,7 @@ class WindowsAclFileAttributeView
// GetFileSecurity does not follow links so when following links we
// need the final target
String path = WindowsLinkSupport.getFinalPath(file, followLinks);
NativeBuffer buffer = getFileSecurity(path, OWNER_SECURITY_INFORMATION);
try {
try (NativeBuffer buffer = getFileSecurity(path, OWNER_SECURITY_INFORMATION)) {
// get the address of the SID
long sidAddress = GetSecurityDescriptorOwner(buffer.address());
if (sidAddress == 0L)
@ -129,8 +128,6 @@ class WindowsAclFileAttributeView
} catch (WindowsException x) {
x.rethrowAsIOException(file);
return null;
} finally {
buffer.release();
}
}
@ -146,11 +143,8 @@ class WindowsAclFileAttributeView
// ALLOW and DENY entries in DACL;
// AUDIT entries in SACL (ignore for now as it requires privileges)
NativeBuffer buffer = getFileSecurity(path, DACL_SECURITY_INFORMATION);
try {
try (NativeBuffer buffer = getFileSecurity(path, DACL_SECURITY_INFORMATION)) {
return WindowsSecurityDescriptor.getAcl(buffer.address());
} finally {
buffer.release();
}
}
@ -173,7 +167,7 @@ class WindowsAclFileAttributeView
// ConvertStringSidToSid allocates memory for SID so must invoke
// LocalFree to free it when we are done
long pOwner = 0L;
long pOwner;
try {
pOwner = ConvertStringSidToSid(owner.sidString());
} catch (WindowsException x) {
@ -183,26 +177,21 @@ class WindowsAclFileAttributeView
// Allocate buffer for security descriptor, initialize it, set
// owner information and update the file.
try {
NativeBuffer buffer = NativeBuffers.getNativeBuffer(SIZEOF_SECURITY_DESCRIPTOR);
try (NativeBuffer buffer = NativeBuffers.getNativeBuffer(SIZEOF_SECURITY_DESCRIPTOR)) {
InitializeSecurityDescriptor(buffer.address());
SetSecurityDescriptorOwner(buffer.address(), pOwner);
// may need SeRestorePrivilege to set the owner
WindowsSecurity.Privilege priv =
WindowsSecurity.enablePrivilege("SeRestorePrivilege");
try {
InitializeSecurityDescriptor(buffer.address());
SetSecurityDescriptorOwner(buffer.address(), pOwner);
// may need SeRestorePrivilege to set the owner
WindowsSecurity.Privilege priv =
WindowsSecurity.enablePrivilege("SeRestorePrivilege");
try {
SetFileSecurity(path,
OWNER_SECURITY_INFORMATION,
buffer.address());
} finally {
priv.drop();
}
} catch (WindowsException x) {
x.rethrowAsIOException(file);
SetFileSecurity(path,
OWNER_SECURITY_INFORMATION,
buffer.address());
} finally {
buffer.release();
priv.drop();
}
} catch (WindowsException x) {
x.rethrowAsIOException(file);
} finally {
LocalFree(pOwner);
}

View file

@ -1,5 +1,5 @@
/*
* Copyright (c) 2008, 2019, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2008, 2022, 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
@ -263,9 +263,7 @@ class WindowsFileAttributes
static WindowsFileAttributes readAttributes(long handle)
throws WindowsException
{
NativeBuffer buffer = NativeBuffers
.getNativeBuffer(SIZEOF_FILE_INFORMATION);
try {
try (NativeBuffer buffer = NativeBuffers.getNativeBuffer(SIZEOF_FILE_INFORMATION)) {
long address = buffer.address();
GetFileInformationByHandle(handle, address);
@ -275,18 +273,13 @@ class WindowsFileAttributes
.getInt(address + OFFSETOF_FILE_INFORMATION_ATTRIBUTES);
if (isReparsePoint(fileAttrs)) {
int size = MAXIMUM_REPARSE_DATA_BUFFER_SIZE;
NativeBuffer reparseBuffer = NativeBuffers.getNativeBuffer(size);
try {
try (NativeBuffer reparseBuffer = NativeBuffers.getNativeBuffer(size)) {
DeviceIoControlGetReparsePoint(handle, reparseBuffer.address(), size);
reparseTag = (int)unsafe.getLong(reparseBuffer.address());
} finally {
reparseBuffer.release();
}
}
return fromFileInformation(address, reparseTag);
} finally {
buffer.release();
}
}
@ -300,9 +293,8 @@ class WindowsFileAttributes
WindowsException firstException = null;
// GetFileAttributesEx is the fastest way to read the attributes
NativeBuffer buffer =
NativeBuffers.getNativeBuffer(SIZEOF_FILE_ATTRIBUTE_DATA);
try {
try (NativeBuffer buffer =
NativeBuffers.getNativeBuffer(SIZEOF_FILE_ATTRIBUTE_DATA)) {
long address = buffer.address();
GetFileAttributesEx(path.getPathForWin32Calls(), address);
// if reparse point then file may be a sym link; otherwise
@ -315,8 +307,6 @@ class WindowsFileAttributes
if (x.lastError() != ERROR_SHARING_VIOLATION)
throw x;
firstException = x;
} finally {
buffer.release();
}
// For sharing violations, fallback to FindFirstFile if the file
@ -326,8 +316,7 @@ class WindowsFileAttributes
char last = search.charAt(search.length() -1);
if (last == ':' || last == '\\')
throw firstException;
buffer = getBufferForFindData();
try {
try (NativeBuffer buffer = getBufferForFindData()) {
long handle = FindFirstFile(search, buffer.address());
FindClose(handle);
WindowsFileAttributes attrs = fromFindData(buffer.address());
@ -340,8 +329,6 @@ class WindowsFileAttributes
return attrs;
} catch (WindowsException ignore) {
throw firstException;
} finally {
buffer.release();
}
}
}

View file

@ -514,17 +514,12 @@ class WindowsFileCopy {
try {
int request = (DACL_SECURITY_INFORMATION |
OWNER_SECURITY_INFORMATION | GROUP_SECURITY_INFORMATION);
NativeBuffer buffer =
WindowsAclFileAttributeView.getFileSecurity(path, request);
try {
try {
SetFileSecurity(target.getPathForWin32Calls(), request,
try (NativeBuffer buffer =
WindowsAclFileAttributeView.getFileSecurity(path, request)) {
SetFileSecurity(target.getPathForWin32Calls(), request,
buffer.address());
} catch (WindowsException x) {
x.rethrowAsIOException(target);
}
} finally {
buffer.release();
} catch (WindowsException x) {
x.rethrowAsIOException(target);
}
} finally {
priv.drop();

View file

@ -33,8 +33,6 @@ import java.net.URI;
import java.util.concurrent.ExecutorService;
import java.io.*;
import java.util.*;
import java.security.AccessController;
import jdk.internal.misc.Unsafe;
import jdk.internal.util.StaticProperty;
import sun.nio.ch.ThreadPool;
import sun.security.util.SecurityConstants;
@ -302,12 +300,11 @@ class WindowsFileSystemProvider
// read security descriptor containing ACL (symlinks are followed)
boolean hasRights = false;
String target = WindowsLinkSupport.getFinalPath(file, true);
NativeBuffer aclBuffer = WindowsAclFileAttributeView
try (NativeBuffer aclBuffer = WindowsAclFileAttributeView
.getFileSecurity(target,
DACL_SECURITY_INFORMATION
| OWNER_SECURITY_INFORMATION
| GROUP_SECURITY_INFORMATION);
try {
| GROUP_SECURITY_INFORMATION)) {
hasRights = checkAccessMask(aclBuffer.address(), rights,
FILE_GENERIC_READ,
FILE_GENERIC_WRITE,
@ -315,8 +312,6 @@ class WindowsFileSystemProvider
FILE_ALL_ACCESS);
} catch (WindowsException exc) {
exc.rethrowAsIOException(file);
} finally {
aclBuffer.release();
}
return hasRights;
}

View file

@ -1,5 +1,5 @@
/*
* Copyright (c) 2008, 2021, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2008, 2022, 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
@ -269,8 +269,7 @@ class WindowsLinkSupport {
*/
private static String readLinkImpl(long handle) throws IOException {
int size = MAXIMUM_REPARSE_DATA_BUFFER_SIZE;
NativeBuffer buffer = NativeBuffers.getNativeBuffer(size);
try {
try (NativeBuffer buffer = NativeBuffers.getNativeBuffer(size)) {
try {
DeviceIoControlGetReparsePoint(handle, buffer.address(), size);
} catch (WindowsException x) {
@ -334,8 +333,6 @@ class WindowsLinkSupport {
throw new IOException("Symbolic link target is invalid");
}
return target;
} finally {
buffer.release();
}
}

View file

@ -495,13 +495,10 @@ class WindowsNativeDispatcher {
static VolumeInformation GetVolumeInformation(String root)
throws WindowsException
{
NativeBuffer buffer = asNativeBuffer(root);
try {
try (NativeBuffer buffer = asNativeBuffer(root)) {
VolumeInformation info = new VolumeInformation();
GetVolumeInformation0(buffer.address(), info);
return info;
} finally {
buffer.release();
}
}
static class VolumeInformation {
@ -526,11 +523,8 @@ class WindowsNativeDispatcher {
* )
*/
static int GetDriveType(String root) throws WindowsException {
NativeBuffer buffer = asNativeBuffer(root);
try {
try (NativeBuffer buffer = asNativeBuffer(root)) {
return GetDriveType0(buffer.address());
} finally {
buffer.release();
}
}
private static native int GetDriveType0(long lpRoot) throws WindowsException;
@ -546,13 +540,10 @@ class WindowsNativeDispatcher {
static DiskFreeSpace GetDiskFreeSpaceEx(String path)
throws WindowsException
{
NativeBuffer buffer = asNativeBuffer(path);
try {
try (NativeBuffer buffer = asNativeBuffer(path)) {
DiskFreeSpace space = new DiskFreeSpace();
GetDiskFreeSpaceEx0(buffer.address(), space);
return space;
} finally {
buffer.release();
}
}
@ -568,13 +559,10 @@ class WindowsNativeDispatcher {
static DiskFreeSpace GetDiskFreeSpace(String path)
throws WindowsException
{
NativeBuffer buffer = asNativeBuffer(path);
try {
try (NativeBuffer buffer = asNativeBuffer(path)) {
DiskFreeSpace space = new DiskFreeSpace();
GetDiskFreeSpace0(buffer.address(), space);
return space;
} finally {
buffer.release();
}
}
@ -609,11 +597,8 @@ class WindowsNativeDispatcher {
* @return lpFileName
*/
static String GetVolumePathName(String path) throws WindowsException {
NativeBuffer buffer = asNativeBuffer(path);
try {
try (NativeBuffer buffer = asNativeBuffer(path)) {
return GetVolumePathName0(buffer.address());
} finally {
buffer.release();
}
}
private static native String GetVolumePathName0(long lpFileName)
@ -653,12 +638,9 @@ class WindowsNativeDispatcher {
long pSecurityDescriptor,
int nLength) throws WindowsException
{
NativeBuffer buffer = asNativeBuffer(path);
try {
try (NativeBuffer buffer = asNativeBuffer(path)) {
return GetFileSecurity0(buffer.address(), requestedInformation,
pSecurityDescriptor, nLength);
} finally {
buffer.release();
}
}
private static native int GetFileSecurity0(long lpFileName,
@ -678,12 +660,9 @@ class WindowsNativeDispatcher {
long pSecurityDescriptor)
throws WindowsException
{
NativeBuffer buffer = asNativeBuffer(path);
try {
try (NativeBuffer buffer = asNativeBuffer(path)) {
// may be called with elevated privileges so always run on current thread
SetFileSecurity0(buffer.address(), securityInformation, pSecurityDescriptor);
} finally {
buffer.release();
}
}
static native void SetFileSecurity0(long lpFileName, int securityInformation,
@ -835,11 +814,8 @@ class WindowsNativeDispatcher {
long pSid,
int cbSid) throws WindowsException
{
NativeBuffer buffer = asNativeBuffer(accountName);
try {
try (NativeBuffer buffer = asNativeBuffer(accountName)) {
return LookupAccountName0(buffer.address(), pSid, cbSid);
} finally {
buffer.release();
}
}
private static native int LookupAccountName0(long lpAccountName, long pSid,
@ -874,11 +850,8 @@ class WindowsNativeDispatcher {
static long ConvertStringSidToSid(String sidString)
throws WindowsException
{
NativeBuffer buffer = asNativeBuffer(sidString);
try {
try (NativeBuffer buffer = asNativeBuffer(sidString)) {
return ConvertStringSidToSid0(buffer.address());
} finally {
buffer.release();
}
}
private static native long ConvertStringSidToSid0(long lpStringSid)
@ -974,11 +947,8 @@ class WindowsNativeDispatcher {
/**
*/
static long LookupPrivilegeValue(String name) throws WindowsException {
NativeBuffer buffer = asNativeBuffer(name);
try {
try (NativeBuffer buffer = asNativeBuffer(name)) {
return LookupPrivilegeValue0(buffer.address());
} finally {
buffer.release();
}
}
private static native long LookupPrivilegeValue0(long lpName)
@ -1036,13 +1006,9 @@ class WindowsNativeDispatcher {
static void CreateHardLink(String newFile, String existingFile)
throws WindowsException
{
NativeBuffer newFileBuffer = asNativeBuffer(newFile);
NativeBuffer existingFileBuffer = asNativeBuffer(existingFile);
try {
try (NativeBuffer newFileBuffer = asNativeBuffer(newFile);
NativeBuffer existingFileBuffer = asNativeBuffer(existingFile)) {
CreateHardLink0(newFileBuffer.address(), existingFileBuffer.address());
} finally {
existingFileBuffer.release();
newFileBuffer.release();
}
}
private static native void CreateHardLink0(long newFileBuffer,
@ -1057,11 +1023,8 @@ class WindowsNativeDispatcher {
* )
*/
static String GetFullPathName(String path) throws WindowsException {
NativeBuffer buffer = asNativeBuffer(path);
try {
try (NativeBuffer buffer = asNativeBuffer(path)) {
return GetFullPathName0(buffer.address());
} finally {
buffer.release();
}
}
private static native String GetFullPathName0(long pathAddress)

View file

@ -1,5 +1,5 @@
/*
* Copyright (c) 2008, 2021, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2008, 2022, 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
@ -140,7 +140,7 @@ class WindowsUserPrincipals {
}
// invoke LookupAccountName to get buffer size needed for SID
int size = 0;
int size;
try {
size = LookupAccountName(name, 0L, 0);
} catch (WindowsException x) {
@ -151,8 +151,7 @@ class WindowsUserPrincipals {
assert size > 0;
// allocate buffer and re-invoke LookupAccountName get SID
NativeBuffer sidBuffer = NativeBuffers.getNativeBuffer(size);
try {
try (NativeBuffer sidBuffer = NativeBuffers.getNativeBuffer(size)) {
int newSize = LookupAccountName(name, sidBuffer.address(), size);
if (newSize != size) {
// can this happen?
@ -163,8 +162,6 @@ class WindowsUserPrincipals {
return fromSid(sidBuffer.address());
} catch (WindowsException x) {
throw new IOException(name + ": " + x.errorString());
} finally {
sidBuffer.release();
}
}
}