Skip to content

Commit

Permalink
tests/qtest: xlnx-canfd-test: Fix code coverity issues
Browse files Browse the repository at this point in the history
Following are done to fix the coverity issues:
1. Change read_data to fix the CID 1512899: Out-of-bounds access (OVERRUN)
2. Fix match_rx_tx_data to fix CID 1512900: Logically dead code (DEADCODE)
3. Replace rand() in generate_random_data() with g_rand_int()

Signed-off-by: Vikram Garhwal <vikram.garhwal@amd.com>
Message-id: 20230628202758.16398-1-vikram.garhwal@amd.com
Reviewed-by: Peter Maydell <peter.maydell@linaro.org>
Signed-off-by: Peter Maydell <peter.maydell@linaro.org>
  • Loading branch information
Vikram Garhwal authored and pm215 committed Jul 6, 2023
1 parent 9719f12 commit b52aa86
Showing 1 changed file with 11 additions and 22 deletions.
33 changes: 11 additions & 22 deletions tests/qtest/xlnx-canfd-test.c
Original file line number Diff line number Diff line change
Expand Up @@ -170,23 +170,23 @@ static void generate_random_data(uint32_t *buf_tx, bool is_canfd_frame)
/* Generate random TX data for CANFD frame. */
if (is_canfd_frame) {
for (int i = 0; i < CANFD_FRAME_SIZE - 2; i++) {
buf_tx[2 + i] = rand();
buf_tx[2 + i] = g_random_int();
}
} else {
/* Generate random TX data for CAN frame. */
for (int i = 0; i < CAN_FRAME_SIZE - 2; i++) {
buf_tx[2 + i] = rand();
buf_tx[2 + i] = g_random_int();
}
}
}

static void read_data(QTestState *qts, uint64_t can_base_addr, uint32_t *buf_rx)
static void read_data(QTestState *qts, uint64_t can_base_addr, uint32_t *buf_rx,
uint32_t frame_size)
{
uint32_t int_status;
uint32_t fifo_status_reg_value;
/* At which RX FIFO the received data is stored. */
uint8_t store_ind = 0;
bool is_canfd_frame = false;

/* Read the interrupt on CANFD rx. */
int_status = qtest_readl(qts, can_base_addr + R_ISR_OFFSET) & ISR_RXOK;
Expand All @@ -207,16 +207,9 @@ static void read_data(QTestState *qts, uint64_t can_base_addr, uint32_t *buf_rx)
buf_rx[0] = qtest_readl(qts, can_base_addr + R_RX0_ID_OFFSET);
buf_rx[1] = qtest_readl(qts, can_base_addr + R_RX0_DLC_OFFSET);

is_canfd_frame = (buf_rx[1] >> DLC_FD_BIT_SHIFT) & 1;

if (is_canfd_frame) {
for (int i = 0; i < CANFD_FRAME_SIZE - 2; i++) {
buf_rx[i + 2] = qtest_readl(qts,
can_base_addr + R_RX0_DATA1_OFFSET + 4 * i);
}
} else {
buf_rx[2] = qtest_readl(qts, can_base_addr + R_RX0_DATA1_OFFSET);
buf_rx[3] = qtest_readl(qts, can_base_addr + R_RX0_DATA2_OFFSET);
for (int i = 0; i < frame_size - 2; i++) {
buf_rx[i + 2] = qtest_readl(qts,
can_base_addr + R_RX0_DATA1_OFFSET + 4 * i);
}

/* Clear the RX interrupt. */
Expand Down Expand Up @@ -272,10 +265,6 @@ static void match_rx_tx_data(const uint32_t *buf_tx, const uint32_t *buf_rx,
g_assert_cmpint((buf_rx[size] & DLC_FD_BIT_MASK), ==,
(buf_tx[size] & DLC_FD_BIT_MASK));
} else {
if (!is_canfd_frame && size == 4) {
break;
}

g_assert_cmpint(buf_rx[size], ==, buf_tx[size]);
}

Expand Down Expand Up @@ -318,7 +307,7 @@ static void test_can_data_transfer(void)
write_data(qts, CANFD0_BASE_ADDR, buf_tx, false);

send_data(qts, CANFD0_BASE_ADDR);
read_data(qts, CANFD1_BASE_ADDR, buf_rx);
read_data(qts, CANFD1_BASE_ADDR, buf_rx, CAN_FRAME_SIZE);
match_rx_tx_data(buf_tx, buf_rx, false);

qtest_quit(qts);
Expand Down Expand Up @@ -358,7 +347,7 @@ static void test_canfd_data_transfer(void)
write_data(qts, CANFD0_BASE_ADDR, buf_tx, true);

send_data(qts, CANFD0_BASE_ADDR);
read_data(qts, CANFD1_BASE_ADDR, buf_rx);
read_data(qts, CANFD1_BASE_ADDR, buf_rx, CANFD_FRAME_SIZE);
match_rx_tx_data(buf_tx, buf_rx, true);

qtest_quit(qts);
Expand Down Expand Up @@ -397,15 +386,15 @@ static void test_can_loopback(void)
write_data(qts, CANFD0_BASE_ADDR, buf_tx, true);

send_data(qts, CANFD0_BASE_ADDR);
read_data(qts, CANFD0_BASE_ADDR, buf_rx);
read_data(qts, CANFD0_BASE_ADDR, buf_rx, CANFD_FRAME_SIZE);
match_rx_tx_data(buf_tx, buf_rx, true);

generate_random_data(buf_tx, true);

write_data(qts, CANFD1_BASE_ADDR, buf_tx, true);

send_data(qts, CANFD1_BASE_ADDR);
read_data(qts, CANFD1_BASE_ADDR, buf_rx);
read_data(qts, CANFD1_BASE_ADDR, buf_rx, CANFD_FRAME_SIZE);
match_rx_tx_data(buf_tx, buf_rx, true);

qtest_quit(qts);
Expand Down

0 comments on commit b52aa86

Please sign in to comment.