Skip to content

Commit

Permalink
Merge pull request #764 from matthiasblaesing/varargs_fix
Browse files Browse the repository at this point in the history
Fix calling functions with only VarArgs (without fixed parameters)
  • Loading branch information
matthiasblaesing authored Feb 4, 2017
2 parents f893069 + 54e5faf commit 68bd600
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ Bug Fixes
---------
* [#754](https://github.com/java-native-access/jna/issues/754): Move MSVC build to standard stdbool.h and require Visual C++ 2015 (sizeof(bool) = 1 is now also true on MSVC build) - [@matthiasblaesing](https://github.com/matthiasblaesing).
* [#399](https://github.com/java-native-access/jna/issues/399): Check native version before attempting to call into native code - [@matthiasblaesing](https://github.com/matthiasblaesing).
* [#763](https://github.com/java-native-access/jna/issues/763): Fix vararg calls without fixed parts - [@matthiasblaesing](https://github.com/matthiasblaesing).

Release 4.3.0
=============
Expand Down
3 changes: 2 additions & 1 deletion src/com/sun/jna/Function.java
Original file line number Diff line number Diff line change
Expand Up @@ -327,10 +327,11 @@ Object invoke(Method invokingMethod, Class<?>[] paramTypes, Class<?> returnType,

TypeMapper mapper = (TypeMapper)options.get(Library.OPTION_TYPE_MAPPER);
boolean allowObjects = Boolean.TRUE.equals(options.get(Library.OPTION_ALLOW_OBJECTS));
boolean isVarArgs = args.length > 0 && invokingMethod != null ? isVarArgs(invokingMethod) : false;
int fixedArgs = args.length > 0 && invokingMethod != null ? fixedArgs(invokingMethod) : 0;
for (int i=0; i < args.length; i++) {
Class<?> paramType = invokingMethod != null
? (fixedArgs > 0 && i >= paramTypes.length-1
? (isVarArgs && i >= paramTypes.length-1
? paramTypes[paramTypes.length-1].getComponentType()
: paramTypes[i])
: null;
Expand Down
7 changes: 7 additions & 0 deletions test/com/sun/jna/VarArgsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ protected List<String> getFieldOrder() {
public int addVarArgs(String fmt, Number... args);
public String returnStringVarArgs(String fmt, Object... args);
public void modifyStructureVarArgs(String fmt, Object arg1, Object... args);
public String returnStringVarArgs(String... args);
}
TestLibrary lib;
@Override
Expand Down Expand Up @@ -89,6 +90,12 @@ public void testStringVarArgs() {
lib.returnStringVarArgs("", args));
}

public void testStringVarArgsFull() {
Object[] args = new Object[] { "Test" };
assertEquals("Did not return correct string", args[0],
lib.returnStringVarArgs("", "Test"));
}

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 68bd600

Please sign in to comment.