Skip to content

Commit 83c67e1

Browse files
committed
Fix macOS sysctl size_t parameters
1 parent f89905a commit 83c67e1

File tree

2 files changed

+74
-32
lines changed

2 files changed

+74
-32
lines changed

contrib/platform/src/com/sun/jna/platform/mac/SystemB.java

+40-16
Original file line numberDiff line numberDiff line change
@@ -623,10 +623,10 @@ class Timezone extends Structure {
623623
* The sysctl() function retrieves system information and allows processes with
624624
* appropriate privileges to set system information. The information available
625625
* from sysctl() consists of integers, strings, and tables.
626-
*
626+
* <p>
627627
* The state is described using a "Management Information Base" (MIB) style
628628
* name, listed in name, which is a namelen length array of integers.
629-
*
629+
* <p>
630630
* The information is copied into the buffer specified by oldp. The size of the
631631
* buffer is given by the location specified by oldlenp before the call, and
632632
* that location gives the amount of data copied after a successful call and
@@ -635,32 +635,41 @@ class Timezone extends Structure {
635635
* as much data as fits in the buffer provided and returns with the error code
636636
* ENOMEM. If the old value is not desired, oldp and oldlenp should be set to
637637
* NULL.
638-
*
638+
* <p>
639639
* The size of the available data can be determined by calling sysctl() with the
640640
* NULL argument for oldp. The size of the available data will be returned in
641641
* the location pointed to by oldlenp. For some operations, the amount of space
642642
* may change often. For these operations, the system attempts to round up so
643643
* that the returned size is large enough for a call to return the data shortly
644644
* thereafter.
645-
*
645+
* <p>
646646
* To set a new value, newp is set to point to a buffer of length newlen from
647647
* which the requested value is to be taken. If a new value is not to be set,
648648
* newp should be set to NULL and newlen set to 0.
649649
*
650650
* @param name
651-
* MIB array of integers
651+
* a Management Information Base (MIB) array of integers
652652
* @param namelen
653-
* length of the MIB array
653+
* the length of the array in {@code name}
654654
* @param oldp
655-
* Information retrieved
655+
* A buffer to hold the information retrieved
656656
* @param oldlenp
657-
* Size of information retrieved
657+
* Size of the buffer, a pointer to a {@link size_t} value
658658
* @param newp
659-
* Information to be written
659+
* To set a new value, a buffer of information to be written. May be
660+
* null if no value is to be set.
660661
* @param newlen
661-
* Size of information to be written
662+
* Size of the information to be written. May be 0 if no value is to
663+
* be set.
662664
* @return 0 on success; sets errno on failure
663665
*/
666+
int sysctl(int[] name, int namelen, Pointer oldp, Pointer oldlenp, Pointer newp, size_t newlen);
667+
668+
/**
669+
* @deprecated Use
670+
* {@link #sysctl(int[], int, Pointer, Pointer, Pointer, com.sun.jna.platform.unix.LibCAPI.size_t)}
671+
*/
672+
@Deprecated
664673
int sysctl(int[] name, int namelen, Pointer oldp, IntByReference oldlenp, Pointer newp, int newlen);
665674

666675
/**
@@ -671,15 +680,24 @@ class Timezone extends Structure {
671680
* @param name
672681
* ASCII representation of the MIB name
673682
* @param oldp
674-
* Information retrieved
683+
* A buffer to hold the information retrieved
675684
* @param oldlenp
676-
* Size of information retrieved
685+
* Size of the buffer, a pointer to a {@link size_t} value
677686
* @param newp
678-
* Information to be written
687+
* To set a new value, a buffer of information to be written. May be
688+
* null if no value is to be set.
679689
* @param newlen
680-
* Size of information to be written
690+
* Size of the information to be written. May be 0 if no value is to
691+
* be set.
681692
* @return 0 on success; sets errno on failure
682693
*/
694+
int sysctlbyname(String name, Pointer oldp, Pointer oldlenp, Pointer newp, size_t newlen);
695+
696+
/**
697+
* @deprecated Use
698+
* {@link #sysctlbyname(String, Pointer, Pointer, Pointer, com.sun.jna.platform.unix.LibCAPI.size_t)}
699+
*/
700+
@Deprecated
683701
int sysctlbyname(String name, Pointer oldp, IntByReference oldlenp, Pointer newp, int newlen);
684702

685703
/**
@@ -705,12 +723,18 @@ class Timezone extends Structure {
705723
* ASCII representation of the name
706724
* @param mibp
707725
* Integer array containing the corresponding name vector.
708-
* @param size
726+
* @param sizep
709727
* On input, number of elements in the returned array; on output, the
710728
* number of entries copied.
711729
* @return 0 on success; sets errno on failure
712730
*/
713-
int sysctlnametomib(String name, Pointer mibp, IntByReference size);
731+
int sysctlnametomib(String name, Pointer mibp, Pointer sizep);
732+
733+
/**
734+
* @deprecated Use {@link #sysctlnametomib(String, Pointer, Pointer)}
735+
*/
736+
@Deprecated
737+
int sysctlnametomib(String name, Pointer mibp, IntByReference sizep);
714738

715739
/**
716740
* The host_processor_info function returns information about processors.

contrib/platform/test/com/sun/jna/platform/mac/SystemBTest.java

+34-16
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@
5454
import com.sun.jna.ptr.PointerByReference;
5555

5656
import junit.framework.TestCase;
57-
57+
import static com.sun.jna.platform.unix.LibCAPI.size_t;
5858
/**
5959
* Exercise the {@link SystemB} class.
6060
*/
@@ -64,33 +64,51 @@ public void testSysctl() {
6464
final String mibName = "hw.logicalcpu";
6565
final int nCpu = Runtime.getRuntime().availableProcessors();
6666

67-
IntByReference size = new IntByReference(SystemB.INT_SIZE);
68-
Pointer p = new Memory(size.getValue());
69-
int ret = SystemB.INSTANCE.sysctlbyname(mibName, p, size, null, 0);
67+
size_t size = new size_t(Integer.BYTES);
68+
Pointer p = new Memory(size.longValue());
69+
// plen would be easier with a size_tByReference
70+
Pointer plen = new Memory(Native.SIZE_T_SIZE);
71+
if (Native.SIZE_T_SIZE > 4) {
72+
plen.setLong(0, size.longValue());
73+
} else {
74+
plen.setInt(0, size.intValue());
75+
}
76+
int ret = SystemB.INSTANCE.sysctlbyname(mibName, p, plen, null, size_t.ZERO);
7077
assertEquals(ret, 0);
7178
// These values should be equal unless affinity is set, limiting nCpu
7279
assertTrue(p.getInt(0) >= nCpu);
7380

74-
size = new IntByReference();
75-
ret = SystemB.INSTANCE.sysctlnametomib(mibName, null, size);
81+
size = new size_t(Integer.BYTES);
82+
// sizep and sizepval would be easier with a size_tByReference
83+
Pointer sizep = new Memory(Native.SIZE_T_SIZE);
84+
ret = SystemB.INSTANCE.sysctlnametomib(mibName, null, sizep);
7685
assertEquals(ret, 0);
77-
// Size should be 2
78-
assertEquals(size.getValue(), 2);
86+
// Array size should be 2
87+
int sizepval = Native.SIZE_T_SIZE > 4 ? (int) sizep.getLong(0) : sizep.getInt(0);
88+
assertEquals(sizepval, 2);
7989

80-
Pointer mibp = new Memory(size.getValue() * SystemB.INT_SIZE);
81-
ret = SystemB.INSTANCE.sysctlnametomib(mibName, mibp, size);
90+
Pointer mibp = new Memory(sizepval * SystemB.INT_SIZE);
91+
ret = SystemB.INSTANCE.sysctlnametomib(mibName, mibp, sizep);
8292
assertEquals(ret, 0);
83-
// Size should be 2
84-
assertEquals(size.getValue(), 2);
93+
// Array size should still be 2
94+
sizepval = Native.SIZE_T_SIZE > 4 ? (int) sizep.getLong(0) : sizep.getInt(0);
95+
assertEquals(sizepval, 2);
8596

86-
int[] mib = mibp.getIntArray(0, size.getValue());
97+
int[] mib = mibp.getIntArray(0, sizepval);
8798
// mib should be { 6, 103(?) }
8899
assertEquals(mib.length, 2);
89100
assertEquals(mib[0], 6);
90101

91-
size = new IntByReference(SystemB.INT_SIZE);
92-
p = new Memory(size.getValue());
93-
ret = SystemB.INSTANCE.sysctl(mib, mib.length, p, size, null, 0);
102+
size = new size_t(Integer.BYTES);
103+
p = new Memory(size.longValue());
104+
// plen would be easier with a size_tByReference
105+
plen = new Memory(Native.SIZE_T_SIZE);
106+
if (Native.SIZE_T_SIZE > 4) {
107+
plen.setLong(0, size.longValue());
108+
} else {
109+
plen.setInt(0, size.intValue());
110+
}
111+
ret = SystemB.INSTANCE.sysctl(mib, mib.length, p, plen, null, size_t.ZERO);
94112
assertTrue(p.getInt(0) >= nCpu);
95113
}
96114

0 commit comments

Comments
 (0)