From c542d3d0c8a845dfe6bf6f1c7b5279354c1ceeed Mon Sep 17 00:00:00 2001 From: Paul Eggert Date: Fri, 16 Jun 2023 16:34:19 -0700 Subject: [PATCH] Port to strict C99 struct hack Portability bug caught by GCC 13 -fstrict-flex-arrays. * gnulib.modules: Add flexmember. * src/create.c (struct link): * src/exclist.c (struct excfile): * src/extract.c (struct delayed_link, struct string_list): Include . Use FLEXIBLE_ARRAY_MEMBER, for portability to strict C99 or later. All storage allocations changed to use FLEXNSIZEOF. --- gnulib.modules | 1 + src/create.c | 6 +++--- src/exclist.c | 6 ++++-- src/extract.c | 16 +++++++++------- 4 files changed, 17 insertions(+), 12 deletions(-) diff --git a/gnulib.modules b/gnulib.modules index 84732f5b..064f9349 100644 --- a/gnulib.modules +++ b/gnulib.modules @@ -41,6 +41,7 @@ fdopendir fdutimensat file-has-acl fileblocks +flexmember fnmatch-gnu fprintftime free-posix diff --git a/src/create.c b/src/create.c index 07db3f6d..658c9de9 100644 --- a/src/create.c +++ b/src/create.c @@ -22,6 +22,7 @@ #include #include +#include #include #include "common.h" @@ -36,7 +37,7 @@ struct link dev_t dev; ino_t ino; nlink_t nlink; - char name[1]; + char name[FLEXIBLE_ARRAY_MEMBER]; }; struct exclusion_tag @@ -1518,8 +1519,7 @@ file_count_links (struct tar_stat_info *st) absolute_names_option)); transform_name (&linkname, XFORM_LINK); - lp = xmalloc (offsetof (struct link, name) - + strlen (linkname) + 1); + lp = xmalloc (FLEXNSIZEOF (struct link, name, strlen (linkname) + 1)); lp->ino = st->stat.st_ino; lp->dev = st->stat.st_dev; lp->nlink = st->stat.st_nlink; diff --git a/src/exclist.c b/src/exclist.c index 7546e411..c9c34315 100644 --- a/src/exclist.c +++ b/src/exclist.c @@ -19,6 +19,7 @@ */ #include #include +#include #include #include #include "common.h" @@ -40,7 +41,7 @@ struct excfile { struct excfile *next; int flags; - char name[1]; + char name[FLEXIBLE_ARRAY_MEMBER]; }; static struct excfile *excfile_head, *excfile_tail; @@ -48,7 +49,8 @@ static struct excfile *excfile_head, *excfile_tail; void excfile_add (const char *name, int flags) { - struct excfile *p = xmalloc (sizeof (*p) + strlen (name)); + struct excfile *p = xmalloc (FLEXNSIZEOF (struct excfile, name, + strlen (name) + 1)); p->next = NULL; p->flags = flags; strcpy (p->name, name); diff --git a/src/extract.c b/src/extract.c index 7adc7aff..bc2abbfe 100644 --- a/src/extract.c +++ b/src/extract.c @@ -22,6 +22,7 @@ #include #include #include +#include #include #include #include @@ -180,7 +181,7 @@ struct delayed_link struct xattr_map xattr_map; /* The desired target of the desired link. */ - char target[1]; + char target[FLEXIBLE_ARRAY_MEMBER]; }; static Hash_table *delayed_link_table; @@ -188,7 +189,7 @@ static Hash_table *delayed_link_table; struct string_list { struct string_list *next; - char string[1]; + char string[FLEXIBLE_ARRAY_MEMBER]; }; static size_t @@ -1134,7 +1135,7 @@ extract_dir (char *file_name, int typeflag) status = 0; break; } - + errno = EEXIST; } @@ -1470,8 +1471,8 @@ create_placeholder_file (char *file_name, bool is_symlink, bool *interdir_made, p->mtime = current_stat_info.mtime; } p->change_dir = chdir_current; - p->sources = xmalloc (offsetof (struct string_list, string) - + strlen (file_name) + 1); + p->sources = xmalloc (FLEXNSIZEOF (struct string_list, string, + strlen (file_name) + 1)); p->sources->next = 0; strcpy (p->sources->string, file_name); p->cntx_name = NULL; @@ -1534,8 +1535,9 @@ extract_link (char *file_name, int typeflag) if (ds && ds->change_dir == chdir_current && BIRTHTIME_EQ (ds->birthtime, get_stat_birthtime (&st1))) { - struct string_list *p = xmalloc (offsetof (struct string_list, string) - + strlen (file_name) + 1); + struct string_list *p + = xmalloc (FLEXNSIZEOF (struct string_list, + string, strlen (file_name) + 1)); strcpy (p->string, file_name); p->next = ds->sources; ds->sources = p; -- 2.47.2