Nico Huber has submitted this change. ( https://review.coreboot.org/c/flashrom/+/55267 )
Change subject: treewide: Drop unnecessary uses of memset/memcpy ......................................................................
treewide: Drop unnecessary uses of memset/memcpy
Simply provide an initialiser or use a direct assignment instead.
Change-Id: I07385375cd8eec8a95874001b402b2c17ec09e09 Signed-off-by: Angel Pons th3fanbus@gmail.com Reviewed-on: https://review.coreboot.org/c/flashrom/+/55267 Tested-by: build bot (Jenkins) no-reply@coreboot.org Reviewed-by: Nico Huber nico.h@gmx.de --- M flashrom.c M ichspi.c M jlink_spi.c M linux_mtd.c M physmap.c M raiden_debug_spi.c M stlinkv3_spi.c M usbblaster_spi.c 8 files changed, 21 insertions(+), 49 deletions(-)
Approvals: build bot (Jenkins): Verified Nico Huber: Looks good to me, approved
diff --git a/flashrom.c b/flashrom.c index cd2e9a7..b804e2d 100644 --- a/flashrom.c +++ b/flashrom.c @@ -761,7 +761,7 @@ msg_gerr("Out of memory!\n"); exit(1); } - memcpy(flash->chip, chip, sizeof(*flash->chip)); + *flash->chip = *chip; flash->mst = mst;
if (map_flash(flash) != 0) @@ -1619,11 +1619,9 @@ static void print_sysinfo(void) { #if IS_WINDOWS - SYSTEM_INFO si; - OSVERSIONINFOEX osvi; + SYSTEM_INFO si = { 0 }; + OSVERSIONINFOEX osvi = { 0 };
- memset(&si, 0, sizeof(SYSTEM_INFO)); - memset(&osvi, 0, sizeof(OSVERSIONINFOEX)); msg_ginfo(" on Windows"); /* Tell Windows which version of the structure we want. */ osvi.dwOSVersionInfoSize = sizeof(OSVERSIONINFOEX); diff --git a/ichspi.c b/ichspi.c index 20dac1b..091e4e2 100644 --- a/ichspi.c +++ b/ichspi.c @@ -1720,7 +1720,7 @@ char *arg; int ich_spi_rw_restricted = 0; int desc_valid = 0; - struct ich_descriptors desc; + struct ich_descriptors desc = { 0 }; enum ich_spi_mode { ich_auto, ich_hwseq, @@ -1731,8 +1731,6 @@ ich_generation = ich_gen; ich_spibar = spibar;
- memset(&desc, 0x00, sizeof(desc)); - /* Moving registers / bits */ switch (ich_gen) { case CHIPSET_100_SERIES_SUNRISE_POINT: diff --git a/jlink_spi.c b/jlink_spi.c index 6342e7f..0b862cf 100644 --- a/jlink_spi.c +++ b/jlink_spi.c @@ -358,9 +358,8 @@ goto init_err; }
- uint8_t caps[JAYLINK_DEV_EXT_CAPS_SIZE]; + uint8_t caps[JAYLINK_DEV_EXT_CAPS_SIZE] = { 0 };
- memset(caps, 0, sizeof(caps)); ret = jaylink_get_caps(jaylink_devh, caps);
if (ret != JAYLINK_OK) { diff --git a/linux_mtd.c b/linux_mtd.c index a1af069..b068164 100644 --- a/linux_mtd.c +++ b/linux_mtd.c @@ -315,8 +315,7 @@ if (snprintf(sysfs_path, sizeof(sysfs_path), "%s/mtd%d/", LINUX_MTD_SYSFS_ROOT, dev_num) < 0) goto linux_mtd_setup_exit;
- char buf[4]; - memset(buf, 0, sizeof(buf)); + char buf[4] = { 0 }; if (read_sysfs_string(sysfs_path, "type", buf, sizeof(buf))) return 1;
diff --git a/physmap.c b/physmap.c index 6c36814..796860c 100644 --- a/physmap.c +++ b/physmap.c @@ -433,8 +433,7 @@
int setup_cpu_msr(int cpu) { - char msrfilename[64]; - memset(msrfilename, 0, sizeof(msrfilename)); + char msrfilename[64] = { 0 }; snprintf(msrfilename, sizeof(msrfilename), "/dev/cpu/%d/msr", cpu);
if (fd_msr != -1) { @@ -509,8 +508,7 @@
int setup_cpu_msr(int cpu) { - char msrfilename[64]; - memset(msrfilename, 0, sizeof(msrfilename)); + char msrfilename[64] = { 0 }; snprintf(msrfilename, sizeof(msrfilename), "/dev/amdmsr");
if (fd_msr != -1) { @@ -591,8 +589,7 @@
int setup_cpu_msr(int cpu) { - char msrfilename[64]; - memset(msrfilename, 0, sizeof(msrfilename)); + char msrfilename[64] = { 0 }; snprintf(msrfilename, sizeof(msrfilename), "/dev/cpu%d", cpu);
if (fd_msr != -1) { diff --git a/raiden_debug_spi.c b/raiden_debug_spi.c index 950b8af..4e177e5 100644 --- a/raiden_debug_spi.c +++ b/raiden_debug_spi.c @@ -1510,12 +1510,10 @@ found = 1; goto loop_end; } else { - unsigned char dev_serial[32]; + unsigned char dev_serial[32] = { 0 }; struct libusb_device_descriptor descriptor; int rc;
- memset(dev_serial, 0, sizeof(dev_serial)); - if (libusb_get_device_descriptor(device->device, &descriptor)) { msg_pdbg("USB: Failed to get device descriptor.\n"); goto loop_end; @@ -1591,7 +1589,7 @@ return SPI_GENERIC_ERROR; }
- memcpy(spi_config, &spi_master_raiden_debug, sizeof(*spi_config)); + *spi_config = spi_master_raiden_debug;
data->dev = device; data->in_ep = in_endpoint; diff --git a/stlinkv3_spi.c b/stlinkv3_spi.c index 2453162..8b62740 100644 --- a/stlinkv3_spi.c +++ b/stlinkv3_spi.c @@ -156,14 +156,12 @@ */ static int stlinkv3_get_clk(uint32_t *bridge_input_clk, libusb_device_handle *stlinkv3_handle) { - uint8_t command[16]; + uint8_t command[16] = { 0 }; uint8_t answer[12];
if (bridge_input_clk == NULL) return -1;
- memset(command, 0, sizeof(command)); - command[0] = STLINK_BRIDGE_COMMAND; command[1] = STLINK_BRIDGE_GET_CLOCK; command[2] = STLINK_SPI_COM; @@ -234,9 +232,7 @@ static int stlinkv3_check_version(enum fw_version_check_result *result, libusb_device_handle *stlinkv3_handle) { uint8_t answer[12]; - uint8_t command[16]; - - memset(command, 0, sizeof(command)); + uint8_t command[16] = { 0 };
command[0] = ST_GETVERSION_EXT; command[1] = 0x80; @@ -256,7 +252,7 @@
static int stlinkv3_spi_open(uint16_t reqested_freq_in_kHz, libusb_device_handle *stlinkv3_handle) { - uint8_t command[16]; + uint8_t command[16] = { 0 }; uint8_t answer[2]; uint16_t SCK_freq_in_kHz; enum spi_prescaler prescaler; @@ -284,8 +280,6 @@ } msg_pinfo("SCK frequency set to %d kHz\n", SCK_freq_in_kHz);
- memset(command, 0, sizeof(command)); - command[0] = STLINK_BRIDGE_COMMAND; command[1] = STLINK_BRIDGE_INIT_SPI; command[2] = SPI_DIRECTION_2LINES_FULLDUPLEX; @@ -305,11 +299,9 @@
static int stlinkv3_get_last_readwrite_status(uint32_t *status, libusb_device_handle *stlinkv3_handle) { - uint8_t command[16]; + uint8_t command[16] = { 0 }; uint16_t answer[4];
- memset(command, 0, sizeof(command)); - command[0] = STLINK_BRIDGE_COMMAND; command[1] = STLINK_BRIDGE_GET_RWCMD_STATUS;
@@ -325,11 +317,9 @@
static int stlinkv3_spi_set_SPI_NSS(enum spi_nss_level nss_level, libusb_device_handle *stlinkv3_handle) { - uint8_t command[16]; + uint8_t command[16] = { 0 }; uint8_t answer[2];
- memset(command, 0, sizeof(command)); - command[0] = STLINK_BRIDGE_COMMAND; command[1] = STLINK_BRIDGE_CS_SPI; command[2] = (uint8_t) (nss_level); @@ -350,7 +340,7 @@ { struct stlinkv3_spi_data *stlinkv3_data = flash->mst->spi.data; libusb_device_handle *stlinkv3_handle = stlinkv3_data->handle; - uint8_t command[16]; + uint8_t command[16] = { 0 }; int rc = 0; int actual_length = 0; uint32_t rw_status = 0; @@ -361,8 +351,6 @@ return -1; }
- memset(command, 0, sizeof(command)); - command[0] = STLINK_BRIDGE_COMMAND; command[1] = STLINK_BRIDGE_WRITE_SPI; command[2] = (uint8_t)write_cnt; @@ -452,11 +440,9 @@ static int stlinkv3_spi_shutdown(void *data) { struct stlinkv3_spi_data *stlinkv3_data = data; - uint8_t command[16]; + uint8_t command[16] = { 0 }; uint8_t answer[2];
- memset(command, 0, sizeof(command)); - command[0] = STLINK_BRIDGE_COMMAND; command[1] = STLINK_BRIDGE_CLOSE; command[2] = STLINK_SPI_COM; diff --git a/usbblaster_spi.c b/usbblaster_spi.c index 115876e..5f240df 100644 --- a/usbblaster_spi.c +++ b/usbblaster_spi.c @@ -74,9 +74,8 @@
static int send_write(unsigned int writecnt, const unsigned char *writearr, struct ftdi_context ftdic) { - uint8_t buf[BUF_SIZE]; + uint8_t buf[BUF_SIZE] = { 0 };
- memset(buf, 0, sizeof(buf)); while (writecnt) { unsigned int i; unsigned int n_write = min(writecnt, BUF_SIZE - 1); @@ -101,8 +100,7 @@ { int i; unsigned int n_read; - uint8_t buf[BUF_SIZE]; - memset(buf, 0, sizeof(buf)); + uint8_t buf[BUF_SIZE] = { 0 };
n_read = readcnt; while (n_read) { @@ -181,7 +179,7 @@ /* Returns 0 upon success, a negative number upon errors. */ int usbblaster_spi_init(void) { - uint8_t buf[BUF_SIZE + 1]; + uint8_t buf[BUF_SIZE + 1] = { 0 }; struct ftdi_context ftdic; struct usbblaster_spi_data *usbblaster_data;
@@ -209,7 +207,6 @@ return -1; }
- memset(buf, 0, sizeof(buf)); buf[sizeof(buf)-1] = BIT_LED | BIT_CS; if (ftdi_write_data(&ftdic, buf, sizeof(buf)) < 0) { msg_perr("USB-Blaster reset write failed\n");