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
file-has-acl
fileblocks
flexmember
fnmatch-gnu
fprintftime
free-posix

View File

@@ -22,6 +22,7 @@
#include <system.h>
#include <areadlink.h>
#include <flexmember.h>
#include <quotearg.h>
#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;

View File

@@ -19,6 +19,7 @@
*/
#include <system.h>
#include <quotearg.h>
#include <flexmember.h>
#include <fnmatch.h>
#include <wordsplit.h>
#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);

View File

@@ -22,6 +22,7 @@
#include <system.h>
#include <quotearg.h>
#include <errno.h>
#include <flexmember.h>
#include <hash.h>
#include <priv-set.h>
#include <root-uid.h>
@@ -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;