refactor: use flexible arrays instead of the length-of-one trick (#22072)

The "length-of-one" trick, where the last element of a struct is an
array of size 1, but extra size is allocated when calling malloc where
it uses more than 1 element in the array, cause problems with some
compilers. Some compilers set _FORTIFY_SOURCE=2 by default which
incorrectly considers it as an overflow. More information:
https://github.com/neovim/neovim/issues/223#issuecomment-1413828554

Using flexible array members allows us to to properly convey to the
compiler that its size may be larger than 1. This also enables us to
remove lengthy workarounds that are unreliable, as they depend on
CMAKE_BUILD_TYPE which isn't defined for multi-config generators.

Closes: https://github.com/neovim/neovim/issues/223
This commit is contained in:
dundargoc
2023-02-02 23:56:25 +01:00
committed by GitHub
parent 0ea4156464
commit ebd2372f92
11 changed files with 11 additions and 53 deletions

View File

@@ -117,7 +117,7 @@ struct pointer_block {
uint16_t pb_id; // ID for pointer block: PTR_ID
uint16_t pb_count; // number of pointers in this block
uint16_t pb_count_max; // maximum value for pb_count
PTR_EN pb_pointer[1]; // list of pointers to blocks (actually longer)
PTR_EN pb_pointer[]; // list of pointers to blocks
// followed by empty space until end of page
};
@@ -133,7 +133,7 @@ struct data_block {
unsigned db_txt_end; // byte just after data block
// linenr_T db_line_count;
long db_line_count; // number of lines in this block
unsigned db_index[1]; // index for start of line (actually bigger)
unsigned db_index[]; // index for start of line
// followed by empty space up to db_txt_start
// followed by the text in the lines until
// end of page