Skip to content

Commit 6549a8c

Browse files
GustavoARSilvaacmel
authored andcommitted
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] KSPP/linux#21 [3] commit 7649773 ("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>
1 parent 961224d commit 6549a8c

14 files changed

Lines changed: 18 additions & 18 deletions

‎tools/perf/bench/sched-messaging.c‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ struct sender_context {
4040
unsigned int num_fds;
4141
int ready_out;
4242
int wakefd;
43-
int out_fds[0];
43+
int out_fds[];
4444
};
4545

4646
struct receiver_context {

‎tools/perf/builtin-inject.c‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ struct perf_inject {
5151
struct event_entry {
5252
struct list_head node;
5353
u32 tid;
54-
union perf_event event[0];
54+
union perf_event event[];
5555
};
5656

5757
static int output_bytes(struct perf_inject *inject, void *buf, size_t sz)

‎tools/perf/builtin-script.c‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2449,7 +2449,7 @@ static int __cmd_script(struct perf_script *script)
24492449
struct script_spec {
24502450
struct list_head node;
24512451
struct scripting_ops *ops;
2452-
char spec[0];
2452+
char spec[];
24532453
};
24542454

24552455
static LIST_HEAD(script_specs);

‎tools/perf/builtin-timechart.c‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ struct sample_wrapper {
128128
struct sample_wrapper *next;
129129

130130
u64 timestamp;
131-
unsigned char data[0];
131+
unsigned char data[];
132132
};
133133

134134
#define TYPE_NONE 0

‎tools/perf/util/annotate.h‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ struct annotation_line {
144144
u32 idx;
145145
int idx_asm;
146146
int data_nr;
147-
struct annotation_data data[0];
147+
struct annotation_data data[];
148148
};
149149

150150
struct disasm_line {
@@ -227,7 +227,7 @@ void symbol__calc_percent(struct symbol *sym, struct evsel *evsel);
227227
struct sym_hist {
228228
u64 nr_samples;
229229
u64 period;
230-
struct sym_hist_entry addr[0];
230+
struct sym_hist_entry addr[];
231231
};
232232

233233
struct cyc_hist {

‎tools/perf/util/cputopo.h‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ struct numa_topology_node {
2222

2323
struct numa_topology {
2424
u32 nr;
25-
struct numa_topology_node nodes[0];
25+
struct numa_topology_node nodes[];
2626
};
2727

2828
struct cpu_topology *cpu_topology__new(void);

‎tools/perf/util/dso.h‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ struct dso_cache {
137137
struct rb_node rb_node;
138138
u64 offset;
139139
u64 size;
140-
char data[0];
140+
char data[];
141141
};
142142

143143
struct auxtrace_cache;
@@ -209,7 +209,7 @@ struct dso {
209209
struct nsinfo *nsinfo;
210210
struct dso_id id;
211211
refcount_t refcnt;
212-
char name[0];
212+
char name[];
213213
};
214214

215215
/* dso__for_each_symbol - iterate over the symbols of given type

‎tools/perf/util/event.h‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ struct sample_read {
7979

8080
struct ip_callchain {
8181
u64 nr;
82-
u64 ips[0];
82+
u64 ips[];
8383
};
8484

8585
struct branch_stack;

‎tools/perf/util/jitdump.c‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ struct debug_line_info {
5757
unsigned long vma;
5858
unsigned int lineno;
5959
/* The filename format is unspecified, absolute path, relative etc. */
60-
char const filename[0];
60+
char const filename[];
6161
};
6262

6363
struct jit_tool {

‎tools/perf/util/jitdump.h‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -93,15 +93,15 @@ struct debug_entry {
9393
uint64_t addr;
9494
int lineno; /* source line number starting at 1 */
9595
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 */
9797
};
9898

9999
struct jr_code_debug_info {
100100
struct jr_prefix p;
101101

102102
uint64_t code_addr;
103103
uint64_t nr_entry;
104-
struct debug_entry entries[0];
104+
struct debug_entry entries[];
105105
};
106106

107107
struct jr_code_unwinding_info {
@@ -110,7 +110,7 @@ struct jr_code_unwinding_info {
110110
uint64_t unwinding_size;
111111
uint64_t eh_frame_hdr_size;
112112
uint64_t mapped_size;
113-
const char unwinding_data[0];
113+
const char unwinding_data[];
114114
};
115115

116116
union jr_entry {

0 commit comments

Comments
 (0)