From: Paolo Bonzini <pbonzini@redhat.com>
To: Markus Armbruster <armbru@redhat.com>
Cc: Peter Maydell <peter.maydell@linaro.org>,
QEMU Developers <qemu-devel@nongnu.org>
Subject: Re: use of uninitialized variable involving visit_type_uint32() and friends
Date: Fri, 1 Apr 2022 13:16:19 +0200 [thread overview]
Message-ID: <e17c3f3b-000f-4bab-1e3a-2adbafbcdcbb@redhat.com> (raw)
In-Reply-To: <87y20p88qq.fsf@pond.sub.org>
On 4/1/22 11:15, Markus Armbruster wrote:
> + assert (v->type == expected_type);
> + if (expected_type & (VISITOR_INPUT | VISITOR_DEALLOC)) {
>
> Backwards.
Yes, I always get input vs output wrong.
> With an input visitor @v,
>
> visit_type_uint32(v, "name", &val, errp)
>
> stores to @val without looking at it first. In other words,
> uninitialized @val is fine, just like for val = ...
>
> Note: you don't actually need VISITOR_DEALLOC here, because a
> deallocation visitor isn't going to do anything for non-pointer values.
There's a philosophical question on whether other deallocation visitors
can exist than "the" deallocation visitor, but it's not particularly
germane to the topic.
> Two changes:
>
> * Skip copying to and from full-width buffer @value:
>
> - Skip @value = *obj when we're going to overwrite @value without
> reading it first.
>
> This leaves @value uninitialized instead of initializing it from a
> (commonly) uninitialized variable.
>
> I'm not sure how this helps static analysis, but if it does...
If it can do really serious interprocedural analysis, it _might_ be able
to see through the visitor constructor and know that the "value = *obj"
is not initialized (e.g. "all callers of object_property_set use an
input visitor"). I doubt that honestly, but a man can dream.
If the conditionals are enough to shut it up, then we won the battle
(for now).
If the conditionals are not enough to shut it up, then you have a bit
more confidence when marking the false positives.
> - Skip *obj = @value when value must be *obj anyway.
>
> Should have no observable effect.
>
> Again, I'm not sure how this helps static analysis.
Mostly consistency, could also be changed to an assert(*obj == value);
/* output visitors don't really need obj to be passed by reference */
> * Pass visitor type in addition to the visitor. Can you explain why
> that's useful?
Because it communicates what the caller expects: "I have left this
uninitialized because I expect my "v" argument to be the kind of visitor
that fills it in". It's this argument that gives me the confidence
needed to shut up Coverity's false positives.
Embedding the visitor type in the signature makes it impossible not to
pass it, unlike e.g. an assertion in every getter or setter.
Paolo
next prev parent reply other threads:[~2022-04-01 11:17 UTC|newest]
Thread overview: 10+ messages / expand[flat|nested] mbox.gz Atom feed top
2022-03-31 17:35 use of uninitialized variable involving visit_type_uint32() and friends Peter Maydell
2022-03-31 22:27 ` Daniel Henrique Barboza
2022-04-01 8:07 ` Paolo Bonzini
2022-04-01 9:15 ` Markus Armbruster
2022-04-01 11:16 ` Paolo Bonzini [this message]
2022-04-01 13:11 ` Markus Armbruster
2022-04-01 15:46 ` Paolo Bonzini
2022-04-04 6:24 ` Markus Armbruster
2022-06-27 13:33 ` Peter Maydell
2022-06-27 15:33 ` Markus Armbruster
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=e17c3f3b-000f-4bab-1e3a-2adbafbcdcbb@redhat.com \
--to=pbonzini@redhat.com \
--cc=armbru@redhat.com \
--cc=peter.maydell@linaro.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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).