Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Problem with sqlite3_create_function_v2 #61

Closed
gwenn opened this issue Apr 10, 2016 · 3 comments
Closed

Problem with sqlite3_create_function_v2 #61

gwenn opened this issue Apr 10, 2016 · 3 comments
Milestone

Comments

@gwenn
Copy link

gwenn commented Apr 10, 2016

Hello,
I am getting the following error when calling sqlite3_create_function_v2:

21: misuse at line 134792 of [e9bb4cf40f]

https://www.sqlite.org/2016/sqlite-autoconf-3120000.tar.gz

  if( zFunctionName==0 ||
      (xSFunc && (xFinal || xStep)) || 
      (!xSFunc && (xFinal && !xStep)) ||
      (!xSFunc && (!xFinal && xStep)) ||
      (nArg<-1 || nArg>SQLITE_MAX_FUNCTION_ARG) ||
      (255<(nName = sqlite3Strlen30( zFunctionName))) ){
    return SQLITE_MISUSE_BKPT; // line 134792
  }

https://github.com/gwenn/sqlite-jna/blob/jnr/src/main/java/org/sqlite/SQLite.java#L769

@IgnoreError
int sqlite3_create_function_v2(@In Pointer pDb, @In @Encoding("UTF-8")String functionName, int nArg, int eTextRep, @In Pointer pApp, @In ScalarCallback xFunc, @In AggregateStepCallback xStep, @In AggregateFinalCallback xFinal, @In Pointer xDestroy);

https://github.com/gwenn/sqlite-jna/blob/jnr/src/main/java/org/sqlite/SQLite.java#L379

static int sqlite3_create_function_v2(Pointer pDb, String functionName, int nArg, int eTextRep, Pointer pApp, ScalarCallback xFunc, AggregateStepCallback xStep, AggregateFinalCallback xFinal, Pointer xDestroy) {
    return library.sqlite3_create_function_v2(pDb, functionName, nArg, eTextRep, pApp, xFunc, xStep, xFinal, xDestroy);
}

If I manually change the signature and force callback(s) to be null, it works:

static int sqlite3_create_function_v2(Pointer pDb, String functionName, int nArg, int eTextRep, Pointer pApp, ScalarCallback xFunc, AggregateStepCallback xStep, AggregateFinalCallback xFinal, Pointer xDestroy) {
    return library.sqlite3_create_function_v2(pDb, functionName, nArg, eTextRep, pApp, null, xStep, xFinal, xDestroy);
}
// ...
@IgnoreError
int sqlite3_create_function_v2(@In Pointer pDb, @In @Encoding("UTF-8")String functionName, int nArg, int eTextRep, @In Pointer pApp, @In Pointer xFunc, @In AggregateStepCallback xStep, @In AggregateFinalCallback xFinal, @In Pointer xDestroy);

or

static int sqlite3_create_function_v2(Pointer pDb, String functionName, int nArg, int eTextRep, Pointer pApp, ScalarCallback xFunc, AggregateStepCallback xStep, AggregateFinalCallback xFinal, Pointer xDestroy) {
    return library.sqlite3_create_function_v2(pDb, functionName, nArg, eTextRep, pApp, xFunc, null, null, xDestroy);
}
// ...
@IgnoreError
int sqlite3_create_function_v2(@In Pointer pDb, @In @Encoding("UTF-8")String functionName, int nArg, int eTextRep, @In Pointer pApp, @In ScalarCallback xFunc, @In Pointer xStep, @In Pointer xFinal, @In Pointer xDestroy);

It seems that JNR is passing a non-null function pointer to C even when the Java callback/delegate is null.
Regards.

@pepijnve
Copy link
Contributor

pepijnve commented Sep 17, 2016

@gwenn I ran into the same problem while trying to convert your sqlite-jna library to jnr. It seems that JNR instantiates a native trampoline and passes a pointer to it on the native function even if the Java callback object is null.
At https://github.com/jnr/jnr-ffi/blob/master/src/main/java/jnr/ffi/provider/jffi/NativeClosureManager.java#L95 I added

if (value == null) {
  return null;
}

at the beginning of ClosureSite#toNative and that seems to resolve the issue.

I haven't found a way to intercept this via the public API so this will have to get fixed in JNR itself. Would be good to get confirmation from one of the JNR maintainers that passing null for callback parameters is indeed not working correctly yet.

@gwenn
Copy link
Author

gwenn commented Sep 18, 2016

@pepijnve On my side, I fixed the issue with this commit gwenn/sqlite-jna@1593a96
Regards.

@headius
Copy link
Member

headius commented Sep 21, 2016

#77 has been merged, so I believe the next jnr-ffi release will work properly here.

Hoping to wrap up this and other PRs by end of week, so we can put out a 2.0.10 (or 2.1).

@headius headius added this to the 2.0.10 milestone Sep 21, 2016
@headius headius closed this as completed Sep 21, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants