Skip to content

Commit 3ec6d66

Browse files
authored
Validate arguments early (#911)
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
1 parent 56dafdd commit 3ec6d66

File tree

2 files changed

+21
-5
lines changed

2 files changed

+21
-5
lines changed

openssl-dynamic/src/main/c/ssl.c

+5-5
Original file line numberDiff line numberDiff line change
@@ -994,6 +994,7 @@ TCN_IMPLEMENT_CALL(jint /* status */, SSL, bioWrite)(TCN_STDARGS,
994994

995995
TCN_CHECK_NULL(bio, bioAddress, 0);
996996
TCN_CHECK_NULL(wbuf, wbufAddress, 0);
997+
TCN_CHECK_POSITIVE_OR_ZERO(wlen, wlen must be >= 0, 0);
997998

998999
return BIO_write(bio, wbuf, wlen);
9991000
}
@@ -1008,7 +1009,7 @@ TCN_IMPLEMENT_CALL(void, SSL, bioSetByteBuffer)(TCN_STDARGS,
10081009
struct TCN_bio_bytebuffer* bioUserData = NULL;
10091010
TCN_CHECK_NULL(bio, bioAddress, /* void */);
10101011
TCN_CHECK_NULL(buffer, bufferAddress, /* void */);
1011-
1012+
TCN_CHECK_POSITIVE_OR_ZERO(maxUsableBytes, maxUsableBytes must be >= 0, /* void */);
10121013
bioUserData = (struct TCN_bio_bytebuffer*) BIO_get_data(bio);
10131014
TCN_ASSERT(bioUserData != NULL);
10141015

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

10571058
TCN_CHECK_NULL(ssl_, ssl, 0);
10581059
TCN_CHECK_NULL(w, wbuf, 0);
1060+
TCN_CHECK_POSITIVE_OR_ZERO(wlen, wlen must be >= 0, 0);
10591061

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

10711073
TCN_CHECK_NULL(ssl_, ssl, 0);
10721074
TCN_CHECK_NULL(r, rbuf, 0);
1075+
TCN_CHECK_POSITIVE_OR_ZERO(rlen, rlen must be >=, 0);
10731076

10741077
return SSL_read(ssl_, r, rlen);
10751078
}
@@ -1136,10 +1139,7 @@ TCN_IMPLEMENT_CALL(jlong, SSL, bioNewByteBuffer)(TCN_STDARGS,
11361139

11371140
TCN_CHECK_NULL(ssl_, ssl, 0);
11381141

1139-
if (nonApplicationBufferSize <= 0) {
1140-
tcn_ThrowException(e, "nonApplicationBufferSize <= 0");
1141-
return 0;
1142-
}
1142+
TCN_CHECK_POSITIVE(nonApplicationBufferSize, nonApplicationBufferSize must be > 0, 0);
11431143

11441144
bio = BIO_new(BIO_java_bytebuffer());
11451145
if (bio == NULL) {

openssl-dynamic/src/main/c/tcn.h

+16
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,22 @@ jstring tcn_new_stringn(JNIEnv *, const char *, size_t);
111111
} \
112112
NETTY_JNI_UTIL_END_MACRO
113113

114+
#define TCN_CHECK_POSITIVE_OR_ZERO(V, M, R) \
115+
NETTY_JNI_UTIL_BEGIN_MACRO \
116+
if (V < 0) { \
117+
tcn_ThrowIllegalArgumentException(e, #M); \
118+
return R; \
119+
} \
120+
NETTY_JNI_UTIL_END_MACRO
121+
122+
#define TCN_CHECK_POSITIVE(V, M, R) \
123+
NETTY_JNI_UTIL_BEGIN_MACRO \
124+
if (V <= 0) { \
125+
tcn_ThrowIllegalArgumentException(e, #M); \
126+
return R; \
127+
} \
128+
NETTY_JNI_UTIL_END_MACRO
129+
114130
#define TCN_FREE_JSTRING(V) \
115131
NETTY_JNI_UTIL_BEGIN_MACRO \
116132
if (c##V) \

0 commit comments

Comments
 (0)