From 3bd47524c044168b01e54e772d23c7fa723dc0d5 Mon Sep 17 00:00:00 2001 From: Angel Pons Date: Mon, 7 Jun 2021 12:33:53 +0200 Subject: treewide: Drop most cases of `sizeof(struct ...)` Spelling out the struct type name hurts readability and introduces opportunities for bugs to happen when the pointer variable type is changed but the corresponding sizeof is (are) not. TEST=`make CONFIG_EVERYTHING=yes CONFIG_JLINK_SPI=no VERSION=none -j` with and without this patch; the flashrom executable does not change. Change-Id: Icc0b60ca6ef9f5ece6ed2a0e03600bb6ccd7dcc6 Signed-off-by: Angel Pons Reviewed-on: https://review.coreboot.org/c/flashrom/+/55266 Tested-by: build bot (Jenkins) Reviewed-by: Nico Huber Reviewed-by: Edward O'Callaghan --- bitbang_spi.c | 2 +- flashrom.c | 4 ++-- fmap.c | 2 +- hwaccess.c | 2 +- ichspi.c | 2 +- it85spi.c | 2 +- it87spi.c | 2 +- layout.c | 2 +- libflashrom.c | 2 +- lspcon_i2c_spi.c | 2 +- pcidev.c | 2 +- physmap.c | 2 +- raiden_debug_spi.c | 6 +++--- realtek_mst_i2c_spi.c | 2 +- sb600spi.c | 2 +- sfdp.c | 2 +- usb_device.c | 2 +- wbsio_spi.c | 2 +- 18 files changed, 21 insertions(+), 21 deletions(-) diff --git a/bitbang_spi.c b/bitbang_spi.c index e94191084..3d973c587 100644 --- a/bitbang_spi.c +++ b/bitbang_spi.c @@ -162,7 +162,7 @@ int register_spi_bitbang_master(const struct bitbang_spi_master *master, void *s return ERROR_FLASHROM_BUG; } - struct bitbang_spi_master_data *data = calloc(1, sizeof(struct bitbang_spi_master_data)); + struct bitbang_spi_master_data *data = calloc(1, sizeof(*data)); data->master = master; if (spi_data) data->spi_data = spi_data; diff --git a/flashrom.c b/flashrom.c index f5a39b8ba..cd2e9a7e9 100644 --- a/flashrom.c +++ b/flashrom.c @@ -756,12 +756,12 @@ int probe_flash(struct registered_master *mst, int startchip, struct flashctx *f } /* Start filling in the dynamic data. */ - flash->chip = calloc(1, sizeof(struct flashchip)); + flash->chip = calloc(1, sizeof(*flash->chip)); if (!flash->chip) { msg_gerr("Out of memory!\n"); exit(1); } - memcpy(flash->chip, chip, sizeof(struct flashchip)); + memcpy(flash->chip, chip, sizeof(*flash->chip)); flash->mst = mst; if (map_flash(flash) != 0) diff --git a/fmap.c b/fmap.c index 2f34a5ccf..b18cbf799 100644 --- a/fmap.c +++ b/fmap.c @@ -195,7 +195,7 @@ static int fmap_bsearch_rom(struct fmap **fmap_out, struct flashctx *const flash if (prepare_flash_access(flashctx, true, false, false, false)) return 1; - fmap = malloc(sizeof(struct fmap)); + fmap = malloc(sizeof(*fmap)); if (!fmap) { msg_gerr("Out of memory.\n"); goto _free_ret; diff --git a/hwaccess.c b/hwaccess.c index 48ccb3485..3a9ca5780 100644 --- a/hwaccess.c +++ b/hwaccess.c @@ -247,7 +247,7 @@ static int undo_mmio_write(void *p) #define register_undo_mmio_write(a, c) \ { \ struct undo_mmio_write_data *undo_mmio_write_data; \ - undo_mmio_write_data = malloc(sizeof(struct undo_mmio_write_data)); \ + undo_mmio_write_data = malloc(sizeof(*undo_mmio_write_data)); \ if (!undo_mmio_write_data) { \ msg_gerr("Out of memory!\n"); \ exit(1); \ diff --git a/ichspi.c b/ichspi.c index 80fcff5eb..20dac1b48 100644 --- a/ichspi.c +++ b/ichspi.c @@ -1731,7 +1731,7 @@ int ich_init_spi(void *spibar, enum ich_chipset ich_gen) ich_generation = ich_gen; ich_spibar = spibar; - memset(&desc, 0x00, sizeof(struct ich_descriptors)); + memset(&desc, 0x00, sizeof(desc)); /* Moving registers / bits */ switch (ich_gen) { diff --git a/it85spi.c b/it85spi.c index 810f062c5..9812fc722 100644 --- a/it85spi.c +++ b/it85spi.c @@ -341,7 +341,7 @@ int it85xx_spi_init(struct superio s) (unsigned int)base); #endif - data = calloc(1, sizeof(struct it85spi_data)); + data = calloc(1, sizeof(*data)); if (!data) { msg_perr("Unable to allocate space for extra SPI master data.\n"); return SPI_GENERIC_ERROR; diff --git a/it87spi.c b/it87spi.c index 92c95fa40..cbf830bd4 100644 --- a/it87spi.c +++ b/it87spi.c @@ -411,7 +411,7 @@ static uint16_t it87spi_probe(uint16_t port) free(param); exit_conf_mode_ite(port); - struct it8716f_spi_data *data = calloc(1, sizeof(struct it8716f_spi_data)); + struct it8716f_spi_data *data = calloc(1, sizeof(*data)); if (!data) { msg_perr("Unable to allocate space for extra SPI master data.\n"); return SPI_GENERIC_ERROR; diff --git a/layout.c b/layout.c index 4d1dd560f..cd6b458d9 100644 --- a/layout.c +++ b/layout.c @@ -135,7 +135,7 @@ int register_include_arg(struct layout_include_args **args, const char *arg) } } - tmp = malloc(sizeof(struct layout_include_args)); + tmp = malloc(sizeof(*tmp)); if (tmp == NULL) { msg_gerr("Could not allocate memory"); goto error; diff --git a/libflashrom.c b/libflashrom.c index 09a4e4d6a..d0296d02e 100644 --- a/libflashrom.c +++ b/libflashrom.c @@ -182,7 +182,7 @@ struct flashrom_board_info *flashrom_supported_boards(void) ++boards_known_size; struct flashrom_board_info *supported_boards = - malloc(boards_known_size * sizeof(struct flashrom_board_info)); + malloc(boards_known_size * sizeof(*supported_boards)); if (supported_boards != NULL) { for (; i < boards_known_size; ++i) { diff --git a/lspcon_i2c_spi.c b/lspcon_i2c_spi.c index 7f5e3efda..9a66b3121 100644 --- a/lspcon_i2c_spi.c +++ b/lspcon_i2c_spi.c @@ -446,7 +446,7 @@ int lspcon_i2c_spi_init(void) return ret; } - struct lspcon_i2c_spi_data *data = calloc(1, sizeof(struct lspcon_i2c_spi_data)); + struct lspcon_i2c_spi_data *data = calloc(1, sizeof(*data)); if (!data) { msg_perr("Unable to allocate space for extra SPI master data.\n"); i2c_close(fd); diff --git a/pcidev.c b/pcidev.c index 892f7b1cc..9ffe05cae 100644 --- a/pcidev.c +++ b/pcidev.c @@ -296,7 +296,7 @@ static int undo_pci_write(void *p) #define register_undo_pci_write(a, b, c) \ { \ struct undo_pci_write_data *undo_pci_write_data; \ - undo_pci_write_data = malloc(sizeof(struct undo_pci_write_data)); \ + undo_pci_write_data = malloc(sizeof(*undo_pci_write_data)); \ if (!undo_pci_write_data) { \ msg_gerr("Out of memory!\n"); \ exit(1); \ diff --git a/physmap.c b/physmap.c index 72d589911..6c3681461 100644 --- a/physmap.c +++ b/physmap.c @@ -292,7 +292,7 @@ static void *physmap_common(const char *descr, uintptr_t phys_addr, size_t len, } if (autocleanup) { - struct undo_physmap_data *d = malloc(sizeof(struct undo_physmap_data)); + struct undo_physmap_data *d = malloc(sizeof(*d)); if (d == NULL) { msg_perr("%s: Out of memory!\n", __func__); physunmap_unaligned(virt_addr, len); diff --git a/raiden_debug_spi.c b/raiden_debug_spi.c index f67bbe5ec..950b8afb1 100644 --- a/raiden_debug_spi.c +++ b/raiden_debug_spi.c @@ -1579,19 +1579,19 @@ loop_end: (request_enable == RAIDEN_DEBUG_SPI_REQ_ENABLE_EC)) usleep(50 * 1000); - struct spi_master *spi_config = calloc(1, sizeof(struct spi_master)); + struct spi_master *spi_config = calloc(1, sizeof(*spi_config)); if (!spi_config) { msg_perr("Unable to allocate space for SPI master.\n"); return SPI_GENERIC_ERROR; } - struct raiden_debug_spi_data *data = calloc(1, sizeof(struct raiden_debug_spi_data)); + struct raiden_debug_spi_data *data = calloc(1, sizeof(*data)); if (!data) { free(spi_config); msg_perr("Unable to allocate space for extra SPI master data.\n"); return SPI_GENERIC_ERROR; } - memcpy(spi_config, &spi_master_raiden_debug, sizeof(struct spi_master)); + memcpy(spi_config, &spi_master_raiden_debug, sizeof(*spi_config)); data->dev = device; data->in_ep = in_endpoint; diff --git a/realtek_mst_i2c_spi.c b/realtek_mst_i2c_spi.c index 95c017fa0..91c8aa51e 100644 --- a/realtek_mst_i2c_spi.c +++ b/realtek_mst_i2c_spi.c @@ -504,7 +504,7 @@ int realtek_mst_i2c_spi_init(void) return ret; } - struct realtek_mst_i2c_spi_data *data = calloc(1, sizeof(struct realtek_mst_i2c_spi_data)); + struct realtek_mst_i2c_spi_data *data = calloc(1, sizeof(*data)); if (!data) { msg_perr("Unable to allocate space for extra SPI master data.\n"); return SPI_GENERIC_ERROR; diff --git a/sb600spi.c b/sb600spi.c index c0579470d..c4b25e527 100644 --- a/sb600spi.c +++ b/sb600spi.c @@ -777,7 +777,7 @@ int sb600_probe_spi(struct pci_dev *dev) if (handle_imc(dev, amd_gen) != 0) return ERROR_FATAL; - struct sb600spi_data *data = calloc(1, sizeof(struct sb600spi_data)); + struct sb600spi_data *data = calloc(1, sizeof(*data)); if (!data) { msg_perr("Unable to allocate space for extra SPI master data.\n"); return SPI_GENERIC_ERROR; diff --git a/sfdp.c b/sfdp.c index 97f562079..530588fb0 100644 --- a/sfdp.c +++ b/sfdp.c @@ -300,7 +300,7 @@ int probe_spi_sfdp(struct flashctx *flash) /* Fetch all parameter headers, even if we don't use them all (yet). */ hbuf = malloc((nph + 1) * 8); - hdrs = malloc((nph + 1) * sizeof(struct sfdp_tbl_hdr)); + hdrs = malloc((nph + 1) * sizeof(*hdrs)); if (hbuf == NULL || hdrs == NULL ) { msg_gerr("Out of memory!\n"); goto cleanup_hdrs; diff --git a/usb_device.c b/usb_device.c index 5a468dd55..a4f6d123b 100644 --- a/usb_device.c +++ b/usb_device.c @@ -99,7 +99,7 @@ static int check_match(struct usb_match_value const *match_value, int value) static void add_device(struct usb_device *device, struct usb_device **devices) { - struct usb_device *copy = malloc(sizeof(struct usb_device)); + struct usb_device *copy = malloc(sizeof(*copy)); assert(copy != NULL); diff --git a/wbsio_spi.c b/wbsio_spi.c index d17d49dc1..c3814aab6 100644 --- a/wbsio_spi.c +++ b/wbsio_spi.c @@ -207,7 +207,7 @@ int wbsio_check_for_spi(void) "1024 kB!\n", __func__); max_rom_decode.spi = 1024 * 1024; - struct wbsio_spi_data * data = calloc(1, sizeof(struct wbsio_spi_data)); + struct wbsio_spi_data *data = calloc(1, sizeof(*data)); if (!data) { msg_perr("Unable to allocate space for extra SPI master data.\n"); return SPI_GENERIC_ERROR; -- cgit v1.2.3