Laszlo Ersek
2021-Oct-11 22:36 UTC
[Libguestfs] [PATCH 2/3] lib/proto: suppress "may be used uninitialized" in send_file_complete()
gcc emits the following warning:> proto.c: In function ?send_file_complete?: > proto.c:437:10: error: ?buf? may be used uninitialized > [-Werror=maybe-uninitialized] > 437 | return send_file_chunk (g, 0, buf, 0); > | ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~In theory, passing the 1-byte array "buf", with indeterminate contents, to xdr_bytes() ultimately, could be fine -- assuming xdr_bytes() never reads the contents of the buffer, due to the buffer size being zero. However, the xdr_bytes() manual does not seem to guarantee this (it also does not explicitly permit passing a NULL buffer alongside size=0, which would be even simpler for the caller). In order to shut up the compiler, just zero-initialize the buffer -- that's simpler than adding diagnostics pragmas. The "maybe-uninitialized" warning is otherwise very useful, so keep it globally enabled (per WARN_CFLAGS / WERROR_CFLAGS). Signed-off-by: Laszlo Ersek <lersek at redhat.com> --- lib/proto.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/proto.c b/lib/proto.c index 3976e98b56d0..f798ece05e32 100644 --- a/lib/proto.c +++ b/lib/proto.c @@ -433,7 +433,7 @@ send_file_cancellation (guestfs_h *g) static int send_file_complete (guestfs_h *g) { - char buf[1]; + char buf[1] = { '\0' }; return send_file_chunk (g, 0, buf, 0); } -- 2.19.1.3.g30247aa5d201
Eric Blake
2021-Oct-12 14:03 UTC
[Libguestfs] [PATCH 2/3] lib/proto: suppress "may be used uninitialized" in send_file_complete()
On Tue, Oct 12, 2021 at 12:36:26AM +0200, Laszlo Ersek wrote:> In order to shut up the compiler, just zero-initialize the buffer -- > that's simpler than adding diagnostics pragmas. The "maybe-uninitialized" > warning is otherwise very useful, so keep it globally enabled (per > WARN_CFLAGS / WERROR_CFLAGS)....> +++ b/lib/proto.c > @@ -433,7 +433,7 @@ send_file_cancellation (guestfs_h *g) > static int > send_file_complete (guestfs_h *g) > { > - char buf[1]; > + char buf[1] = { '\0' }; > return send_file_chunk (g, 0, buf, 0);If it were me writing this, I would have done this to shave typing: char c = '\0'; return send_file_chunk (g, 0, &c, 0); or even abbreviated with: char c = 0; In fact, since send_file_chunk takes a const char *, we could get away with: return send_file_chunk (g, 0, "", 0); But your way is fine, too, and we aren't in a code golf competition. -- Eric Blake, Principal Software Engineer Red Hat, Inc. +1-919-301-3266 Virtualization: qemu.org | libvirt.org