Skip to content

Commit f1348d3

Browse files
AlessandroLuodkalowsk
authored andcommitted
drivers: i2c: ambiq: buffer overflow issue fixed
Bugfix for possible out-of-bounds operation when buffer address is not 4 bytes aligned Signed-off-by: Hao Luo <[email protected]>
1 parent 2862ed1 commit f1348d3

File tree

1 file changed

+4
-8
lines changed

1 file changed

+4
-8
lines changed

drivers/i2c/i2c_ambiq.c

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -124,11 +124,9 @@ static int i2c_ambiq_read(const struct device *dev, struct i2c_msg *hdr_msg,
124124
return -E2BIG;
125125
}
126126
#if defined(CONFIG_SOC_SERIES_APOLLO3X)
127-
trans.ui32Instr = (*(uint32_t *)hdr_msg->buf)
128-
& (0xFFFFFFFFUL >> (32 - (hdr_msg->len * 8)));
127+
memcpy(&trans.ui32Instr, hdr_msg->buf, hdr_msg->len);
129128
#else
130-
trans.ui64Instr = (*(uint64_t *)hdr_msg->buf)
131-
& (0xFFFFFFFFFFFFFFFFULL >> (64 - (hdr_msg->len * 8)));
129+
memcpy(&trans.ui64Instr, hdr_msg->buf, hdr_msg->len);
132130
#endif
133131
trans.ui32InstrLen = hdr_msg->len;
134132
}
@@ -179,11 +177,9 @@ static int i2c_ambiq_write(const struct device *dev, struct i2c_msg *hdr_msg,
179177
return -E2BIG;
180178
}
181179
#if defined(CONFIG_SOC_SERIES_APOLLO3X)
182-
trans.ui32Instr = (*(uint32_t *)hdr_msg->buf)
183-
& (0xFFFFFFFFUL >> (32 - (hdr_msg->len * 8)));
180+
memcpy(&trans.ui32Instr, hdr_msg->buf, hdr_msg->len);
184181
#else
185-
trans.ui64Instr = (*(uint64_t *)hdr_msg->buf)
186-
& (0xFFFFFFFFFFFFFFFFULL >> (64 - (hdr_msg->len * 8)));
182+
memcpy(&trans.ui64Instr, hdr_msg->buf, hdr_msg->len);
187183
#endif
188184
trans.ui32InstrLen = hdr_msg->len;
189185
}

0 commit comments

Comments
 (0)