Skip to content

Commit

Permalink
Merge pull request #882 from matthiasblaesing/close_raf
Browse files Browse the repository at this point in the history
Correctly close RandomAccessFile in ELFAnalyser#runDetection
  • Loading branch information
matthiasblaesing authored Dec 8, 2017
2 parents 028fff0 + fa4d1c9 commit 1bbfc02
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 28 deletions.
1 change: 1 addition & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
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).
* [#882](https://github.com/java-native-access/jna/pull/882): Correctly close file in `ELFAnalyser#runDetection`, 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
Original file line number Diff line number Diff line change
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 1bbfc02

Please sign in to comment.