[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[Qemu-devel] [PATCH v10 16/25] qapi: Don't cast Enum* to int*
From: |
Eric Blake |
Subject: |
[Qemu-devel] [PATCH v10 16/25] qapi: Don't cast Enum* to int* |
Date: |
Fri, 29 Jan 2016 06:48:52 -0700 |
C compilers are allowed to represent enums as a smaller type
than int, if all enum values fit in the smaller type. There
are even compiler flags that force the use of this smaller
representation, although using them changes the ABI of a
binary. Therefore, our generated code for visit_type_ENUM()
(for all qapi enums) was wrong for casting Enum* to int* when
calling visit_type_enum().
It appears that no one has been using compiler ABI switches
for qemu, because if they had, we are potentially dereferencing
beyond bounds or even risking a SIGBUS on platforms where
unaligned pointer dereferencing is fatal. But it is still
better to avoid the practice entirely, and just use the correct
types.
This matches the fix for alternate qapi types, done earlier in
commit 0426d53 "qapi: Simplify visiting of alternate types",
with generated code changing as:
| void visit_type_QType(Visitor *v, QType *obj, const char *name, Error **errp)
| {
|- visit_type_enum(v, (int *)obj, QType_lookup, "QType", name, errp);
|+ int value = *obj;
|+ visit_type_enum(v, &value, QType_lookup, "QType", name, errp);
|+ *obj = value;
| }
Signed-off-by: Eric Blake <address@hidden>
Reviewed-by: Marc-André Lureau <address@hidden>
---
v10: s/tmp/value/, shorter commit message
v9: mention earlier commit id, enhance commit message
v8: no change
v7: rebase on typo fix
v6: new patch
---
scripts/qapi-visit.py | 5 +++--
1 file changed, 3 insertions(+), 2 deletions(-)
diff --git a/scripts/qapi-visit.py b/scripts/qapi-visit.py
index f98bb5f..ba75667 100644
--- a/scripts/qapi-visit.py
+++ b/scripts/qapi-visit.py
@@ -178,12 +178,13 @@ out:
def gen_visit_enum(name):
- # FIXME cast from enum *obj to int * invalidly assumes enum is int
return mcgen('''
void visit_type_%(c_name)s(Visitor *v, %(c_name)s *obj, const char *name,
Error **errp)
{
- visit_type_enum(v, (int *)obj, %(c_name)s_lookup, "%(name)s", name, errp);
+ int value = *obj;
+ visit_type_enum(v, &value, %(c_name)s_lookup, "%(name)s", name, errp);
+ *obj = value;
}
''',
c_name=c_name(name), name=name)
--
2.5.0
- [Qemu-devel] [PATCH v10 07/25] hmp: Cache use of qapi visitor, (continued)
- [Qemu-devel] [PATCH v10 07/25] hmp: Cache use of qapi visitor, Eric Blake, 2016/01/29
- [Qemu-devel] [PATCH v10 01/25] qobject: Document more shortcomings in our number handling, Eric Blake, 2016/01/29
- [Qemu-devel] [PATCH v10 05/25] qapi: Drop dead parameter in gen_params(), Eric Blake, 2016/01/29
- [Qemu-devel] [PATCH v10 08/25] vl: Ensure qapi visitor properly ends struct visit, Eric Blake, 2016/01/29
- [Qemu-devel] [PATCH v10 11/25] qapi: Track all failures between visit_start/stop, Eric Blake, 2016/01/29
- [Qemu-devel] [PATCH v10 03/25] qapi: Drop dead dealloc visitor variable, Eric Blake, 2016/01/29
- [Qemu-devel] [PATCH v10 13/25] qapi: Prefer type_int64 over type_int in visitors, Eric Blake, 2016/01/29
- [Qemu-devel] [PATCH v10 14/25] qapi: Make all visitors supply uint64 callbacks, Eric Blake, 2016/01/29
- [Qemu-devel] [PATCH v10 06/25] hmp: Drop pointless allocation during qapi visit, Eric Blake, 2016/01/29
- [Qemu-devel] [PATCH v10 09/25] balloon: Improve use of qapi visitor, Eric Blake, 2016/01/29
- [Qemu-devel] [PATCH v10 16/25] qapi: Don't cast Enum* to int*,
Eric Blake <=
- [Qemu-devel] [PATCH v10 02/25] qapi: Avoid use of misnamed DO_UPCAST(), Eric Blake, 2016/01/29
- [Qemu-devel] [PATCH v10 17/25] qom: Use typedef for Visitor, Eric Blake, 2016/01/29
- [Qemu-devel] [PATCH v10 25/25] qmp: Don't abuse stack to track qmp-output root, Eric Blake, 2016/01/29
- [Qemu-devel] [PATCH v10 15/25] qapi: Consolidate visitor small integer callbacks, Eric Blake, 2016/01/29
- [Qemu-devel] [PATCH v10 22/25] qapi: Tighten qmp_input_end_list(), Eric Blake, 2016/01/29
- [Qemu-devel] [PATCH v10 23/25] qapi: Drop unused error argument for list and implicit struct, Eric Blake, 2016/01/29
- [Qemu-devel] [PATCH v10 21/25] qapi: Drop unused 'kind' for struct/enum visit, Eric Blake, 2016/01/29
- [Qemu-devel] [PATCH v10 10/25] qapi: Improve generated event use of qapi visitor, Eric Blake, 2016/01/29
- [Qemu-devel] [PATCH v10 24/25] qmp: Fix reference-counting of qnull on empty output visit, Eric Blake, 2016/01/29
- [Qemu-devel] [PATCH v10 20/25] qapi: Swap 'name' in visit_* callbacks to match public API, Eric Blake, 2016/01/29