All of lore.kernel.org
 help / color / mirror / Atom feed
From: Markus Armbruster <armbru@redhat.com>
To: Peter Xu <peterx@redhat.com>
Cc: Kevin Wolf <kwolf@redhat.com>,
	pbonzini@redhat.com, qemu-devel@nongnu.org,
	qemu-block@nongnu.org
Subject: Re: [Qemu-devel] [PATCH 2/8] block: fix unbounded stack for dump_qdict
Date: Tue, 08 Mar 2016 09:12:45 +0100	[thread overview]
Message-ID: <87twkhe6bm.fsf@blackfin.pond.sub.org> (raw)
In-Reply-To: <1457420446-25276-3-git-send-email-peterx@redhat.com> (Peter Xu's message of "Tue, 8 Mar 2016 15:00:40 +0800")

Peter Xu <peterx@redhat.com> writes:

> Suggested-by: Paolo Bonzini <pbonzini@redhat.com>
> CC: Markus Armbruster <armbru@redhat.com>
> CC: Kevin Wolf <kwolf@redhat.com>
> CC: qemu-block@nongnu.org
> Signed-off-by: Peter Xu <peterx@redhat.com>
> ---
>  block/qapi.c | 5 ++++-
>  1 file changed, 4 insertions(+), 1 deletion(-)
>
> diff --git a/block/qapi.c b/block/qapi.c
> index db2d3fb..687e577 100644
> --- a/block/qapi.c
> +++ b/block/qapi.c
> @@ -638,9 +638,12 @@ static void dump_qdict(fprintf_function func_fprintf, void *f, int indentation,
>          QType type = qobject_type(entry->value);
>          bool composite = (type == QTYPE_QDICT || type == QTYPE_QLIST);
>          const char *format = composite ? "%*s%s:\n" : "%*s%s: ";

Unrelated to your patch: ugh!

Printf formats should be literals whenever possible, to make it easy for
the compiler to warn you when you screw up.  It's trivially possible
here!  Instead of

           func_fprintf(f, format, indentation * 4, "", key);

do

           func_fprintf(f, "%*s%s:%c", indentation * 4, "", key,
                        composite ? '\n', ' ');;

> -        char key[strlen(entry->key) + 1];
> +#define __KEY_LEN (256)
> +        char key[__KEY_LEN];
>          int i;
>  
> +        assert(strlen(entry->key) + 1 <= __KEY_LEN);
> +#undef __KEY_LEN
>          /* replace dashes with spaces in key (variable) names */
>          for (i = 0; entry->key[i]; i++) {
>              key[i] = entry->key[i] == '-' ? ' ' : entry->key[i];

I'm afraid this isn't a good idea.  It relies on the non-local argument
that nobody will ever put a key longer than 255 into a qdict that gets
dumped.  That may even be the case, but you need to *prove* it, not just
assert it.  The weakest acceptable proof might be assertions in every
place that put keys into a dict that might get dumped.  I suspect that's
practical and maintainable only if there's a single place that does it.

If this was a good idea, I'd recommend to avoid the awkward macro:

           char key[256];
           int i;
   
           assert(strlen(entry->key) + 1 <= ARRAY_SIZE(key));

There are several other ways to limit the stack usage:

1. Move the array from stack to heap.  Fine unless it's on a hot path.
   As far as I can tell, this dumping business is for HMP and qemu-io,
   i.e. not hot.

2. Use a stack array for short strings, switch to the heap for large
   strings.  More complex, but may be worth it on a hot path where most
   strings are short.

3. Instead of creating a new string with '-' replaced for printing,
   print characters.  Can be okay with buffered I/O, but obviously not
   on a hot path.

4. Like 3., but print substrings not containing '-'.

  reply	other threads:[~2016-03-08  8:12 UTC|newest]

Thread overview: 57+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2016-03-08  7:00 [Qemu-devel] [PATCH 0/8] Fix several unbounded stack usage Peter Xu
2016-03-08  7:00 ` [Qemu-devel] [PATCH 1/8] qdict: fix unbounded stack for qdict_array_entries Peter Xu
2016-03-08  8:22   ` Markus Armbruster
2016-03-08 10:19     ` Kevin Wolf
2016-03-08 16:21       ` Eric Blake
2016-03-08 16:30         ` Kevin Wolf
2016-03-08 16:50           ` Daniel P. Berrange
2016-03-09  2:57       ` Peter Xu
2016-03-09  3:04         ` Eric Blake
2016-03-09  3:27           ` Peter Xu
2016-03-09  9:48           ` Kevin Wolf
2016-03-09 13:23             ` Markus Armbruster
2016-03-09 13:57               ` Kevin Wolf
2016-03-09 21:03                 ` Markus Armbruster
2016-03-10  1:30                   ` Peter Xu
2016-03-08  7:00 ` [Qemu-devel] [PATCH 2/8] block: fix unbounded stack for dump_qdict Peter Xu
2016-03-08  8:12   ` Markus Armbruster [this message]
2016-03-08  8:53     ` Fam Zheng
2016-03-08 13:47       ` Markus Armbruster
2016-03-09  3:00         ` Peter Xu
2016-03-08  9:31     ` Peter Xu
2016-03-08 13:50       ` Markus Armbruster
2016-03-08 12:17     ` Paolo Bonzini
2016-03-09  3:18       ` Peter Xu
2016-03-08  7:00 ` [Qemu-devel] [PATCH 3/8] usb: fix unbounded stack for ohci_td_pkt Peter Xu
2016-03-08 12:20   ` Paolo Bonzini
2016-03-09  4:59     ` Peter Xu
2016-03-08  7:00 ` [Qemu-devel] [PATCH 4/8] usb: fix unbounded stack for xhci_dma_write_u32s Peter Xu
2016-03-08  7:26   ` Peter Maydell
2016-03-09  5:12     ` Peter Xu
2016-03-08 12:21   ` Paolo Bonzini
2016-03-09  5:08     ` Peter Xu
2016-03-09  7:53       ` Paolo Bonzini
2016-03-09  8:07         ` Peter Xu
2016-03-09  8:34           ` Markus Armbruster
2016-03-09  9:19             ` Peter Xu
2016-03-09 12:52               ` Markus Armbruster
2016-03-09 12:59           ` Paolo Bonzini
2016-03-10  2:07             ` Peter Xu
2016-03-08  7:00 ` [Qemu-devel] [PATCH 5/8] usb: fix unbounded stack for inotify_watchfn Peter Xu
2016-03-08  7:20   ` Peter Maydell
2016-03-08 12:22     ` Paolo Bonzini
2016-03-09  5:22       ` Peter Xu
2016-03-08 12:22   ` Paolo Bonzini
2016-03-09  5:23     ` Peter Xu
2016-03-08  7:00 ` [Qemu-devel] [PATCH 6/8] usb: fix unbounded stack for usb_mtp_add_str Peter Xu
2016-03-08  8:10   ` Gerd Hoffmann
2016-03-09  5:29     ` Peter Xu
2016-03-08  7:00 ` [Qemu-devel] [PATCH 7/8] migration: fix unbounded stack for source_return_path_thread Peter Xu
2016-03-08  9:48   ` Juan Quintela
2016-03-08 12:27     ` Paolo Bonzini
2016-03-08 12:26   ` Paolo Bonzini
2016-03-09  5:27     ` Peter Xu
2016-03-08  7:00 ` [Qemu-devel] [PATCH 8/8] hw/i386: fix unbounded stack for load_multiboot Peter Xu
2016-03-08  7:17   ` Peter Maydell
2016-03-08 12:29   ` Paolo Bonzini
2016-03-09  5:39     ` Peter Xu

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=87twkhe6bm.fsf@blackfin.pond.sub.org \
    --to=armbru@redhat.com \
    --cc=kwolf@redhat.com \
    --cc=pbonzini@redhat.com \
    --cc=peterx@redhat.com \
    --cc=qemu-block@nongnu.org \
    --cc=qemu-devel@nongnu.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 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.