gd32f4xx_at24: Fix uninitialized use of local variables i2c and at24

Prior to this commit, i2c and at24 may be used uninitialized on line
113, 117, 129, and 143 when gd32_at24_wr_test() is called the second
time because these two variables are not static. I don't think making
them static is the correct fix because i2c is released at the end of
this function. Fixed by removing the `if (!initialized)` condition
surrounding the initialization of i2c and at24, making them initialized
at every call to this function.

Signed-off-by: Mingjie Shen <shen497@purdue.edu>
This commit is contained in:
Mingjie Shen
2024-08-26 19:34:20 -04:00
committed by Xiang Xiao
parent 234c7d58bf
commit 51c4a5c2d6
@@ -73,16 +73,11 @@ int gd32_at24_wr_test(int minor)
{ {
struct i2c_master_s *i2c; struct i2c_master_s *i2c;
struct mtd_dev_s *at24; struct mtd_dev_s *at24;
static bool initialized = false;
int ret; int ret;
ssize_t nblocks; ssize_t nblocks;
uint8_t *read_buf; uint8_t *read_buf;
/* Have we already initialized? */ /* Get the I2C port driver */
if (!initialized)
{
/* No.. Get the I2C port driver */
finfo("Initialize TWI%d\n", AT24_BUS); finfo("Initialize TWI%d\n", AT24_BUS);
i2c = gd32_i2cbus_initialize(AT24_BUS); i2c = gd32_i2cbus_initialize(AT24_BUS);
@@ -103,11 +98,6 @@ int gd32_at24_wr_test(int minor)
return -ENODEV; return -ENODEV;
} }
/* Now we are initializeed */
initialized = true;
}
/* Write start block is START_BLOCK, number of block is 2 */ /* Write start block is START_BLOCK, number of block is 2 */
nblocks = at24->bwrite(at24, START_BLOCK, NBLOCK, write_buf); nblocks = at24->bwrite(at24, START_BLOCK, NBLOCK, write_buf);