Skip to contentSkip to conversation

spi: atmel: Fix test unsigned var < 0

PATCH In review Axel Lin 3 participants 5 messages Mar 22, 2014 – Mar 27, 2014 2 messages not in the archive

Subsystems (3)
lore
Share & tools
lore

Axel Lin

author
#0 lore raw

PATCH 1/1 spi: atmel: Fix test unsigned var < 0

current_remaining_bytes is an unsigned long and cannot be below 0.

---
 drivers/spi/spi-atmel.c | 8 +++-----
 1 file changed, 3 insertions(+), 5 deletions(-)
drivers/spi/spi-atmel.c +3 -5
index bda961e..3e8c4cc 100644
@@ -873,13 +873,11 @@ atmel_spi_pump_pio_data(struct atmel_spi *as, struct spi_transfer *xfer)
 	} else {
 		spi_readl(as, RDR);
 	}
-	if (xfer->bits_per_word > 8) {
+
+	if (xfer->bits_per_word > 8)
 		as->current_remaining_bytes -= 2;
-		if (as->current_remaining_bytes < 0)
-			as->current_remaining_bytes = 0;
-	} else {
+	else
 		as->current_remaining_bytes--;
-	}
 }
 
 /* Interrupt
Signature · 8 lines
1.8.3.2



--
To unsubscribe from this list: send the line "unsubscribe linux-spi" in
the body of a message to [email protected]
More majordomo info at  http://vger.kernel.org/majordomo-info.html
#1 lore raw

drivers/spi/spi-atmel.c atmel_spi_pump_pio_data(struct atmel_spi *as, struct spi_transfer *xfer) · line 882

Quoted 12 lines · Axel Lin

current_remaining_bytes is an unsigned long and cannot be below 0.

drivers/spi/spi-atmel.c +3 -5
-	if (xfer->bits_per_word > 8) {
+
+	if (xfer->bits_per_word > 8)
 		as->current_remaining_bytes -= 2;
-		if (as->current_remaining_bytes < 0)
-			as->current_remaining_bytes = 0;
-	} else {
+	else
 		as->current_remaining_bytes--;
-	}

This removes an error check in the case that we set the remaining bytes to -1. The length validation the core does should ensure that never happens but it seems wrong to just ignore that - we should at least note in the changelog that the analysis has been done.

Are you sure that the best fix isn't to just use an int here?

A message that is not in the archive (sent off-list or not yet received): [email protected]

author
#3 lore raw

2014-03-27 0:51 GMT+08:00 Mark Brown <[email protected]>:

Quoted 14 lines · Axel Lin

drivers/spi/spi-atmel.c atmel_spi_pump_pio_data(struct atmel_spi *as, struct spi_transfer *xfer) · line 882

Quoted 12 lines

current_remaining_bytes is an unsigned long and cannot be below 0.

drivers/spi/spi-atmel.c +3 -5
-     if (xfer->bits_per_word > 8) {
+
+     if (xfer->bits_per_word > 8)
              as->current_remaining_bytes -= 2;
-             if (as->current_remaining_bytes < 0)
-                     as->current_remaining_bytes = 0;
-     } else {
+     else
              as->current_remaining_bytes--;
-     }
Quoted 4 lines

This removes an error check in the case that we set the remaining bytes to -1. The length validation the core does should ensure that never happens but it seems wrong to just ignore that - we should at least note in the changelog that the analysis has been done.

I thought it never happen and there is no bug report for this so it's safe with this patch.

Quoted 2 lines

Are you sure that the best fix isn't to just use an int here?

Wenyou,
Any comments for this?

Regards,
Axel
--
To unsubscribe from this list: send the line "unsubscribe linux-spi" in the body of a message to [email protected] More majordomo info at http://vger.kernel.org/majordomo-info.html

A message that is not in the archive (sent off-list or not yet received): CAFRkauBh+4-NmneTh=io7grG4MpSZ98riybAOOpEff+KiPZUjg-JsoAwUIsXosN+BqQ9rBEUg@public.gmane.org

#5 lore raw
Quoted 27 lines

-----Original Message-----
From: Axel Lin [mailto:[email protected]]
Sent: Thursday, March 27, 2014 8:46 AM
To: Mark Brown
Cc: Yang, Wenyou; linux-spi
Subject: Re: [PATCH] spi: atmel: Fix test unsigned var < 0

2014-03-27 0:51 GMT+08:00 Mark Brown <[email protected]>:

Quoted 14 lines · Axel Lin

Review

Quoted 12 lines

current_remaining_bytes is an unsigned long and cannot be below 0.

(quoted diff) +3 -5
-     if (xfer->bits_per_word > 8) {
+
+     if (xfer->bits_per_word > 8)
              as->current_remaining_bytes -= 2;
-             if (as->current_remaining_bytes < 0)
-                     as->current_remaining_bytes = 0;
-     } else {
+     else
              as->current_remaining_bytes--;
-     }
Quoted 4 lines

This removes an error check in the case that we set the remaining bytes to -1. The length validation the core does should ensure that never happens but it seems wrong to just ignore that - we should at least note in the changelog that the analysis has been done.

Quoted 7 lines

I thought it never happen and there is no bug report for this so it's safe with this patch.

Quoted 2 lines

Are you sure that the best fix isn't to just use an int here?

Wenyou,
Any comments for this?

Like Mark said, It seems it is more reasonable using a signed int, instead of unsigned int.

-->8 ---

drivers/spi/spi-atmel.c +3 -1
index 8005f98..485e6cc 100644
@@ -224,7 +224,7 @@ struct atmel_spi {
 	struct platform_device	*pdev;
 
 	struct spi_transfer	*current_transfer;
-	unsigned long		current_remaining_bytes;
+	int			current_remaining_bytes;
 	int			done_status;
 
 	struct completion	xfer_completion;
@@ -1110,6 +1110,8 @@ static int atmel_spi_one_transfer(struct spi_master *master,
 				atmel_spi_next_xfer_pio(master, xfer);
 			} else {
 				as->current_remaining_bytes -= len;
+				if (as->current_remaining_bytes < 0)
+					as->current_remaining_bytes = 0;
 			}
 		} else {
 			atmel_spi_next_xfer_pio(master, xfer);

--<8 ---

Best Regards,
Wenyou Yang
--
To unsubscribe from this list: send the line "unsubscribe linux-spi" in the body of a message to [email protected] More majordomo info at http://vger.kernel.org/majordomo-info.html

author
#6 lore raw

2014-03-27 9:13 GMT+08:00 Yang, Wenyou <[email protected]>:

Quoted 31 lines
Quoted 31 lines

-----Original Message-----
From: Axel Lin [mailto:[email protected]]
Sent: Thursday, March 27, 2014 8:46 AM
To: Mark Brown
Cc: Yang, Wenyou; linux-spi
Subject: Re: [PATCH] spi: atmel: Fix test unsigned var < 0

2014-03-27 0:51 GMT+08:00 Mark Brown <[email protected]>:

Quoted 14 lines · Axel Lin

Review

Quoted 12 lines

current_remaining_bytes is an unsigned long and cannot be below 0.

(quoted diff) +3 -5
-     if (xfer->bits_per_word > 8) {
+
+     if (xfer->bits_per_word > 8)
              as->current_remaining_bytes -= 2;
-             if (as->current_remaining_bytes < 0)
-                     as->current_remaining_bytes = 0;
-     } else {
+     else
              as->current_remaining_bytes--;
-     }
Quoted 4 lines

This removes an error check in the case that we set the remaining bytes to -1. The length validation the core does should ensure that never happens but it seems wrong to just ignore that - we should at least note in the changelog that the analysis has been done.

I thought it never happen and there is no bug report for this so it's safe with this patch.

Quoted 1 line
Quoted 5 lines
Quoted 3 lines
Quoted 1 line

Are you sure that the best fix isn't to just use an int here?

Wenyou,
Any comments for this?

Like Mark said, It seems it is more reasonable using a signed int, instead of unsigned int.

OK. I'm sending a formal patch with your Signed-off-by now.

Regards,
Axel
--
To unsubscribe from this list: send the line "unsubscribe linux-spi" in the body of a message to [email protected] More majordomo info at http://vger.kernel.org/majordomo-info.html