cluster-devel.redhat.com archive mirror
 help / color / mirror / Atom feed
From: Andrew Price <anprice@redhat.com>
To: cluster-devel.redhat.com
Subject: [Cluster-devel] [PATCH] tunegfs2: Add some malloc error checking
Date: Thu,  4 Aug 2011 10:49:58 +0100	[thread overview]
Message-ID: <1312451398-3124-1-git-send-email-anprice@redhat.com> (raw)

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.

Signed-off-by: Andrew Price <anprice@redhat.com>
---
 gfs2/tune/super.c |    7 +++++++
 1 files changed, 7 insertions(+), 0 deletions(-)

diff --git a/gfs2/tune/super.c b/gfs2/tune/super.c
index 65e8d5b..49d87d6 100644
--- a/gfs2/tune/super.c
+++ b/gfs2/tune/super.c
@@ -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 */
-- 
1.7.6



             reply	other threads:[~2011-08-04  9:49 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2011-08-04  9:49 Andrew Price [this message]
2011-08-04  9:56 ` [Cluster-devel] [PATCH] tunegfs2: Add some malloc error checking Steven Whitehouse

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1312451398-3124-1-git-send-email-anprice@redhat.com \
    --to=anprice@redhat.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).