Commit 4b566406085bc1fd5b6ba309fae70e6f6d6fa94e

Authored by Gustavo A. R. Silva
Committed by Dan Williams
1 parent 11a48a5a18

ACPI: NFIT: Replace zero-length array with flexible-array member

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]

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 <gustavo@embeddedor.com>
Link: https://lore.kernel.org/r/20200319195046.GA452@embeddedor.com
Signed-off-by: Dan Williams <dan.j.williams@intel.com>

Showing 1 changed file with 6 additions and 6 deletions Side-by-side Diff

drivers/acpi/nfit/nfit.h
... ... @@ -144,32 +144,32 @@
144 144 unsigned long ars_state;
145 145 u32 clear_err_unit;
146 146 u32 max_ars;
147   - struct acpi_nfit_system_address spa[0];
  147 + struct acpi_nfit_system_address spa[];
148 148 };
149 149  
150 150 struct nfit_dcr {
151 151 struct list_head list;
152   - struct acpi_nfit_control_region dcr[0];
  152 + struct acpi_nfit_control_region dcr[];
153 153 };
154 154  
155 155 struct nfit_bdw {
156 156 struct list_head list;
157   - struct acpi_nfit_data_region bdw[0];
  157 + struct acpi_nfit_data_region bdw[];
158 158 };
159 159  
160 160 struct nfit_idt {
161 161 struct list_head list;
162   - struct acpi_nfit_interleave idt[0];
  162 + struct acpi_nfit_interleave idt[];
163 163 };
164 164  
165 165 struct nfit_flush {
166 166 struct list_head list;
167   - struct acpi_nfit_flush_address flush[0];
  167 + struct acpi_nfit_flush_address flush[];
168 168 };
169 169  
170 170 struct nfit_memdev {
171 171 struct list_head list;
172   - struct acpi_nfit_memory_map memdev[0];
  172 + struct acpi_nfit_memory_map memdev[];
173 173 };
174 174  
175 175 enum nfit_mem_flags {