Skip to contentSkip to conversation

compiler: add ignore_value() macro

PATCH In review Felipe Franciosi 2 participants 8 messages Sep 21, 2016 – Nov 18, 2016

lore
Share & tools
lore
#0 lore raw

PATCH 1/2 compiler: add ignore_value() macro

On GCC versions 3.4 and newer, simply using (void) in front of a function that has been declared with WUR will no longer suppress a compilation warning. This commit brings the ignore_value() macro from GNULIB's ignore_value.h, licensed under the terms of LGPLv2+.

See the link below for the original author's comment:
https://lists.nongnu.org/archive/html/qemu-devel/2016-09/msg05148.html

---
 include/qemu/compiler.h | 8 ++++++++
 1 file changed, 8 insertions(+)
include/qemu/compiler.h +8 -0
index 338d3a6..655d0c7 100644
@@ -26,6 +26,14 @@
 #define QEMU_WARN_UNUSED_RESULT
 #endif
 
+/* The ignore_value() macro comes from GNULIB's LGPLv2+ ignore-value.h */
+#if QEMU_GNUC_PREREQ(3, 4)
+# define ignore_value(x) \
+         (__extension__ ({ __typeof__ (x) __x = (x); (void) __x; }))
+#else
+# define ignore_value(x) ((void) (x))
+#endif
+
 #if QEMU_GNUC_PREREQ(4, 0)
 #define QEMU_SENTINEL __attribute__((sentinel))
 #else
Signature · 1 line
1.9.5
#1 lore raw

PATCH 2/2 replay: Ignore the return value of fwrite()

If building with GCC 3.4 or newer (and using -Werror=unused-result), replay-internal.c will fail to compile due to a call to fwrite() where the return value is not used. Since fwrite() is declared with WUR in glibc, callers should check the return value or find other ways to ignore it. The error message in this specific case is:

replay/replay-internal.c: In function ‘replay_put_array’:
replay/replay-internal.c:68:15: error: ignoring return value of ‘fwrite’, declared with attribute warn_unused_result [-Werror=unused-result]
fwrite(buf, 1, size, replay_file);
^

This commit wraps the fwrite() call with the ignore_value() macro, which currently suppresses the error for existing GCC versions.

---
 replay/replay-internal.  | 0
 replay/replay-internal.c | 2 +-
 2 files changed, 1 insertion(+), 1 deletion(-)
 create mode 100644 replay/replay-internal.
replay/replay-internal. new file +0 -0
new file mode 100644
index 0000000..e69de29
replay/replay-internal.c +1 -1
index 5835e8d..61de8f9 100644
@@ -65,7 +65,7 @@ void replay_put_array(const uint8_t *buf, size_t size)
 {
     if (replay_file) {
         replay_put_dword(size);
-        fwrite(buf, 1, size, replay_file);
+        ignore_value(fwrite(buf, 1, size, replay_file));
     }
 }
 
Signature · 1 line
1.9.5
#2 lore raw

Re: [Qemu-devel] [PATCH 2/2] replay: Ignore the return value of fwrite()

Quoted 6 lines · Felipe Franciosi

If building with GCC 3.4 or newer (and using -Werror=unused-result), replay-internal.c will fail to compile due to a call to fwrite() where the return value is not used. Since fwrite() is declared with WUR in glibc, callers should check the return value or find other ways to ignore it. The error message in this specific case is:

Quoted 8 lines

replay/replay-internal.c: In function ‘replay_put_array’:
replay/replay-internal.c:68:15: error: ignoring return value of ‘fwrite’, declared with attribute warn_unused_result [-Werror=unused-result]
fwrite(buf, 1, size, replay_file);
^

This commit wraps the fwrite() call with the ignore_value() macro, which currently suppresses the error for existing GCC versions.

This explains what you did, but not quite why. In other words, convince me that ignoring the error is the right thing to do in the first place...

Quoted 16 lines
---
 replay/replay-internal.  | 0
 replay/replay-internal.c | 2 +-
 2 files changed, 1 insertion(+), 1 deletion(-)
 create mode 100644 replay/replay-internal.
replay/replay-internal. new file +0 -0
new file mode 100644
index 0000000..e69de29
replay/replay-internal.c +0 -0
index 5835e8d..61de8f9 100644

replay/replay-internal.c void replay_put_array(const uint8_t *buf, size_t size) · line 68

Quoted 5 lines
replay/replay-internal.c +1 -1
@@ -65,7 +65,7 @@ void replay_put_array(const uint8_t *buf, size_t size)
 {
     if (replay_file) {
         replay_put_dword(size);
-        fwrite(buf, 1, size, replay_file);
+        ignore_value(fwrite(buf, 1, size, replay_file));

I would be more convinced that this patch is correct if you added a comment here why fwrite() failures can be ignored in this situation, so that someone doesn't undo your commit to add in proper error handling.

Signature · 2 lines
Eric Blake   eblake redhat com    +1-919-301-3266
Libvirt virtualization library http://libvirt.org
#3 lore raw

Re: [Qemu-devel] [PATCH 2/2] replay: Ignore the return value of fwrite()

Quoted 16 lines · Eric Blake
Quoted 12 lines · Felipe Franciosi

If building with GCC 3.4 or newer (and using -Werror=unused-result), replay-internal.c will fail to compile due to a call to fwrite() where the return value is not used. Since fwrite() is declared with WUR in glibc, callers should check the return value or find other ways to ignore it. The error message in this specific case is:

replay/replay-internal.c: In function ‘replay_put_array’:
replay/replay-internal.c:68:15: error: ignoring return value of ‘fwrite’, declared with attribute warn_unused_result [-Werror=unused-result]

            fwrite(buf, 1, size, replay_file);
                  ^
Quoted 5 lines
Quoted 2 lines

This commit wraps the fwrite() call with the ignore_value() macro, which currently suppresses the error for existing GCC versions.

This explains what you did, but not quite why. In other words, convince me that ignoring the error is the right thing to do in the first place...

Sure, that's why I've linked these two in a patch series. :)

Quoted 18 lines
Quoted 18 lines

---
replay/replay-internal. | 0
replay/replay-internal.c | 2 +-

2 files changed, 1 insertion(+), 1 deletion(-)

create mode 100644 replay/replay-internal.

replay/replay-internal. new file +0 -0
new file mode 100644
index 0000000..e69de29
replay/replay-internal.c +0 -0
index 5835e8d..61de8f9 100644
@@ -65,7 +65,7 @@ void replay_put_array(const uint8_t *buf, size_t size)
 {
Quoted 8 lines

replay/replay-internal.c void replay_put_array(const uint8_t *buf, size_t size) · line 68

Quoted 4 lines
replay/replay-internal.c +1 -1
    if (replay_file) {
        replay_put_dword(size);
-        fwrite(buf, 1, size, replay_file);
+        ignore_value(fwrite(buf, 1, size, replay_file));

I would be more convinced that this patch is correct if you added a comment here why fwrite() failures can be ignored in this situation, so that someone doesn't undo your commit to add in proper error handling.

Right, so there are two things to be discussed here:
1) This patch merely fixes the build. It doesn't change the current behaviour.
2) We need to check whether fwrite() succeed.

The real question is: are we happy with 1? Or do we want to go down route 2?

Pavel already flagged that we should probably be checking whether fwrite() succeed. This is something I briefly mentioned in another e-mail [1], but then the direction got changed to use ignore_value() instead.
[1] https://lists.nongnu.org/archive/html/qemu-devel/2016-09/msg05039.html

If we want to go down route 2, we should arguably revisit the whole replay implementation. For instance, other calls around "replay_file" (such as putc, fseek, etc) are all ignored today. Also worth noting, several checks while reading from "replay_file" result in an error_report(), but without any special handling. Maybe the underlying idea is that if you lost the ability to write to (or seek) a file stream, you have bigger problems to worry about and your host is catastrophically failing in another way.

Thoughts?

Felipe

Quoted 5 lines
Signature · 3 lines
-- 
Eric Blake   eblake redhat com    +1-919-301-3266
Libvirt virtualization library http://libvirt.org
Reviewed-by on v1 1/2
#4 lore raw
In reply to Felipe Franciosi
Quoted 5 lines · Felipe Franciosi

On GCC versions 3.4 and newer, simply using (void) in front of a function that has been declared with WUR will no longer suppress a compilation warning. This commit brings the ignore_value() macro from GNULIB's ignore_value.h, licensed under the terms of LGPLv2+.

Quoted 7 lines

See the link below for the original author's comment:
https://lists.nongnu.org/archive/html/qemu-devel/2016-09/msg05148.html

---
 include/qemu/compiler.h | 8 ++++++++
 1 file changed, 8 insertions(+)
Signature · 2 lines
Eric Blake   eblake redhat com    +1-919-301-3266
Libvirt virtualization library http://libvirt.org
#5 lore raw
Quoted 12 lines · Eric Blake
Quoted 8 lines · Felipe Franciosi

On GCC versions 3.4 and newer, simply using (void) in front of a function that has been declared with WUR will no longer suppress a compilation warning. This commit brings the ignore_value() macro from GNULIB's ignore_value.h, licensed under the terms of LGPLv2+.

See the link below for the original author's comment:
https://lists.nongnu.org/archive/html/qemu-devel/2016-09/msg05148.html

Quoted 11 lines
Quoted 4 lines

---
include/qemu/compiler.h | 8 ++++++++

1 file changed, 8 insertions(+)
Signature · 2 lines
Eric Blake   eblake redhat com    +1-919-301-3266
Libvirt virtualization library http://libvirt.org

Hi Eric,

Now that the header and licensing of compiler.h got amended, can we look at this again?

I think the right solution (at least for the moment) is to stick with my series to fix the build. 1/2) We get the ignore_value() macro in.
2/2) We ignore the return value of that fwrite().

As I clarified on my last e-mail in this thread (21/sept), there are many other calls around the replay code which could potentially fail and are all unchecked. We could at least fix the build now and then talk about a separate series to address those, if neccesary.

Thanks,
Felipe

#6 lore raw
Quoted 31 lines · Felipe Franciosi
Quoted 13 lines · Eric Blake
Quoted 8 lines · Felipe Franciosi

On GCC versions 3.4 and newer, simply using (void) in front of a function that has been declared with WUR will no longer suppress a compilation warning. This commit brings the ignore_value() macro from GNULIB's ignore_value.h, licensed under the terms of LGPLv2+.

See the link below for the original author's comment:
https://lists.nongnu.org/archive/html/qemu-devel/2016-09/msg05148.html

Quoted 11 lines
Quoted 4 lines

---
include/qemu/compiler.h | 8 ++++++++

1 file changed, 8 insertions(+)
Signature · 2 lines
Eric Blake   eblake redhat com    +1-919-301-3266
Libvirt virtualization library http://libvirt.org

Hi Eric,

Now that the header and licensing of compiler.h got amended, can we look at this again?

Quoted 8 lines

I think the right solution (at least for the moment) is to stick with my series to fix the build. 1/2) We get the ignore_value() macro in.
2/2) We ignore the return value of that fwrite().

As I clarified on my last e-mail in this thread (21/sept), there are many other calls around the replay code which could potentially fail and are all unchecked. We could at least fix the build now and then talk about a separate series to address those, if neccesary.

Thanks,
Felipe

Ping?

#7 lore raw
Quoted 20 lines · Felipe Franciosi
Quoted 20 lines · Felipe Franciosi
Quoted 18 lines · Eric Blake
Quoted 12 lines · Felipe Franciosi

On GCC versions 3.4 and newer, simply using (void) in front of a function that has been declared with WUR will no longer suppress a compilation warning. This commit brings the ignore_value() macro from GNULIB's ignore_value.h, licensed under the terms of LGPLv2+.

See the link below for the original author's comment:
https://lists.nongnu.org/archive/html/qemu-devel/2016-09/msg05148.html

---
include/qemu/compiler.h | 8 ++++++++

1 file changed, 8 insertions(+)
Quoted 5 lines
Quoted 2 lines
Quoted 2 lines

Ping?

It's missed 2.8, but I still think it is a useful patch; I just commented on another thread about a case where a cast-to-void might look better with the use of ignore_value().

Signature · 2 lines
Eric Blake   eblake redhat com    +1-919-301-3266
Libvirt virtualization library http://libvirt.org