From d37cd3c69781c0d77af13dec81eea45b4c434c52 Mon Sep 17 00:00:00 2001 From: Marko Kreen Date: Fri, 4 Jan 2008 07:57:01 +0000 Subject: [PATCH] proper usage of flexible array --- include/bouncer.h | 3 ++- include/sbuf.h | 4 +++- include/system.h | 9 +++++++++ src/main.c | 5 ++--- src/sbuf.c | 2 +- 5 files changed, 17 insertions(+), 6 deletions(-) diff --git a/include/bouncer.h b/include/bouncer.h index ff02358..47dfdb9 100644 --- a/include/bouncer.h +++ b/include/bouncer.h @@ -257,7 +257,8 @@ struct PgSocket { SBuf sbuf; /* stream buffer, must be last */ }; -#define PG_SOCKET_SIZE (sizeof(PgSocket) + cf_sbuf_len + SBUF_MAX_REWRITE) +#define RAW_SOCKET_SIZE offsetof(struct PgSocket, sbuf.buf) +#define PG_SOCKET_SIZE (RAW_SOCKET_SIZE + cf_sbuf_len + SBUF_MAX_REWRITE) /* where to store old fd info during SHOW FDS result processing */ #define tmp_sk_oldfd request_time diff --git a/include/sbuf.h b/include/sbuf.h index 18a2c74..b21e5ba 100644 --- a/include/sbuf.h +++ b/include/sbuf.h @@ -85,9 +85,11 @@ struct SBuf { SBuf *dst; /* target SBuf for current packet */ - uint8_t buf[0]; /* data buffer follows (cf_sbuf_len + SBUF_MAX_REWRITE) */ + uint8_t buf[FLEX_ARRAY];/* data buffer follows (cf_sbuf_len + SBUF_MAX_REWRITE) */ }; +#define RAW_SBUF_SIZE offsetof(struct SBuf, buf) + #define sbuf_socket(sbuf) ((sbuf)->sock) void sbuf_init(SBuf *sbuf, sbuf_cb_t proto_fn, void *arg); diff --git a/include/system.h b/include/system.h index 58568ed..0838101 100644 --- a/include/system.h +++ b/include/system.h @@ -56,6 +56,15 @@ #include #endif +/* how to specify array with unknown length */ +#if defined(__STDC_VERSION__) && (__STDC_VERSION__ >= 199901L) +#define FLEX_ARRAY +#elif defined(__GNUC__) +#define FLEX_ARRAY +#else +#define FLEX_ARRAY 1 +#endif + /* gcc has hew positive aspects too */ #ifdef __GNUC__ diff --git a/src/main.c b/src/main.c index 9a3d3e7..38f4773 100644 --- a/src/main.c +++ b/src/main.c @@ -435,9 +435,8 @@ static void check_limits(void) List *item; PgDatabase *db; - log_noise("event: %lu, SBuf: %lu, PgSocket: %lu, Full PgSocket: %lu, buf ofs: %lu", - sizeof(struct event), sizeof(SBuf), sizeof(PgSocket), - PG_SOCKET_SIZE, offsetof(SBuf, buf)); + log_noise("event: %lu, SBuf: %lu, PgSocket: %lu, Full PgSocket: %lu", + sizeof(struct event), RAW_SBUF_SIZE, RAW_SOCKET_SIZE, PG_SOCKET_SIZE); /* load limits */ err = getrlimit(RLIMIT_NOFILE, &lim); diff --git a/src/sbuf.c b/src/sbuf.c index c1662ef..7b66739 100644 --- a/src/sbuf.c +++ b/src/sbuf.c @@ -71,7 +71,7 @@ static bool sbuf_after_connect_check(SBuf *sbuf) _MUSTCHECK; /* initialize SBuf with proto handler */ void sbuf_init(SBuf *sbuf, sbuf_cb_t proto_fn, void *arg) { - memset(sbuf, 0, sizeof(*sbuf)); + memset(sbuf, 0, RAW_SBUF_SIZE); sbuf->proto_cb_arg = arg; sbuf->proto_cb = proto_fn; } -- 2.39.5