diff --git a/CHANGES.md b/CHANGES.md index 6016125704..62da040d04 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -16,6 +16,7 @@ Bug Fixes * [#867](https://github.com/java-native-access/jna/issues/867): Fix memory leak in `COMLateBindingObject#getStringProperty` - [@matthiasblaesing](https://github.com/matthiasblaesing). * [#871](https://github.com/java-native-access/jna/issues/871): Fix mapping of libc function `gethostname`, `sethostname`, `getdomainname` and `setdomainname` and bind `com.sun.jna.platform.win32.Winsock2.gethostname(byte[], int)` - [@matthiasblaesing](https://github.com/matthiasblaesing). * [#876](https://github.com/java-native-access/jna/pull/876): Restore java 6 compatibility - [@matthiasblaesing](https://github.com/matthiasblaesing). +* [#880](https://github.com/java-native-access/jna/pull/880): Correctly close file in ELFAnalyser, fix suggested by [@Sylvyrfysh](https://github.com/Sylvyrfysh) in [#880](https://github.com/java-native-access/jna/pull/880) - [@matthiasblaesing](https://github.com/matthiasblaesing). Breaking Changes ---------------- diff --git a/src/com/sun/jna/ELFAnalyser.java b/src/com/sun/jna/ELFAnalyser.java index 6e7d9901ec..ba843821c7 100644 --- a/src/com/sun/jna/ELFAnalyser.java +++ b/src/com/sun/jna/ELFAnalyser.java @@ -104,37 +104,45 @@ private ELFAnalyser(String filename) { } private void runDetection() throws IOException { - // run precheck - only of if the file at least hold an ELF header parsing - // runs further. RandomAccessFile raf = new RandomAccessFile(filename, "r"); - if (raf.length() > 4) { - byte[] magic = new byte[4]; - raf.seek(0); - raf.read(magic); - if (Arrays.equals(magic, ELF_MAGIC)) { - ELF = true; + try { + // run precheck - only of if the file at least hold an ELF header parsing + // runs further. + if (raf.length() > 4) { + byte[] magic = new byte[4]; + raf.seek(0); + raf.read(magic); + if (Arrays.equals(magic, ELF_MAGIC)) { + ELF = true; + } } - } - if (!ELF) { - return; - } - raf.seek(4); - // The total header size depends on the pointer size of the platform - // so before the header is loaded the pointer size has to be determined - byte sizeIndicator = raf.readByte(); - _64Bit = sizeIndicator == EI_CLASS_64BIT; - raf.seek(0); - ByteBuffer headerData = ByteBuffer.allocate(_64Bit ? 64 : 52); - raf.getChannel().read(headerData, 0); - bigEndian = headerData.get(5) == EI_DATA_BIG_ENDIAN; - headerData.order(bigEndian ? ByteOrder.BIG_ENDIAN : ByteOrder.LITTLE_ENDIAN); + if (!ELF) { + return; + } + raf.seek(4); + // The total header size depends on the pointer size of the platform + // so before the header is loaded the pointer size has to be determined + byte sizeIndicator = raf.readByte(); + _64Bit = sizeIndicator == EI_CLASS_64BIT; + raf.seek(0); + ByteBuffer headerData = ByteBuffer.allocate(_64Bit ? 64 : 52); + raf.getChannel().read(headerData, 0); + bigEndian = headerData.get(5) == EI_DATA_BIG_ENDIAN; + headerData.order(bigEndian ? ByteOrder.BIG_ENDIAN : ByteOrder.LITTLE_ENDIAN); + + arm = headerData.get(0x12) == E_MACHINE_ARM; - arm = headerData.get(0x12) == E_MACHINE_ARM; - - if(arm) { - int flags = headerData.getInt(_64Bit ? 0x30 : 0x24); - armHardFloat = (flags & EF_ARM_ABI_FLOAT_HARD) == EF_ARM_ABI_FLOAT_HARD; - armSoftFloat = !armHardFloat; + if(arm) { + int flags = headerData.getInt(_64Bit ? 0x30 : 0x24); + armHardFloat = (flags & EF_ARM_ABI_FLOAT_HARD) == EF_ARM_ABI_FLOAT_HARD; + armSoftFloat = !armHardFloat; + } + } finally { + try { + raf.close(); + } catch (IOException ex) { + // Swallow - closing + } } } }