Skip to content

Commit

Permalink
Validate arguments early (#911)
Browse files Browse the repository at this point in the history
Motivation:

We should validate arguments early to match what we expect.

Modifications:

Validate and throw IllegalArgumentException if methods are called with
unexpected values

Result:

More complete validation that fail early
  • Loading branch information
normanmaurer authored Feb 10, 2025
1 parent 56dafdd commit 3ec6d66
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 5 deletions.
10 changes: 5 additions & 5 deletions openssl-dynamic/src/main/c/ssl.c
Original file line number Diff line number Diff line change
Expand Up @@ -994,6 +994,7 @@ TCN_IMPLEMENT_CALL(jint /* status */, SSL, bioWrite)(TCN_STDARGS,

TCN_CHECK_NULL(bio, bioAddress, 0);
TCN_CHECK_NULL(wbuf, wbufAddress, 0);
TCN_CHECK_POSITIVE_OR_ZERO(wlen, wlen must be >= 0, 0);

return BIO_write(bio, wbuf, wlen);
}
Expand All @@ -1008,7 +1009,7 @@ TCN_IMPLEMENT_CALL(void, SSL, bioSetByteBuffer)(TCN_STDARGS,
struct TCN_bio_bytebuffer* bioUserData = NULL;
TCN_CHECK_NULL(bio, bioAddress, /* void */);
TCN_CHECK_NULL(buffer, bufferAddress, /* void */);

TCN_CHECK_POSITIVE_OR_ZERO(maxUsableBytes, maxUsableBytes must be >= 0, /* void */);
bioUserData = (struct TCN_bio_bytebuffer*) BIO_get_data(bio);
TCN_ASSERT(bioUserData != NULL);

Expand Down Expand Up @@ -1056,6 +1057,7 @@ TCN_IMPLEMENT_CALL(jint /* status */, SSL, writeToSSL)(TCN_STDARGS,

TCN_CHECK_NULL(ssl_, ssl, 0);
TCN_CHECK_NULL(w, wbuf, 0);
TCN_CHECK_POSITIVE_OR_ZERO(wlen, wlen must be >= 0, 0);

return SSL_write(ssl_, w, wlen);
}
Expand All @@ -1070,6 +1072,7 @@ TCN_IMPLEMENT_CALL(jint /* status */, SSL, readFromSSL)(TCN_STDARGS,

TCN_CHECK_NULL(ssl_, ssl, 0);
TCN_CHECK_NULL(r, rbuf, 0);
TCN_CHECK_POSITIVE_OR_ZERO(rlen, rlen must be >=, 0);

return SSL_read(ssl_, r, rlen);
}
Expand Down Expand Up @@ -1136,10 +1139,7 @@ TCN_IMPLEMENT_CALL(jlong, SSL, bioNewByteBuffer)(TCN_STDARGS,

TCN_CHECK_NULL(ssl_, ssl, 0);

if (nonApplicationBufferSize <= 0) {
tcn_ThrowException(e, "nonApplicationBufferSize <= 0");
return 0;
}
TCN_CHECK_POSITIVE(nonApplicationBufferSize, nonApplicationBufferSize must be > 0, 0);

bio = BIO_new(BIO_java_bytebuffer());
if (bio == NULL) {
Expand Down
16 changes: 16 additions & 0 deletions openssl-dynamic/src/main/c/tcn.h
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,22 @@ jstring tcn_new_stringn(JNIEnv *, const char *, size_t);
} \
NETTY_JNI_UTIL_END_MACRO

#define TCN_CHECK_POSITIVE_OR_ZERO(V, M, R) \
NETTY_JNI_UTIL_BEGIN_MACRO \
if (V < 0) { \
tcn_ThrowIllegalArgumentException(e, #M); \
return R; \
} \
NETTY_JNI_UTIL_END_MACRO

#define TCN_CHECK_POSITIVE(V, M, R) \
NETTY_JNI_UTIL_BEGIN_MACRO \
if (V <= 0) { \
tcn_ThrowIllegalArgumentException(e, #M); \
return R; \
} \
NETTY_JNI_UTIL_END_MACRO

#define TCN_FREE_JSTRING(V) \
NETTY_JNI_UTIL_BEGIN_MACRO \
if (c##V) \
Expand Down

0 comments on commit 3ec6d66

Please sign in to comment.