Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

perf tools: Replace zero-length array with flexible-array

The current codebase makes use of the zero-length array language
extension to the C90 standard, but the preferred mechanism to declare
variable-length types such as these ones is a flexible array
member[1][2], introduced in C99:

struct foo {
int stuff;
struct boo array[];
};

By making use of the mechanism above, we will get a compiler warning in
case the flexible array does not occur last in the structure, which will
help us prevent some kind of undefined behavior bugs from being
inadvertently introduced[3] to the codebase from now on.

Also, notice that, dynamic memory allocations won't be affected by this
change:

"Flexible array members have incomplete type, and so the sizeof operator
may not be applied. As a quirk of the original implementation of
zero-length arrays, sizeof evaluates to zero."[1]

sizeof(flexible-array-member) triggers a warning because flexible array
members have incomplete type[1]. There are some instances of code in
which the sizeof operator is being incorrectly/erroneously applied to
zero-length arrays and the result is zero. Such instances may be hiding
some bugs. So, this work (flexible-array member conversions) will also
help to get completely rid of those sorts of issues.

This issue was found with the help of Coccinelle.

[1] https://gcc.gnu.org/onlinedocs/gcc/Zero-Length.html
[2] https://github.com/KSPP/linux/issues/21
[3] commit 76497732932f ("cxgb3/l2t: Fix undefined behaviour")

Signed-off-by: Gustavo A. R. Silva <gustavoars@kernel.org>
Cc: Adrian Hunter <adrian.hunter@intel.com>
Cc: Alexander Shishkin <alexander.shishkin@linux.intel.com>
Cc: Gustavo A. R. Silva <gustavo@embeddedor.com>
Cc: Ian Rogers <irogers@google.com>
Cc: Jiri Olsa <jolsa@redhat.com>
Cc: Mark Rutland <mark.rutland@arm.com>
Cc: Namhyung Kim <namhyung@kernel.org>
Cc: Peter Zijlstra <peterz@infradead.org>
Link: http://lore.kernel.org/lkml/20200515172926.GA31976@embeddedor
Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>

authored by

Gustavo A. R. Silva and committed by
Arnaldo Carvalho de Melo
6549a8c0 961224db

+18 -18
+1 -1
tools/perf/bench/sched-messaging.c
··· 40 40 unsigned int num_fds; 41 41 int ready_out; 42 42 int wakefd; 43 - int out_fds[0]; 43 + int out_fds[]; 44 44 }; 45 45 46 46 struct receiver_context {
+1 -1
tools/perf/builtin-inject.c
··· 51 51 struct event_entry { 52 52 struct list_head node; 53 53 u32 tid; 54 - union perf_event event[0]; 54 + union perf_event event[]; 55 55 }; 56 56 57 57 static int output_bytes(struct perf_inject *inject, void *buf, size_t sz)
+1 -1
tools/perf/builtin-script.c
··· 2449 2449 struct script_spec { 2450 2450 struct list_head node; 2451 2451 struct scripting_ops *ops; 2452 - char spec[0]; 2452 + char spec[]; 2453 2453 }; 2454 2454 2455 2455 static LIST_HEAD(script_specs);
+1 -1
tools/perf/builtin-timechart.c
··· 128 128 struct sample_wrapper *next; 129 129 130 130 u64 timestamp; 131 - unsigned char data[0]; 131 + unsigned char data[]; 132 132 }; 133 133 134 134 #define TYPE_NONE 0
+2 -2
tools/perf/util/annotate.h
··· 144 144 u32 idx; 145 145 int idx_asm; 146 146 int data_nr; 147 - struct annotation_data data[0]; 147 + struct annotation_data data[]; 148 148 }; 149 149 150 150 struct disasm_line { ··· 227 227 struct sym_hist { 228 228 u64 nr_samples; 229 229 u64 period; 230 - struct sym_hist_entry addr[0]; 230 + struct sym_hist_entry addr[]; 231 231 }; 232 232 233 233 struct cyc_hist {
+1 -1
tools/perf/util/cputopo.h
··· 22 22 23 23 struct numa_topology { 24 24 u32 nr; 25 - struct numa_topology_node nodes[0]; 25 + struct numa_topology_node nodes[]; 26 26 }; 27 27 28 28 struct cpu_topology *cpu_topology__new(void);
+2 -2
tools/perf/util/dso.h
··· 137 137 struct rb_node rb_node; 138 138 u64 offset; 139 139 u64 size; 140 - char data[0]; 140 + char data[]; 141 141 }; 142 142 143 143 struct auxtrace_cache; ··· 209 209 struct nsinfo *nsinfo; 210 210 struct dso_id id; 211 211 refcount_t refcnt; 212 - char name[0]; 212 + char name[]; 213 213 }; 214 214 215 215 /* dso__for_each_symbol - iterate over the symbols of given type
+1 -1
tools/perf/util/event.h
··· 79 79 80 80 struct ip_callchain { 81 81 u64 nr; 82 - u64 ips[0]; 82 + u64 ips[]; 83 83 }; 84 84 85 85 struct branch_stack;
+1 -1
tools/perf/util/jitdump.c
··· 57 57 unsigned long vma; 58 58 unsigned int lineno; 59 59 /* The filename format is unspecified, absolute path, relative etc. */ 60 - char const filename[0]; 60 + char const filename[]; 61 61 }; 62 62 63 63 struct jit_tool {
+3 -3
tools/perf/util/jitdump.h
··· 93 93 uint64_t addr; 94 94 int lineno; /* source line number starting at 1 */ 95 95 int discrim; /* column discriminator, 0 is default */ 96 - const char name[0]; /* null terminated filename, \xff\0 if same as previous entry */ 96 + const char name[]; /* null terminated filename, \xff\0 if same as previous entry */ 97 97 }; 98 98 99 99 struct jr_code_debug_info { ··· 101 101 102 102 uint64_t code_addr; 103 103 uint64_t nr_entry; 104 - struct debug_entry entries[0]; 104 + struct debug_entry entries[]; 105 105 }; 106 106 107 107 struct jr_code_unwinding_info { ··· 110 110 uint64_t unwinding_size; 111 111 uint64_t eh_frame_hdr_size; 112 112 uint64_t mapped_size; 113 - const char unwinding_data[0]; 113 + const char unwinding_data[]; 114 114 }; 115 115 116 116 union jr_entry {
+1 -1
tools/perf/util/ordered-events.h
··· 29 29 30 30 struct ordered_events_buffer { 31 31 struct list_head list; 32 - struct ordered_event event[0]; 32 + struct ordered_event event[]; 33 33 }; 34 34 35 35 struct ordered_events {
+1 -1
tools/perf/util/pstack.c
··· 15 15 struct pstack { 16 16 unsigned short top; 17 17 unsigned short max_nr_entries; 18 - void *entries[0]; 18 + void *entries[]; 19 19 }; 20 20 21 21 struct pstack *pstack__new(unsigned short max_nr_entries)
+1 -1
tools/perf/util/symbol.h
··· 55 55 u8 inlined:1; 56 56 u8 arch_sym; 57 57 bool annotate2; 58 - char name[0]; 58 + char name[]; 59 59 }; 60 60 61 61 void symbol__delete(struct symbol *sym);
+1 -1
tools/perf/util/unwind-libunwind-local.c
··· 243 243 * encoded_t fde_addr; 244 244 * } binary_search_table[fde_count]; 245 245 */ 246 - char data[0]; 246 + char data[]; 247 247 } __packed; 248 248 249 249 static int unwind_spec_ehframe(struct dso *dso, struct machine *machine,