Skip to content

Commit 3fc246e

Browse files
shemmingertmonjalo
authored andcommitted
test/argparse: change initialization to workaround LTO
When compiled with Link Time Optimization, the existing code generated an error, because the compiler was unable to intuit that there was space in the flexible array. In function ‘test_argparse_copy’, inlined from ‘test_argparse_init_obj’ at ../app/test/test_argparse.c:108:2, inlined from ‘test_argparse_opt_callback_parse_int_of_no_val’ at ../app/test/test_argparse.c:490:8: ../app/test/test_argparse.c:96:17: warning: ‘memcpy’ writing 56 bytes into a region of size 0 overflows the destination memcpy(&dst->args[i], &src->args[i], sizeof(src->args[i])); Initializing a structure with flexible array is special case and compiler expands the structure to fit. But inside the copy function it no longer knew that. The workaround is to put the copy inside the same function and use structure assignment. Also macro should be upper case. Fixes: 6c5c657 ("argparse: verify argument config") Cc: [email protected] Signed-off-by: Stephen Hemminger <[email protected]> Acked-by: Bruce Richardson <[email protected]> Acked-by: Chengwen Feng <[email protected]>
1 parent 8dc7bf9 commit 3fc246e

File tree

1 file changed

+15
-16
lines changed

1 file changed

+15
-16
lines changed

app/test/test_argparse.c

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ test_argparse_callback(uint32_t index, const char *value, void *opaque)
7272
}
7373

7474
/* valid templater, must contain at least two args. */
75-
#define argparse_templater() { \
75+
#define ARGPARSE_TEMPLATE { \
7676
.prog_name = "test_argparse", \
7777
.usage = "-a xx -b yy", \
7878
.descriptor = NULL, \
@@ -88,25 +88,24 @@ test_argparse_callback(uint32_t index, const char *value, void *opaque)
8888
}, \
8989
}
9090

91-
static void
92-
test_argparse_copy(struct rte_argparse *dst, struct rte_argparse *src)
93-
{
94-
uint32_t i;
95-
memcpy(dst, src, sizeof(*src));
96-
for (i = 0; /* NULL */; i++) {
97-
memcpy(&dst->args[i], &src->args[i], sizeof(src->args[i]));
98-
if (src->args[i].name_long == NULL)
99-
break;
100-
}
101-
}
10291

10392
static struct rte_argparse *
10493
test_argparse_init_obj(void)
10594
{
106-
static struct rte_argparse backup = argparse_templater();
107-
static struct rte_argparse obj = argparse_templater();
108-
/* Because obj may be overwritten, do a deep copy. */
109-
test_argparse_copy(&obj, &backup);
95+
/* Note: initialization of structure with flexible array
96+
* increases the size of the variable to match.
97+
*/
98+
static const struct rte_argparse backup = ARGPARSE_TEMPLATE;
99+
static struct rte_argparse obj = ARGPARSE_TEMPLATE;
100+
unsigned int i;
101+
102+
obj = backup;
103+
for (i = 0; ; i++) {
104+
obj.args[i] = backup.args[i];
105+
if (backup.args[i].name_long == NULL)
106+
break;
107+
}
108+
110109
return &obj;
111110
}
112111

0 commit comments

Comments
 (0)