ec/google/chromeec: Fix Coverity Scan error (BAD_SHIFT)

A recent Coverity scan found an issue with the way the
EC_HOST_EVENT_MASK macro was being used. It was being passed values
between 0 and 63, but since it is doing basically (1ULL << (value - 1)),
this caused a shift of -1 when `i` is 0 and also doesn't reach the 63rd
bit of the mask. This is fixed by incrementing the start and end
conditions of the loop by 1, so the event mask ranges from bits 0 to 63,
instead of -1 to 62.

Found-by: Coverity CID 1430218
Signed-off-by: Tim Wawrzynczak <twawrzynczak@chromium.org>
Change-Id: I6a7cfa64545f3d313de24407f0a91b48368f2a8a
Reviewed-on: https://review.coreboot.org/c/coreboot/+/43460
Tested-by: build bot (Jenkins) <no-reply@coreboot.org>
Reviewed-by: Furquan Shaikh <furquan@google.com>
Reviewed-by: Karthik Ramasubramanian <kramasub@google.com>
This commit is contained in:
Tim Wawrzynczak 2020-07-14 13:30:46 -06:00
parent 60c619f6a3
commit 7777e1c30b
1 changed files with 8 additions and 1 deletions

View File

@ -393,7 +393,14 @@ void google_chromeec_log_events(uint64_t mask)
return;
events = google_chromeec_get_events_b() & mask;
for (i = 0; i < sizeof(events) * 8; i++) {
/*
* This loop starts at 1 because the EC_HOST_EVENT_MASK macro subtracts
* 1 from its argument before applying the left-shift operator. This
* prevents a left-shift of -1 happening, and covers the entire 64-bit
* range of the event mask.
*/
for (i = 1; i <= sizeof(events) * 8; i++) {
if (EC_HOST_EVENT_MASK(i) & events)
elog_add_event_byte(ELOG_TYPE_EC_EVENT, i);
}