[PATCH v2 3/7] staging/wilc1000: Sanitize linux_spi_read

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Removed unneeded newlines.
Moved variable definitions to the top.
Improved control flow to get rid of indents.
Replaced while with for.
Added additional error message and improved existing ones.

Signed-off-by: Janosch Frank <kernel@xxxxxxxxxxxxxxxx>
---
 drivers/staging/wilc1000/linux_wlan_spi.c | 79 +++++++++++++++----------------
 1 file changed, 39 insertions(+), 40 deletions(-)

diff --git a/drivers/staging/wilc1000/linux_wlan_spi.c b/drivers/staging/wilc1000/linux_wlan_spi.c
index 595e274..b0dd486 100644
--- a/drivers/staging/wilc1000/linux_wlan_spi.c
+++ b/drivers/staging/wilc1000/linux_wlan_spi.c
@@ -218,70 +218,69 @@ int linux_spi_write(u8 *b, u32 len)
 
 int linux_spi_read(u8 *rb, u32 rlen)
 {
-	int ret;
+	int ret, i;
+	int blk = rlen / TXRX_PHASE_SIZE;
+	int remainder = rlen % TXRX_PHASE_SIZE;
 	struct spi_message msg;
 	struct spi_transfer tr;
 
-	if (rlen > 0) {
-		int i = 0;
-
-		int blk = rlen / TXRX_PHASE_SIZE;
-		int remainder = rlen % TXRX_PHASE_SIZE;
+	if (!rlen) {
+		PRINT_ER("Zero length read.\n");
+		return 0;
+	}
 
-		if (blk) {
-			while (i < blk)	{
-				linux_spi_msg_init(&msg, &tr, TXRX_PHASE_SIZE,
-						   NULL,
-						   rb + (i * TXRX_PHASE_SIZE));
+	if (!rb) {
+		PRINT_ER("Read buffer NULL.\n");
+		return 0;
+	}
 
-				ret = spi_sync(wilc_spi_dev, &msg);
-				if (ret < 0) {
-					PRINT_ER("SPI transaction failed\n");
-				}
-				i++;
-			}
+	for (i = 0; i < blk; i++) {
+		linux_spi_msg_init(&msg, &tr, TXRX_PHASE_SIZE, NULL,
+				   rb + (i * TXRX_PHASE_SIZE));
+		ret = spi_sync(wilc_spi_dev, &msg);
+		if (ret < 0) {
+			PRINT_ER("SPI sync failed and returned %d.\n", ret);
+			return 0;
 		}
-		if (remainder) {
-			linux_spi_msg_init(&msg, &tr, remainder, NULL,
-					   rb + (blk * TXRX_PHASE_SIZE));
+	}
 
-			ret = spi_sync(wilc_spi_dev, &msg);
-			if (ret < 0) {
-				PRINT_ER("SPI transaction failed\n");
-			}
-		}
-	} else {
-		PRINT_ER("can't read data with the following length: %u\n", rlen);
-		ret = -1;
+	if (remainder) {
+		linux_spi_msg_init(&msg, &tr, remainder, NULL,
+				   rb + (blk * TXRX_PHASE_SIZE));
+		ret = spi_sync(wilc_spi_dev, &msg);
+		if (ret < 0)
+			PRINT_ER("SPI sync failed and returned %d.\n", ret);
 	}
+
 	/* change return value to match WILC interface */
 	(ret < 0) ? (ret = 0) : (ret = 1);
-
 	return ret;
 }
 
 #else
 int linux_spi_read(u8 *rb, u32 rlen)
 {
-
 	int ret;
 	struct spi_message msg;
 	struct spi_transfer tr;
 
-	if (rlen > 0) {
-		linux_spi_msg_init(&msg, &tr, rlen, NULL, rb);
+	if (!rlen) {
+		PRINT_ER("Zero length read.\n");
+		return 0;
+	}
 
-		ret = spi_sync(wilc_spi_dev, &msg);
-		if (ret < 0) {
-			PRINT_ER("SPI transaction failed\n");
-		}
-	} else {
-		PRINT_ER("can't read data with the following length: %u\n", rlen);
-		ret = -1;
+	if (!rb) {
+		PRINT_ER("Read buffer NULL.\n");
+		return 0;
 	}
+
+	linux_spi_msg_init(&msg, &tr, rlen, NULL, rb);
+	ret = spi_sync(wilc_spi_dev, &msg);
+	if (ret < 0)
+		PRINT_ER("SPI sync failed and returned %d.\n", ret);
+
 	/* change return value to match WILC interface */
 	(ret < 0) ? (ret = 0) : (ret = 1);
-
 	return ret;
 }
 
-- 
2.5.0

_______________________________________________
devel mailing list
devel@xxxxxxxxxxxxxxxxxxxxxx
http://driverdev.linuxdriverproject.org/mailman/listinfo/driverdev-devel



[Index of Archives]     [Linux Driver Backports]     [DMA Engine]     [Linux GPIO]     [Linux SPI]     [Video for Linux]     [Linux USB Devel]     [Linux Coverity]     [Linux Audio Users]     [Linux Kernel]     [Linux SCSI]     [Yosemite Backpacking]
  Powered by Linux