[PATCH i-g-t] C files: fix calloc calls with inverted arguments

Mauro Carvalho Chehab mauro.chehab at linux.intel.com
Wed Apr 10 13:07:06 UTC 2024


From: Mauro Carvalho Chehab <mchehab at kernel.org>

The new gcc version 14 now complains when calloc is called
with inverted arguments:

	../benchmarks/gem_exec_reloc.c:85:31: warning: ‘calloc’ sizes specified with ‘sizeof’ in the earlier argument and not in the later argument [-Wcalloc-transposed-args]
	   85 |         target = calloc(sizeof(*target), num_relocs);
	      |                               ^
	../benchmarks/gem_exec_reloc.c:85:31: note: earlier argument should specify number of elements, later size of each element

Replace all occurrences of calloc that were warned on gcc 14 by
placing the arguments at the right order.

Logic fixed using this small python script, written specifically
to catch gcc calloc warnings:

    #!/usr/bin/env python3
    import re
    warnings = [
    	"lib/igt_kms.c:2781",
    	"lib/igt_kms.c:2809",
    	"lib/igt_kms.c:2858",
    	"lib/igt_chamelium.c:156",
    	"lib/igt_chamelium.c:1519",
    	"tests/kms_atomic_transition.c:483",
    	"tests/kms_atomic_transition.c:485",
    	"tests/kms_atomic_transition.c:487",
    	"tests/kms_flip.c:426",
    	"tests/kms_flip.c:427",
    	"tests/intel/gem_exec_alignment.c:204",
    	"tests/intel/gem_exec_alignment.c:409",
    	"tests/intel/gem_exec_fair.c:1121",
    	"tests/intel/gem_exec_fair.c:1122",
    	"tests/intel/gem_fence_thrash.c:153",
    	"tests/intel/gem_fence_thrash.c:234",
    	"tests/intel/gem_ppgtt.c:432",
    	"tests/intel/gem_ppgtt.c:459",
    	"tests/intel/gem_render_tiled_blits.c:152",
    	"tests/intel/gem_userptr_blits.c:1433",
    	"tests/chamelium/kms_chamelium_frames.c:943",
    	"tests/amdgpu/amd_multidisplay_modeset.c:242",
    	"benchmarks/gem_exec_reloc.c:83",
    	"benchmarks/gem_exec_reloc.c:84",
    	"benchmarks/gem_exec_reloc.c:85",
    	"benchmarks/gem_latency.c:196",
    	"assembler/main.c:400",
    	"assembler/gram.y:219",
    	"assembler/gram.y:231",
    	"assembler/gram.y:242",
    ]
    split_file = re.compile(r"([^\:]+):(\d+)")
    calloc = re.compile(r"(calloc\s*\()(.*)\,\s*([\w\d\ \+\*\-\>]+)(\))")
    for f in warnings:
        match = split_file.match(f)
        if not match:
            continue
        fname = match.group(1)
        line_number = int(match.group(2))
        new = ""
        with open(fname, 'r', encoding = 'utf8') as fp:
            for i, line in enumerate(fp):
                ln = i + 1
                if ln == line_number:
                    new_line = calloc.sub(r"\1\3, \2\4", line)
                    if new_line != line:
                        line = new_line
                    else:
                        print(f"{ln}: FAILED: {line}")
                new += line
        with open(fname, 'w', encoding = 'utf8') as fp:
            fp.write(new)

Signed-off-by: Mauro Carvalho Chehab <mchehab at kernel.org>
---
 assembler/gram.y                        | 6 +++---
 assembler/main.c                        | 2 +-
 benchmarks/gem_exec_reloc.c             | 6 +++---
 benchmarks/gem_latency.c                | 2 +-
 lib/igt_chamelium.c                     | 4 ++--
 lib/igt_kms.c                           | 6 +++---
 tests/amdgpu/amd_multidisplay_modeset.c | 2 +-
 tests/chamelium/kms_chamelium_frames.c  | 2 +-
 tests/intel/gem_exec_alignment.c        | 4 ++--
 tests/intel/gem_exec_fair.c             | 4 ++--
 tests/intel/gem_fence_thrash.c          | 4 ++--
 tests/intel/gem_ppgtt.c                 | 4 ++--
 tests/intel/gem_render_tiled_blits.c    | 2 +-
 tests/intel/gem_userptr_blits.c         | 2 +-
 tests/kms_atomic_transition.c           | 6 +++---
 tests/kms_flip.c                        | 4 ++--
 16 files changed, 30 insertions(+), 30 deletions(-)

diff --git a/assembler/gram.y b/assembler/gram.y
index 77975e072896..79873b222253 100644
--- a/assembler/gram.y
+++ b/assembler/gram.y
@@ -216,7 +216,7 @@ brw_program_add_instruction(struct brw_program *p,
 {
     struct brw_program_instruction *list_entry;
 
-    list_entry = calloc(sizeof(struct brw_program_instruction), 1);
+    list_entry = calloc(1, sizeof(struct brw_program_instruction));
     list_entry->type = GEN4ASM_INSTRUCTION_GEN;
     list_entry->insn.gen = instruction->insn.gen;
     brw_program_append_entry(p, list_entry);
@@ -228,7 +228,7 @@ brw_program_add_relocatable(struct brw_program *p,
 {
     struct brw_program_instruction *list_entry;
 
-    list_entry = calloc(sizeof(struct brw_program_instruction), 1);
+    list_entry = calloc(1, sizeof(struct brw_program_instruction));
     list_entry->type = GEN4ASM_INSTRUCTION_GEN_RELOCATABLE;
     list_entry->insn.gen = instruction->insn.gen;
     list_entry->reloc = instruction->reloc;
@@ -239,7 +239,7 @@ static void brw_program_add_label(struct brw_program *p, const char *label)
 {
     struct brw_program_instruction *list_entry;
 
-    list_entry = calloc(sizeof(struct brw_program_instruction), 1);
+    list_entry = calloc(1, sizeof(struct brw_program_instruction));
     list_entry->type = GEN4ASM_INSTRUCTION_LABEL;
     list_entry->insn.label.name = strdup(label);
     brw_program_append_entry(p, list_entry);
diff --git a/assembler/main.c b/assembler/main.c
index 2d39d4536a7f..77cc98b80d21 100644
--- a/assembler/main.c
+++ b/assembler/main.c
@@ -397,7 +397,7 @@ int main(int argc, char **argv)
 	    if (entry1 && is_label(entry1) && is_entry_point(entry1)) {
 		// insert NOP instructions until (inst_offset+1) % 4 == 0
 		while (((inst_offset+1) % 4) != 0) {
-		    tmp_entry = calloc(sizeof(*tmp_entry), 1);
+		    tmp_entry = calloc(1, sizeof(*tmp_entry));
 		    tmp_entry->insn.gen.header.opcode = BRW_OPCODE_NOP;
 		    entry->next = tmp_entry;
 		    tmp_entry->next = entry1;
diff --git a/benchmarks/gem_exec_reloc.c b/benchmarks/gem_exec_reloc.c
index dadc064f0ca4..0610308669f2 100644
--- a/benchmarks/gem_exec_reloc.c
+++ b/benchmarks/gem_exec_reloc.c
@@ -80,9 +80,9 @@ static int run(unsigned batch_size,
 	struct drm_i915_gem_relocation_entry *mem_reloc = NULL;
 	int *target;
 
-	gem_exec = calloc(sizeof(*gem_exec), num_objects + 1);
-	mem_reloc = calloc(sizeof(*mem_reloc), num_relocs);
-	target = calloc(sizeof(*target), num_relocs);
+	gem_exec = calloc(num_objects + 1, sizeof(*gem_exec));
+	mem_reloc = calloc(num_relocs, sizeof(*mem_reloc));
+	target = calloc(num_relocs, sizeof(*target));
 
 	fd = drm_open_driver(DRIVER_INTEL);
 
diff --git a/benchmarks/gem_latency.c b/benchmarks/gem_latency.c
index cc8b3de55df3..6abf366f9285 100644
--- a/benchmarks/gem_latency.c
+++ b/benchmarks/gem_latency.c
@@ -193,7 +193,7 @@ static void setup_workload(struct producer *p, int gen,
 	struct drm_i915_gem_relocation_entry *reloc;
 	int offset;
 
-	reloc = calloc(sizeof(*reloc), 2*factor);
+	reloc = calloc(2*factor, sizeof(*reloc));
 
 	p->workload_dispatch.exec[0].handle = scratch;
 	p->workload_dispatch.exec[1].relocation_count = 2*factor;
diff --git a/lib/igt_chamelium.c b/lib/igt_chamelium.c
index 881e1892cf87..016d5356630c 100644
--- a/lib/igt_chamelium.c
+++ b/lib/igt_chamelium.c
@@ -153,7 +153,7 @@ struct chamelium_port **chamelium_get_ports(struct chamelium *chamelium,
 {
 	int i;
 	struct chamelium_port **ret =
-		calloc(sizeof(void*), chamelium->port_count);
+		calloc(chamelium->port_count, sizeof(void*));
 
 	*count = chamelium->port_count;
 	for (i = 0; i < chamelium->port_count; i++)
@@ -1516,7 +1516,7 @@ igt_crc_t *chamelium_read_captured_crcs(struct chamelium *chamelium,
 	res = chamelium_rpc(chamelium, NULL, "GetCapturedChecksums", "(in)", 0);
 
 	*frame_count = xmlrpc_array_size(&chamelium->env, res);
-	ret = calloc(sizeof(igt_crc_t), *frame_count);
+	ret = calloc(*frame_count, sizeof(igt_crc_t));
 
 	for (i = 0; i < *frame_count; i++) {
 		xmlrpc_array_read_item(&chamelium->env, res, i, &elem);
diff --git a/lib/igt_kms.c b/lib/igt_kms.c
index 2a518eb8d936..60339565a192 100644
--- a/lib/igt_kms.c
+++ b/lib/igt_kms.c
@@ -2778,7 +2778,7 @@ void igt_display_require(igt_display_t *display, int drm_fd)
 		     resources->count_crtcs, IGT_MAX_PIPES);
 
 	display->n_pipes = IGT_MAX_PIPES;
-	display->pipes = calloc(sizeof(igt_pipe_t), display->n_pipes);
+	display->pipes = calloc(display->n_pipes, sizeof(igt_pipe_t));
 	igt_assert_f(display->pipes, "Failed to allocate memory for %d pipes\n", display->n_pipes);
 
 	for (i = 0; i < resources->count_crtcs; i++) {
@@ -2806,7 +2806,7 @@ void igt_display_require(igt_display_t *display, int drm_fd)
 	igt_assert(plane_resources);
 
 	display->n_planes = plane_resources->count_planes;
-	display->planes = calloc(sizeof(igt_plane_t), display->n_planes);
+	display->planes = calloc(display->n_planes, sizeof(igt_plane_t));
 	igt_assert_f(display->planes, "Failed to allocate memory for %d planes\n", display->n_planes);
 
 	for (i = 0; i < plane_resources->count_planes; ++i) {
@@ -2855,7 +2855,7 @@ void igt_display_require(igt_display_t *display, int drm_fd)
 		}
 
 		igt_assert_lt(0, n_planes);
-		pipe->planes = calloc(sizeof(igt_plane_t), n_planes);
+		pipe->planes = calloc(n_planes, sizeof(igt_plane_t));
 		igt_assert_f(pipe->planes, "Failed to allocate memory for %d planes\n", n_planes);
 		last_plane = n_planes - 1;
 
diff --git a/tests/amdgpu/amd_multidisplay_modeset.c b/tests/amdgpu/amd_multidisplay_modeset.c
index 4a54f92e0a40..f940973525b0 100644
--- a/tests/amdgpu/amd_multidisplay_modeset.c
+++ b/tests/amdgpu/amd_multidisplay_modeset.c
@@ -239,7 +239,7 @@ static void multiple_display_test(struct data_t *data, enum sub_test test_mode)
 			      num_disps > data->display.n_outputs,
 		      "ASIC does not have %d outputs/pipes\n", num_disps);
 
-	buf = calloc(sizeof(struct igt_fb), num_disps);
+	buf = calloc(num_disps, sizeof(struct igt_fb));
 	igt_assert_f(buf, "Failed to allocate memory\n");
 
 	/* For mode test, it is max number of modes for
diff --git a/tests/chamelium/kms_chamelium_frames.c b/tests/chamelium/kms_chamelium_frames.c
index 05eeca593f87..a585e7e01d87 100644
--- a/tests/chamelium/kms_chamelium_frames.c
+++ b/tests/chamelium/kms_chamelium_frames.c
@@ -940,7 +940,7 @@ static void test_display_planes_random(chamelium_data_t *data,
 	overlay_planes_count = (rand() % overlay_planes_max) + 1;
 	igt_debug("Using %d overlay planes\n", overlay_planes_count);
 
-	overlay_fbs = calloc(sizeof(struct igt_fb), overlay_planes_count);
+	overlay_fbs = calloc(overlay_planes_count, sizeof(struct igt_fb));
 
 	for (i = 0; i < overlay_planes_count; i++) {
 		struct igt_fb *overlay_fb = &overlay_fbs[i];
diff --git a/tests/intel/gem_exec_alignment.c b/tests/intel/gem_exec_alignment.c
index 0e9f78362401..6e5496166af3 100644
--- a/tests/intel/gem_exec_alignment.c
+++ b/tests/intel/gem_exec_alignment.c
@@ -201,7 +201,7 @@ naughty_child(int i915, int link, uint32_t shared, unsigned int flags)
 		flags |= EXEC_OBJECT_SUPPORTS_48B_ADDRESS;
 
 	/* Fill the low-priority address space */
-	obj = calloc(sizeof(*obj), count);
+	obj = calloc(count, sizeof(*obj));
 	igt_assert(obj);
 
 	memset(&execbuf, 0, sizeof(execbuf));
@@ -406,7 +406,7 @@ setup_many(int i915, unsigned long *out)
 		count = file_max();
 	igt_require_memory(count, 4096, CHECK_RAM);
 
-	obj = calloc(sizeof(*obj), count);
+	obj = calloc(count, sizeof(*obj));
 	igt_assert(obj);
 
 	flags = 0;
diff --git a/tests/intel/gem_exec_fair.c b/tests/intel/gem_exec_fair.c
index c903b6eddb3c..e71fa2f0d6e8 100644
--- a/tests/intel/gem_exec_fair.c
+++ b/tests/intel/gem_exec_fair.c
@@ -1118,8 +1118,8 @@ static void deadline(int i915, const intel_ctx_cfg_t *cfg,
 		(frame_ns / 1000 / 1000 + 2) * switch_ns + parent_ns;
 	struct intel_execution_engine2 pe = pick_default(i915, cfg);
 	struct intel_execution_engine2 ve = pick_engine(i915, cfg, "vcs0");
-	struct drm_i915_gem_exec_fence *fences = calloc(sizeof(*fences), 32);
-	struct drm_i915_gem_exec_object2 *obj = calloc(sizeof(*obj), 32);
+	struct drm_i915_gem_exec_fence *fences = calloc(32, sizeof(*fences));
+	struct drm_i915_gem_exec_object2 *obj = calloc(32, sizeof(*obj));
 	struct drm_i915_gem_execbuffer2 execbuf = {
 		.buffers_ptr = to_user_pointer(obj),
 		.cliprects_ptr = to_user_pointer(fences),
diff --git a/tests/intel/gem_fence_thrash.c b/tests/intel/gem_fence_thrash.c
index 0df7319fced7..e01e5a8a7e31 100644
--- a/tests/intel/gem_fence_thrash.c
+++ b/tests/intel/gem_fence_thrash.c
@@ -150,7 +150,7 @@ _bo_write_verify(struct test *t)
 	igt_assert(t->tiling >= 0 && t->tiling <= I915_TILING_Y);
 	igt_assert_lt(0, t->num_surfaces);
 
-	s = calloc(sizeof(*s), t->num_surfaces);
+	s = calloc(t->num_surfaces, sizeof(*s));
 	igt_assert(s);
 
 	for (k = 0; k < t->num_surfaces; k++)
@@ -231,7 +231,7 @@ static int run_test(int threads_per_fence, void *f, int tiling,
 		 num_fences, tiling, surfaces_per_thread);
 
 	if (threads_per_fence) {
-		threads = calloc(sizeof(*threads), num_threads);
+		threads = calloc(num_threads, sizeof(*threads));
 		igt_assert(threads != NULL);
 
 		for (n = 0; n < num_threads; n++)
diff --git a/tests/intel/gem_ppgtt.c b/tests/intel/gem_ppgtt.c
index 9eb995f20230..e6a4651da88c 100644
--- a/tests/intel/gem_ppgtt.c
+++ b/tests/intel/gem_ppgtt.c
@@ -429,7 +429,7 @@ igt_main
 		mem_per_test = SIZE;
 		igt_require_memory(nchild + 1, mem_per_test, CHECK_RAM);
 
-		rcs = calloc(sizeof(*rcs), nchild);
+		rcs = calloc(nchild, sizeof(*rcs));
 		igt_assert(rcs);
 
 		fork_bcs_copy(30, 0x4000, bcs, 1);
@@ -456,7 +456,7 @@ igt_main
 		mem_per_test = SIZE + mem_per_ctx;
 		igt_require_memory(1 + nchild, mem_per_test, CHECK_RAM);
 
-		rcs = calloc(sizeof(*rcs), nchild);
+		rcs = calloc(nchild, sizeof(*rcs));
 		igt_assert(rcs);
 
 		fork_rcs_copy(30, 0x8000 / nchild, rcs, nchild, CREATE_CONTEXT);
diff --git a/tests/intel/gem_render_tiled_blits.c b/tests/intel/gem_render_tiled_blits.c
index 3c4b062b016d..4b49c2a3658c 100644
--- a/tests/intel/gem_render_tiled_blits.c
+++ b/tests/intel/gem_render_tiled_blits.c
@@ -149,7 +149,7 @@ static void run_test (int fd, int count)
 		igt_info("Using a snoop linear buffer for comparisons\n");
 	}
 
-	bufs = calloc(sizeof(*bufs), count);
+	bufs = calloc(count, sizeof(*bufs));
 	start_val = malloc(sizeof(*start_val)*count);
 
 	for (i = 0; i < count; i++) {
diff --git a/tests/intel/gem_userptr_blits.c b/tests/intel/gem_userptr_blits.c
index 51948db0c90e..66f2a9416c97 100644
--- a/tests/intel/gem_userptr_blits.c
+++ b/tests/intel/gem_userptr_blits.c
@@ -1430,7 +1430,7 @@ static void store_dword_rand(int i915, const intel_ctx_t *ctx,
 	batchsz = count * 16 + 4;
 	batchsz = ALIGN(batchsz, 4096);
 
-	reloc = calloc(sizeof(*reloc), count);
+	reloc = calloc(count, sizeof(*reloc));
 
 	memset(obj, 0, sizeof(obj));
 	obj[0].handle = target;
diff --git a/tests/kms_atomic_transition.c b/tests/kms_atomic_transition.c
index e8992790d31e..29dd8ac4e4e8 100644
--- a/tests/kms_atomic_transition.c
+++ b/tests/kms_atomic_transition.c
@@ -480,11 +480,11 @@ static void prepare_fencing(data_t *data, enum pipe pipe)
 	igt_require_sw_sync();
 
 	n_planes = data->display.pipes[pipe].n_planes;
-	timeline = calloc(sizeof(*timeline), n_planes);
+	timeline = calloc(n_planes, sizeof(*timeline));
 	igt_assert_f(timeline != NULL, "Failed to allocate memory for timelines\n");
-	thread = calloc(sizeof(*thread), n_planes);
+	thread = calloc(n_planes, sizeof(*thread));
 	igt_assert_f(thread != NULL, "Failed to allocate memory for thread\n");
-	seqno = calloc(sizeof(*seqno), n_planes);
+	seqno = calloc(n_planes, sizeof(*seqno));
 	igt_assert_f(seqno != NULL, "Failed to allocate memory for seqno\n");
 
 	for_each_plane_on_pipe(&data->display, pipe, plane)
diff --git a/tests/kms_flip.c b/tests/kms_flip.c
index 3973ec862436..15c3b5ba275d 100755
--- a/tests/kms_flip.c
+++ b/tests/kms_flip.c
@@ -423,8 +423,8 @@ static void emit_fence_stress(struct test_output *o)
 	igt_require(bops);
 
 	igt_assert(num_fences);
-	bo = calloc(sizeof(*bo), num_fences);
-	exec = calloc(sizeof(*exec), num_fences+1);
+	bo = calloc(num_fences, sizeof(*bo));
+	exec = calloc(num_fences+1, sizeof(*exec));
 	for (i = 0; i < num_fences - 1; i++) {
 		uint32_t tiling = I915_TILING_X;
 		bo[i] = intel_buf_create(bops, 1024, 1024, 32, 0, tiling,
-- 
2.44.0



More information about the igt-dev mailing list