1
0
Fork 0
mirror of https://github.com/betaflight/betaflight.git synced 2025-07-14 03:50:02 +03:00

Fix bugs found by cppcheck

This commit is contained in:
Štěpán Dalecký 2021-12-28 13:42:47 +01:00
parent 8402d30c0d
commit 420fb85759
8 changed files with 16 additions and 14 deletions

View file

@ -4729,7 +4729,7 @@ STATIC_UNIT_TESTED void cliSet(const char *cmdName, char *cmdline)
static const char *getMcuTypeById(mcuTypeId_e id) static const char *getMcuTypeById(mcuTypeId_e id)
{ {
if (id < MCU_TYPE_UNKNOWN) { if (id < ARRAYLEN(mcuTypeNames)) {
return mcuTypeNames[id]; return mcuTypeNames[id];
} else { } else {
return "UNKNOWN"; return "UNKNOWN";
@ -5664,7 +5664,7 @@ static void cliDmaopt(const char *cmdName, char *cmdline)
const timerHardware_t *timer = NULL; const timerHardware_t *timer = NULL;
pch = strtok_r(NULL, " ", &saveptr); pch = strtok_r(NULL, " ", &saveptr);
if (entry) { if (entry) {
index = atoi(pch) - 1; index = pch ? (atoi(pch) - 1) : -1;
if (index < 0 || index >= entry->maxIndex || (entry->presenceMask != MASK_IGNORED && !(entry->presenceMask & BIT(index + 1)))) { if (index < 0 || index >= entry->maxIndex || (entry->presenceMask != MASK_IGNORED && !(entry->presenceMask & BIT(index + 1)))) {
cliPrintErrorLinef(cmdName, "BAD INDEX: '%s'", pch ? pch : ""); cliPrintErrorLinef(cmdName, "BAD INDEX: '%s'", pch ? pch : "");
return; return;

View file

@ -102,7 +102,7 @@ uint16_t i2cGetErrorCounter(void)
// Blocking write // Blocking write
bool i2cWrite(I2CDevice device, uint8_t addr_, uint8_t reg_, uint8_t data) bool i2cWrite(I2CDevice device, uint8_t addr_, uint8_t reg_, uint8_t data)
{ {
if (device == I2CINVALID || device > I2CDEV_COUNT) { if (device == I2CINVALID || device >= I2CDEV_COUNT) {
return false; return false;
} }
@ -128,7 +128,7 @@ bool i2cWrite(I2CDevice device, uint8_t addr_, uint8_t reg_, uint8_t data)
// Non-blocking write // Non-blocking write
bool i2cWriteBuffer(I2CDevice device, uint8_t addr_, uint8_t reg_, uint8_t len_, uint8_t *data) bool i2cWriteBuffer(I2CDevice device, uint8_t addr_, uint8_t reg_, uint8_t len_, uint8_t *data)
{ {
if (device == I2CINVALID || device > I2CDEV_COUNT) { if (device == I2CINVALID || device >= I2CDEV_COUNT) {
return false; return false;
} }
@ -157,7 +157,7 @@ bool i2cWriteBuffer(I2CDevice device, uint8_t addr_, uint8_t reg_, uint8_t len_,
// Blocking read // Blocking read
bool i2cRead(I2CDevice device, uint8_t addr_, uint8_t reg_, uint8_t len, uint8_t* buf) bool i2cRead(I2CDevice device, uint8_t addr_, uint8_t reg_, uint8_t len, uint8_t* buf)
{ {
if (device == I2CINVALID || device > I2CDEV_COUNT) { if (device == I2CINVALID || device >= I2CDEV_COUNT) {
return false; return false;
} }
@ -184,7 +184,7 @@ bool i2cRead(I2CDevice device, uint8_t addr_, uint8_t reg_, uint8_t len, uint8_t
// Non-blocking read // Non-blocking read
bool i2cReadBuffer(I2CDevice device, uint8_t addr_, uint8_t reg_, uint8_t len, uint8_t* buf) bool i2cReadBuffer(I2CDevice device, uint8_t addr_, uint8_t reg_, uint8_t len, uint8_t* buf)
{ {
if (device == I2CINVALID || device > I2CDEV_COUNT) { if (device == I2CINVALID || device >= I2CDEV_COUNT) {
return false; return false;
} }

View file

@ -165,7 +165,7 @@ static bool i2cHandleHardwareFailure(I2CDevice device)
bool i2cWriteBuffer(I2CDevice device, uint8_t addr_, uint8_t reg_, uint8_t len_, uint8_t *data) bool i2cWriteBuffer(I2CDevice device, uint8_t addr_, uint8_t reg_, uint8_t len_, uint8_t *data)
{ {
if (device == I2CINVALID || device > I2CDEV_COUNT) { if (device == I2CINVALID || device >= I2CDEV_COUNT) {
return false; return false;
} }
@ -238,7 +238,7 @@ bool i2cWrite(I2CDevice device, uint8_t addr_, uint8_t reg_, uint8_t data)
bool i2cReadBuffer(I2CDevice device, uint8_t addr_, uint8_t reg_, uint8_t len, uint8_t* buf) bool i2cReadBuffer(I2CDevice device, uint8_t addr_, uint8_t reg_, uint8_t len, uint8_t* buf)
{ {
if (device == I2CINVALID || device > I2CDEV_COUNT) { if (device == I2CINVALID || device >= I2CDEV_COUNT) {
return false; return false;
} }

View file

@ -80,7 +80,7 @@ uint32_t i2cTimeoutUserCallback(void)
void i2cInit(I2CDevice device) void i2cInit(I2CDevice device)
{ {
if (device == I2CINVALID || device > I2CDEV_COUNT) { if (device == I2CINVALID || device >= I2CDEV_COUNT) {
return; return;
} }
@ -129,7 +129,7 @@ uint16_t i2cGetErrorCounter(void)
bool i2cWrite(I2CDevice device, uint8_t addr_, uint8_t reg, uint8_t data) bool i2cWrite(I2CDevice device, uint8_t addr_, uint8_t reg, uint8_t data)
{ {
if (device == I2CINVALID || device > I2CDEV_COUNT) { if (device == I2CINVALID || device >= I2CDEV_COUNT) {
return false; return false;
} }
@ -203,7 +203,7 @@ bool i2cWrite(I2CDevice device, uint8_t addr_, uint8_t reg, uint8_t data)
bool i2cRead(I2CDevice device, uint8_t addr_, uint8_t reg, uint8_t len, uint8_t* buf) bool i2cRead(I2CDevice device, uint8_t addr_, uint8_t reg, uint8_t len, uint8_t* buf)
{ {
if (device == I2CINVALID || device > I2CDEV_COUNT) { if (device == I2CINVALID || device >= I2CDEV_COUNT) {
return false; return false;
} }

View file

@ -218,6 +218,6 @@ FAST_CODE_NOINLINE float applyFeedforwardLimit(int axis, float value, float Kp,
bool shouldApplyFeedforwardLimits(int axis) bool shouldApplyFeedforwardLimits(int axis)
{ {
return feedforwardMaxRateLimit[axis] != 0.0f && axis < FD_YAW; return axis < FD_YAW && feedforwardMaxRateLimit[axis] != 0.0f;
} }
#endif #endif

View file

@ -2294,7 +2294,7 @@ static afatfsOperationStatus_e afatfs_extendSubdirectoryContinue(afatfsFile_t *d
} }
// Seek back to the beginning of the cluster // Seek back to the beginning of the cluster
afatfs_assert(afatfs_fseekAtomic(directory, -AFATFS_SECTOR_SIZE * (afatfs.sectorsPerCluster - 1))); afatfs_assert(afatfs_fseekAtomic(directory, -AFATFS_SECTOR_SIZE * ((int32_t)afatfs.sectorsPerCluster - 1)));
opState->phase = AFATFS_EXTEND_SUBDIRECTORY_PHASE_SUCCESS; opState->phase = AFATFS_EXTEND_SUBDIRECTORY_PHASE_SUCCESS;
goto doMore; goto doMore;
break; break;

View file

@ -3681,6 +3681,7 @@ static mspResult_e mspProcessInCommand(mspDescriptor_t srcDesc, int16_t cmdMSP,
sbufReadData(src, boardName, MIN(length, MAX_BOARD_NAME_LENGTH)); sbufReadData(src, boardName, MIN(length, MAX_BOARD_NAME_LENGTH));
if (length > MAX_BOARD_NAME_LENGTH) { if (length > MAX_BOARD_NAME_LENGTH) {
sbufAdvance(src, length - MAX_BOARD_NAME_LENGTH); sbufAdvance(src, length - MAX_BOARD_NAME_LENGTH);
length = MAX_BOARD_NAME_LENGTH;
} }
boardName[length] = '\0'; boardName[length] = '\0';
length = sbufReadU8(src); length = sbufReadU8(src);
@ -3688,6 +3689,7 @@ static mspResult_e mspProcessInCommand(mspDescriptor_t srcDesc, int16_t cmdMSP,
sbufReadData(src, manufacturerId, MIN(length, MAX_MANUFACTURER_ID_LENGTH)); sbufReadData(src, manufacturerId, MIN(length, MAX_MANUFACTURER_ID_LENGTH));
if (length > MAX_MANUFACTURER_ID_LENGTH) { if (length > MAX_MANUFACTURER_ID_LENGTH) {
sbufAdvance(src, length - MAX_MANUFACTURER_ID_LENGTH); sbufAdvance(src, length - MAX_MANUFACTURER_ID_LENGTH);
length = MAX_MANUFACTURER_ID_LENGTH;
} }
manufacturerId[length] = '\0'; manufacturerId[length] = '\0';

View file

@ -493,7 +493,7 @@ static void cRleEncodeStream(sbuf_t *source, sbuf_t *dest, uint8_t maxDestLen)
c |= CRSF_RLE_CHAR_REPEATED_MASK; c |= CRSF_RLE_CHAR_REPEATED_MASK;
const uint8_t fullBatches = (runLength / CRSF_RLE_MAX_RUN_LENGTH); const uint8_t fullBatches = (runLength / CRSF_RLE_MAX_RUN_LENGTH);
const uint8_t remainder = (runLength % CRSF_RLE_MAX_RUN_LENGTH); const uint8_t remainder = (runLength % CRSF_RLE_MAX_RUN_LENGTH);
const uint8_t totalBatches = fullBatches + (remainder) ? 1 : 0; const uint8_t totalBatches = fullBatches + (remainder ? 1 : 0);
if (destRemaining >= totalBatches * CRSF_RLE_BATCH_SIZE) { if (destRemaining >= totalBatches * CRSF_RLE_BATCH_SIZE) {
for (unsigned int i = 1; i <= totalBatches; i++) { for (unsigned int i = 1; i <= totalBatches; i++) {
const uint8_t batchLength = (i < totalBatches) ? CRSF_RLE_MAX_RUN_LENGTH : remainder; const uint8_t batchLength = (i < totalBatches) ? CRSF_RLE_MAX_RUN_LENGTH : remainder;