[flashrom] [commit] r1370 - trunk

repository service svn at flashrom.org
Wed Jul 13 00:35:22 CEST 2011


Author: stefanct
Date: Wed Jul 13 00:35:21 2011
New Revision: 1370
URL: http://flashrom.org/trac/flashrom/changeset/1370

Log:
fix unchecked malloc calls and casts of malloc return values

in the long term the exit calls should be replaced by returns.
until then this is the correct way to handle failures.

the casts are not needed (in C) and we don't cast malloc return values anywhere else.

Signed-off-by: Stefan Tauner <stefan.tauner at student.tuwien.ac.at>
Acked-by: Uwe Hermann <uwe at hermann-uwe.de>

Modified:
   trunk/flashrom.c
   trunk/hwaccess.c
   trunk/pcidev.c
   trunk/serial.c

Modified: trunk/flashrom.c
==============================================================================
--- trunk/flashrom.c	Wed Jul 13 00:01:44 2011	(r1369)
+++ trunk/flashrom.c	Wed Jul 13 00:35:21 2011	(r1370)
@@ -1513,7 +1513,11 @@
 	unsigned int usable_erasefunctions = count_usable_erasers(flash);
 
 	msg_cinfo("Erasing and writing flash chip... ");
-	curcontents = (uint8_t *) malloc(size);
+	curcontents = malloc(size);
+	if (!curcontents) {
+		msg_gerr("Out of memory!\n");
+		exit(1);
+	}
 	/* Copy oldcontents to curcontents to avoid clobbering oldcontents. */
 	memcpy(curcontents, oldcontents, size);
 
@@ -1880,10 +1884,18 @@
 		goto out_nofree;
 	}
 
-	oldcontents = (uint8_t *) malloc(size);
+	oldcontents = malloc(size);
+	if (!oldcontents) {
+		msg_gerr("Out of memory!\n");
+		exit(1);
+	}
 	/* Assume worst case: All bits are 0. */
 	memset(oldcontents, 0x00, size);
-	newcontents = (uint8_t *) malloc(size);
+	newcontents = malloc(size);
+	if (!newcontents) {
+		msg_gerr("Out of memory!\n");
+		exit(1);
+	}
 	/* Assume best case: All bits should be 1. */
 	memset(newcontents, 0xff, size);
 	/* Side effect of the assumptions above: Default write action is erase

Modified: trunk/hwaccess.c
==============================================================================
--- trunk/hwaccess.c	Wed Jul 13 00:01:44 2011	(r1369)
+++ trunk/hwaccess.c	Wed Jul 13 00:35:21 2011	(r1370)
@@ -226,6 +226,10 @@
 {									\
 	struct undo_mmio_write_data *undo_mmio_write_data;		\
 	undo_mmio_write_data = malloc(sizeof(struct undo_mmio_write_data)); \
+	if (!undo_mmio_write_data) {					\
+		msg_gerr("Out of memory!\n");				\
+		exit(1);						\
+	}								\
 	undo_mmio_write_data->addr = a;					\
 	undo_mmio_write_data->type = mmio_write_type_##c;		\
 	undo_mmio_write_data->c##data = mmio_read##c(a);		\

Modified: trunk/pcidev.c
==============================================================================
--- trunk/pcidev.c	Wed Jul 13 00:01:44 2011	(r1369)
+++ trunk/pcidev.c	Wed Jul 13 00:35:21 2011	(r1370)
@@ -295,6 +295,10 @@
 {									\
 	struct undo_pci_write_data *undo_pci_write_data;		\
 	undo_pci_write_data = malloc(sizeof(struct undo_pci_write_data)); \
+	if (!undo_pci_write_data) {					\
+		msg_gerr("Out of memory!\n");				\
+		exit(1);						\
+	}								\
 	undo_pci_write_data->dev = *a;					\
 	undo_pci_write_data->reg = b;					\
 	undo_pci_write_data->type = pci_write_type_##c;			\

Modified: trunk/serial.c
==============================================================================
--- trunk/serial.c	Wed Jul 13 00:01:44 2011	(r1369)
+++ trunk/serial.c	Wed Jul 13 00:35:21 2011	(r1370)
@@ -110,6 +110,8 @@
 	    (tolower((unsigned char)dev[1]) == 'o') &&
 	    (tolower((unsigned char)dev[2]) == 'm')) {
 		dev2 = malloc(strlen(dev) + 5);
+		if (!dev2)
+			sp_die("Error: Out of memory");
 		strcpy(dev2, "\\\\.\\");
 		strcpy(dev2 + 4, dev);
 	}




More information about the flashrom mailing list