Re: [PATCH] staging: vt6655: check for memory allocation failures

From: Ji-Hun Kim
Date: Wed Mar 28 2018 - 21:56:48 EST


On Wed, Mar 28, 2018 at 05:55:57PM +0800, Jia-Ju Bai wrote:
> >@@ -646,7 +649,8 @@ static void device_init_td1_ring(struct vnt_private *priv)
> > i++, curr += sizeof(struct vnt_tx_desc)) {
> > desc = &priv->apTD1Rings[i];
> > desc->td_info = kzalloc(sizeof(*desc->td_info), GFP_KERNEL);
> >-
> >+ if (WARN_ON(!desc->td_info))
> >+ return;
> > desc->td_info->buf = priv->tx1_bufs + i * PKT_BUF_SZ;
> > desc->td_info->buf_dma = priv->tx_bufs_dma1 + i * PKT_BUF_SZ;
>
> I think the bugs you found are right.
> But your patch is not correct, because it is dangerous to return directly.
> I think you should return an error and then implement error handling
> code for these functions.
>
Yes, it needs to free previous allocated values in the for loop. Directly
return could make memory leaks. I am going to make patch v2.

- Delete WARN_ON which could make crashes on some machines.
- Add freeing sequences for previously allocated memory when kzalloc()
failed instead of returning directly.

Does these changes would be fine on this bug?