From c4e4f78cb32aad6e76927d4a4b00ed72b636f6d7 Mon Sep 17 00:00:00 2001 From: Mingjie Shen Date: Mon, 22 May 2023 14:42:12 -0400 Subject: [PATCH] i2c: Check return value of get_i2c_port() get_i2c_port() returns null values in some cases, and omitting the check could crash the program. Signed-off-by: Mingjie Shen --- common/i2c_master.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/common/i2c_master.c b/common/i2c_master.c index fd8af40385..639ddc08a5 100644 --- a/common/i2c_master.c +++ b/common/i2c_master.c @@ -97,6 +97,7 @@ static int chip_i2c_xfer_with_notify(const int port, int ret; uint16_t addr_flags = slave_addr_flags; const struct i2c_port_t *i2c_port = get_i2c_port(port); + ASSERT(i2c_port); if (IS_ENABLED(CONFIG_I2C_XFER_BOARD_CALLBACK)) i2c_start_xfer_notify(port, slave_addr_flags); @@ -1011,7 +1012,7 @@ int i2c_set_freq(int port, enum i2c_freq freq) { int ret; - if (!(get_i2c_port(port)->flags & I2C_PORT_FLAG_DYNAMIC_SPEED)) + if (get_i2c_port(port) && !(get_i2c_port(port)->flags & I2C_PORT_FLAG_DYNAMIC_SPEED)) return EC_ERROR_INVAL; i2c_lock(port, 1);