diff options
author | Nick Mathewson <nickm@torproject.org> | 2011-01-06 15:59:05 -0500 |
---|---|---|
committer | Nick Mathewson <nickm@torproject.org> | 2011-01-06 15:59:05 -0500 |
commit | d4165ef8b4c2ea697a0b73d80efc6575c0f2279a (patch) | |
tree | 62bd907a4f382ceb3111273ac0dc187088a63ae5 /configure.in | |
parent | fd8f7991e47d02cc345af6f190a7b480703822df (diff) | |
download | tor-d4165ef8b4c2ea697a0b73d80efc6575c0f2279a.tar.gz tor-d4165ef8b4c2ea697a0b73d80efc6575c0f2279a.zip |
Use autoconf's FLEXIBLE_ARRAY_MEMBER for unspecified-length arrays
C99 allows a syntax for structures whose last element is of
unspecified length:
struct s {
int elt1;
...
char last_element[];
};
Recent (last-5-years) autoconf versions provide an
AC_C_FLEXIBLE_ARRAY_MEMBER test that defines FLEXIBLE_ARRAY_MEMBER
to either no tokens (if you have c99 flexible array support) or to 1
(if you don't). At that point you just use offsetof
[STRUCT_OFFSET() for us] to see where last_element begins, and
allocate your structures like:
struct s {
int elt1;
...
char last_element[FLEXIBLE_ARRAY_MEMBER];
};
tor_malloc(STRUCT_OFFSET(struct s, last_element) +
n_elements*sizeof(char));
The advantages are:
1) It's easier to see which structures and elements are of
unspecified length.
2) The compiler and related checking tools can also see which
structures and elements are of unspecified length, in case they
wants to try weird bounds-checking tricks or something.
3) The compiler can warn us if we do something dumb, like try
to stack-allocate a flexible-length structure.
Diffstat (limited to 'configure.in')
-rw-r--r-- | configure.in | 22 |
1 files changed, 22 insertions, 0 deletions
diff --git a/configure.in b/configure.in index 6ca5531e0b..be1ee33ca2 100644 --- a/configure.in +++ b/configure.in @@ -158,6 +158,28 @@ AM_CONDITIONAL(NAT_PMP, test x$natpmp = xtrue) AM_CONDITIONAL(MINIUPNPC, test x$upnp = xtrue) AM_PROG_CC_C_O +ifdef([AC_C_FLEXIBLE_ARRAY_MEMBER], [ +AC_C_FLEXIBLE_ARRAY_MEMBER +], [ + dnl Maybe we've got an old autoconf... + AC_CACHE_CHECK([for flexible array members], + tor_cv_c_flexarray, + [AC_COMPILE_IFELSE( + AC_LANG_PROGRAM([ + struct abc { int a; char b[]; }; +], [ + struct abc *def = malloc(sizeof(struct abc)+sizeof(char)); + def->b[0] = 33; +]), + [tor_cv_c_flexarray=yes], + [tor_cv_c_flexarray=no])]) + if test $tor_cv_flexarray = yes ; then + AC_DEFINE([FLEXIBLE_ARRAY_MEMBER], []) + else + AC_DEFINE([FLEXIBLE_ARRAY_MEMBER], [1]) + fi +]) + AC_PATH_PROG([SHA1SUM], [sha1sum], none) AC_PATH_PROG([OPENSSL], [openssl], none) |