summaryrefslogtreecommitdiff
path: root/lib-src
diff options
context:
space:
mode:
authorPaul Eggert <eggert@cs.ucla.edu>2016-09-07 18:08:45 -0700
committerPaul Eggert <eggert@cs.ucla.edu>2016-09-07 18:10:11 -0700
commitd2f1971dd570439da4198fa76603b53b072060f8 (patch)
tree38b1ddbeda27b6ed6ac52205169624608cc597fd /lib-src
parent12a7e0f88eaa68aabe7e32589e2d5c8f776f6346 (diff)
Port flexible array members to GCC + valgrind
These changes are needed to conform to the C standard's rule for allocating structs containing flexible array members. C11 says that malloc (offsetof (struct s, m) + n) does not suffice to allocate a struct with an n-byte tail; instead, malloc’s arg should be rounded up to the nearest multiple of alignof (struct s). Although this is arguably a defect in C11, gcc -O2 + valgrind sometimes complains when this rule is violated, and when debugging it’s better to keep valgrind happy. For details please see the thread containing the message at: https://gcc.gnu.org/ml/gcc-patches/2016-09/msg00416.html * lib-src/ebrowse.c, src/alloc.c, src/image.c, src/process.c: Include flexmember.h. * lib-src/ebrowse.c (add_sym, add_member, make_namespace) (register_namespace_alias): * src/alloc.c (SDATA_SIZE, allocate_string_data): * src/image.c (xpm_cache_color, imagemagick_create_cache): * src/process.c (Fmake_network_process): Use FLEXSIZEOF instead of offsetof and addition. * src/alloc.c (SDATA_SIZE, vector_alignment): Use FLEXALIGNOF instead of sizeof (ptrdiff_t). * src/lisp.h (ALIGNOF_STRUCT_LISP_VECTOR): Remove, as alloc.c can now calculate this on its own.
Diffstat (limited to 'lib-src')
-rw-r--r--lib-src/ebrowse.c11
1 files changed, 6 insertions, 5 deletions
diff --git a/lib-src/ebrowse.c b/lib-src/ebrowse.c
index c59181f946..7a262005df 100644
--- a/lib-src/ebrowse.c
+++ b/lib-src/ebrowse.c
@@ -32,6 +32,7 @@ along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. */
#define SEEK_END 2
#endif
+#include <flexmember.h>
#include <min-max.h>
/* Files are read in chunks of this number of bytes. */
@@ -582,7 +583,7 @@ add_sym (const char *name, struct sym *nested_in_class)
puts (name);
}
- sym = xmalloc (offsetof (struct sym, name) + strlen (name) + 1);
+ sym = xmalloc (FLEXSIZEOF (struct sym, name, strlen (name) + 1));
memset (sym, 0, offsetof (struct sym, name));
strcpy (sym->name, name);
sym->namesp = scope;
@@ -867,8 +868,8 @@ add_global_decl (char *name, char *regexp, int pos, unsigned int hash, int var,
static struct member *
add_member (struct sym *cls, char *name, int var, int sc, unsigned int hash)
{
- struct member *m = xmalloc (offsetof (struct member, name)
- + strlen (name) + 1);
+ struct member *m = xmalloc (FLEXSIZEOF (struct member, name,
+ strlen (name) + 1));
struct member **list;
struct member *p;
struct member *prev;
@@ -978,7 +979,7 @@ mark_inherited_virtual (void)
static struct sym *
make_namespace (char *name, struct sym *context)
{
- struct sym *s = xmalloc (offsetof (struct sym, name) + strlen (name) + 1);
+ struct sym *s = xmalloc (FLEXSIZEOF (struct sym, name, strlen (name) + 1));
memset (s, 0, offsetof (struct sym, name));
strcpy (s->name, name);
s->next = all_namespaces;
@@ -1062,7 +1063,7 @@ register_namespace_alias (char *new_name, struct link *old_name)
if (streq (new_name, al->name) && (al->namesp == current_namespace))
return;
- al = xmalloc (offsetof (struct alias, name) + strlen (new_name) + 1);
+ al = xmalloc (FLEXSIZEOF (struct alias, name, strlen (new_name) + 1));
strcpy (al->name, new_name);
al->next = namespace_alias_table[h];
al->namesp = current_namespace;