All of lore.kernel.org
 help / color / mirror / Atom feed
From: Dan Carpenter <dan.carpenter@oracle.com>
To: Matteo Semenzato <mattew8898@gmail.com>
Cc: gregkh@linuxfoundation.org, lidza.louina@gmail.com,
	markh@compro.net, devel@driverdev.osuosl.org,
	linux-kernel@vger.kernel.org
Subject: Re: [PATCH] Staging: dgnc: check kzalloc result
Date: Mon, 9 Mar 2015 14:24:12 +0300	[thread overview]
Message-ID: <20150309112412.GC10964@mwanda> (raw)
In-Reply-To: <1425833647-21686-1-git-send-email-mattew8898@gmail.com>

On Sun, Mar 08, 2015 at 05:54:07PM +0100, Matteo Semenzato wrote:
> From: Matteo Semenzato <mattew8898@gmail.com>
> 
> Return -ENOMEM if allocating brd->flipbuf fails.
> 
> Signed-off-by: Matteo Semenzato <mattew8898@gmail.com>
> ---
>  drivers/staging/dgnc/dgnc_driver.c | 4 ++++
>  1 file changed, 4 insertions(+)
> 
> diff --git a/drivers/staging/dgnc/dgnc_driver.c b/drivers/staging/dgnc/dgnc_driver.c
> index f177d3a..711af02 100644
> --- a/drivers/staging/dgnc/dgnc_driver.c
> +++ b/drivers/staging/dgnc/dgnc_driver.c
> @@ -622,6 +622,10 @@ static int dgnc_found_board(struct pci_dev *pdev, int id)
>  	 * context, and there are no locks held.
>  	 */
>  	brd->flipbuf = kzalloc(MYFLIPLEN, GFP_KERNEL);
> +	if (!brd->flipbuf) {
> +		kfree(brd);
> +		return -ENOMEM;

This leaves a freed pointer in "dgnc_Board[dgnc_NumBoards]" which is
sort of ugly.  It doesn't free "brd->msgbuf_head" which is a bug.

Please read my essay on error hanling:
https://plus.google.com/106378716002406849458

What we want is :

	ret = do_one();
	if (ret)
		return ret;
	ret = do_two();
	if (ret)
		goto undo_one;

	ret = do_three();
	if (ret)
		goto undo_two;

	return 0;

err_undo_two:
	undo_two();
err_undo_one:
	undo_one();

	return ret;

Basically, a list of commands and a mirror list of undos.  This is
standard kernel style and the error handling basically writes itself
automatically.

regards,
dan carpenter


> +	}
>  
>  	wake_up_interruptible(&brd->state_wait);
>  
> -- 
> 2.3.1
> 
> _______________________________________________
> devel mailing list
> devel@linuxdriverproject.org
> http://driverdev.linuxdriverproject.org/mailman/listinfo/driverdev-devel

      reply	other threads:[~2015-03-09 11:24 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2015-03-08 16:54 [PATCH] Staging: dgnc: check kzalloc result Matteo Semenzato
2015-03-09 11:24 ` Dan Carpenter [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20150309112412.GC10964@mwanda \
    --to=dan.carpenter@oracle.com \
    --cc=devel@driverdev.osuosl.org \
    --cc=gregkh@linuxfoundation.org \
    --cc=lidza.louina@gmail.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=markh@compro.net \
    --cc=mattew8898@gmail.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.