All of lore.kernel.org
 help / color / mirror / Atom feed
From: Mark Brown <broonie@opensource.wolfsonmicro.com>
To: Takashi Iwai <tiwai@suse.de>
Cc: alsa-devel@alsa-project.org, Dan Carpenter <error27@gmail.com>,
	Jassi Brar <jassi.brar@samsung.com>,
	kernel-janitors@vger.kernel.org,
	Peter Ujfalusi <peter.ujfalusi@nokia.com>,
	linux-kernel@vger.kernel.org, Liam Girdwood <lrg@slimlogic.co.uk>
Subject: Re: [patch] ASoC: soc: snprintf() doesn't return negative
Date: Tue, 12 Oct 2010 10:56:05 +0100	[thread overview]
Message-ID: <20101012095605.GC30933@rakim.wolfsonmicro.main> (raw)
In-Reply-To: <s5hvd57eo60.wl%tiwai@suse.de>

On Tue, Oct 12, 2010 at 11:49:27AM +0200, Takashi Iwai wrote:
> Mark Brown wrote:

> Argh, yes, I'm (again) confused by that behavior.
> The problem is the potential buffer overflow, indeed.  snprintf()
> returns the size that would be printed.  Thus a safe code would be
> like:
> 
> 	list_for_each_entry(dai, &dai_list, list) {
>  		int len = snprintf(buf + ret, PAGE_SIZE - ret, "%s\n", dai->name);
>  		if (len < 0)
>  			continue;
>  		ret += len;
> 		if (ret >= PAGE_SIZE) {
> 			ret = PAGE_SIZE;
> 			break;
> 		}
> 	}

Yes, this form is better for that variant of the loop - that is safe and
legible without relying on current implementation details of snprintf().

WARNING: multiple messages have this Message-ID (diff)
From: Mark Brown <broonie@opensource.wolfsonmicro.com>
To: Takashi Iwai <tiwai@suse.de>
Cc: alsa-devel@alsa-project.org, Dan Carpenter <error27@gmail.com>,
	Jassi Brar <jassi.brar@samsung.com>,
	kernel-janitors@vger.kernel.org,
	Peter Ujfalusi <peter.ujfalusi@nokia.com>,
	linux-kernel@vger.kernel.org, Liam Girdwood <lrg@slimlogic.co.uk>
Subject: Re: [patch] ASoC: soc: snprintf() doesn't return negative
Date: Tue, 12 Oct 2010 09:56:05 +0000	[thread overview]
Message-ID: <20101012095605.GC30933@rakim.wolfsonmicro.main> (raw)
In-Reply-To: <s5hvd57eo60.wl%tiwai@suse.de>

On Tue, Oct 12, 2010 at 11:49:27AM +0200, Takashi Iwai wrote:
> Mark Brown wrote:

> Argh, yes, I'm (again) confused by that behavior.
> The problem is the potential buffer overflow, indeed.  snprintf()
> returns the size that would be printed.  Thus a safe code would be
> like:
> 
> 	list_for_each_entry(dai, &dai_list, list) {
>  		int len = snprintf(buf + ret, PAGE_SIZE - ret, "%s\n", dai->name);
>  		if (len < 0)
>  			continue;
>  		ret += len;
> 		if (ret >= PAGE_SIZE) {
> 			ret = PAGE_SIZE;
> 			break;
> 		}
> 	}

Yes, this form is better for that variant of the loop - that is safe and
legible without relying on current implementation details of snprintf().

WARNING: multiple messages have this Message-ID (diff)
From: Mark Brown <broonie@opensource.wolfsonmicro.com>
To: Takashi Iwai <tiwai@suse.de>
Cc: Dan Carpenter <error27@gmail.com>,
	Liam Girdwood <lrg@slimlogic.co.uk>,
	Jaroslav Kysela <perex@perex.cz>,
	Peter Ujfalusi <peter.ujfalusi@nokia.com>,
	Jassi Brar <jassi.brar@samsung.com>,
	alsa-devel@alsa-project.org, kernel-janitors@vger.kernel.org,
	linux-kernel@vger.kernel.org
Subject: Re: [patch] ASoC: soc: snprintf() doesn't return negative
Date: Tue, 12 Oct 2010 10:56:05 +0100	[thread overview]
Message-ID: <20101012095605.GC30933@rakim.wolfsonmicro.main> (raw)
In-Reply-To: <s5hvd57eo60.wl%tiwai@suse.de>

On Tue, Oct 12, 2010 at 11:49:27AM +0200, Takashi Iwai wrote:
> Mark Brown wrote:

> Argh, yes, I'm (again) confused by that behavior.
> The problem is the potential buffer overflow, indeed.  snprintf()
> returns the size that would be printed.  Thus a safe code would be
> like:
> 
> 	list_for_each_entry(dai, &dai_list, list) {
>  		int len = snprintf(buf + ret, PAGE_SIZE - ret, "%s\n", dai->name);
>  		if (len < 0)
>  			continue;
>  		ret += len;
> 		if (ret >= PAGE_SIZE) {
> 			ret = PAGE_SIZE;
> 			break;
> 		}
> 	}

Yes, this form is better for that variant of the loop - that is safe and
legible without relying on current implementation details of snprintf().

  reply	other threads:[~2010-10-12  9:56 UTC|newest]

Thread overview: 33+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2010-10-11  3:54 [patch] ASoC: soc: snprintf() doesn't return negative Dan Carpenter
2010-10-11  3:54 ` Dan Carpenter
2010-10-11 10:07 ` Liam Girdwood
2010-10-11 10:07   ` Liam Girdwood
2010-10-11 10:40 ` Mark Brown
2010-10-11 10:40   ` Mark Brown
2010-10-11 16:40   ` Dan Carpenter
2010-10-11 16:40     ` Dan Carpenter
2010-10-11 16:40     ` Dan Carpenter
2010-10-11 18:51     ` Mark Brown
2010-10-11 18:51       ` Mark Brown
2010-10-11 18:51       ` Mark Brown
2010-10-11 19:45       ` Dan Carpenter
2010-10-11 19:45         ` Dan Carpenter
2010-10-11 19:45         ` Dan Carpenter
2010-10-11 20:57         ` Takashi Iwai
2010-10-11 20:57           ` Takashi Iwai
2010-10-11 20:57           ` Takashi Iwai
2010-10-12  9:35           ` Mark Brown
2010-10-12  9:35             ` Mark Brown
2010-10-12  9:35             ` Mark Brown
2010-10-12  9:49             ` Takashi Iwai
2010-10-12  9:49               ` Takashi Iwai
2010-10-12  9:49               ` Takashi Iwai
2010-10-12  9:56               ` Mark Brown [this message]
2010-10-12  9:56                 ` Mark Brown
2010-10-12  9:56                 ` Mark Brown
2010-10-12 10:40                 ` Dan Carpenter
2010-10-12 10:40                   ` Dan Carpenter
2010-10-12 10:40                   ` Dan Carpenter
2010-10-11 21:11         ` Dan Carpenter
2010-10-11 21:11           ` Dan Carpenter
2010-10-11 21:11           ` Dan Carpenter

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=20101012095605.GC30933@rakim.wolfsonmicro.main \
    --to=broonie@opensource.wolfsonmicro.com \
    --cc=alsa-devel@alsa-project.org \
    --cc=error27@gmail.com \
    --cc=jassi.brar@samsung.com \
    --cc=kernel-janitors@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=lrg@slimlogic.co.uk \
    --cc=peter.ujfalusi@nokia.com \
    --cc=tiwai@suse.de \
    /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.