summaryrefslogtreecommitdiff
path: root/src/or/buffers.c
diff options
context:
space:
mode:
authorNick Mathewson <nickm@torproject.org>2011-01-06 15:59:05 -0500
committerNick Mathewson <nickm@torproject.org>2011-01-06 15:59:05 -0500
commitd4165ef8b4c2ea697a0b73d80efc6575c0f2279a (patch)
tree62bd907a4f382ceb3111273ac0dc187088a63ae5 /src/or/buffers.c
parentfd8f7991e47d02cc345af6f190a7b480703822df (diff)
downloadtor-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 'src/or/buffers.c')
-rw-r--r--src/or/buffers.c4
1 files changed, 2 insertions, 2 deletions
diff --git a/src/or/buffers.c b/src/or/buffers.c
index 9f393b9874..48acf505ef 100644
--- a/src/or/buffers.c
+++ b/src/or/buffers.c
@@ -68,8 +68,8 @@ typedef struct chunk_t {
size_t datalen; /**< The number of bytes stored in this chunk */
size_t memlen; /**< The number of usable bytes of storage in <b>mem</b>. */
char *data; /**< A pointer to the first byte of data stored in <b>mem</b>. */
- char mem[1]; /**< The actual memory used for storage in this chunk. May be
- * more than one byte long. */
+ char mem[FLEXIBLE_ARRAY_MEMBER]; /**< The actual memory used for storage in
+ * this chunk. */
} chunk_t;
#define CHUNK_HEADER_LEN STRUCT_OFFSET(chunk_t, mem[0])