Skip to content

Commit b8bdacb

Browse files
Merge pull request #1092 from matthiasblaesing/github-1091
Check target number to be greater than zero, before calling `Structure#toArray`
2 parents 869fa9a + 1bfce08 commit b8bdacb

File tree

2 files changed

+86
-86
lines changed

2 files changed

+86
-86
lines changed

CHANGES.md

+1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ Features
1010

1111
Bug Fixes
1212
---------
13+
* [#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).
1314

1415
Release 5.3.1
1516
=============

contrib/platform/src/com/sun/jna/platform/win32/Netapi32Util.java

+85-86
Original file line numberDiff line numberDiff line change
@@ -211,20 +211,20 @@ public static LocalGroup[] getLocalGroups(String serverName) {
211211
if (LMErr.NERR_Success != rc || bufptr.getValue() == Pointer.NULL) {
212212
throw new Win32Exception(rc);
213213
}
214-
LMAccess.LOCALGROUP_INFO_1 group = new LMAccess.LOCALGROUP_INFO_1(bufptr.getValue());
215-
LMAccess.LOCALGROUP_INFO_1[] groups = (LOCALGROUP_INFO_1[]) group.toArray(entriesRead.getValue());
216214

217215
ArrayList<LocalGroup> result = new ArrayList<LocalGroup>();
218-
for(LOCALGROUP_INFO_1 lgpi : groups) {
219-
LocalGroup lgp = new LocalGroup();
220-
if (lgpi.lgrui1_name != null) {
221-
lgp.name = lgpi.lgrui1_name.toString();
222-
}
223-
if (lgpi.lgrui1_comment != null) {
224-
lgp.comment = lgpi.lgrui1_comment.toString();
216+
217+
if (entriesRead.getValue() > 0) {
218+
LMAccess.LOCALGROUP_INFO_1 group = new LMAccess.LOCALGROUP_INFO_1(bufptr.getValue());
219+
LMAccess.LOCALGROUP_INFO_1[] groups = (LOCALGROUP_INFO_1[]) group.toArray(entriesRead.getValue());
220+
for (LOCALGROUP_INFO_1 lgpi : groups) {
221+
LocalGroup lgp = new LocalGroup();
222+
lgp.name = lgpi.lgrui1_name;
223+
lgp.comment = lgpi.lgrui1_comment;
224+
result.add(lgp);
225225
}
226-
result.add(lgp);
227226
}
227+
228228
return result.toArray(new LocalGroup[0]);
229229
} finally {
230230
if (bufptr.getValue() != Pointer.NULL) {
@@ -260,20 +260,20 @@ public static Group[] getGlobalGroups(String serverName) {
260260
if (LMErr.NERR_Success != rc || bufptr.getValue() == Pointer.NULL) {
261261
throw new Win32Exception(rc);
262262
}
263-
LMAccess.GROUP_INFO_1 group = new LMAccess.GROUP_INFO_1(bufptr.getValue());
264-
LMAccess.GROUP_INFO_1[] groups = (LMAccess.GROUP_INFO_1[]) group.toArray(entriesRead.getValue());
265263

266264
ArrayList<LocalGroup> result = new ArrayList<LocalGroup>();
267-
for(LMAccess.GROUP_INFO_1 lgpi : groups) {
268-
LocalGroup lgp = new LocalGroup();
269-
if (lgpi.grpi1_name != null) {
270-
lgp.name = lgpi.grpi1_name.toString();
271-
}
272-
if (lgpi.grpi1_comment != null) {
273-
lgp.comment = lgpi.grpi1_comment.toString();
265+
266+
if (entriesRead.getValue() > 0) {
267+
LMAccess.GROUP_INFO_1 group = new LMAccess.GROUP_INFO_1(bufptr.getValue());
268+
LMAccess.GROUP_INFO_1[] groups = (LMAccess.GROUP_INFO_1[]) group.toArray(entriesRead.getValue());
269+
for (LMAccess.GROUP_INFO_1 lgpi : groups) {
270+
LocalGroup lgp = new LocalGroup();
271+
lgp.name = lgpi.grpi1_name;
272+
lgp.comment = lgpi.grpi1_comment;
273+
result.add(lgp);
274274
}
275-
result.add(lgp);
276275
}
276+
277277
return result.toArray(new LocalGroup[0]);
278278
} finally {
279279
if (bufptr.getValue() != Pointer.NULL) {
@@ -310,16 +310,21 @@ public static User[] getUsers(String serverName) {
310310
if (LMErr.NERR_Success != rc || bufptr.getValue() == Pointer.NULL) {
311311
throw new Win32Exception(rc);
312312
}
313-
LMAccess.USER_INFO_1 user = new LMAccess.USER_INFO_1(bufptr.getValue());
314-
LMAccess.USER_INFO_1[] users = (LMAccess.USER_INFO_1[]) user.toArray(entriesRead.getValue());
313+
315314
ArrayList<User> result = new ArrayList<User>();
316-
for(LMAccess.USER_INFO_1 lu : users) {
317-
User auser = new User();
318-
if (lu.usri1_name != null) {
319-
auser.name = lu.usri1_name.toString();
315+
316+
if (entriesRead.getValue() > 0) {
317+
LMAccess.USER_INFO_1 user = new LMAccess.USER_INFO_1(bufptr.getValue());
318+
LMAccess.USER_INFO_1[] users = (LMAccess.USER_INFO_1[]) user.toArray(entriesRead.getValue());
319+
for (LMAccess.USER_INFO_1 lu : users) {
320+
User auser = new User();
321+
if (lu.usri1_name != null) {
322+
auser.name = lu.usri1_name;
323+
}
324+
result.add(auser);
320325
}
321-
result.add(auser);
322326
}
327+
323328
return result.toArray(new User[0]);
324329
} finally {
325330
if (bufptr.getValue() != Pointer.NULL) {
@@ -365,15 +370,17 @@ public static Group[] getUserLocalGroups(String userName, String serverName) {
365370
if (rc != LMErr.NERR_Success) {
366371
throw new Win32Exception(rc);
367372
}
368-
LOCALGROUP_USERS_INFO_0 lgroup = new LOCALGROUP_USERS_INFO_0(bufptr.getValue());
369-
LOCALGROUP_USERS_INFO_0[] lgroups = (LOCALGROUP_USERS_INFO_0[]) lgroup.toArray(entriesread.getValue());
370373
ArrayList<Group> result = new ArrayList<Group>();
371-
for (LOCALGROUP_USERS_INFO_0 lgpi : lgroups) {
372-
LocalGroup lgp = new LocalGroup();
373-
if (lgpi.lgrui0_name != null) {
374-
lgp.name = lgpi.lgrui0_name.toString();
374+
if (entriesread.getValue() > 0) {
375+
LOCALGROUP_USERS_INFO_0 lgroup = new LOCALGROUP_USERS_INFO_0(bufptr.getValue());
376+
LOCALGROUP_USERS_INFO_0[] lgroups = (LOCALGROUP_USERS_INFO_0[]) lgroup.toArray(entriesread.getValue());
377+
for (LOCALGROUP_USERS_INFO_0 lgpi : lgroups) {
378+
LocalGroup lgp = new LocalGroup();
379+
if (lgpi.lgrui0_name != null) {
380+
lgp.name = lgpi.lgrui0_name;
381+
}
382+
result.add(lgp);
375383
}
376-
result.add(lgp);
377384
}
378385
return result.toArray(new Group[0]);
379386
} finally {
@@ -412,16 +419,21 @@ public static Group[] getUserGroups(String userName, String serverName) {
412419
if (rc != LMErr.NERR_Success) {
413420
throw new Win32Exception(rc);
414421
}
415-
GROUP_USERS_INFO_0 lgroup = new GROUP_USERS_INFO_0(bufptr.getValue());
416-
GROUP_USERS_INFO_0[] lgroups = (GROUP_USERS_INFO_0[]) lgroup.toArray(entriesread.getValue());
422+
417423
ArrayList<Group> result = new ArrayList<Group>();
418-
for (GROUP_USERS_INFO_0 lgpi : lgroups) {
419-
Group lgp = new Group();
420-
if (lgpi.grui0_name != null) {
421-
lgp.name = lgpi.grui0_name.toString();
424+
425+
if (entriesread.getValue() > 0) {
426+
GROUP_USERS_INFO_0 lgroup = new GROUP_USERS_INFO_0(bufptr.getValue());
427+
GROUP_USERS_INFO_0[] lgroups = (GROUP_USERS_INFO_0[]) lgroup.toArray(entriesread.getValue());
428+
for (GROUP_USERS_INFO_0 lgpi : lgroups) {
429+
Group lgp = new Group();
430+
if (lgpi.grui0_name != null) {
431+
lgp.name = lgpi.grui0_name;
432+
}
433+
result.add(lgp);
422434
}
423-
result.add(lgp);
424435
}
436+
425437
return result.toArray(new Group[0]);
426438
} finally {
427439
if (bufptr.getValue() != Pointer.NULL) {
@@ -485,24 +497,14 @@ public static DomainController getDC() {
485497
throw new Win32Exception(rc);
486498
}
487499
DomainController dc = new DomainController();
488-
if (pdci.dci.DomainControllerAddress != null) {
489-
dc.address = pdci.dci.DomainControllerAddress.toString();
490-
}
500+
dc.address = pdci.dci.DomainControllerAddress;
491501
dc.addressType = pdci.dci.DomainControllerAddressType;
492-
if (pdci.dci.ClientSiteName != null) {
493-
dc.clientSiteName = pdci.dci.ClientSiteName.toString();
494-
}
495-
if (pdci.dci.DnsForestName != null) {
496-
dc.dnsForestName = pdci.dci.DnsForestName.toString();
497-
}
502+
dc.clientSiteName = pdci.dci.ClientSiteName;
503+
dc.dnsForestName = pdci.dci.DnsForestName;
498504
dc.domainGuid = pdci.dci.DomainGuid;
499-
if (pdci.dci.DomainName != null) {
500-
dc.domainName = pdci.dci.DomainName.toString();
501-
}
505+
dc.domainName = pdci.dci.DomainName;
502506
dc.flags = pdci.dci.Flags;
503-
if (pdci.dci.DomainControllerName != null) {
504-
dc.name = pdci.dci.DomainControllerName.toString();
505-
}
507+
dc.name = pdci.dci.DomainControllerName;
506508
rc = Netapi32.INSTANCE.NetApiBufferFree(pdci.dci.getPointer());
507509
if (LMErr.NERR_Success != rc) {
508510
throw new Win32Exception(rc);
@@ -635,28 +637,32 @@ public static DomainTrust[] getDomainTrusts(String serverName) {
635637
throw new Win32Exception(rc);
636638
}
637639
try {
638-
DS_DOMAIN_TRUSTS domainTrustRefs = new DS_DOMAIN_TRUSTS(domainsPointerRef.getValue());
639-
DS_DOMAIN_TRUSTS[] domainTrusts = (DS_DOMAIN_TRUSTS[]) domainTrustRefs.toArray(new DS_DOMAIN_TRUSTS[domainTrustCount.getValue()]);
640640
ArrayList<DomainTrust> trusts = new ArrayList<DomainTrust>(domainTrustCount.getValue());
641-
for(DS_DOMAIN_TRUSTS domainTrust : domainTrusts) {
642-
DomainTrust t = new DomainTrust();
643-
if (domainTrust.DnsDomainName != null) {
644-
t.DnsDomainName = domainTrust.DnsDomainName.toString();
645-
}
646-
if (domainTrust.NetbiosDomainName != null) {
647-
t.NetbiosDomainName = domainTrust.NetbiosDomainName.toString();
648-
}
649-
t.DomainSid = domainTrust.DomainSid;
650-
if (domainTrust.DomainSid != null) {
651-
t.DomainSidString = Advapi32Util.convertSidToStringSid(domainTrust.DomainSid);
652-
}
653-
t.DomainGuid = domainTrust.DomainGuid;
654-
if (domainTrust.DomainGuid != null) {
655-
t.DomainGuidString = Ole32Util.getStringFromGUID(domainTrust.DomainGuid);
641+
642+
if(domainTrustCount.getValue() > 0) {
643+
DS_DOMAIN_TRUSTS domainTrustRefs = new DS_DOMAIN_TRUSTS(domainsPointerRef.getValue());
644+
DS_DOMAIN_TRUSTS[] domainTrusts = (DS_DOMAIN_TRUSTS[]) domainTrustRefs.toArray(new DS_DOMAIN_TRUSTS[domainTrustCount.getValue()]);
645+
for (DS_DOMAIN_TRUSTS domainTrust : domainTrusts) {
646+
DomainTrust t = new DomainTrust();
647+
if (domainTrust.DnsDomainName != null) {
648+
t.DnsDomainName = domainTrust.DnsDomainName;
649+
}
650+
if (domainTrust.NetbiosDomainName != null) {
651+
t.NetbiosDomainName = domainTrust.NetbiosDomainName;
652+
}
653+
t.DomainSid = domainTrust.DomainSid;
654+
if (domainTrust.DomainSid != null) {
655+
t.DomainSidString = Advapi32Util.convertSidToStringSid(domainTrust.DomainSid);
656+
}
657+
t.DomainGuid = domainTrust.DomainGuid;
658+
if (domainTrust.DomainGuid != null) {
659+
t.DomainGuidString = Ole32Util.getStringFromGUID(domainTrust.DomainGuid);
660+
}
661+
t.flags = domainTrust.Flags;
662+
trusts.add(t);
656663
}
657-
t.flags = domainTrust.Flags;
658-
trusts.add(t);
659664
}
665+
660666
return trusts.toArray(new DomainTrust[0]);
661667
} finally {
662668
rc = Netapi32.INSTANCE.NetApiBufferFree(domainsPointerRef.getValue());
@@ -672,22 +678,15 @@ public static UserInfo getUserInfo(String accountName) {
672678

673679
public static UserInfo getUserInfo(String accountName, String domainName) {
674680
PointerByReference bufptr = new PointerByReference();
675-
int rc = -1;
676681
try {
677-
rc = Netapi32.INSTANCE.NetUserGetInfo(domainName, accountName, (short)23, bufptr);
682+
int rc = Netapi32.INSTANCE.NetUserGetInfo(domainName, accountName, (short)23, bufptr);
678683
if (rc == LMErr.NERR_Success) {
679684
USER_INFO_23 info_23 = new USER_INFO_23(bufptr.getValue());
680685
UserInfo userInfo = new UserInfo();
681-
if (info_23.usri23_comment != null) {
682-
userInfo.comment = info_23.usri23_comment.toString();
683-
}
686+
userInfo.comment = info_23.usri23_comment;
684687
userInfo.flags = info_23.usri23_flags;
685-
if (info_23.usri23_full_name != null) {
686-
userInfo.fullName = info_23.usri23_full_name.toString();
687-
}
688-
if (info_23.usri23_name != null) {
689-
userInfo.name = info_23.usri23_name.toString();
690-
}
688+
userInfo.fullName = info_23.usri23_full_name;
689+
userInfo.name = info_23.usri23_name;
691690
if (info_23.usri23_user_sid != null) {
692691
userInfo.sidString = Advapi32Util.convertSidToStringSid(info_23.usri23_user_sid);
693692
}

0 commit comments

Comments
 (0)