[hal] Properly error check readCANStreamSession (#6108)

This commit is contained in:
Thad House
2023-12-29 21:57:00 -08:00
committed by GitHub
parent cc631d2a69
commit 636ef58d94
3 changed files with 19 additions and 1 deletions

View File

@@ -135,6 +135,17 @@ Java_edu_wpi_first_hal_can_CANJNI_readCANStreamSession
(JNIEnv* env, jclass, jint sessionHandle, jobjectArray messages,
jint messagesToRead)
{
if (messages == nullptr) {
ThrowNullPointerException(env, "messages cannot be null");
return 0;
}
jsize messagesArrayLen = env->GetArrayLength(messages);
if (messagesArrayLen < messagesToRead) {
messagesToRead = messagesArrayLen;
}
uint32_t handle = static_cast<uint32_t>(sessionHandle);
uint32_t messagesRead = 0;

View File

@@ -46,6 +46,7 @@ static JException canMessageNotFoundExCls;
static JException canMessageNotAllowedExCls;
static JException canNotInitializedExCls;
static JException uncleanStatusExCls;
static JException nullPointerEx;
static JClass powerDistributionVersionCls;
static JClass pwmConfigDataResultCls;
static JClass canStatusCls;
@@ -85,7 +86,8 @@ static const JExceptionInit exceptions[] = {
&canMessageNotAllowedExCls},
{"edu/wpi/first/hal/can/CANNotInitializedException",
&canNotInitializedExCls},
{"edu/wpi/first/hal/util/UncleanStatusException", &uncleanStatusExCls}};
{"edu/wpi/first/hal/util/UncleanStatusException", &uncleanStatusExCls},
{"java/lang/NullPointerException", &nullPointerEx}};
namespace hal {
@@ -212,6 +214,10 @@ void ReportCANError(JNIEnv* env, int32_t status, int message_id) {
}
}
void ThrowNullPointerException(JNIEnv* env, std::string_view msg) {
nullPointerEx.Throw(env, msg);
}
void ThrowCANStreamOverflowException(JNIEnv* env, jobjectArray messages,
jint length) {
static jmethodID constructor =

View File

@@ -51,6 +51,7 @@ inline bool CheckCANStatus(JNIEnv* env, int32_t status, int32_t message_id) {
return status == 0;
}
void ThrowNullPointerException(JNIEnv* env, std::string_view msg);
void ThrowCANStreamOverflowException(JNIEnv* env, jobjectArray messages,
jint length);
void ThrowIllegalArgumentException(JNIEnv* env, std::string_view msg);