[Stgt-devel] [PATCH] replace valloc and fix memory leak

Ming Zhang blackmagic02881
Fri May 11 18:03:11 CEST 2007


Hi All

A trivial patch to replace obsoleted valloc with memalign and also fix
some potential memory leak.

Signed-off-by Ming Zhang <blackmagic02881 at gmail.com>

diff --git a/usr/mmc.c b/usr/mmc.c
index e9cc479..f245af2 100644
--- a/usr/mmc.c
+++ b/usr/mmc.c
@@ -33,6 +33,7 @@
 #include <stdint.h>
 #include <unistd.h>
 #include <linux/fs.h>
+#include <malloc.h>
 
 #include "list.h"
 #include "util.h"
@@ -66,7 +67,7 @@ static int mmc_read_toc(int host_no, struct scsi_cmd *cmd)
 {
 	uint8_t *data;
 
-	data = valloc(pagesize);
+	data = memalign(pagesize, pagesize);
 	if (!data) {
 		cmd->len = 0;
 		sense_data_build(cmd, HARDWARE_ERROR, 0, 0);
@@ -102,7 +103,7 @@ static int mmc_read_capacity(int host_no, struct scsi_cmd *cmd)
 	uint64_t size;
 	uint32_t *data;
 
-	data = valloc(pagesize);
+	data = memalign(pagesize, pagesize);
 	if (!data) {
 		cmd->len = 0;
 		sense_data_build(cmd, HARDWARE_ERROR, 0, 0);
diff --git a/usr/osd.c b/usr/osd.c
index 46bf0a0..39de9ad 100644
--- a/usr/osd.c
+++ b/usr/osd.c
@@ -24,6 +24,7 @@
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
+#include <malloc.h>
 
 #include "list.h"
 #include "util.h"
@@ -38,7 +39,7 @@
 
 static int osd_inquiry(int host_no, struct scsi_cmd *cmd)
 {
-	uint8_t *data, *scb = cmd->scb;
+	uint8_t *data = NULL, *scb = cmd->scb;
 	int len, ret = SAM_STAT_CHECK_CONDITION;
 	unsigned char key = ILLEGAL_REQUEST, asc = 0x25;
 
@@ -46,7 +47,7 @@ static int osd_inquiry(int host_no, struct scsi_cmd *cmd)
 	if ((scb[1] & 0x3) == 0 && scb[2] != 0)
 		goto sense;
 
-	data = valloc(pagesize);
+	data = memalign(pagesize, pagesize);
 	if (!data) {
 		key = HARDWARE_ERROR;
 		asc = 0;
@@ -123,6 +124,8 @@ static int osd_inquiry(int host_no, struct scsi_cmd *cmd)
 
 	return SAM_STAT_GOOD;
 sense:
+	if (data)
+		free(data);
 	sense_data_build(cmd, key, asc, 0);
 	cmd->len = 0;
 	return SAM_STAT_CHECK_CONDITION;
diff --git a/usr/sbc.c b/usr/sbc.c
index a22d3b0..9c905d5 100644
--- a/usr/sbc.c
+++ b/usr/sbc.c
@@ -30,6 +30,7 @@
 #include <stdint.h>
 #include <unistd.h>
 #include <linux/fs.h>
+#include <malloc.h>
 
 #include "list.h"
 #include "util.h"
@@ -138,7 +139,7 @@ static int sbc_read_capacity(int host_no, struct scsi_cmd *cmd)
 		goto sense;
 	}
 
-	data = valloc(pagesize);
+	data = memalign(pagesize, pagesize);
 	if (!data) {
 		key = HARDWARE_ERROR;
 		asc = 0;
@@ -272,7 +273,7 @@ static int sbc_mode_sense(int host_no, struct scsi_cmd *cmd)
 	} else
 		goto sense;
 
-	data = valloc(pagesize);
+	data = memalign(pagesize, pagesize);
 	if (!data) {
 		key = HARDWARE_ERROR;
 		asc = 0;
@@ -332,6 +333,8 @@ static int sbc_mode_sense(int host_no, struct scsi_cmd *cmd)
 	cmd->uaddr = (unsigned long) data;
 	return ret;
 sense:
+	if (data)
+		free(data);
 	cmd->len = 0;
 	sense_data_build(cmd, key, asc, 0);
 	return SAM_STAT_CHECK_CONDITION;
diff --git a/usr/spc.c b/usr/spc.c
index b922a45..15e2519 100644
--- a/usr/spc.c
+++ b/usr/spc.c
@@ -23,6 +23,7 @@
 #include <stdlib.h>
 #include <string.h>
 #include <stdint.h>
+#include <malloc.h>
 
 #include "list.h"
 #include "util.h"
@@ -37,7 +38,7 @@
 int spc_inquiry(int host_no, struct scsi_cmd *cmd)
 {
 	int len, ret = SAM_STAT_CHECK_CONDITION;
-	uint8_t *data;
+	uint8_t *data = NULL;
 	uint8_t *scb = cmd->scb;
 	unsigned char device_type = cmd->c_target->dev_type_template.type;
 	char *product_id = cmd->c_target->dev_type_template.pid;
@@ -46,7 +47,7 @@ int spc_inquiry(int host_no, struct scsi_cmd *cmd)
 	if (((scb[1] & 0x3) == 0x3) || (!(scb[1] & 0x3) && scb[2]))
 		goto sense;
 
-	data = valloc(pagesize);
+	data = memalign(pagesize, pagesize);
 	if (!data) {
 		key = HARDWARE_ERROR;
 		asc = 0;
@@ -138,6 +139,8 @@ int spc_inquiry(int host_no, struct scsi_cmd *cmd)
 
 	return SAM_STAT_GOOD;
 sense:
+	if (data)
+		free(data);
 	cmd->len = 0;
 	sense_data_build(cmd, key, asc, 0);
 	return SAM_STAT_CHECK_CONDITION;
@@ -155,7 +158,7 @@ int spc_report_luns(int host_no, struct scsi_cmd *cmd)
 	if (alen < 16)
 		goto sense;
 
-	data = valloc(pagesize);
+	data = memalign(pagesize, pagesize);
 	if (!data) {
 		key = HARDWARE_ERROR;
 		asc = 0;





More information about the stgt mailing list