Skip to contentSkip to conversation

gcc: change zero-sized array to fexlible array

PATCH In review Zhao Heming 2 participants 7 messages Aug 20, 2020 – Sep 3, 2020

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 gcc: change zero-sized array to fexlible array

this patch makes gcc happy with compiling option: [-Wstringop-overflow=]

---
 device_mapper/libdm-common.c | 2 +-
 lib/activate/fs.c            | 2 +-
 libdm/libdm-common.c         | 2 +-
 3 files changed, 3 insertions(+), 3 deletions(-)
device_mapper/libdm-common.c +1 -1
index bc691d6..3627457 100644
@@ -1445,7 +1445,7 @@ struct node_op_parms {
 	char *old_name;
 	int warn_if_udev_failed;
 	unsigned rely_on_udev;
-	char names[0];
+	char names[];
 };
 
 static void _store_str(char **pos, char **ptr, const char *str)
lib/activate/fs.c +1 -1
index b2c99fc..96f7df6 100644
@@ -313,7 +313,7 @@ struct fs_op_parms {
 	char *lv_name;
 	char *dev;
 	char *old_lv_name;
-	char names[0];
+	char names[];
 };
 
 static void _store_str(char **pos, char **ptr, const char *str)
libdm/libdm-common.c +1 -1
index d75c704..d587c32 100644
@@ -1443,7 +1443,7 @@ struct node_op_parms {
 	char *old_name;
 	int warn_if_udev_failed;
 	unsigned rely_on_udev;
-	char names[0];
+	char names[];
 };
 
 static void _store_str(char **pos, char **ptr, const char *str)
Signature · 1 line
1.8.3.1
#1 lore raw

more info or discussion with this patch.

I met this compiling issue when I was creating openSUSE package. I'm a little confused & wonder why Fedora can pass this gcc warning.

Quoted 13 lines · Zhao Heming

this patch makes gcc happy with compiling option: [-Wstringop-overflow=]

---
  device_mapper/libdm-common.c | 2 +-
  lib/activate/fs.c            | 2 +-
  libdm/libdm-common.c         | 2 +-
  3 files changed, 3 insertions(+), 3 deletions(-)
device_mapper/libdm-common.c +0 -0
index bc691d6..3627457 100644
#2 lore raw
In reply to Zhao Heming

ping.

@Marian & other maintainers

Quoted 49 lines · Zhao Heming

this patch makes gcc happy with compiling option: [-Wstringop-overflow=]

---
  device_mapper/libdm-common.c | 2 +-
  lib/activate/fs.c            | 2 +-
  libdm/libdm-common.c         | 2 +-
  3 files changed, 3 insertions(+), 3 deletions(-)
device_mapper/libdm-common.c +1 -1
index bc691d6..3627457 100644
@@ -1445,7 +1445,7 @@ struct node_op_parms {
  	char *old_name;
  	int warn_if_udev_failed;
  	unsigned rely_on_udev;
-	char names[0];
+	char names[];
  };
 
  static void _store_str(char **pos, char **ptr, const char *str)
lib/activate/fs.c +1 -1
index b2c99fc..96f7df6 100644
@@ -313,7 +313,7 @@ struct fs_op_parms {
  	char *lv_name;
  	char *dev;
  	char *old_lv_name;
-	char names[0];
+	char names[];
  };
 
  static void _store_str(char **pos, char **ptr, const char *str)
libdm/libdm-common.c +1 -1
index d75c704..d587c32 100644
@@ -1443,7 +1443,7 @@ struct node_op_parms {
  	char *old_name;
  	int warn_if_udev_failed;
  	unsigned rely_on_udev;
-	char names[0];
+	char names[];
  };
 
  static void _store_str(char **pos, char **ptr, const char *str)
#3 lore raw

Dne 26. 08. 20 v 7:18 [email protected] napsal(a):

Quoted 15 lines

ping.

@Marian & other maintainers

Quoted 9 lines · Zhao Heming

this patch makes gcc happy with compiling option: [-Wstringop-overflow=]

---
   device_mapper/libdm-common.c | 2 +-
   lib/activate/fs.c            | 2 +-
   libdm/libdm-common.c         | 2 +-
   3 files changed, 3 insertions(+), 3 deletions(-)
Quoted 12 lines

device_mapper/libdm-common.c struct node_op_parms { · line 1450

Quoted 12 lines
device_mapper/libdm-common.c +1 -1
index bc691d6..3627457 100644
@@ -1445,7 +1445,7 @@ struct node_op_parms {
   	char *old_name;
   	int warn_if_udev_failed;
   	unsigned rely_on_udev;
-	char names[0];
+	char names[];
   };
 

Hi

Here I don't see much problem with patch itself - except it seems to be somewhat mysterious. Lvm2 is not strict ISO C compilable - there are far to many other things which would need to be fixed - thus flexible arrays shouldn't be an issue.

I'm curious why these 3 were important in your case - while other 26 appearances passed for you just fine (see attachment).

So can you paste what sort of gcc problem do you actually try to solve ?

Regards

Zdenek

From: Zdenek Kabelac <[email protected]>
Date: Fri, 28 Aug 2020 19:15:01 +0200
Subject: gcc: change zero-sized array to fexlible array II

---
 base/data-struct/hash.c                | 2 +-
 base/data-struct/radix-tree-adaptive.c | 2 +-
 daemons/lvmlockd/lvmlockd-internal.h   | 2 +-
 device_mapper/all.h                    | 6 +++---
 device_mapper/misc/dm-ioctl.h          | 8 ++++----
 lib/device/dev-cache.c                 | 2 +-
 lib/format_text/layout.h               | 6 +++---
 lib/label/label.c                      | 2 +-
 lib/metadata/lv_manip.c                | 2 +-
 libdm/datastruct/hash.c                | 2 +-
 libdm/libdevmapper.h                   | 6 +++---
 libdm/libdm-stats.c                    | 2 +-
 libdm/misc/dm-ioctl.h                  | 8 ++++----
 tools/tools.h                          | 2 +-
 14 files changed, 26 insertions(+), 26 deletions(-)
base/data-struct/hash.c +1 -1
index 0a0541d2e..5ef5ed276 100644
@@ -22,7 +22,7 @@ struct dm_hash_node {
 	void *data;
 	unsigned data_len;
 	unsigned keylen;
-	char key[0];
+	char key[];
 };
 
 struct dm_hash_table {
base/data-struct/radix-tree-adaptive.c +1 -1
index b9ba4179f..3a46cc14b 100644
@@ -47,7 +47,7 @@ struct value_chain {
 struct prefix_chain {
 	struct value child;
 	unsigned len;
-	uint8_t prefix[0];
+	uint8_t prefix[];
 };
 
 struct node4 {
daemons/lvmlockd/lvmlockd-internal.h +1 -1
index 191c44926..14bdfeed0 100644
@@ -158,7 +158,7 @@ struct resource {
 	struct list_head locks;
 	struct list_head actions;
 	char lv_args[MAX_ARGS+1];
-	char lm_data[0];		/* lock manager specific data */
+	char lm_data[];			/* lock manager specific data */
 };
 
 #define LD_LF_PERSISTENT 0x00000001
device_mapper/all.h +3 -3
index c3c6219e3..ace3b69a0 100644
@@ -164,20 +164,20 @@ struct dm_info {
 struct dm_deps {
 	uint32_t count;
 	uint32_t filler;
-	uint64_t device[0];
+	uint64_t device[];
 };
 
 struct dm_names {
 	uint64_t dev;
 	uint32_t next;		/* Offset to next struct from start of this struct */
-	char name[0];
+	char name[];
 };
 
 struct dm_versions {
 	uint32_t next;		/* Offset to next struct from start of this struct */
 	uint32_t version[3];
 
-	char name[0];
+	char name[];
 };
 
 int dm_get_library_version(char *version, size_t size);
device_mapper/misc/dm-ioctl.h +4 -4
index cc2374c36..49954a764 100644
@@ -183,7 +183,7 @@ struct dm_target_spec {
 struct dm_target_deps {
 	uint32_t count;	/* Array size */
 	uint32_t padding;	/* unused */
-	uint64_t dev[0];	/* out */
+	uint64_t dev[];		/* out */
 };
 
 /*
@@ -193,7 +193,7 @@ struct dm_name_list {
 	uint64_t dev;
 	uint32_t next;		/* offset to the next record from
 				   the _start_ of this */
-	char name[0];
+	char name[];
 };
 
 /*
@@ -203,7 +203,7 @@ struct dm_target_versions {
         uint32_t next;
         uint32_t version[3];
 
-        char name[0];
+        char name[];
 };
 
 /*
@@ -212,7 +212,7 @@ struct dm_target_versions {
 struct dm_target_msg {
 	uint64_t sector;	/* Device sector */
 
-	char message[0];
+	char message[];
 };
 
 /*
lib/device/dev-cache.c +1 -1
index c3f7c49be..d4e2658aa 100644
@@ -35,7 +35,7 @@ struct dev_iter {
 
 struct dir_list {
 	struct dm_list list;
-	char dir[0];
+	char dir[];
 };
 
 static struct {
lib/format_text/layout.h +3 -3
index c3dfe8e7a..df7ed3a78 100644
@@ -33,7 +33,7 @@ struct pv_header_extension {
 	uint32_t version;
 	uint32_t flags;
 	/* NULL-terminated list of bootloader areas */
-	struct disk_locn bootloader_areas_xl[0];
+	struct disk_locn bootloader_areas_xl[];
 } __attribute__ ((packed));
 
 /* Fields with the suffix _xl should be xlate'd wherever they appear */
@@ -46,7 +46,7 @@ struct pv_header {
 
 	/* NULL-terminated list of data areas followed by */
 	/* NULL-terminated list of metadata area headers */
-	struct disk_locn disk_areas_xl[0];	/* Two lists */
+	struct disk_locn disk_areas_xl[];	/* Two lists */
 } __attribute__ ((packed));
 
 /*
@@ -76,7 +76,7 @@ struct mda_header {
 	uint64_t start;		/* Absolute start byte of mda_header */
 	uint64_t size;		/* Size of metadata area */
 
-	struct raw_locn raw_locns[0];	/* NULL-terminated list */
+	struct raw_locn raw_locns[];	/* NULL-terminated list */
 } __attribute__ ((packed));
 
 struct mda_header *raw_read_mda_header(const struct format_type *fmt,
lib/label/label.c +1 -1
index 4d369d4f1..8e68f9303 100644
@@ -43,7 +43,7 @@ struct labeller_i {
 	struct dm_list list;
 
 	struct labeller *l;
-	char name[0];
+	char name[];
 };
 
 static struct dm_list _labellers;
lib/metadata/lv_manip.c +1 -1
index c47ec72e2..95ca2df39 100644
@@ -1798,7 +1798,7 @@ struct alloc_handle {
 	 * Contains area_count lists of areas allocated to data stripes
 	 * followed by log_area_count lists of areas allocated to log stripes.
 	 */
-	struct dm_list alloced_areas[0];
+	struct dm_list alloced_areas[];
 };
 
 /*
libdm/datastruct/hash.c +1 -1
index 4c940039a..726347ea2 100644
@@ -20,7 +20,7 @@ struct dm_hash_node {
 	void *data;
 	unsigned data_len;
 	unsigned keylen;
-	char key[0];
+	char key[];
 };
 
 struct dm_hash_table {
libdm/libdevmapper.h +3 -3
index e9b140563..059b96f0b 100644
@@ -165,20 +165,20 @@ struct dm_info {
 struct dm_deps {
 	uint32_t count;
 	uint32_t filler;
-	uint64_t device[0];
+	uint64_t device[];
 };
 
 struct dm_names {
 	uint64_t dev;
 	uint32_t next;		/* Offset to next struct from start of this struct */
-	char name[0];
+	char name[];
 };
 
 struct dm_versions {
 	uint32_t next;		/* Offset to next struct from start of this struct */
 	uint32_t version[3];
 
-	char name[0];
+	char name[];
 };
 
 int dm_get_library_version(char *version, size_t size);
libdm/libdm-stats.c +1 -1
index 5379bed5e..f8d79d807 100644
@@ -65,7 +65,7 @@ struct dm_histogram {
 	const struct dm_stats_region *region;
 	uint64_t sum; /* Sum of histogram bin counts. */
 	int nr_bins; /* Number of histogram bins assigned. */
-	struct dm_histogram_bin bins[0];
+	struct dm_histogram_bin bins[];
 };
 
 /*
libdm/misc/dm-ioctl.h +4 -4
index 52a7a93d1..55dee2148 100644
@@ -183,7 +183,7 @@ struct dm_target_spec {
 struct dm_target_deps {
 	uint32_t count;	/* Array size */
 	uint32_t padding;	/* unused */
-	uint64_t dev[0];	/* out */
+	uint64_t dev[];		/* out */
 };
 
 /*
@@ -193,7 +193,7 @@ struct dm_name_list {
 	uint64_t dev;
 	uint32_t next;		/* offset to the next record from
 				   the _start_ of this */
-	char name[0];
+	char name[];
 };
 
 /*
@@ -203,7 +203,7 @@ struct dm_target_versions {
         uint32_t next;
         uint32_t version[3];
 
-        char name[0];
+        char name[];
 };
 
 /*
@@ -212,7 +212,7 @@ struct dm_target_versions {
 struct dm_target_msg {
 	uint64_t sector;	/* Device sector */
 
-	char message[0];
+	char message[];
 };
 
 /*
tools/tools.h +1 -1
index 4b944e2fd..befff5708 100644
@@ -110,7 +110,7 @@ struct arg_values {
 struct arg_value_group_list {
         struct dm_list list;
 	uint32_t prio;
-        struct arg_values arg_values[0];
+	struct arg_values arg_values[];
 };
 
 #define PERMITTED_READ_ONLY 	0x00000002
Signature · 1 line
2.27.0
#4 lore raw
Quoted 33 lines · Zdenek Kabelac

Dne 26. 08. 20 v 7:18 [email protected] napsal(a):

Quoted 15 lines

ping.

@Marian & other maintainers

Quoted 9 lines · Zhao Heming

this patch makes gcc happy with compiling option: [-Wstringop-overflow=]

---
�� device_mapper/libdm-common.c | 2 +-
�� lib/activate/fs.c����������� | 2 +-
�� libdm/libdm-common.c�������� | 2 +-
�� 3 files changed, 3 insertions(+), 3 deletions(-)

Quoted 11 lines

device_mapper/libdm-common.c struct node_op_parms { · line 1449

Quoted 11 lines
device_mapper/libdm-common.c +1 -1
index bc691d6..3627457 100644
@@ -1445,7 +1445,7 @@ struct node_op_parms {
 ������ char *old_name;
 ������ int warn_if_udev_failed;
 ������ unsigned rely_on_udev;
-��� char names[0];
+��� char names[];
 �� };

Hi

Here I don't see much problem with patch itself - except it seems to be somewhat mysterious. Lvm2 is not strict ISO C compilable - there are far to many other things which would need to be fixed - thus flexible arrays shouldn't be an issue.

Quoted 7 lines

I'm curious why these 3 were important in your case - while other 26 appearances passed for you just fine (see attachment).

So can you paste what sort of gcc problem do you actually try to solve ?

Regards

Zdenek

These 3 zero-sized array blocked me to create lvm2 package for opensuse tumbleweed. Opensuse uses OBS (open build service: https://openbuildservice.org/) to create rpm packages. In my eyes, the OBS is black-box, it only showed these 3 warning and stopped building work. So I filed patch only about these 3 places.

the build error just related with [-Wstringop-overflow=]

```
[   72s] In function 'strcpy',
[   72s]     inlined from '_store_str' at device_mapper/libdm-common.c:1453:2,
[   72s]     inlined from '_stack_node_op' at device_mapper/libdm-common.c:1589:2:
[   72s] /usr/include/bits/string_fortified.h:90:10: warning: '__builtin_strcpy' writing 1 or more bytes into a region of size 0 overflows the destination [-Wstringop-overflow=]
[   72s]    90 |   return __builtin___strcpy_chk (__dest, __src, __bos (__dest));
```

thanks,
heming

#5 lore raw

Dne 29. 08. 20 v 5:11 [email protected] napsal(a):

Quoted 6 lines

These 3 zero-sized array blocked me to create lvm2 package for opensuse tumbleweed. Opensuse uses OBS (open build service: https://openbuildservice.org/) to create rpm packages. In my eyes, the OBS is black-box, it only showed these 3 warning and stopped building work. So I filed patch only about these 3 places.

Quoted 9 lines

the build error just related with [-Wstringop-overflow=]

```
[   72s] In function 'strcpy',
[   72s]     inlined from '_store_str' at device_mapper/libdm-common.c:1453:2,
[   72s]     inlined from '_stack_node_op' at device_mapper/libdm-common.c:1589:2:
[   72s] /usr/include/bits/string_fortified.h:90:10: warning: '__builtin_strcpy' writing 1 or more bytes into a region of size 0 overflows the destination [-Wstringop-overflow=]
[   72s]    90 |   return __builtin___strcpy_chk (__dest, __src, __bos (__dest));
```

Hi

So patches have've been pushed into master HEAD - so maybe you can try to validate whether this changes things for your builder server - we can then consider backporting to stable-2.0 branch.

Regards

Zdenek

#6 lore raw

Hello Zdenek,

For dependency, I patched below 3 patches, the opensuse tumbleweed building completed successfully

```