[igt-dev] [PATCH i-g-t] tools/i915-perf: Fix compiler warning

Petri Latvala petri.latvala at intel.com
Thu Feb 20 13:12:45 UTC 2020


Use flexible array member in the first struct instead of two structs
and a 0-length array so compiler knows we really meant to read and
write past it.

Signed-off-by: Petri Latvala <petri.latvala at intel.com>
Cc: Lionel Landwerlin <lionel.g.landwerlin at intel.com>
---
 tools/i915-perf/i915_perf_control.c           | 24 +++++++------------
 tools/i915-perf/i915_perf_recorder.c          |  7 ++++--
 tools/i915-perf/i915_perf_recorder_commands.h |  5 +---
 3 files changed, 15 insertions(+), 21 deletions(-)

diff --git a/tools/i915-perf/i915_perf_control.c b/tools/i915-perf/i915_perf_control.c
index a8d0d30f..3722f2b1 100644
--- a/tools/i915-perf/i915_perf_control.c
+++ b/tools/i915-perf/i915_perf_control.c
@@ -91,28 +91,22 @@ main(int argc, char *argv[])
 		if (dump_file[0] == '/') {
 			uint32_t total_len =
 				sizeof(struct recorder_command_base) + strlen(dump_file) + 1;
-			struct {
-				struct recorder_command_base base;
-				struct recorder_command_dump dump;
-			} *data = malloc(total_len);
+			struct recorder_command_base *data = malloc(total_len);
 
-			data->base.command = RECORDER_COMMAND_DUMP;
-			data->base.size = total_len;
-			snprintf((char *) data->dump.path, strlen(dump_file) + 1, "%s", dump_file);
+			data->command = RECORDER_COMMAND_DUMP;
+			data->size = total_len;
+			snprintf((char *) data->path, strlen(dump_file) + 1, "%s", dump_file);
 
 			fwrite(data, total_len, 1, command_fifo_file);
 		} else {
 			char *cwd = get_current_dir_name();
 			uint32_t path_len = strlen(cwd) + 1 + strlen(dump_file) + 1;
 			uint32_t total_len = sizeof(struct recorder_command_base) + path_len;
-			struct {
-				struct recorder_command_base base;
-				struct recorder_command_dump dump;
-			} *data = malloc(total_len);
-
-			data->base.command = RECORDER_COMMAND_DUMP;
-			data->base.size = total_len;
-			snprintf((char *) data->dump.path, path_len, "%s/%s", cwd, dump_file);
+			struct recorder_command_base *data = malloc(total_len);
+
+			data->command = RECORDER_COMMAND_DUMP;
+			data->size = total_len;
+			snprintf((char *) data->path, path_len, "%s/%s", cwd, dump_file);
 
 			fwrite(data, total_len, 1, command_fifo_file);
 		}
diff --git a/tools/i915-perf/i915_perf_recorder.c b/tools/i915-perf/i915_perf_recorder.c
index 760cabf1..bd477746 100644
--- a/tools/i915-perf/i915_perf_recorder.c
+++ b/tools/i915-perf/i915_perf_recorder.c
@@ -605,12 +605,15 @@ read_command_file(struct recording_context *ctx)
 	switch (header.command) {
 	case RECORDER_COMMAND_DUMP: {
 		uint32_t len = header.size - sizeof(header), offset = 0;
-		struct recorder_command_dump *dump = malloc(len);
+		struct recorder_command_base *dump = malloc(sizeof(header) + len);
 		FILE *file;
 
+		/* Not really needed since current code only accesses dump->path but for completeness... */
+		memcpy(dump, &header, sizeof(header));
+
 		while (offset < len &&
 		       ((ret = read(ctx->command_fifo_fd,
-				    (void *) dump + offset, len - offset)) > 0
+				    (void *) dump->path + offset, len - offset)) > 0
 			|| errno == EAGAIN)) {
 			if (ret > 0)
 				offset += ret;
diff --git a/tools/i915-perf/i915_perf_recorder_commands.h b/tools/i915-perf/i915_perf_recorder_commands.h
index 4855d80f..5d84ca82 100644
--- a/tools/i915-perf/i915_perf_recorder_commands.h
+++ b/tools/i915-perf/i915_perf_recorder_commands.h
@@ -32,8 +32,5 @@ enum recorder_command {
 struct recorder_command_base {
 	uint32_t command;
 	uint32_t size;
-};
-
-struct recorder_command_dump {
-	uint8_t path[0];
+	uint8_t path[];
 };
-- 
2.20.1



More information about the igt-dev mailing list