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 <flexmember.h>.  Use FLEXIBLE_ARRAY_MEMBER, for
portability to strict C99 or later.  All storage
allocations changed to use FLEXNSIZEOF.
This commit is contained in:
Paul Eggert
2023-06-16 16:34:19 -07:00
parent 4c7a3798d8
commit c542d3d0c8
4 changed files with 17 additions and 12 deletions

View File

@@ -41,6 +41,7 @@ fdopendir
fdutimensat fdutimensat
file-has-acl file-has-acl
fileblocks fileblocks
flexmember
fnmatch-gnu fnmatch-gnu
fprintftime fprintftime
free-posix free-posix

View File

@@ -22,6 +22,7 @@
#include <system.h> #include <system.h>
#include <areadlink.h> #include <areadlink.h>
#include <flexmember.h>
#include <quotearg.h> #include <quotearg.h>
#include "common.h" #include "common.h"
@@ -36,7 +37,7 @@ struct link
dev_t dev; dev_t dev;
ino_t ino; ino_t ino;
nlink_t nlink; nlink_t nlink;
char name[1]; char name[FLEXIBLE_ARRAY_MEMBER];
}; };
struct exclusion_tag struct exclusion_tag
@@ -1518,8 +1519,7 @@ file_count_links (struct tar_stat_info *st)
absolute_names_option)); absolute_names_option));
transform_name (&linkname, XFORM_LINK); transform_name (&linkname, XFORM_LINK);
lp = xmalloc (offsetof (struct link, name) lp = xmalloc (FLEXNSIZEOF (struct link, name, strlen (linkname) + 1));
+ strlen (linkname) + 1);
lp->ino = st->stat.st_ino; lp->ino = st->stat.st_ino;
lp->dev = st->stat.st_dev; lp->dev = st->stat.st_dev;
lp->nlink = st->stat.st_nlink; lp->nlink = st->stat.st_nlink;

View File

@@ -19,6 +19,7 @@
*/ */
#include <system.h> #include <system.h>
#include <quotearg.h> #include <quotearg.h>
#include <flexmember.h>
#include <fnmatch.h> #include <fnmatch.h>
#include <wordsplit.h> #include <wordsplit.h>
#include "common.h" #include "common.h"
@@ -40,7 +41,7 @@ struct excfile
{ {
struct excfile *next; struct excfile *next;
int flags; int flags;
char name[1]; char name[FLEXIBLE_ARRAY_MEMBER];
}; };
static struct excfile *excfile_head, *excfile_tail; static struct excfile *excfile_head, *excfile_tail;
@@ -48,7 +49,8 @@ static struct excfile *excfile_head, *excfile_tail;
void void
excfile_add (const char *name, int flags) 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->next = NULL;
p->flags = flags; p->flags = flags;
strcpy (p->name, name); strcpy (p->name, name);

View File

@@ -22,6 +22,7 @@
#include <system.h> #include <system.h>
#include <quotearg.h> #include <quotearg.h>
#include <errno.h> #include <errno.h>
#include <flexmember.h>
#include <hash.h> #include <hash.h>
#include <priv-set.h> #include <priv-set.h>
#include <root-uid.h> #include <root-uid.h>
@@ -180,7 +181,7 @@ struct delayed_link
struct xattr_map xattr_map; struct xattr_map xattr_map;
/* The desired target of the desired link. */ /* The desired target of the desired link. */
char target[1]; char target[FLEXIBLE_ARRAY_MEMBER];
}; };
static Hash_table *delayed_link_table; static Hash_table *delayed_link_table;
@@ -188,7 +189,7 @@ static Hash_table *delayed_link_table;
struct string_list struct string_list
{ {
struct string_list *next; struct string_list *next;
char string[1]; char string[FLEXIBLE_ARRAY_MEMBER];
}; };
static size_t static size_t
@@ -1134,7 +1135,7 @@ extract_dir (char *file_name, int typeflag)
status = 0; status = 0;
break; break;
} }
errno = EEXIST; 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->mtime = current_stat_info.mtime;
} }
p->change_dir = chdir_current; p->change_dir = chdir_current;
p->sources = xmalloc (offsetof (struct string_list, string) p->sources = xmalloc (FLEXNSIZEOF (struct string_list, string,
+ strlen (file_name) + 1); strlen (file_name) + 1));
p->sources->next = 0; p->sources->next = 0;
strcpy (p->sources->string, file_name); strcpy (p->sources->string, file_name);
p->cntx_name = NULL; p->cntx_name = NULL;
@@ -1534,8 +1535,9 @@ extract_link (char *file_name, int typeflag)
if (ds && ds->change_dir == chdir_current if (ds && ds->change_dir == chdir_current
&& BIRTHTIME_EQ (ds->birthtime, get_stat_birthtime (&st1))) && BIRTHTIME_EQ (ds->birthtime, get_stat_birthtime (&st1)))
{ {
struct string_list *p = xmalloc (offsetof (struct string_list, string) struct string_list *p
+ strlen (file_name) + 1); = xmalloc (FLEXNSIZEOF (struct string_list,
string, strlen (file_name) + 1));
strcpy (p->string, file_name); strcpy (p->string, file_name);
p->next = ds->sources; p->next = ds->sources;
ds->sources = p; ds->sources = p;