mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Kyle Moffett <mrmacman_g4@mac.com>
To: Matthew Wilcox <matthew@wil.cx>
Cc: Linus Torvalds <torvalds@osdl.org>, Andrew Morton <akpm@osdl.org>,
	linux-kernel@vger.kernel.org,
	Matthew Wilcox <willy@linux.intel.com>
Subject: Re: [PATCH 1/4] stringbuf: A string buffer implementation
Date: Wed, 24 Oct 2007 20:07:23 -0400	[thread overview]
Message-ID: <314B5322-3575-4449-90D2-3068BDC3F64D@mac.com> (raw)
In-Reply-To: <20071024212110.GG27248@parisc-linux.org>

On Oct 24, 2007, at 17:21:10, Matthew Wilcox wrote:
> On Wed, Oct 24, 2007 at 04:59:48PM -0400, Kyle Moffett wrote:
>> This seems unlikely to work reliably as the various "v*printf"  
>> functions modify the va_list argument they are passed.  It may  
>> happen to work on your particular architecture depending on how  
>> that argument data is passed and stored, but you probably actually  
>> want to make a copy of the varargs list for the first vsnprintf call.
>
> I based what I did on how printk works:
>
>         va_start(args, fmt);
>         r = vprintk(fmt, args);
>         va_end(args);
>
> It doesn't call va_* anywhere else.  I don't claim to be a varargs  
> expert, but if I'm wrong, I'm at least wrong the same way that  
> printk is, so not in any way that's significant for any other  
> architecture Linux runs on.

No, the problem is what happens when you don't have enough space  
allocated:  you call "vsnprintf(s, len, format, args);" and then  
later call "vsprintf(s, format, args);" with the *SAME* "args".   
That's what's broken.

So this is wrong:
> va_list args;
> va_start(args, fmt);
> r1 = vprintk(fmt, args);
> r2 = vprintk(fmt, args);
> va_end(args);

To fix it, you have 2 options.

Option 1:
> va_list args;
> va_start(args, fmt);
> r1 = vprintk(fmt, args);
> va_end(args);
> va_start(args, fmt);
> r2 = vprintk(fmt, args);
> va_end(args);

Option 2:
> va_list args, argscopy;
> va_start(args, fmt);
> va_copy(argscopy, args);
> r1 = vprintk(fmt, argscopy);
> va_end(argscopy);
> r2 = vprintk(fmt, args);
> va_end(args);

Now in a function which *receives* a va_list from one of its callers,  
"Option 1" isn't an option because you don't have the original stack  
frame, so the result looks like this:

> void func1(const char *fmt, ...)
> {
> 	va_list ap;
> 	va_start(ap, fmt);
> 	func2(fmt, ap);
> 	va_end(ap);
> }
>
> void func2(const char *fmt, va_list ap)
> {
> 	va_list ap2;
> 	va_copy(ap2, ap);
> 	vprintk(fmt, ap2);
> 	va_end(ap2);
> 	vprintk(fmt, ap);
> }

Cheers,
Kyle Moffett


  reply	other threads:[~2007-10-25  0:07 UTC|newest]

Thread overview: 41+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2007-10-24 19:58 Stringbuf, v2 Matthew Wilcox
2007-10-24 19:59 ` [PATCH 1/4] stringbuf: A string buffer implementation Matthew Wilcox
2007-10-24 19:59   ` [PATCH 2/4] isdn: Use stringbuf Matthew Wilcox
2007-10-24 19:59     ` [PATCH 3/4] sound: " Matthew Wilcox
2007-10-24 19:59       ` [PATCH 4/4] partitions: Fix non-atomic printk Matthew Wilcox
2007-10-24 20:59   ` [PATCH 1/4] stringbuf: A string buffer implementation Kyle Moffett
2007-10-24 21:21     ` Matthew Wilcox
2007-10-25  0:07       ` Kyle Moffett [this message]
2007-10-25  3:23         ` Matthew Wilcox
2007-10-26  2:11   ` Rusty Russell
2007-10-26  3:41     ` Joe Perches
2007-10-26  5:05       ` Joe Perches
2007-10-26 11:57     ` Matthew Wilcox
2007-10-26 20:57       ` Matt Mackall
2007-10-27 10:09         ` Rusty Russell
2007-10-29  3:03           ` Matt Mackall
2007-10-29  5:38             ` Rusty Russell
2007-10-27 11:47     ` Pekka Enberg
2007-10-27 12:50       ` Rusty Russell
2007-10-27 16:34         ` Pekka Enberg
2007-10-27 16:48           ` Matthew Wilcox
2007-10-24 20:51 ` Stringbuf, v2 Joe Perches
2007-10-24 20:57   ` Matthew Wilcox
2007-10-24 21:06     ` Joe Perches
2007-10-24 21:34       ` Matthew Wilcox
  -- strict thread matches above, loose matches on Subject: below --
2007-10-23 21:12 [PATCH 1/4] stringbuf: A string buffer implementation Matthew Wilcox
2007-10-23 22:11 ` Matt Mackall
2007-10-24  1:49   ` Matthew Wilcox
2007-10-24 15:20     ` Matt Mackall
2007-10-24 15:30       ` Matthew Wilcox
2007-10-23 23:43 ` Linus Torvalds
2007-10-24  2:30   ` Matthew Wilcox
2007-10-24  2:45     ` Andrew Morton
2007-10-24  2:19 ` Eric St-Laurent
2007-10-24  2:35   ` Matthew Wilcox
2007-10-24  2:48     ` Eric St-Laurent
2007-10-24 13:21 ` Florian Weimer
2007-10-24 14:02   ` Matthew Wilcox
2007-10-26 12:05 ` Pekka Enberg
2007-10-27  7:31 ` Pavel Machek
2007-10-30 15:26 ` Denys Vlasenko

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=314B5322-3575-4449-90D2-3068BDC3F64D@mac.com \
    --to=mrmacman_g4@mac.com \
    --cc=akpm@osdl.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=matthew@wil.cx \
    --cc=torvalds@osdl.org \
    --cc=willy@linux.intel.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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®