From: Rasmus Villemoes <linux@rasmusvillemoes.dk>
To: Maurizio Lombardi <mlombard@redhat.com>
Cc: tj@kernel.org, joe@perches.com, linux-kernel@vger.kernel.org
Subject: Re: [RFC PATCH 0/3] fix *pbl format support
Date: Wed, 16 Sep 2015 14:27:23 +0200 [thread overview]
Message-ID: <87d1xi4kic.fsf@rasmusvillemoes.dk> (raw)
In-Reply-To: <1442394523-19176-1-git-send-email-mlombard@redhat.com> (Maurizio Lombardi's message of "Wed, 16 Sep 2015 11:08:40 +0200")
On Wed, Sep 16 2015, Maurizio Lombardi <mlombard@redhat.com> wrote:
> Hi,
>
> I tried to fix the "*pb[l]" format issue while taking care of the problems
> discussed in this thread:
>
> https://lkml.org/lkml/2015/9/9/153
>
> I would like to know whether this approach is more acceptable to you:
>
> PATCH 1 modifies the code so that the printf_spec struct is not passed by value
> anymore, instead a const pointer is used and the structure is copied to
> a local variable only when necessary.
>
If we want to fix the problem with 3/3, then this seems obviously
necessary. There may be stuff we want to optimize later (for example, I
don't think we should always make a local copy of the entire struct; if
we're only modifying one of the fields, it's better to copy that field
to a local variable and use that).
Nit: Please don't say that the parameter is passed around _on the
stack_. Making it fit in 8 bytes is very much so that sane architectures
have a chance to pass it in a register, and _how_ parameters are passed
around is in any case very arch-dependent. Just say "struct printf_spec
is passed by value".
>
> PATCH 2 modifies the bitmap_*_string() functions so they'll append
>"..." to the
> output string whenever the buffer is not sufficiently large.
>
> example of output:
>
> *pb: cccccccc,...
> *pbl: 1-2,5-7,...
This part I really don't like. We shouldn't make the output depend on
the size of the output buffer (other than truncating it if necessary, of
course).
I haven't looked carefully at your code, but it does seem that you make
sure that at least the return value is as expected, which will make
kasprintf work. But it seems there is another kasprintf
problem. [reminder: kasprintf works by doing a va_copy, then doing a
first call of vsnprintf, passing NULL for the buffer and 0 for the
length to determine the size to allocate, and then doing the actual
formatting with a second call]
+ if (buf >= end && buf_start != end) {
+ int spc = 0;
+ char *trunc = end - 1;
+
+ while (trunc > buf_start) {
+ if (*trunc == ',' && spc > 3) {
+ trunc++;
+ break;
+ }
+ trunc--;
+ spc++;
+ }
On the first call from kasprintf, we have end == NULL + 0 == NULL.
Suppose the format is "hello world %pb". By the time the bitmap helper
is called, we have advanced buf away from end, so the stored buf_start
is != end, and also of course buf >= end. Then we set trunc = (void*)-1,
and trunc will continue to be > buf_start for a very very long time...
I may have misread, or it might be fixable, but I really don't like
playing these subtle games. snprintf already provides a method to
reliably detect truncation; it is up to the user to decide whether and
how to deal with that. But yes, this of course requires that snprintf
actually attempted to format the entire bitmap, which in turn requires
some way to pass the correct size all the way through to the bitmap
formatter.
> PATCH 3 increases the size of printf_spec.field_width (from s16 to s32).
I'm not yet completely convinced this is the right solution. Obviously,
if other problems with the small .field_width size show up, this might
be necessary, but as long as it's only the %pb formatter (and so far
only a single user of that), I think smaller/other hammers should be
thought about. So far I think there've been two alternatives: (1)
reintroduce the dedicated bitmap pretty printer(s), (2) my half-ugly
proposal allowing the user to pass struct printf_bitmap to the %pbh[l]
specifier. I'll try to actually code up (2).
Rasmus
next prev parent reply other threads:[~2015-09-16 12:27 UTC|newest]
Thread overview: 10+ messages / expand[flat|nested] mbox.gz Atom feed top
2015-09-16 9:08 Maurizio Lombardi
2015-09-16 9:08 ` [RFC PATCH 1/3] lib/vsprintf.c: Do not pass printf_spec by value on stack Maurizio Lombardi
2015-09-16 9:08 ` [RFC PATCH 2/3] lib/vsprintf.c: append "..." if the *pb[l] output has been truncated Maurizio Lombardi
2015-09-16 9:08 ` [RFC PATCH 3/3] lib/vsprintf.c: increase the size of the field_width variable Maurizio Lombardi
2015-09-16 12:27 ` Rasmus Villemoes [this message]
2015-09-16 12:53 ` [RFC PATCH 0/3] fix *pbl format support Maurizio Lombardi
2015-09-16 17:45 ` Tejun Heo
2015-09-16 20:35 ` Rasmus Villemoes
2015-09-21 14:54 ` Maurizio Lombardi
2015-09-21 16:24 ` Rasmus Villemoes
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=87d1xi4kic.fsf@rasmusvillemoes.dk \
--to=linux@rasmusvillemoes.dk \
--cc=joe@perches.com \
--cc=linux-kernel@vger.kernel.org \
--cc=mlombard@redhat.com \
--cc=tj@kernel.org \
/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
Powered by JetHome