Skip to contentSkip to conversation

tunegfs2: Add some malloc error checking

PATCH New Andrew Price 2 participants 2 messages Aug 4, 2011

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 tunegfs2: Add some malloc error checking

Static analysis found a possible null pointer dereference due to a missing check in read_super. The malloc'd memory was also not being freed on error conditions. This patch adds a check for a null pointer and frees the allocated memory.

---
 gfs2/tune/super.c |    7 +++++++
 1 files changed, 7 insertions(+), 0 deletions(-)
gfs2/tune/super.c +7 -0
index 65e8d5b..49d87d6 100644
@@ -94,14 +94,21 @@ int read_super(struct tunegfs2 *tfs)
 	int n;
        	tfs->sb_start = GFS2_SB_ADDR << GFS2_BASIC_BLOCK_SHIFT;
 	block = malloc(sizeof(char) * GFS2_DEFAULT_BSIZE);
+	if (!block) {
+		perror("read_super: malloc");
+		return EX_UNAVAILABLE;
+	}
 	n = pread(tfs->fd, block, GFS2_DEFAULT_BSIZE, tfs->sb_start);
 	if (n < 0) {
 		perror("read_super: pread");
+		free(block);
 		return EX_IOERR;
 	}
 	tfs->sb = block;
 	if (be32_to_cpu(tfs->sb->sb_header.mh_magic) != GFS2_MAGIC) {
 		fprintf(stderr, _("Not a GFS/GFS2 device\n"));
+		tfs->sb = NULL;
+		free(block);
 		return EX_IOERR;
 	}
 	/* Ensure that table and proto are NULL terminated */
Signature · 1 line
1.7.6
#1 lore raw

PATCH 1/1 tunegfs2: Add some malloc error checking

Hi,

Looks good,

Steve.

Quoted 36 lines · Andrew Price

Static analysis found a possible null pointer dereference due to a missing check in read_super. The malloc'd memory was also not being freed on error conditions. This patch adds a check for a null pointer and frees the allocated memory.

---
 gfs2/tune/super.c |    7 +++++++
 1 files changed, 7 insertions(+), 0 deletions(-)
gfs2/tune/super.c +7 -0
index 65e8d5b..49d87d6 100644
@@ -94,14 +94,21 @@ int read_super(struct tunegfs2 *tfs)
 	int n;
        	tfs->sb_start = GFS2_SB_ADDR << GFS2_BASIC_BLOCK_SHIFT;
 	block = malloc(sizeof(char) * GFS2_DEFAULT_BSIZE);
+	if (!block) {
+		perror("read_super: malloc");
+		return EX_UNAVAILABLE;
+	}
 	n = pread(tfs->fd, block, GFS2_DEFAULT_BSIZE, tfs->sb_start);
 	if (n < 0) {
 		perror("read_super: pread");
+		free(block);
 		return EX_IOERR;
 	}
 	tfs->sb = block;
 	if (be32_to_cpu(tfs->sb->sb_header.mh_magic) != GFS2_MAGIC) {
 		fprintf(stderr, _("Not a GFS/GFS2 device\n"));
+		tfs->sb = NULL;
+		free(block);
 		return EX_IOERR;
 	}
 	/* Ensure that table and proto are NULL terminated */