From 38886f0dc4372c295484b398ca9a4d065e317718 Mon Sep 17 00:00:00 2001 From: mcc Date: Fri, 28 Jun 2019 22:31:00 -0400 Subject: [PATCH] Patch sds.h so that it can be #included from C++ Empty arrays aren't allowed in C++, so a single dummy element has to be added. sds works by scary cast magic so this dummy element is never actually allocated. A #define is used in this patch so in C this compiles exactly the same as before. --- src/lib/sds/sds.h | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/src/lib/sds/sds.h b/src/lib/sds/sds.h index 18209eee..7cdb3aa6 100644 --- a/src/lib/sds/sds.h +++ b/src/lib/sds/sds.h @@ -43,6 +43,12 @@ extern const char *SDS_NOINIT; typedef char *sds; +#ifdef __cplusplus +#define DUMMY_ELEMENT 1 +#else +#define DUMMY_ELEMENT +#endif + #ifdef _MSC_VER #define PACK(decl) __pragma(pack(push, 1)) decl __pragma(pack(pop)) @@ -60,31 +66,31 @@ typedef long ssize_t; * However is here to document the layout of type 5 SDS strings. */ PACK(struct sdshdr5 { unsigned char flags; /* 3 lsb of type, and 5 msb of string length */ - char buf[]; + char buf[DUMMY_ELEMENT]; }); PACK(struct sdshdr8 { uint8_t len; /* used */ uint8_t alloc; /* excluding the header and null terminator */ unsigned char flags; /* 3 lsb of type, 5 unused bits */ - char buf[]; + char buf[DUMMY_ELEMENT]; }); PACK(struct sdshdr16 { uint16_t len; /* used */ uint16_t alloc; /* excluding the header and null terminator */ unsigned char flags; /* 3 lsb of type, 5 unused bits */ - char buf[]; + char buf[DUMMY_ELEMENT]; }); PACK(struct sdshdr32 { uint32_t len; /* used */ uint32_t alloc; /* excluding the header and null terminator */ unsigned char flags; /* 3 lsb of type, 5 unused bits */ - char buf[]; + char buf[DUMMY_ELEMENT]; }); PACK(struct sdshdr64 { uint64_t len; /* used */ uint64_t alloc; /* excluding the header and null terminator */ unsigned char flags; /* 3 lsb of type, 5 unused bits */ - char buf[]; + char buf[DUMMY_ELEMENT]; }); #undef PACK