From bf5d5093fcc37298b8359237195dc92c978a5536 Mon Sep 17 00:00:00 2001 From: Lee Leahy Date: Tue, 9 May 2017 16:37:58 -0700 Subject: drivers/storage: Fix array references Fix bug detected by coverity to handle the zero capacity case. Specific changes: * Reduce loop count by one to handle zero capacity case * Use structure instead of dual arrays * Move structures into display_capacity routine Coverity Issues: * 1374931 * 1374932 * 1374933 * 1374934 TEST=Build and run on Galileo Gen2 Change-Id: Ie5c96e78417b667438a00ee22c70894a00d13291 Signed-off-by: Lee Leahy Reviewed-on: https://review.coreboot.org/19643 Tested-by: build bot (Jenkins) Reviewed-by: Aaron Durbin --- src/drivers/storage/storage.c | 77 +++++++++++++++++-------------------------- 1 file changed, 31 insertions(+), 46 deletions(-) diff --git a/src/drivers/storage/storage.c b/src/drivers/storage/storage.c index 57477f9a03..8d7692c69b 100644 --- a/src/drivers/storage/storage.c +++ b/src/drivers/storage/storage.c @@ -30,41 +30,9 @@ #define DECIMAL_CAPACITY_MULTIPLIER 1000ULL #define HEX_CAPACITY_MULTIPLIER 1024ULL -static const char * const hex_unit_name[] = { - "TiB", "GiB", "MiB", "KiB", "B" -}; -static const char * const decimal_unit_name[] = { - "TB", "GB", "MB", "KB", "B" -}; - -static const uint64_t decimal_capacity_table[] = { - /* TB */ - DECIMAL_CAPACITY_MULTIPLIER * DECIMAL_CAPACITY_MULTIPLIER - * DECIMAL_CAPACITY_MULTIPLIER * DECIMAL_CAPACITY_MULTIPLIER, - /* GB */ - DECIMAL_CAPACITY_MULTIPLIER * DECIMAL_CAPACITY_MULTIPLIER - * DECIMAL_CAPACITY_MULTIPLIER, - /* MB */ - DECIMAL_CAPACITY_MULTIPLIER * DECIMAL_CAPACITY_MULTIPLIER, - /* KB */ - DECIMAL_CAPACITY_MULTIPLIER, - /* B */ - 1 -}; - -static const uint64_t hex_capacity_table[] = { - /* TiB */ - HEX_CAPACITY_MULTIPLIER * HEX_CAPACITY_MULTIPLIER - * HEX_CAPACITY_MULTIPLIER * HEX_CAPACITY_MULTIPLIER, - /* GiB */ - HEX_CAPACITY_MULTIPLIER * HEX_CAPACITY_MULTIPLIER - * HEX_CAPACITY_MULTIPLIER, - /* MiB */ - HEX_CAPACITY_MULTIPLIER * HEX_CAPACITY_MULTIPLIER, - /* KiB */ - HEX_CAPACITY_MULTIPLIER, - /* B */ - 1 +struct capacity { + const char * const units; + uint64_t bytes; }; static void display_capacity(struct storage_media *media, int partition_number) @@ -77,6 +45,26 @@ static void display_capacity(struct storage_media *media, int partition_number) int index; const char *name; const char *separator; + const struct capacity decimal_list[] = { + {"TB", DECIMAL_CAPACITY_MULTIPLIER * DECIMAL_CAPACITY_MULTIPLIER + * DECIMAL_CAPACITY_MULTIPLIER + * DECIMAL_CAPACITY_MULTIPLIER}, + {"GB", DECIMAL_CAPACITY_MULTIPLIER * DECIMAL_CAPACITY_MULTIPLIER + * DECIMAL_CAPACITY_MULTIPLIER}, + {"MB", DECIMAL_CAPACITY_MULTIPLIER + * DECIMAL_CAPACITY_MULTIPLIER}, + {"KB", DECIMAL_CAPACITY_MULTIPLIER}, + {"B", 1} + }; + const struct capacity hex_list[] = { + {"TiB", HEX_CAPACITY_MULTIPLIER * HEX_CAPACITY_MULTIPLIER + * HEX_CAPACITY_MULTIPLIER * HEX_CAPACITY_MULTIPLIER}, + {"GiB", HEX_CAPACITY_MULTIPLIER * HEX_CAPACITY_MULTIPLIER + * HEX_CAPACITY_MULTIPLIER}, + {"MiB", HEX_CAPACITY_MULTIPLIER * HEX_CAPACITY_MULTIPLIER}, + {"KiB", HEX_CAPACITY_MULTIPLIER}, + {"B", 1} + }; /* Get the partition name */ capacity = media->capacity[partition_number]; @@ -86,23 +74,20 @@ static void display_capacity(struct storage_media *media, int partition_number) separator = ": "; /* Determine the decimal divisor for the capacity */ - ASSERT(ARRAY_SIZE(decimal_capacity_table) - == ARRAY_SIZE(decimal_unit_name)); - for (index = 0; index < ARRAY_SIZE(decimal_capacity_table); index++) { - if (capacity >= decimal_capacity_table[index]) + for (index = 0; index < ARRAY_SIZE(decimal_list) - 1; index++) { + if (capacity >= decimal_list[index].bytes) break; } - decimal_divisor = decimal_capacity_table[index]; - decimal_units = decimal_unit_name[index]; + decimal_divisor = decimal_list[index].bytes; + decimal_units = decimal_list[index].units; /* Determine the hex divisor for the capacity */ - ASSERT(ARRAY_SIZE(hex_capacity_table) == ARRAY_SIZE(hex_unit_name)); - for (index = 0; index < ARRAY_SIZE(hex_capacity_table); index++) { - if (capacity >= hex_capacity_table[index]) + for (index = 0; index < ARRAY_SIZE(hex_list) - 1; index++) { + if (capacity >= hex_list[index].bytes) break; } - hex_divisor = hex_capacity_table[index]; - hex_units = hex_unit_name[index]; + hex_divisor = hex_list[index].bytes; + hex_units = hex_list[index].units; /* Display the capacity */ sdhc_debug("%3lld.%03lld %sytes (%3lld.%03lld %sytes)%s%s\n", -- cgit v1.2.3