Skip to content

Commit

Permalink
Correctly close RandomAccessFile in ELFAnalyser#runDetection
Browse files Browse the repository at this point in the history
This closes #880
  • Loading branch information
matthiasblaesing committed Dec 3, 2017
1 parent 028fff0 commit 02ffb64
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 28 deletions.
1 change: 1 addition & 0 deletions CHANGES.md
Expand Up @@ -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
----------------
Expand Down
64 changes: 36 additions & 28 deletions src/com/sun/jna/ELFAnalyser.java
Expand Up @@ -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
}
}
}
}

0 comments on commit 02ffb64

Please sign in to comment.