Skip to contentSkip to conversation

build: fix unused-result warnings

PATCH In review Thomas Wood 3 participants 9 messages Sep 7, 2015 – Sep 10, 2015 Library code

lore
Share & tools
lore
#0 lore raw

PATCH 1/2 build: fix unused-result warnings

---
 benchmarks/kms_vblank.c        | 7 ++++---
 debugger/eudb.c                | 4 +++-
 lib/igt_aux.c                  | 8 ++++++--
 overlay/gpu-top.c              | 4 +++-
 overlay/overlay.c              | 4 ++--
 tools/intel_l3_udev_listener.c | 4 ++--
 tools/intel_reg.c              | 3 ++-
 7 files changed, 22 insertions(+), 12 deletions(-)
benchmarks/kms_vblank.c +4 -3
index f9dbe3d..3d2f977 100644
@@ -41,6 +41,7 @@
 #include <drm.h>
 #include <xf86drm.h>
 #include "drmtest.h"
+#include "assert.h"
 
 static double elapsed(const struct timespec *start,
 		      const struct timespec *end,
@@ -89,7 +90,7 @@ static void vblank_query(int fd, int busy)
 
 	printf("%f\n", 1e6/elapsed(&start, &end, count));
 	if (busy)
-		read(fd, &event, sizeof(event));
+		assert(read(fd, &event, sizeof(event)) != -1);
 }
 
 static void vblank_event(int fd, int busy)
@@ -118,14 +119,14 @@ static void vblank_event(int fd, int busy)
 		vbl.request.sequence = 0;
 		drmIoctl(fd, DRM_IOCTL_WAIT_VBLANK, &vbl);
 
-		read(fd, &event, sizeof(event));
+		assert(read(fd, &event, sizeof(event)) != -1);
 		count++;
 	} while ((event.sequence - seq) <= 120);
 	clock_gettime(CLOCK_MONOTONIC, &end);
 
 	printf("%f\n", 1e6/elapsed(&start, &end, count));
 	if (busy)
-		read(fd, &event, sizeof(event));
+		assert(read(fd, &event, sizeof(event)) != -1);
 }
 
 int main(int argc, char **argv)
debugger/eudb.c +3 -1
index 39c5cca..275a27e 100644
@@ -147,7 +147,9 @@ dump_debug(void *buf, size_t count) {
 	if (!debug_fd)
 		debug_fd = open(debug_file, O_CREAT | O_WRONLY | O_TRUNC, S_IRWXO);
 
-	write(debug_fd, buf, count);
+	if (write(debug_fd, buf, count) == -1)
+		fprintf(stderr, "Error writing to debug file: %s\n",
+			strerror(errno));
 }
 
 static volatile void *
lib/igt_aux.c +6 -2
index c26d167..e77392c 100644
@@ -767,8 +767,12 @@ static void igt_module_param_exit_handler(int sig)
 
 		fd = open(file_path, O_RDWR);
 		if (fd >= 0) {
-			write(fd, data->original_value,
-			      strlen(data->original_value));
+			int size = strlen (data->original_value);
+
+			if (size != write(fd, data->original_value, size))
+				igt_warn("%s may not have been reset to its"
+					 " original value\n", file_path);
+
 			close(fd);
 		}
 	}
overlay/gpu-top.c +3 -1
index d1f5ec8..4097cd0 100644
@@ -29,6 +29,7 @@
 #include <unistd.h>
 #include <fcntl.h>
 #include <errno.h>
+#include <assert.h>
 
 #include "perf.h"
 #include "igfx.h"
@@ -260,7 +261,8 @@ static void mmio_init(struct gpu_top *gt)
 		mmio_ring_emit(&render_ring, 1000, payload);
 		mmio_ring_emit(&bsd_ring, 1000, payload);
 		mmio_ring_emit(&blt_ring, 1000, payload);
-		write(fd[1], payload, sizeof(payload));
+		assert(write(fd[1], payload, sizeof(payload))
+		       == sizeof(payload));
 	}
 }
 
overlay/overlay.c +2 -2
index 035e02c..3c0dbb4 100644
@@ -910,8 +910,8 @@ int main(int argc, char **argv)
 	if (daemonize && daemon(0, 0))
 		return EINVAL;
 
-	if (renice)
-		nice(renice);
+	if (renice && (nice(renice) == -1))
+		fprintf(stderr, "Could not renice: %s\n", strerror(errno));
 
 	signal(SIGUSR1, signal_snapshot);
 
tools/intel_l3_udev_listener.c +2 -2
index 261630e..0b94c1c 100644
@@ -113,10 +113,10 @@ again:
 
 	udev_device_unref(udev_dev);
 
-	asprintf(&err_msg, "Parity error detected on: %d,%d,%d,%d. "
+	assert(asprintf(&err_msg, "Parity error detected on: %d,%d,%d,%d. "
 			"Try to run intel_l3_parity -r %d -b %d -s %d -w %d -d",
 			loc->slice, loc->row, loc->bank, loc->subbank,
-			loc->row, loc->bank, loc->subbank, loc->slice);
+			loc->row, loc->bank, loc->subbank, loc->slice) != -1);
 	if (daemon) {
 		syslog(LOG_INFO, "%s\n", err_msg);
 		goto again;
tools/intel_reg.c +2 -1
index 95760db..bef3287 100644
@@ -506,7 +506,8 @@ static int intel_reg_snapshot(struct config *config, int argc, char *argv[])
 	intel_mmio_use_pci_bar(config->pci_dev);
 
 	/* XXX: error handling */
-	write(1, igt_global_mmio, config->pci_dev->regions[mmio_bar].size);
+	if (write(1, igt_global_mmio, config->pci_dev->regions[mmio_bar].size) == -1)
+		fprintf(stderr, "Error writing snapshot: %s", strerror(errno));
 
 	if (config->verbosity > 0)
 		printf("use this with --mmio=FILE --devid=0x%04X\n",
Signature · 6 lines
1.9.1

_______________________________________________
Intel-gfx mailing list
[email protected]
http://lists.freedesktop.org/mailman/listinfo/intel-gfx
#1 lore raw

PATCH 2/2 tests/gem_pwrite_snooped: fix const cast warning

---
 tests/gem_pwrite_snooped.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
tests/gem_pwrite_snooped.c +2 -2
index d3f6223..29142c3 100644
@@ -76,14 +76,14 @@ static void blit(drm_intel_bo *dst, drm_intel_bo *src,
 	intel_batchbuffer_free(batch);
 }
 
-static void *memchr_inv(const void *s, int c, size_t n)
+static void const *memchr_inv(const void *s, int c, size_t n)
 {
 	const unsigned char *us = s;
 	unsigned char uc = c;
 
 	while (n--) {
 		if (*us != uc)
-			return (void *) us;
+			return (void const *) us;
 		us++;
 	}
 
Signature · 6 lines
1.9.1

_______________________________________________
Intel-gfx mailing list
[email protected]
http://lists.freedesktop.org/mailman/listinfo/intel-gfx
#2 lore raw

Re: [PATCH i-g-t 2/2] tests/gem_pwrite_snooped: fix const cast warning

Quoted 10 lines · Thomas Wood
---
 tests/gem_pwrite_snooped.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
tests/gem_pwrite_snooped.c +0 -0
index d3f6223..29142c3 100644

tests/gem_pwrite_snooped.c static void blit(drm_intel_bo *dst, drm_intel_bo *src, · line 79

Quoted 5 lines
tests/gem_pwrite_snooped.c +1 -1
@@ -76,14 +76,14 @@ static void blit(drm_intel_bo *dst, drm_intel_bo *src,
 	intel_batchbuffer_free(batch);
 }
 
-static void *memchr_inv(const void *s, int c, size_t n)
+static void const *memchr_inv(const void *s, int c, size_t n)

const void *
is the usual way to write that

tests/gem_pwrite_snooped.c static void blit(drm_intel_bo *dst, drm_intel_bo *src, · line 86

Quoted 8 lines
tests/gem_pwrite_snooped.c +1 -1
 {
 	const unsigned char *us = s;
 	unsigned char uc = c;
 
 	while (n--) {
 		if (*us != uc)
-			return (void *) us;
+			return (void const *) us;

No need for the cast if you make it return const void *.

Quoted 10 lines
tests/gem_pwrite_snooped.c +0 -0
 		us++;
 	}
 
Signature · 6 lines
1.9.1

_______________________________________________
Intel-gfx mailing list
[email protected]
http://lists.freedesktop.org/mailman/listinfo/intel-gfx
Signature · 6 lines
Ville Syrjälä
Intel OTC
_______________________________________________
Intel-gfx mailing list
[email protected]
http://lists.freedesktop.org/mailman/listinfo/intel-gfx
#3 lore raw

Re: [PATCH i-g-t 2/2] tests/gem_pwrite_snooped: fix const cast warning

In reply to Thomas Wood
Quoted 10 lines · Thomas Wood
---
 tests/gem_pwrite_snooped.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
tests/gem_pwrite_snooped.c +0 -0
index d3f6223..29142c3 100644

tests/gem_pwrite_snooped.c static void blit(drm_intel_bo *dst, drm_intel_bo *src, · line 79

Quoted 5 lines
tests/gem_pwrite_snooped.c +1 -1
@@ -76,14 +76,14 @@ static void blit(drm_intel_bo *dst, drm_intel_bo *src,
 	intel_batchbuffer_free(batch);
 }
 
-static void *memchr_inv(const void *s, int c, size_t n)
+static void const *memchr_inv(const void *s, int c, size_t n)

Oh, and I'll just note that I wrote it the way I did orignally so that it matches memchr(). But I suppose matching memchr() exactly is not all that important.

Quoted 18 lines
tests/gem_pwrite_snooped.c +1 -1
 {
 	const unsigned char *us = s;
 	unsigned char uc = c;
 
 	while (n--) {
 		if (*us != uc)
-			return (void *) us;
+			return (void const *) us;
 		us++;
 	}
 
Signature · 6 lines
1.9.1

_______________________________________________
Intel-gfx mailing list
[email protected]
http://lists.freedesktop.org/mailman/listinfo/intel-gfx
Signature · 6 lines
Ville Syrjälä
Intel OTC
_______________________________________________
Intel-gfx mailing list
[email protected]
http://lists.freedesktop.org/mailman/listinfo/intel-gfx
#4 lore raw

Re: [PATCH i-g-t 2/2] tests/gem_pwrite_snooped: fix const cast warning

Quoted 14 lines · Ville Syrjälä
Quoted 13 lines · Thomas Wood
---
 tests/gem_pwrite_snooped.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
tests/gem_pwrite_snooped.c +0 -0
index d3f6223..29142c3 100644
@@ -76,14 +76,14 @@ static void blit(drm_intel_bo *dst, drm_intel_bo *src,
      intel_batchbuffer_free(batch);
 }
 
Quoted 6 lines

tests/gem_pwrite_snooped.c static void blit(drm_intel_bo *dst, drm_intel_bo *src, · line 79

Quoted 2 lines
tests/gem_pwrite_snooped.c +1 -1
-static void *memchr_inv(const void *s, int c, size_t n)
+static void const *memchr_inv(const void *s, int c, size_t n)

Oh, and I'll just note that I wrote it the way I did orignally so that it matches memchr(). But I suppose matching memchr() exactly is not all that important.

It may actually be more useful to remove const from the function parameter rather than add it to the return type. Other than adding or removing the const annotations, there doesn't seem to be any other way of avoiding the warning from -Wcast-qual in this case.

Quoted 15 lines
Quoted 15 lines
tests/gem_pwrite_snooped.c +1 -2
 {
      const unsigned char *us = s;
      unsigned char uc = c;
 
      while (n--) {
              if (*us != uc)
-                     return (void *) us;
+                     return (void const *) us;
              us++;
      }
 
--

1.9.1

Quoted 8 lines
Quoted 4 lines

_______________________________________________
Intel-gfx mailing list
[email protected]
http://lists.freedesktop.org/mailman/listinfo/intel-gfx

--
Ville Syrjälä
Intel OTC

_______________________________________________
Intel-gfx mailing list
[email protected]
http://lists.freedesktop.org/mailman/listinfo/intel-gfx

#5 lore raw

Re: [PATCH i-g-t 2/2] tests/gem_pwrite_snooped: fix const cast warning

Quoted 23 lines · Thomas Wood
Quoted 14 lines · Ville Syrjälä
Quoted 13 lines · Thomas Wood
---
 tests/gem_pwrite_snooped.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
tests/gem_pwrite_snooped.c +0 -0
index d3f6223..29142c3 100644
@@ -76,14 +76,14 @@ static void blit(drm_intel_bo *dst, drm_intel_bo *src,
      intel_batchbuffer_free(batch);
 }
 
Quoted 6 lines

tests/gem_pwrite_snooped.c static void blit(drm_intel_bo *dst, drm_intel_bo *src, · line 79

Quoted 2 lines
tests/gem_pwrite_snooped.c +1 -1
-static void *memchr_inv(const void *s, int c, size_t n)
+static void const *memchr_inv(const void *s, int c, size_t n)

Oh, and I'll just note that I wrote it the way I did orignally so that it matches memchr(). But I suppose matching memchr() exactly is not all that important.

Quoted 2 lines

It may actually be more useful to remove const from the function parameter rather than add it to the return type.

The function doesn't change the data, so passing in const definitely makes sense. I guess it could also use __attribute__((pure))

Quoted 3 lines

Other than adding or
removing the const annotations, there doesn't seem to be any other way of avoiding the warning from -Wcast-qual in this case.

Would be nice if we could have const and non-const versions of the function, so that the constness of the returned type depends on what gets passed in. But this is C and not C++ so we can't have that. And even in C++ you would then have to make the function argument to non-const as well, which loses the information that it doesn't change the data.

Quoted 25 lines
Quoted 25 lines
Quoted 17 lines
tests/gem_pwrite_snooped.c +1 -2
 {
      const unsigned char *us = s;
      unsigned char uc = c;
 
      while (n--) {
              if (*us != uc)
-                     return (void *) us;
+                     return (void const *) us;
              us++;
      }
 
--

1.9.1

Quoted 4 lines

_______________________________________________
Intel-gfx mailing list
[email protected]
http://lists.freedesktop.org/mailman/listinfo/intel-gfx

--
Ville Syrjälä
Intel OTC

Signature · 6 lines
Ville Syrjälä
Intel OTC
_______________________________________________
Intel-gfx mailing list
[email protected]
http://lists.freedesktop.org/mailman/listinfo/intel-gfx
#6 lore raw

PATCH 1/1 tests/gem_pwrite_snooped: disable const cast warning

Disable -Wcast-qual temporarily to allow memchr_inv to return non-const data (similar to memchr), without causing a compiler warning.

Cc Ville Syrjälä <[email protected]>
---
 tests/gem_pwrite_snooped.c | 3 +++
 1 file changed, 3 insertions(+)
tests/gem_pwrite_snooped.c +3 -0
index d3f6223..3bc4bad 100644
@@ -83,7 +83,10 @@ static void *memchr_inv(const void *s, int c, size_t n)
 
 	while (n--) {
 		if (*us != uc)
+#pragma GCC diagnostic push
+#pragma GCC diagnostic ignored "-Wcast-qual"
 			return (void *) us;
+#pragma GCC diagnostic pop
 		us++;
 	}
 
Signature · 6 lines
1.9.1

_______________________________________________
Intel-gfx mailing list
[email protected]
http://lists.freedesktop.org/mailman/listinfo/intel-gfx
#7 lore raw
In reply to Thomas Wood

Em Seg, 2015-09-07 às 12:34 +0100, Thomas Wood escreveu:

Quoted 83 lines
---
 benchmarks/kms_vblank.c        | 7 ++++---
 debugger/eudb.c                | 4 +++-
 lib/igt_aux.c                  | 8 ++++++--
 overlay/gpu-top.c              | 4 +++-
 overlay/overlay.c              | 4 ++--
 tools/intel_l3_udev_listener.c | 4 ++--
 tools/intel_reg.c              | 3 ++-
 7 files changed, 22 insertions(+), 12 deletions(-)
benchmarks/kms_vblank.c +4 -3
index f9dbe3d..3d2f977 100644
@@ -41,6 +41,7 @@
 #include <drm.h>
 #include <xf86drm.h>
 #include "drmtest.h"
+#include "assert.h"
 
 static double elapsed(const struct timespec *start,
 		      const struct timespec *end,
@@ -89,7 +90,7 @@ static void vblank_query(int fd, int busy)
 
 	printf("%f\n", 1e6/elapsed(&start, &end, count));
 	if (busy)
-		read(fd, &event, sizeof(event));
+		assert(read(fd, &event, sizeof(event)) != -1);
 }
 
 static void vblank_event(int fd, int busy)
@@ -118,14 +119,14 @@ static void vblank_event(int fd, int busy)
 		vbl.request.sequence = 0;
 		drmIoctl(fd, DRM_IOCTL_WAIT_VBLANK, &vbl);
 
-		read(fd, &event, sizeof(event));
+		assert(read(fd, &event, sizeof(event)) != -1);
 		count++;
 	} while ((event.sequence - seq) <= 120);
 	clock_gettime(CLOCK_MONOTONIC, &end);
 
 	printf("%f\n", 1e6/elapsed(&start, &end, count));
 	if (busy)
-		read(fd, &event, sizeof(event));
+		assert(read(fd, &event, sizeof(event)) != -1);
 }
 
 int main(int argc, char **argv)
debugger/eudb.c +3 -1
index 39c5cca..275a27e 100644
@@ -147,7 +147,9 @@ dump_debug(void *buf, size_t count) {
 	if (!debug_fd)
 		debug_fd = open(debug_file, O_CREAT | O_WRONLY | 
 O_TRUNC, S_IRWXO);
 
-	write(debug_fd, buf, count);
+	if (write(debug_fd, buf, count) == -1)
+		fprintf(stderr, "Error writing to debug file: %s\n",
+			strerror(errno));
 }
 

static volatile void *

lib/igt_aux.c +4 -2
index c26d167..e77392c 100644
@@ -767,8 +767,12 @@ static void igt_module_param_exit_handler(int 
 sig)
 
 		fd = open(file_path, O_RDWR);
 		if (fd >= 0) {
-			write(fd, data->original_value,
-			      strlen(data->original_value));
+			int size = strlen (data->original_value);
+
+			if (size != write(fd, data->original_value, 
 size))
+				igt_warn("%s may not have been reset 

to its"

lib/igt_aux.c +1 -0
+					 " original value\n", 
Quoted 2 lines

file_path);
+

Can't this cause problems due to the fact that we're in an exit handler and igt_warn() doesn't seem to be Async-Signal-Safe?

/me remembers the malloc locking problem we had in the past with the connector exit handler (bug 83498)

Quoted 80 lines
lib/igt_aux.c +0 -0
 			close(fd);
 		}
 	}
overlay/gpu-top.c +3 -1
index d1f5ec8..4097cd0 100644
@@ -29,6 +29,7 @@
 #include <unistd.h>
 #include <fcntl.h>
 #include <errno.h>
+#include <assert.h>
 
 #include "perf.h"
 #include "igfx.h"
@@ -260,7 +261,8 @@ static void mmio_init(struct gpu_top *gt)
 		mmio_ring_emit(&render_ring, 1000, payload);
 		mmio_ring_emit(&bsd_ring, 1000, payload);
 		mmio_ring_emit(&blt_ring, 1000, payload);
-		write(fd[1], payload, sizeof(payload));
+		assert(write(fd[1], payload, sizeof(payload))
+		       == sizeof(payload));
 	}
 }
 
overlay/overlay.c +2 -2
index 035e02c..3c0dbb4 100644
@@ -910,8 +910,8 @@ int main(int argc, char **argv)
 	if (daemonize && daemon(0, 0))
 		return EINVAL;
 
-	if (renice)
-		nice(renice);
+	if (renice && (nice(renice) == -1))
+		fprintf(stderr, "Could not renice: %s\n", 
 strerror(errno));
 
 	signal(SIGUSR1, signal_snapshot);
tools/intel_l3_udev_listener.c +1 -1
index 261630e..0b94c1c 100644
@@ -113,10 +113,10 @@ again:
 
 	udev_device_unref(udev_dev);
 
-	asprintf(&err_msg, "Parity error detected on: %d,%d,%d,%d. "
+	assert(asprintf(&err_msg, "Parity error detected on: 

%d,%d,%d,%d. "
"Try to run intel_l3_parity -r %d -b %d -s
%d -w %d -d",

tools/intel_l3_udev_listener.c +1 -4
 			loc->slice, loc->row, loc->bank, loc
->subbank,
-			loc->row, loc->bank, loc->subbank, loc
->slice);
+			loc->row, loc->bank, loc->subbank, loc
->slice) != -1);
 	if (daemon) {
 		syslog(LOG_INFO, "%s\n", err_msg);
 		goto again;
tools/intel_reg.c +2 -3
index 95760db..bef3287 100644
@@ -506,7 +506,8 @@ static int intel_reg_snapshot(struct config 
 *config, int argc, char *argv[])
 	intel_mmio_use_pci_bar(config->pci_dev);
 
 	/* XXX: error handling */
-	write(1, igt_global_mmio, config->pci_dev
->regions[mmio_bar].size);
+	if (write(1, igt_global_mmio, config->pci_dev
->regions[mmio_bar].size) == -1)
+		fprintf(stderr, "Error writing snapshot: %s", 

strerror(errno));

tools/intel_reg.c static int intel_reg_snapshot(struct config *config, int argc, char *argv[]) · line 513

Quoted 2 lines
tools/intel_reg.c +0 -0
 	if (config->verbosity > 0)
 		printf("use this with --mmio=FILE --devid=0x%04X\n",

_______________________________________________
Intel-gfx mailing list
[email protected]
http://lists.freedesktop.org/mailman/listinfo/intel-gfx

#8 lore raw

PATCH 1/1 lib: don't use igt_warn in signal handlers

igt_wan and other log functions are not async-signal safe, so should not be used in signal handlers.

---
 lib/igt_aux.c | 11 ++++++++---
 1 file changed, 8 insertions(+), 3 deletions(-)
lib/igt_aux.c +8 -3
index e77392c..04ca25b 100644
@@ -47,6 +47,7 @@
 #include <sys/syscall.h>
 #include <sys/utsname.h>
 #include <termios.h>
+#include <assert.h>
 
 #include "drmtest.h"
 #include "i915_drm.h"
@@ -769,9 +770,13 @@ static void igt_module_param_exit_handler(int sig)
 		if (fd >= 0) {
 			int size = strlen (data->original_value);
 
-			if (size != write(fd, data->original_value, size))
-				igt_warn("%s may not have been reset to its"
-					 " original value\n", file_path);
+			if (size != write(fd, data->original_value, size)) {
+				const char msg[] = "WARNING: Module parameters "
+					"may not have been reset to their "
+					"original values\n";
+				assert(write(STDERR_FILENO, msg, sizeof(msg))
+				       == sizeof(msg));
+			}
 
 			close(fd);
 		}
Signature · 6 lines
1.9.1

_______________________________________________
Intel-gfx mailing list
[email protected]
http://lists.freedesktop.org/mailman/listinfo/intel-gfx