cluster-devel.redhat.com archive mirror
 help / color / mirror / Atom feed
* [Cluster-devel] [PATCH] tunegfs2: Add some malloc error checking
@ 2011-08-04  9:49 Andrew Price
  2011-08-04  9:56 ` Steven Whitehouse
  0 siblings, 1 reply; 2+ messages in thread
From: Andrew Price @ 2011-08-04  9:49 UTC (permalink / raw)
  To: cluster-devel.redhat.com

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



^ permalink raw reply related	[flat|nested] 2+ messages in thread

* [Cluster-devel] [PATCH] tunegfs2: Add some malloc error checking
  2011-08-04  9:49 [Cluster-devel] [PATCH] tunegfs2: Add some malloc error checking Andrew Price
@ 2011-08-04  9:56 ` Steven Whitehouse
  0 siblings, 0 replies; 2+ messages in thread
From: Steven Whitehouse @ 2011-08-04  9:56 UTC (permalink / raw)
  To: cluster-devel.redhat.com

Hi,

Looks good,

Steve.

On Thu, 2011-08-04 at 10:49 +0100, Andrew Price wrote:
> 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 */




^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2011-08-04  9:56 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2011-08-04  9:49 [Cluster-devel] [PATCH] tunegfs2: Add some malloc error checking Andrew Price
2011-08-04  9:56 ` Steven Whitehouse

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).