Skip to content

Commit

Permalink
Increase maximum supported fixed args on varargs calls from 3 to 255
Browse files Browse the repository at this point in the history
  • Loading branch information
andrew-nowak committed Feb 19, 2023
1 parent 54fc662 commit b05eb35
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 3 deletions.
13 changes: 13 additions & 0 deletions native/testlib.c
Original file line number Diff line number Diff line change
Expand Up @@ -908,6 +908,19 @@ addVarArgs(const char *fmt, ...) {
return sum;
}

EXPORT int32_t
addSeveralFixedArgsAndVarArgs(int a, int b, int c, int d, int n_varargs, ...) {
va_list ap;
int32_t sum = a + b + c + d;
va_start(ap, n_varargs);

for (int i = 0; i < n_varargs; i++) {
sum += va_arg(ap, int32_t);
}
va_end(ap);
return sum;
}

EXPORT void
modifyStructureVarArgs(const char* fmt, ...) {
struct _ss {
Expand Down
6 changes: 3 additions & 3 deletions src/com/sun/jna/Function.java
Original file line number Diff line number Diff line change
Expand Up @@ -76,9 +76,9 @@ public interface PostCallRead {
/** Whether to throw an exception if last error is non-zero after call. */
@java.lang.annotation.Native
public static final int THROW_LAST_ERROR = 0x40;
/** Mask for number of fixed args (1-3) for varargs calls. */
/** Mask for number of fixed args (max 255) for varargs calls. */
@java.lang.annotation.Native
public static final int USE_VARARGS = 0x180;
public static final int USE_VARARGS = 0x7F80;

static final Integer INTEGER_TRUE = Integer.valueOf(-1);
static final Integer INTEGER_FALSE = Integer.valueOf(0);
Expand Down Expand Up @@ -410,7 +410,7 @@ Object invoke(Object[] args, Class<?> returnType, boolean allowObjects) {
/* @see NativeLibrary#NativeLibrary(String,String,long,Map) implementation */
Object invoke(Object[] args, Class<?> returnType, boolean allowObjects, int fixedArgs) {
Object result = null;
int callFlags = this.callFlags | ((fixedArgs & 0x3) << 7);
int callFlags = this.callFlags | ((fixedArgs & 0xFF) << 7);
if (returnType == null || returnType==void.class || returnType==Void.class) {
Native.invokeVoid(this, this.peer, callFlags, args);
result = null;
Expand Down
17 changes: 17 additions & 0 deletions test/com/sun/jna/VarArgsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ protected List<String> getFieldOrder() {
}
}
public int addVarArgs(String fmt, Number... args);
public int addSeveralFixedArgsAndVarArgs(int a, int b, int c, int d, int nArgs, Integer... args);
public String returnStringVarArgs(String fmt, Object... args);
public void modifyStructureVarArgs(String fmt, Object... args);
public String returnStringVarArgs2(String fmt, String... args);
Expand Down Expand Up @@ -102,6 +103,22 @@ public void testStringVarArgsFull() {
lib.returnStringVarArgs2("", "Test"));
}

public void testSeveralFixedAndVarArgs() {
int fixedarg1 = 1;
int fixedarg2 = 2;
int fixedarg3 = 3;
int fixedarg4 = 4;
int vararg1 = 100;
int vararg2 = 200;

int expected = fixedarg1 + fixedarg2 + fixedarg3 + fixedarg4 + vararg1 + vararg2;
int result = lib.addSeveralFixedArgsAndVarArgs(fixedarg1, fixedarg2, fixedarg3, fixedarg4,
2, vararg1, vararg2);

assertEquals("varargs not passed correctly with multiple fixed args",
expected, result);
}

public void testAppendNullToVarargs() {
Number[] args = new Number[] { Integer.valueOf(1) };
assertEquals("No trailing NULL was appended to varargs list",
Expand Down

0 comments on commit b05eb35

Please sign in to comment.