Skip to content

Commit

Permalink
[GITHUB-1087] Fix wrong calls to Structure#toArray with zero sized ar…
Browse files Browse the repository at this point in the history
…rays

As a performance optimization calls to Collections#toArray where changed
from providing a correctly sized array to using a zero sized array.

See 07f3ce0

The change is invalid if the object toArray is called on is a Structure,
the calls just look the same. This changeset is a partial revert of the
above mentioned commit.

Closes: java-native-access#1087
  • Loading branch information
matthiasblaesing committed Apr 25, 2019
1 parent db28278 commit 90b8885
Show file tree
Hide file tree
Showing 5 changed files with 14 additions and 4 deletions.
1 change: 1 addition & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ Features

Bug Fixes
---------
* [#1087](https://github.com/java-native-access/jna/pull/1087): Fix wrong calls to Structure#toArray with zero sized arrays - [@matthiasblaesing](https://github.com/matthiasblaesing).

Release 5.3.0
=============
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -670,9 +670,11 @@ public static final WinNT.SYSTEM_LOGICAL_PROCESSOR_INFORMATION[] getLogicalProce
}
}
WinNT.SYSTEM_LOGICAL_PROCESSOR_INFORMATION firstInformation = new WinNT.SYSTEM_LOGICAL_PROCESSOR_INFORMATION(
memory);
memory);
int returnedStructCount = bufferSize.getValue().intValue()
/ sizePerStruct;
return (WinNT.SYSTEM_LOGICAL_PROCESSOR_INFORMATION[]) firstInformation
.toArray(new WinNT.SYSTEM_LOGICAL_PROCESSOR_INFORMATION[0]);
.toArray(new WinNT.SYSTEM_LOGICAL_PROCESSOR_INFORMATION[returnedStructCount]);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -636,7 +636,7 @@ public static DomainTrust[] getDomainTrusts(String serverName) {
}
try {
DS_DOMAIN_TRUSTS domainTrustRefs = new DS_DOMAIN_TRUSTS(domainsPointerRef.getValue());
DS_DOMAIN_TRUSTS[] domainTrusts = (DS_DOMAIN_TRUSTS[]) domainTrustRefs.toArray(new DS_DOMAIN_TRUSTS[0]);
DS_DOMAIN_TRUSTS[] domainTrusts = (DS_DOMAIN_TRUSTS[]) domainTrustRefs.toArray(new DS_DOMAIN_TRUSTS[domainTrustCount.getValue()]);
ArrayList<DomainTrust> trusts = new ArrayList<DomainTrust>(domainTrustCount.getValue());
for(DS_DOMAIN_TRUSTS domainTrust : domainTrusts) {
DomainTrust t = new DomainTrust();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
import com.sun.jna.platform.win32.WinNT.NUMA_NODE_RELATIONSHIP;
import com.sun.jna.platform.win32.WinNT.PROCESSOR_CACHE_TYPE;
import com.sun.jna.platform.win32.WinNT.PROCESSOR_RELATIONSHIP;
import com.sun.jna.platform.win32.WinNT.SYSTEM_LOGICAL_PROCESSOR_INFORMATION;
import com.sun.jna.platform.win32.WinNT.SYSTEM_LOGICAL_PROCESSOR_INFORMATION_EX;

import junit.framework.TestCase;
Expand Down Expand Up @@ -367,6 +368,12 @@ public void testExpandEnvironmentStrings() {
assertEquals("DemoValue", Kernel32Util.expandEnvironmentStrings("%DemoVariable%"));
}

public void testGetLogicalProcessorInformation() {
SYSTEM_LOGICAL_PROCESSOR_INFORMATION[] procInfo = Kernel32Util
.getLogicalProcessorInformation();
assertTrue(procInfo.length > 0);
}

public void testGetLogicalProcessorInformationEx() {
SYSTEM_LOGICAL_PROCESSOR_INFORMATION_EX[] procInfo = Kernel32Util
.getLogicalProcessorInformationEx(WinNT.LOGICAL_PROCESSOR_RELATIONSHIP.RelationAll);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -260,7 +260,7 @@ public void testDsEnumerateDomainTrusts() {
assertTrue(domainTrustCount.getValue() >= 0);

DS_DOMAIN_TRUSTS domainTrustRefs = new DS_DOMAIN_TRUSTS(domainsPointerRef.getValue());
DS_DOMAIN_TRUSTS[] domainTrusts = (DS_DOMAIN_TRUSTS[]) domainTrustRefs.toArray(new DS_DOMAIN_TRUSTS[0]);
DS_DOMAIN_TRUSTS[] domainTrusts = (DS_DOMAIN_TRUSTS[]) domainTrustRefs.toArray(new DS_DOMAIN_TRUSTS[domainTrustCount.getValue()]);

for (DS_DOMAIN_TRUSTS trust : domainTrusts) {
assertTrue(trust.DnsDomainName.length() > 0);
Expand Down

0 comments on commit 90b8885

Please sign in to comment.