Skip to content

Commit

Permalink
qga: Fix undefined C behavior
Browse files Browse the repository at this point in the history
The QAPI struct GuestFileWhence has a comment about how we are
exploiting equivalent values between two different integer types
shared in a union. But C says behavior is undefined on assignments to
overlapping storage when the two types are not the same width, and
indeed, 'int64_t value' and 'enum QGASeek name' are very likely to be
different in width.  Utilize a temporary variable to fix things.

Reported-by: Peter Maydell <peter.maydell@linaro.org>
Fixes: 0b4b493
Fixes: Coverity CID 1421990
Signed-off-by: Eric Blake <eblake@redhat.com>
Signed-off-by: Michael Roth <mdroth@linux.vnet.ibm.com>
(cherry picked from commit a23f38a)
Signed-off-by: Michael Roth <mdroth@linux.vnet.ibm.com>
  • Loading branch information
ebblake authored and mdroth committed Jun 4, 2020
1 parent 4996bd7 commit e353161
Showing 1 changed file with 7 additions and 2 deletions.
9 changes: 7 additions & 2 deletions qga/commands.c
Expand Up @@ -482,10 +482,15 @@ GuestExec *qmp_guest_exec(const char *path,
* the guest's SEEK_ constants. */
int ga_parse_whence(GuestFileWhence *whence, Error **errp)
{
/* Exploit the fact that we picked values to match QGA_SEEK_*. */
/*
* Exploit the fact that we picked values to match QGA_SEEK_*;
* however, we have to use a temporary variable since the union
* members may have different size.
*/
if (whence->type == QTYPE_QSTRING) {
int value = whence->u.name;
whence->type = QTYPE_QNUM;
whence->u.value = whence->u.name;
whence->u.value = value;
}
switch (whence->u.value) {
case QGA_SEEK_SET:
Expand Down

0 comments on commit e353161

Please sign in to comment.