aboutsummaryrefslogtreecommitdiff
path: root/src/or/or.h
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/or.h
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/or.h')
-rw-r--r--src/or/or.h12
1 files changed, 6 insertions, 6 deletions
diff --git a/src/or/or.h b/src/or/or.h
index 2d1fbef59c..8aef2d0233 100644
--- a/src/or/or.h
+++ b/src/or/or.h
@@ -857,8 +857,8 @@ typedef struct cell_t {
typedef struct var_cell_t {
uint8_t command;
circid_t circ_id;
- uint16_t payload_len;
- uint8_t payload[1];
+ uint16_t payload_len; /**< The actual length of <b>payload</b>. */
+ uint8_t payload[FLEXIBLE_ARRAY_MEMBER];
} var_cell_t;
/** A cell as packed for writing to the network. */
@@ -1660,11 +1660,11 @@ typedef struct short_policy_t {
unsigned int is_accept : 1;
/** The actual number of values in 'entries'. */
unsigned int n_entries : 31;
- /** An array of (probably more than 1!) short_policy_entry_t values,
- * each descriping a range of ports that this policy accepts or rejects
- * (depending on the value of is_accept).
+ /** An array of 0 or more short_policy_entry_t values, each descriping a
+ * range of ports that this policy accepts or rejects (depending on the
+ * value of is_accept).
*/
- short_policy_entry_t entries[1];
+ short_policy_entry_t entries[FLEXIBLE_ARRAY_MEMBER];
} short_policy_t;
/** A microdescriptor is the smallest amount of information needed to build a