qapi: zero-initialize all QMP command parameters

In general QMP command parameter values are specified by consumers of the
QMP/HMP interface, but in the case of optional parameters these values may
be left uninitialized.

It is considered a bug for code to make use of optional parameters that have
not been flagged as being present by the marshalling code (via corresponding
has_<parameter> parameter), however our marshalling code will still pass
these uninitialized values on to the corresponding QMP function (to then
be ignored). Some compilers (clang in particular) consider this unsafe
however, and generate warnings as a result. As reported by Peter Maydell:

  This is something clang's -fsanitize=undefined spotted. The
  code generated by qapi-commands.py in qmp-marshal.c for
  qmp_marshal_* functions where there are some optional
  arguments looks like this:

      bool has_force = false;
      bool force;

      mi = qmp_input_visitor_new_strict(QOBJECT(args));
      v = qmp_input_get_visitor(mi);
      visit_type_str(v, &device, "device", errp);
      visit_start_optional(v, &has_force, "force", errp);
      if (has_force) {
          visit_type_bool(v, &force, "force", errp);
      }
      visit_end_optional(v, errp);
      qmp_input_visitor_cleanup(mi);

      if (error_is_set(errp)) {
          goto out;
      }
      qmp_eject(device, has_force, force, errp);

  In the case where has_force is false, we never initialize
  force, but then we use it by passing it to qmp_eject.
  I imagine we don't then actually use the value, but clang
  complains in particular for 'bool' variables because the value
  that ends up being loaded from memory for 'force' is not either
  0 or 1 (being uninitialized stack contents).

Fix this by initializing all QMP command parameters to {0} in the
marshalling code prior to passing them on to the QMP functions.

Signed-off-by: Michael Roth <mdroth@linux.vnet.ibm.com>
Reported-by: Peter Maydell <peter.maydell@linaro.org>
Tested-by: Peter Maydell <peter.maydell@linaro.org>
Reviewed-by: Eric Blake <eblake@redhat.com>
Reviewed-by: Markus Armbruster <armbru@redhat.com>
Signed-off-by: Luiz Capitulino <lcapitulino@redhat.com>
1 file changed
tree: 394f5db89292f081978466db6efbb8748b1216e1
  1. .exrc
  2. .gitignore
  3. .gitmodules
  4. .mailmap
  5. .travis.yml
  6. CODING_STYLE
  7. COPYING
  8. COPYING.LIB
  9. Changelog
  10. HACKING
  11. LICENSE
  12. MAINTAINERS
  13. Makefile
  14. Makefile.objs
  15. Makefile.target
  16. README
  17. VERSION
  18. aio-posix.c
  19. aio-win32.c
  20. arch_init.c
  21. async.c
  22. audio/
  23. backends/
  24. balloon.c
  25. block-migration.c
  26. block.c
  27. block/
  28. blockdev-nbd.c
  29. blockdev.c
  30. blockjob.c
  31. bsd-user/
  32. bt-host.c
  33. bt-vhci.c
  34. configure
  35. coroutine-gthread.c
  36. coroutine-sigaltstack.c
  37. coroutine-ucontext.c
  38. coroutine-win32.c
  39. cpu-exec.c
  40. cpus.c
  41. cputlb.c
  42. default-configs/
  43. device-hotplug.c
  44. device_tree.c
  45. disas.c
  46. disas/
  47. dma-helpers.c
  48. docs/
  49. dump.c
  50. exec.c
  51. fpu/
  52. fsdev/
  53. gdb-xml/
  54. gdbstub.c
  55. hmp-commands.hx
  56. hmp.c
  57. hmp.h
  58. hw/
  59. include/
  60. iohandler.c
  61. ioport.c
  62. iothread.c
  63. kvm-all.c
  64. kvm-stub.c
  65. libcacard/
  66. linux-headers/
  67. linux-user/
  68. main-loop.c
  69. memory.c
  70. memory_mapping.c
  71. migration-exec.c
  72. migration-fd.c
  73. migration-rdma.c
  74. migration-tcp.c
  75. migration-unix.c
  76. migration.c
  77. module-common.c
  78. monitor.c
  79. nbd.c
  80. net/
  81. os-posix.c
  82. os-win32.c
  83. page_cache.c
  84. pc-bios/
  85. po/
  86. qapi-schema.json
  87. qapi/
  88. qdev-monitor.c
  89. qdict-test-data.txt
  90. qemu-bridge-helper.c
  91. qemu-char.c
  92. qemu-coroutine-io.c
  93. qemu-coroutine-lock.c
  94. qemu-coroutine-sleep.c
  95. qemu-coroutine.c
  96. qemu-doc.texi
  97. qemu-file.c
  98. qemu-img-cmds.hx
  99. qemu-img.c
  100. qemu-img.texi
  101. qemu-io-cmds.c
  102. qemu-io.c
  103. qemu-log.c
  104. qemu-nbd.c
  105. qemu-nbd.texi
  106. qemu-options-wrapper.h
  107. qemu-options.h
  108. qemu-options.hx
  109. qemu-seccomp.c
  110. qemu-tech.texi
  111. qemu-timer.c
  112. qemu.nsi
  113. qemu.sasl
  114. qga/
  115. qmp-commands.hx
  116. qmp.c
  117. qobject/
  118. qom/
  119. qtest.c
  120. roms/
  121. rules.mak
  122. savevm.c
  123. scripts/
  124. slirp/
  125. spice-qemu-char.c
  126. stubs/
  127. sysconfigs/
  128. target-alpha/
  129. target-arm/
  130. target-cris/
  131. target-i386/
  132. target-lm32/
  133. target-m68k/
  134. target-microblaze/
  135. target-mips/
  136. target-moxie/
  137. target-openrisc/
  138. target-ppc/
  139. target-s390x/
  140. target-sh4/
  141. target-sparc/
  142. target-unicore32/
  143. target-xtensa/
  144. tcg-runtime.c
  145. tcg/
  146. tci.c
  147. tests/
  148. thread-pool.c
  149. thunk.c
  150. tpm.c
  151. trace-events
  152. trace/
  153. translate-all.c
  154. translate-all.h
  155. ui/
  156. user-exec.c
  157. util/
  158. version.rc
  159. vl.c
  160. vmstate.c
  161. xbzrle.c
  162. xen-common-stub.c
  163. xen-common.c
  164. xen-hvm-stub.c
  165. xen-hvm.c
  166. xen-mapcache.c