[PATCH] staging: et131x: let the freeing of memory more reasonable in error path

Zhao, Gang gamerh2o at gmail.com
Fri Feb 21 05:19:10 UTC 2014


The original code relies on the caller to deal with "fail in the
middle" memory allocation failure in et131x_rx_dma_memory_alloc(). The
relying on the caller to handle this memory allocation failure could
cause some inconveniences:

Code reviewer has to check the caller of this function to make sure
whether rx_ring->fbr[0] is leaked or not when allocation of
rx_ring->fbr[1] fails(By examing if the caller called the correct
freeing function when this function returns error). This unnecessary
check can be avoided. By freeing rx_ring->fbr[0] in this function the
above type of memory leak can't happen at the beginning. This also
makes the code a little more readable.

The changes is to free rx_ring->fbr[0] and set the pointer
rx_ring->fbr[0] to NULL when allocation of rx_ring->fbr[1] fails in
et131x_rx_dma_memory_alloc(). The freeing function
rx_dma_memory_free() can handle this situation correctly.

Signed-off-by: Zhao, Gang <gamerh2o at gmail.com>
---

I didn't notice Alan's et131x patch on the list have been applied.
Now combine my comments on that patch to a new patch for review.

 drivers/staging/et131x/et131x.c | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)

diff --git a/drivers/staging/et131x/et131x.c b/drivers/staging/et131x/et131x.c
index 29895a4..cc9803a 100644
--- a/drivers/staging/et131x/et131x.c
+++ b/drivers/staging/et131x/et131x.c
@@ -2123,8 +2123,11 @@ static int et131x_rx_dma_memory_alloc(struct et131x_adapter *adapter)
 	if (rx_ring->fbr[0] == NULL)
 		return -ENOMEM;
 	rx_ring->fbr[1] = kmalloc(sizeof(struct fbr_lookup), GFP_KERNEL);
-	if (rx_ring->fbr[1] == NULL)
+	if (rx_ring->fbr[1] == NULL) {
+		kfree(rx_ring->fbr[0]);
+		rx_ring->fbr[0] = NULL;
 		return -ENOMEM;
+	}
 
 	/* The first thing we will do is configure the sizes of the buffer
 	 * rings. These will change based on jumbo packet support.  Larger
-- 
1.8.5.3



More information about the devel mailing list