diff options
author | Felix Singer <felixsinger@posteo.net> | 2022-11-26 05:06:58 +0100 |
---|---|---|
committer | Felix Singer <felixsinger@posteo.net> | 2022-12-11 13:20:41 +0000 |
commit | 9eca01d601ccb8afba483b2f5c631a482ca4737e (patch) | |
tree | fe7d89774e29009fc16fe90ad7a41ec223dad09d /libflashrom.c | |
parent | 59ce5615b768fe9d4d2506cb13c6781cd83a12e6 (diff) | |
download | flashrom-9eca01d601ccb8afba483b2f5c631a482ca4737e.tar.gz flashrom-9eca01d601ccb8afba483b2f5c631a482ca4737e.tar.bz2 flashrom-9eca01d601ccb8afba483b2f5c631a482ca4737e.zip |
libflashrom.c: Invert if conditions to improve the readability
Invert some if conditions to improve the readability of the code.
Instead of running some code if the specific condition applies, error
out early and reduce the indentation levels.
Also, while at it, move the initializers for these for-loops iterators
into their constructs. They are only used by them.
Change-Id: I4021d8802cd041dcca29a226af0798ebd9c5a81b
Signed-off-by: Felix Singer <felixsinger@posteo.net>
Reviewed-on: https://review.coreboot.org/c/flashrom/+/68153
Tested-by: build bot (Jenkins) <no-reply@coreboot.org>
Reviewed-by: Edward O'Callaghan <quasisec@chromium.org>
Reviewed-by: Angel Pons <th3fanbus@gmail.com>
Diffstat (limited to 'libflashrom.c')
-rw-r--r-- | libflashrom.c | 72 |
1 files changed, 36 insertions, 36 deletions
diff --git a/libflashrom.c b/libflashrom.c index c83c1bd8c..44f297f66 100644 --- a/libflashrom.c +++ b/libflashrom.c @@ -91,26 +91,26 @@ const char *flashrom_version_info(void) struct flashrom_flashchip_info *flashrom_supported_flash_chips(void) { - unsigned int i = 0; struct flashrom_flashchip_info *supported_flashchips = malloc(flashchips_size * sizeof(*supported_flashchips)); - if (supported_flashchips != NULL) { - for (; i < flashchips_size; ++i) { - supported_flashchips[i].vendor = flashchips[i].vendor; - supported_flashchips[i].name = flashchips[i].name; - supported_flashchips[i].tested.erase = - (enum flashrom_test_state)flashchips[i].tested.erase; - supported_flashchips[i].tested.probe = - (enum flashrom_test_state)flashchips[i].tested.probe; - supported_flashchips[i].tested.read = - (enum flashrom_test_state)flashchips[i].tested.read; - supported_flashchips[i].tested.write = - (enum flashrom_test_state)flashchips[i].tested.write; - supported_flashchips[i].total_size = flashchips[i].total_size; - } - } else { + if (!supported_flashchips) { msg_gerr("Memory allocation error!\n"); + return NULL; + } + + for (unsigned int i = 0; i < flashchips_size; ++i) { + supported_flashchips[i].vendor = flashchips[i].vendor; + supported_flashchips[i].name = flashchips[i].name; + supported_flashchips[i].tested.erase = + (enum flashrom_test_state)flashchips[i].tested.erase; + supported_flashchips[i].tested.probe = + (enum flashrom_test_state)flashchips[i].tested.probe; + supported_flashchips[i].tested.read = + (enum flashrom_test_state)flashchips[i].tested.read; + supported_flashchips[i].tested.write = + (enum flashrom_test_state)flashchips[i].tested.write; + supported_flashchips[i].total_size = flashchips[i].total_size; } return supported_flashchips; @@ -120,7 +120,6 @@ struct flashrom_board_info *flashrom_supported_boards(void) { #if CONFIG_INTERNAL == 1 int boards_known_size = 0; - int i = 0; const struct board_info *binfo = boards_known; while ((binfo++)->vendor) @@ -132,15 +131,16 @@ struct flashrom_board_info *flashrom_supported_boards(void) struct flashrom_board_info *supported_boards = malloc(boards_known_size * sizeof(*supported_boards)); - if (supported_boards != NULL) { - for (; i < boards_known_size; ++i) { - supported_boards[i].vendor = binfo[i].vendor; - supported_boards[i].name = binfo[i].name; - supported_boards[i].working = - (enum flashrom_test_state) binfo[i].working; - } - } else { + if (!supported_boards) { msg_gerr("Memory allocation error!\n"); + return NULL; + } + + for (int i = 0; i < boards_known_size; ++i) { + supported_boards[i].vendor = binfo[i].vendor; + supported_boards[i].name = binfo[i].name; + supported_boards[i].working = + (enum flashrom_test_state) binfo[i].working; } return supported_boards; @@ -153,7 +153,6 @@ struct flashrom_chipset_info *flashrom_supported_chipsets(void) { #if CONFIG_INTERNAL == 1 int chipset_enables_size = 0; - int i = 0; const struct penable *chipset = chipset_enables; while ((chipset++)->vendor_name) @@ -165,17 +164,18 @@ struct flashrom_chipset_info *flashrom_supported_chipsets(void) struct flashrom_chipset_info *supported_chipsets = malloc(chipset_enables_size * sizeof(*supported_chipsets)); - if (supported_chipsets != NULL) { - for (; i < chipset_enables_size; ++i) { - supported_chipsets[i].vendor = chipset[i].vendor_name; - supported_chipsets[i].chipset = chipset[i].device_name; - supported_chipsets[i].vendor_id = chipset[i].vendor_id; - supported_chipsets[i].chipset_id = chipset[i].device_id; - supported_chipsets[i].status = - (enum flashrom_test_state) chipset[i].status; - } - } else { + if (!supported_chipsets) { msg_gerr("Memory allocation error!\n"); + return NULL; + } + + for (int i = 0; i < chipset_enables_size; ++i) { + supported_chipsets[i].vendor = chipset[i].vendor_name; + supported_chipsets[i].chipset = chipset[i].device_name; + supported_chipsets[i].vendor_id = chipset[i].vendor_id; + supported_chipsets[i].chipset_id = chipset[i].device_id; + supported_chipsets[i].status = + (enum flashrom_test_state) chipset[i].status; } return supported_chipsets; |