Skip to content

Commit

Permalink
Check target number to be greater than zero, before calling `Structur…
Browse files Browse the repository at this point in the history
…e#toArray`

Closes: #1091
  • Loading branch information
matthiasblaesing committed May 5, 2019
1 parent 869fa9a commit 0b098db
Show file tree
Hide file tree
Showing 2 changed files with 85 additions and 60 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
---------
* [#1091](https://github.com/java-native-access/jna/issues/1091): Check target number to be greater than zero, before calling `Structure#toArray` in `c.s.j.p.win32.Netapi32Util` - [@trevormagg](https://github.com/trevormaggs), [@matthiasblaesing](https://github.com/matthiasblaesing).

Release 5.3.1
=============
Expand Down
144 changes: 84 additions & 60 deletions contrib/platform/src/com/sun/jna/platform/win32/Netapi32Util.java
Original file line number Diff line number Diff line change
Expand Up @@ -211,20 +211,24 @@ public static LocalGroup[] getLocalGroups(String serverName) {
if (LMErr.NERR_Success != rc || bufptr.getValue() == Pointer.NULL) {
throw new Win32Exception(rc);
}
LMAccess.LOCALGROUP_INFO_1 group = new LMAccess.LOCALGROUP_INFO_1(bufptr.getValue());
LMAccess.LOCALGROUP_INFO_1[] groups = (LOCALGROUP_INFO_1[]) group.toArray(entriesRead.getValue());

ArrayList<LocalGroup> result = new ArrayList<LocalGroup>();
for(LOCALGROUP_INFO_1 lgpi : groups) {
LocalGroup lgp = new LocalGroup();
if (lgpi.lgrui1_name != null) {
lgp.name = lgpi.lgrui1_name.toString();
}
if (lgpi.lgrui1_comment != null) {
lgp.comment = lgpi.lgrui1_comment.toString();

if (entriesRead.getValue() > 0) {
LMAccess.LOCALGROUP_INFO_1 group = new LMAccess.LOCALGROUP_INFO_1(bufptr.getValue());
LMAccess.LOCALGROUP_INFO_1[] groups = (LOCALGROUP_INFO_1[]) group.toArray(entriesRead.getValue());
for (LOCALGROUP_INFO_1 lgpi : groups) {
LocalGroup lgp = new LocalGroup();
if (lgpi.lgrui1_name != null) {
lgp.name = lgpi.lgrui1_name;
}
if (lgpi.lgrui1_comment != null) {
lgp.comment = lgpi.lgrui1_comment;
}
result.add(lgp);
}
result.add(lgp);
}

return result.toArray(new LocalGroup[0]);
} finally {
if (bufptr.getValue() != Pointer.NULL) {
Expand Down Expand Up @@ -260,20 +264,24 @@ public static Group[] getGlobalGroups(String serverName) {
if (LMErr.NERR_Success != rc || bufptr.getValue() == Pointer.NULL) {
throw new Win32Exception(rc);
}
LMAccess.GROUP_INFO_1 group = new LMAccess.GROUP_INFO_1(bufptr.getValue());
LMAccess.GROUP_INFO_1[] groups = (LMAccess.GROUP_INFO_1[]) group.toArray(entriesRead.getValue());

ArrayList<LocalGroup> result = new ArrayList<LocalGroup>();
for(LMAccess.GROUP_INFO_1 lgpi : groups) {
LocalGroup lgp = new LocalGroup();
if (lgpi.grpi1_name != null) {
lgp.name = lgpi.grpi1_name.toString();
}
if (lgpi.grpi1_comment != null) {
lgp.comment = lgpi.grpi1_comment.toString();

if (entriesRead.getValue() > 0) {
LMAccess.GROUP_INFO_1 group = new LMAccess.GROUP_INFO_1(bufptr.getValue());
LMAccess.GROUP_INFO_1[] groups = (LMAccess.GROUP_INFO_1[]) group.toArray(entriesRead.getValue());
for (LMAccess.GROUP_INFO_1 lgpi : groups) {
LocalGroup lgp = new LocalGroup();
if (lgpi.grpi1_name != null) {
lgp.name = lgpi.grpi1_name.toString();
}
if (lgpi.grpi1_comment != null) {
lgp.comment = lgpi.grpi1_comment.toString();
}
result.add(lgp);
}
result.add(lgp);
}

return result.toArray(new LocalGroup[0]);
} finally {
if (bufptr.getValue() != Pointer.NULL) {
Expand Down Expand Up @@ -310,16 +318,21 @@ public static User[] getUsers(String serverName) {
if (LMErr.NERR_Success != rc || bufptr.getValue() == Pointer.NULL) {
throw new Win32Exception(rc);
}
LMAccess.USER_INFO_1 user = new LMAccess.USER_INFO_1(bufptr.getValue());
LMAccess.USER_INFO_1[] users = (LMAccess.USER_INFO_1[]) user.toArray(entriesRead.getValue());

ArrayList<User> result = new ArrayList<User>();
for(LMAccess.USER_INFO_1 lu : users) {
User auser = new User();
if (lu.usri1_name != null) {
auser.name = lu.usri1_name.toString();

if (entriesRead.getValue() > 0) {
LMAccess.USER_INFO_1 user = new LMAccess.USER_INFO_1(bufptr.getValue());
LMAccess.USER_INFO_1[] users = (LMAccess.USER_INFO_1[]) user.toArray(entriesRead.getValue());
for (LMAccess.USER_INFO_1 lu : users) {
User auser = new User();
if (lu.usri1_name != null) {
auser.name = lu.usri1_name;
}
result.add(auser);
}
result.add(auser);
}

return result.toArray(new User[0]);
} finally {
if (bufptr.getValue() != Pointer.NULL) {
Expand Down Expand Up @@ -365,15 +378,17 @@ public static Group[] getUserLocalGroups(String userName, String serverName) {
if (rc != LMErr.NERR_Success) {
throw new Win32Exception(rc);
}
LOCALGROUP_USERS_INFO_0 lgroup = new LOCALGROUP_USERS_INFO_0(bufptr.getValue());
LOCALGROUP_USERS_INFO_0[] lgroups = (LOCALGROUP_USERS_INFO_0[]) lgroup.toArray(entriesread.getValue());
ArrayList<Group> result = new ArrayList<Group>();
for (LOCALGROUP_USERS_INFO_0 lgpi : lgroups) {
LocalGroup lgp = new LocalGroup();
if (lgpi.lgrui0_name != null) {
lgp.name = lgpi.lgrui0_name.toString();
if (entriesread.getValue() > 0) {
LOCALGROUP_USERS_INFO_0 lgroup = new LOCALGROUP_USERS_INFO_0(bufptr.getValue());
LOCALGROUP_USERS_INFO_0[] lgroups = (LOCALGROUP_USERS_INFO_0[]) lgroup.toArray(entriesread.getValue());
for (LOCALGROUP_USERS_INFO_0 lgpi : lgroups) {
LocalGroup lgp = new LocalGroup();
if (lgpi.lgrui0_name != null) {
lgp.name = lgpi.lgrui0_name;
}
result.add(lgp);
}
result.add(lgp);
}
return result.toArray(new Group[0]);
} finally {
Expand Down Expand Up @@ -412,16 +427,21 @@ public static Group[] getUserGroups(String userName, String serverName) {
if (rc != LMErr.NERR_Success) {
throw new Win32Exception(rc);
}
GROUP_USERS_INFO_0 lgroup = new GROUP_USERS_INFO_0(bufptr.getValue());
GROUP_USERS_INFO_0[] lgroups = (GROUP_USERS_INFO_0[]) lgroup.toArray(entriesread.getValue());

ArrayList<Group> result = new ArrayList<Group>();
for (GROUP_USERS_INFO_0 lgpi : lgroups) {
Group lgp = new Group();
if (lgpi.grui0_name != null) {
lgp.name = lgpi.grui0_name.toString();

if (entriesread.getValue() > 0) {
GROUP_USERS_INFO_0 lgroup = new GROUP_USERS_INFO_0(bufptr.getValue());
GROUP_USERS_INFO_0[] lgroups = (GROUP_USERS_INFO_0[]) lgroup.toArray(entriesread.getValue());
for (GROUP_USERS_INFO_0 lgpi : lgroups) {
Group lgp = new Group();
if (lgpi.grui0_name != null) {
lgp.name = lgpi.grui0_name;
}
result.add(lgp);
}
result.add(lgp);
}

return result.toArray(new Group[0]);
} finally {
if (bufptr.getValue() != Pointer.NULL) {
Expand Down Expand Up @@ -635,28 +655,32 @@ public static DomainTrust[] getDomainTrusts(String serverName) {
throw new Win32Exception(rc);
}
try {
DS_DOMAIN_TRUSTS domainTrustRefs = new DS_DOMAIN_TRUSTS(domainsPointerRef.getValue());
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();
if (domainTrust.DnsDomainName != null) {
t.DnsDomainName = domainTrust.DnsDomainName.toString();
}
if (domainTrust.NetbiosDomainName != null) {
t.NetbiosDomainName = domainTrust.NetbiosDomainName.toString();
}
t.DomainSid = domainTrust.DomainSid;
if (domainTrust.DomainSid != null) {
t.DomainSidString = Advapi32Util.convertSidToStringSid(domainTrust.DomainSid);
}
t.DomainGuid = domainTrust.DomainGuid;
if (domainTrust.DomainGuid != null) {
t.DomainGuidString = Ole32Util.getStringFromGUID(domainTrust.DomainGuid);

if(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[domainTrustCount.getValue()]);
for (DS_DOMAIN_TRUSTS domainTrust : domainTrusts) {
DomainTrust t = new DomainTrust();
if (domainTrust.DnsDomainName != null) {
t.DnsDomainName = domainTrust.DnsDomainName;
}
if (domainTrust.NetbiosDomainName != null) {
t.NetbiosDomainName = domainTrust.NetbiosDomainName;
}
t.DomainSid = domainTrust.DomainSid;
if (domainTrust.DomainSid != null) {
t.DomainSidString = Advapi32Util.convertSidToStringSid(domainTrust.DomainSid);
}
t.DomainGuid = domainTrust.DomainGuid;
if (domainTrust.DomainGuid != null) {
t.DomainGuidString = Ole32Util.getStringFromGUID(domainTrust.DomainGuid);
}
t.flags = domainTrust.Flags;
trusts.add(t);
}
t.flags = domainTrust.Flags;
trusts.add(t);
}

return trusts.toArray(new DomainTrust[0]);
} finally {
rc = Netapi32.INSTANCE.NetApiBufferFree(domainsPointerRef.getValue());
Expand Down

0 comments on commit 0b098db

Please sign in to comment.