[hal] Throw exceptions for invalid sizes in I2C and SPI JNI (#4312)

GCC's static analyzer is correctly reporting that resize() requires an
unsigned integer, but the argument provided in the JNI function could be
negative since it's a signed byte. Throwing an exception if the argument
is negative fixes the warning.
This commit is contained in:
Tyler Veness
2022-06-15 21:20:52 -07:00
committed by GitHub
parent aebc272449
commit 666040e3e5
2 changed files with 36 additions and 0 deletions

View File

@@ -64,6 +64,16 @@ Java_edu_wpi_first_hal_I2CJNI_i2CTransactionB
(JNIEnv* env, jclass, jint port, jbyte address, jbyteArray dataToSend,
jbyte sendSize, jbyteArray dataReceived, jbyte receiveSize)
{
if (sendSize < 0) {
ThrowIllegalArgumentException(env, "I2CJNI.i2cTransactionB() sendSize < 0");
return 0;
}
if (receiveSize < 0) {
ThrowIllegalArgumentException(env,
"I2CJNI.i2cTransactionB() receiveSize < 0");
return 0;
}
wpi::SmallVector<uint8_t, 128> recvBuf;
recvBuf.resize(receiveSize);
jint returnValue =
@@ -142,6 +152,11 @@ Java_edu_wpi_first_hal_I2CJNI_i2CReadB
(JNIEnv* env, jclass, jint port, jbyte address, jbyteArray dataReceived,
jbyte receiveSize)
{
if (receiveSize < 0) {
ThrowIllegalArgumentException(env, "I2CJNI.i2cReadB() receiveSize < 0");
return 0;
}
wpi::SmallVector<uint8_t, 128> recvBuf;
recvBuf.resize(receiveSize);
jint returnValue = HAL_ReadI2C(static_cast<HAL_I2CPort>(port), address,