mirror of
https://github.com/wpilibsuite/allwpilib
synced 2026-06-22 01:11:42 +00:00
[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:
@@ -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,
|
||||
|
||||
Reference in New Issue
Block a user