From 7160766ebf97aa33b2cd708146ba84701b5a6fff Mon Sep 17 00:00:00 2001 From: Richard Spiegel Date: Mon, 3 Sep 2018 08:55:51 -0700 Subject: lib/gpio.c: Fix _gpio_base3_value invalid shift Coverity CID 1395334: (BAD_SHIFT) - In function _gpio_base3_value(), if gpio_num is 32 and gpio[31] is floating, the end result is 1 << 32, which does not fit into a int. To avoid a possible error, make it an error to have num_gpio > 31. Function _gpio_base2_value also have the same issue, but the limit would be 32. As in practice it'll never be used with more than 20 GPIO, create a helper function to limit it to 31 and call it everywhere needed. BUG=b:113788440 TEST=Add a fake code to southbridge_final calling the function and printing the result. Build and boot grunt, check result. Change-Id: I0b79725bcbaf120587c7440e176643aaa7a1d5bb Signed-off-by: Richard Spiegel Reviewed-on: https://review.coreboot.org/28445 Reviewed-by: Martin Roth Tested-by: build bot (Jenkins) --- src/lib/gpio.c | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) (limited to 'src') diff --git a/src/lib/gpio.c b/src/lib/gpio.c index da748492fc..4cc039a7da 100644 --- a/src/lib/gpio.c +++ b/src/lib/gpio.c @@ -20,6 +20,14 @@ #include #include +static void _check_num(const char *name, int num) +{ + if ((num > 31) || (num < 1)) { + printk(BIOS_EMERG, "%s: %d ", name, num); + die("is an invalid number of GPIOs"); + } +} + static uint32_t _gpio_base2_value(const gpio_t gpio[], int num_gpio) { uint32_t result = 0; @@ -38,6 +46,7 @@ uint32_t gpio_base2_value(const gpio_t gpio[], int num_gpio) { int i; + _check_num(__func__, num_gpio); for (i = 0; i < num_gpio; i++) gpio_input(gpio[i]); @@ -48,6 +57,7 @@ uint32_t gpio_pulldown_base2_value(const gpio_t gpio[], int num_gpio) { int i; + _check_num(__func__, num_gpio); for (i = 0; i < num_gpio; i++) gpio_input_pulldown(gpio[i]); @@ -58,6 +68,7 @@ uint32_t gpio_pullup_base2_value(const gpio_t gpio[], int num_gpio) { int i; + _check_num(__func__, num_gpio); for (i = 0; i < num_gpio; i++) gpio_input_pullup(gpio[i]); @@ -82,8 +93,8 @@ uint32_t _gpio_base3_value(const gpio_t gpio[], int num_gpio, int binary_first) int index; int temp; char value[32]; - if ((num_gpio > 32) && (num_gpio < 1)) - die("gpio_base3_value: Invalid number of GPIOs"); + + _check_num(__func__, num_gpio); /* Enable internal pull up */ for (index = 0; index < num_gpio; ++index) -- cgit v1.2.3