From 0b098dbbb12bf912f438421e6837b3739dafa0f6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matthias=20Bl=C3=A4sing?= Date: Sun, 5 May 2019 16:38:48 +0200 Subject: [PATCH 1/3] Check target number to be greater than zero, before calling `Structure#toArray` Closes: #1091 --- CHANGES.md | 1 + .../sun/jna/platform/win32/Netapi32Util.java | 144 ++++++++++-------- 2 files changed, 85 insertions(+), 60 deletions(-) diff --git a/CHANGES.md b/CHANGES.md index 781646261c..11b4eca758 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -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 ============= diff --git a/contrib/platform/src/com/sun/jna/platform/win32/Netapi32Util.java b/contrib/platform/src/com/sun/jna/platform/win32/Netapi32Util.java index af942a4c62..97862e7580 100644 --- a/contrib/platform/src/com/sun/jna/platform/win32/Netapi32Util.java +++ b/contrib/platform/src/com/sun/jna/platform/win32/Netapi32Util.java @@ -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 result = new ArrayList(); - 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) { @@ -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 result = new ArrayList(); - 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) { @@ -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 result = new ArrayList(); - 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) { @@ -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 result = new ArrayList(); - 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 { @@ -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 result = new ArrayList(); - 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) { @@ -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 trusts = new ArrayList(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()); From a56845415aad6da515444c7d6d9d088991c7af92 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matthias=20Bl=C3=A4sing?= Date: Sun, 5 May 2019 16:41:52 +0200 Subject: [PATCH 2/3] Remove redundant String#toString calls --- .../sun/jna/platform/win32/Netapi32Util.java | 23 +++++++++---------- 1 file changed, 11 insertions(+), 12 deletions(-) diff --git a/contrib/platform/src/com/sun/jna/platform/win32/Netapi32Util.java b/contrib/platform/src/com/sun/jna/platform/win32/Netapi32Util.java index 97862e7580..319f51f8db 100644 --- a/contrib/platform/src/com/sun/jna/platform/win32/Netapi32Util.java +++ b/contrib/platform/src/com/sun/jna/platform/win32/Netapi32Util.java @@ -273,10 +273,10 @@ public static Group[] getGlobalGroups(String serverName) { for (LMAccess.GROUP_INFO_1 lgpi : groups) { LocalGroup lgp = new LocalGroup(); if (lgpi.grpi1_name != null) { - lgp.name = lgpi.grpi1_name.toString(); + lgp.name = lgpi.grpi1_name; } if (lgpi.grpi1_comment != null) { - lgp.comment = lgpi.grpi1_comment.toString(); + lgp.comment = lgpi.grpi1_comment; } result.add(lgp); } @@ -506,22 +506,22 @@ public static DomainController getDC() { } DomainController dc = new DomainController(); if (pdci.dci.DomainControllerAddress != null) { - dc.address = pdci.dci.DomainControllerAddress.toString(); + dc.address = pdci.dci.DomainControllerAddress; } dc.addressType = pdci.dci.DomainControllerAddressType; if (pdci.dci.ClientSiteName != null) { - dc.clientSiteName = pdci.dci.ClientSiteName.toString(); + dc.clientSiteName = pdci.dci.ClientSiteName; } if (pdci.dci.DnsForestName != null) { - dc.dnsForestName = pdci.dci.DnsForestName.toString(); + dc.dnsForestName = pdci.dci.DnsForestName; } dc.domainGuid = pdci.dci.DomainGuid; if (pdci.dci.DomainName != null) { - dc.domainName = pdci.dci.DomainName.toString(); + dc.domainName = pdci.dci.DomainName; } dc.flags = pdci.dci.Flags; if (pdci.dci.DomainControllerName != null) { - dc.name = pdci.dci.DomainControllerName.toString(); + dc.name = pdci.dci.DomainControllerName; } rc = Netapi32.INSTANCE.NetApiBufferFree(pdci.dci.getPointer()); if (LMErr.NERR_Success != rc) { @@ -696,21 +696,20 @@ public static UserInfo getUserInfo(String accountName) { public static UserInfo getUserInfo(String accountName, String domainName) { PointerByReference bufptr = new PointerByReference(); - int rc = -1; try { - rc = Netapi32.INSTANCE.NetUserGetInfo(domainName, accountName, (short)23, bufptr); + int rc = Netapi32.INSTANCE.NetUserGetInfo(domainName, accountName, (short)23, bufptr); if (rc == LMErr.NERR_Success) { USER_INFO_23 info_23 = new USER_INFO_23(bufptr.getValue()); UserInfo userInfo = new UserInfo(); if (info_23.usri23_comment != null) { - userInfo.comment = info_23.usri23_comment.toString(); + userInfo.comment = info_23.usri23_comment; } userInfo.flags = info_23.usri23_flags; if (info_23.usri23_full_name != null) { - userInfo.fullName = info_23.usri23_full_name.toString(); + userInfo.fullName = info_23.usri23_full_name; } if (info_23.usri23_name != null) { - userInfo.name = info_23.usri23_name.toString(); + userInfo.name = info_23.usri23_name; } if (info_23.usri23_user_sid != null) { userInfo.sidString = Advapi32Util.convertSidToStringSid(info_23.usri23_user_sid); From 1bfce0864bc4eb9a8cd490a9d7976ffefd23cbe5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matthias=20Bl=C3=A4sing?= Date: Mon, 6 May 2019 19:35:55 +0200 Subject: [PATCH 3/3] Remove unnecessary NULL checks --- .../sun/jna/platform/win32/Netapi32Util.java | 48 +++++-------------- 1 file changed, 12 insertions(+), 36 deletions(-) diff --git a/contrib/platform/src/com/sun/jna/platform/win32/Netapi32Util.java b/contrib/platform/src/com/sun/jna/platform/win32/Netapi32Util.java index 319f51f8db..db8222e769 100644 --- a/contrib/platform/src/com/sun/jna/platform/win32/Netapi32Util.java +++ b/contrib/platform/src/com/sun/jna/platform/win32/Netapi32Util.java @@ -219,12 +219,8 @@ public static LocalGroup[] getLocalGroups(String serverName) { 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; - } + lgp.name = lgpi.lgrui1_name; + lgp.comment = lgpi.lgrui1_comment; result.add(lgp); } } @@ -272,12 +268,8 @@ public static Group[] getGlobalGroups(String serverName) { 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; - } - if (lgpi.grpi1_comment != null) { - lgp.comment = lgpi.grpi1_comment; - } + lgp.name = lgpi.grpi1_name; + lgp.comment = lgpi.grpi1_comment; result.add(lgp); } } @@ -505,24 +497,14 @@ public static DomainController getDC() { throw new Win32Exception(rc); } DomainController dc = new DomainController(); - if (pdci.dci.DomainControllerAddress != null) { - dc.address = pdci.dci.DomainControllerAddress; - } + dc.address = pdci.dci.DomainControllerAddress; dc.addressType = pdci.dci.DomainControllerAddressType; - if (pdci.dci.ClientSiteName != null) { - dc.clientSiteName = pdci.dci.ClientSiteName; - } - if (pdci.dci.DnsForestName != null) { - dc.dnsForestName = pdci.dci.DnsForestName; - } + dc.clientSiteName = pdci.dci.ClientSiteName; + dc.dnsForestName = pdci.dci.DnsForestName; dc.domainGuid = pdci.dci.DomainGuid; - if (pdci.dci.DomainName != null) { - dc.domainName = pdci.dci.DomainName; - } + dc.domainName = pdci.dci.DomainName; dc.flags = pdci.dci.Flags; - if (pdci.dci.DomainControllerName != null) { - dc.name = pdci.dci.DomainControllerName; - } + dc.name = pdci.dci.DomainControllerName; rc = Netapi32.INSTANCE.NetApiBufferFree(pdci.dci.getPointer()); if (LMErr.NERR_Success != rc) { throw new Win32Exception(rc); @@ -701,16 +683,10 @@ public static UserInfo getUserInfo(String accountName, String domainName) { if (rc == LMErr.NERR_Success) { USER_INFO_23 info_23 = new USER_INFO_23(bufptr.getValue()); UserInfo userInfo = new UserInfo(); - if (info_23.usri23_comment != null) { - userInfo.comment = info_23.usri23_comment; - } + userInfo.comment = info_23.usri23_comment; userInfo.flags = info_23.usri23_flags; - if (info_23.usri23_full_name != null) { - userInfo.fullName = info_23.usri23_full_name; - } - if (info_23.usri23_name != null) { - userInfo.name = info_23.usri23_name; - } + userInfo.fullName = info_23.usri23_full_name; + userInfo.name = info_23.usri23_name; if (info_23.usri23_user_sid != null) { userInfo.sidString = Advapi32Util.convertSidToStringSid(info_23.usri23_user_sid); }