Author: stefanct Date: Thu Feb 19 00:28:30 2015 New Revision: 1885 URL: http://flashrom.org/trac/flashrom/changeset/1885
Log: Fix a number of problems in mstarddc_spi.c.
Coverity has brought up the following problems:
mstarddc_spi_send_command(): - CID 1270702: bad comparison of malloced pointer 'cmd'. - CID 1270701: a NULL pointer dereference possible because of above.
Simply checking the return value of malloc in a valid way fixes both problems.
mstarddc_spi_init(): - CID 1270699 and 1270700: Memory leak of i2c_device.
This patch revamps the function in various ways to fix these issues and some other irritating bits. It reduces scopes of variables where possible, pushes the code towards our coding standards and introduces a label-based resource cleanup at the end.
Signed-off-by: Stefan Tauner stefan.tauner@alumni.tuwien.ac.at Acked-by: Alexandre Boeglin alex@boeglin.org
Modified: trunk/mstarddc_spi.c
Modified: trunk/mstarddc_spi.c ============================================================================== --- trunk/mstarddc_spi.c Wed Feb 11 23:30:30 2015 (r1884) +++ trunk/mstarddc_spi.c Thu Feb 19 00:28:30 2015 (r1885) @@ -74,37 +74,37 @@ /* Returns 0 upon success, a negative number upon errors. */ int mstarddc_spi_init(void) { - char *i2c_device; - char *i2c_address; - char *noreset; + int ret = 0;
// Get device, address from command-line - i2c_device = extract_programmer_param("dev"); - if (i2c_device && strlen(i2c_device)) { - if ((i2c_address = strchr(i2c_device, ':'))) { + char *i2c_device = extract_programmer_param("dev"); + if (i2c_device != NULL && strlen(i2c_device) > 0) { + char *i2c_address = strchr(i2c_device, ':'); + if (i2c_address != NULL) { *i2c_address = '\0'; i2c_address++; } - if (!i2c_address || !strlen(i2c_address)) { + if (i2c_address == NULL || strlen(i2c_address) == 0) { msg_perr("Error: no address specified.\n" "Use flashrom -p mstarddc_spi:dev=/dev/device:address.\n"); - return -1; + ret = -1; + goto out; } - mstarddc_addr = strtol(i2c_address, NULL, 16); + mstarddc_addr = strtol(i2c_address, NULL, 16); // FIXME: error handling } else { msg_perr("Error: no device specified.\n" "Use flashrom -p mstarddc_spi:dev=/dev/device:address.\n"); - return -1; + ret = -1; + goto out; } - msg_pinfo("Info: Will try to use device %s and address 0x%02x.\n", - i2c_device, mstarddc_addr); + msg_pinfo("Info: Will try to use device %s and address 0x%02x.\n", i2c_device, mstarddc_addr);
// Get noreset=1 option from command-line - if ((noreset = extract_programmer_param("noreset")) - && noreset[0] == '1') + char *noreset = extract_programmer_param("noreset"); + if (noreset != NULL && noreset[0] == '1') mstarddc_doreset = 0; - msg_pinfo("Info: WILL %sreset the device at the end.\n", - mstarddc_doreset ? "" : "NOT "); + free(noreset); + msg_pinfo("Info: Will %sreset the device at the end.\n", mstarddc_doreset ? "" : "NOT "); // Open device if ((mstarddc_fd = open(i2c_device, O_RDWR)) < 0) { switch (errno) { @@ -119,16 +119,17 @@ i2c_device); break; default: - msg_perr("Error opening %s: errno %d.\n", - i2c_device, errno); + msg_perr("Error opening %s: %s.\n", i2c_device, strerror(errno)); } - return -1; + ret = -1; + goto out; } // Set slave address if (ioctl(mstarddc_fd, I2C_SLAVE, mstarddc_addr) < 0) { msg_perr("Error setting slave address 0x%02x: errno %d.\n", mstarddc_addr, errno); - return -1; + ret = -1; + goto out; } // Enable ISP mode uint8_t cmd[5] = { 'M', 'S', 'T', 'A', 'R' }; @@ -141,7 +142,8 @@ msg_perr("Error enabling ISP mode: errno %d & %d.\n" "Please check that device (%s) and address (0x%02x) are correct.\n", enable_err, errno, i2c_device, mstarddc_addr); - return -1; + ret = -1; + goto out; } } // Register shutdown function @@ -149,7 +151,9 @@
// Register programmer register_spi_master(&spi_master_mstarddc); - return 0; +out: + free(i2c_device); + return ret; }
/* Returns 0 upon success, a negative number upon errors. */ @@ -160,9 +164,8 @@ unsigned char *readarr) { int ret = 0; - uint8_t *cmd; - - if ((cmd = malloc((writecnt + 1) * sizeof(uint8_t))) < 0) { + uint8_t *cmd = malloc((writecnt + 1) * sizeof(uint8_t)); + if (cmd == NULL) { msg_perr("Error allocating memory: errno %d.\n", errno); ret = -1; }