Skip to content

Fix possible typecasting issues, typecast cleanup in native functions#98

Merged
JacobBarthelmeh merged 1 commit intowolfSSL:masterfrom
cconlon:typeCasting
Feb 15, 2022
Merged

Fix possible typecasting issues, typecast cleanup in native functions#98
JacobBarthelmeh merged 1 commit intowolfSSL:masterfrom
cconlon:typeCasting

Conversation

@cconlon
Copy link
Copy Markdown
Member

@cconlon cconlon commented Feb 11, 2022

This PR fixes a few typecasting issues, cleans up native argument checking, and handles initial type casting of wolfSSL structure pointers up front in native functions before use or doing argument checking.

Most of this PR is cleanup as described above. Doing the structure pointer casting once on function enter will make it less likely casting issues will come up in the future. In some cases this also reduces the number of casts done in a single function.

This PR does fix runtime issues that can be seen on some platforms when the representation of a pointer stored in a jlong happens to be negative, but we check the value of it against "<= 0" before type casting it back to a structure pointer type.

For example, below is one bug that existed prior to this PR. sslPtr was being passed in from Java (wrapping a native pointer). Since jlong is a signed type, and we checked sslPtr <= 0 before casting back to a pointer, this could erroneously return SSL_FATAL_ERROR:

 JNIEXPORT jint JNICALL Java_com_wolfssl_WolfSSLSession_connect
   (JNIEnv* jenv, jobject jcl, jlong sslPtr)
 {
     ...
     WOLFSSL* ssl = NULL;
     ...

     if (jenv == NULL || sslPtr <= 0) {
         return SSL_FATAL_ERROR;
     }
     ssl = (WOLFSSL*)(uintptr_t)sslPtr;

This PR reworks this to move the type cast before the argument check:

 JNIEXPORT jint JNICALL Java_com_wolfssl_WolfSSLSession_connect
   (JNIEnv* jenv, jobject jcl, jlong sslPtr)
 {
     ...
     WOLFSSL* ssl = (WOLFSSL*)(uintptr_t)sslPtr;
     ...

     if (jenv == NULL || sslPtr == NULL) {
         return SSL_FATAL_ERROR;
     }

@JacobBarthelmeh JacobBarthelmeh merged commit 6a9a625 into wolfSSL:master Feb 15, 2022
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

Successfully merging this pull request may close these issues.

2 participants